
GuitarHero = {

  getWindowWidth : function() {

    var width = document.documentElement && document.documentElement.clientWidth ||
                document.body && document.body.clientWidth ||
                document.body && document.body.parentNode && document.body.parentNode.clientWidth ||
                0;

    return width;

  },

  getWindowHeight : function() {

    var height = document.documentElement && document.documentElement.clientHeight ||
                 document.body && document.body.clientHeight ||
                 document.body && document.body.parentNode && document.body.parentNode.clientHeight ||
                 0;

    return height;

  },

  centeredWindow : function(url,name,width,height,scrollbars,resizeable) {

    var xPos = (screen.availWidth / 2) - (width / 2);
    var yPos = (screen.availHeight / 2) - (height / 2);

    var options = "width=" + width + ",height=" + height + ",resizeable=" + resizeable +",scrollbars=" + scrollbars + ",left=" + xPos + ",top=" + yPos;

    window.open(url,name,options);

  },


  isIE : function() {

    return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);

  },

  changeOpacity : function(id,step,end,interval) {

    if (end > 100)
      end = 100;
    else if (end < 0)
      end = 0;

    if (step > 50)
      step = 50;
    else if (step < 1)
      step = 1;

    var isIE = false;
    var element = document.getElementById(id);
    var current = 0;
    var next;

    // show if necessary
    if (element.style.display != "block") {
      element.style.display = "block";
    }

    // get the proper opacity
    if (element.filters) {

      current = element.filters.alpha.opacity;
      isIE = true;

    } else {

      current = (element.style.opacity ? element.style.opacity * 100 : 0);

    }

    // figure out direction
    if (end > current) {

      next = ((current + step) >= end ? end : (current + step));

    } else if (end < current) {

      next = ((current - step) <= end ? end : (current - step));

    } else {

      // hide it entirely once it's done
      if (end == 0) {
        element.style.display = "none";
      }

      return;

    }

    setTimeout(function() {

      // set the new opacity
      if (isIE) {

        element.filters.alpha.opacity = next;

      } else {

        element.style.opacity = (next / 100);

      }

      GuitarHero.changeOpacity(id,step,end,interval);

    },interval);

  }

}

function GH_Message_Window(width,height,bg_id,message,confirm_action) {

  this.width = width;
  this.height = height;

  this.background_id = bg_id;
  this.message = message;

  this.originalZIndex = 0;

  if (confirm_action != undefined) {

    this.confirmAction = confirm_action;

  } else {

    this.confirmAction = 'none';

  }

  this.originalConfirmAction = this.confirmAction;

  this.messageBox = null;

}

GH_Message_Window.prototype.changeMessage = function(newMessage) {

  this.message = newMessage;

  this.messageBox.replaceChild(document.createTextNode(newMessage),this.messageBox.firstChild);

}

GH_Message_Window.prototype.createAndShow = function(parentWindow,appId) {

  this.application_id = appId;
  this.confirmAction = this.originalConfirmAction + 'song=' + appId;

  var left = window.XMLHttpRequest == null ? document.documentElement.scrollLeft : 0;
  var top = window.XMLHttpRequest == null ? document.documentElement.scrollTop : 0;

  // further gray out the background
  var bgElement = document.getElementById(this.background_id);
  this.originalZIndex = bgElement.style.zIndex;
  bgElement.style.zIndex = 50;

  GuitarHero.changeOpacity(this.background_id,10,70,25);

  // set up the container box
  var msgBox = document.createElement('div');
  msgBox.setAttribute('id','hdn_message_box');
  msgBox.style.position = 'fixed';
  msgBox.style.zIndex = 100;
  msgBox.style.width = this.width + 'px';
  msgBox.style.height = this.height + 'px';
  //msgBox.style.border = '4px solid black';
  //msgBox.style.backgroundColor = '#ffffff';
  msgBox.style.backgroundImage = 'url(graphics/song_purchase_background_400x361.jpg)';
  msgBox.style.left = Math.max((left + (GuitarHero.getWindowWidth() - this.width) / 2), 0) + 'px';
  msgBox.style.top = Math.max((top + (GuitarHero.getWindowHeight() - this.height) / 2), 0) + 'px';
  msgBox.style.overflow = 'hidden';

  msgBox.style.display = "none";
  msgBox.style.opacity = 0;

  var titleBgWidth = this.width - 20;

  // add the blue title bar
  var titleBg = document.createElement('img');
  titleBg.src = 'graphics/pixel_92cee8.gif';
  titleBg.setAttribute('width',titleBgWidth);
  titleBg.setAttribute('height','8');
  titleBg.setAttribute('alt','');
  titleBg.style.position = 'absolute';
  titleBg.style.top = '17px';
  titleBg.style.left = '10px';
  msgBox.appendChild(titleBg);

  // add the actual title
  var title = document.createTextNode('Get This Song?');
  var titleBox = document.createElement('div');
  titleBox.style.fontSize = '18px';
  titleBox.style.fontWeight = 'bold';
  titleBox.style.position = 'absolute';
  titleBox.style.top = '20px';
  titleBox.style.left = '0px';
  titleBox.style.padding = '6px';
  titleBox.style.width = titleBgWidth + 'px';
  titleBox.style.textAlign = 'center';
  titleBox.style.color = '#ffffff';
  //titleBox.style.backgroundColor = '#ffffff';
  titleBox.appendChild(title);
  msgBox.appendChild(titleBox);

  // add the actual title
/*  var title2 = document.createTextNode('Get This Song?');
  var title2Box = document.createElement('div');
  title2Box.style.position = 'absolute';
  title2Box.style.top = '40px';
  title2Box.style.left = '0px';
  title2Box.style.width = titleBgWidth + 'px';
  title2Box.style.backgroundColor = '#ffffff';
  title2Box.appendChild(title2);
  msgBox.appendChild(title2Box);
*/


  // add the message itself
  var messageText = document.createTextNode(this.message);
  var messageTextBox = document.createElement('div');
  this.messageBox = messageTextBox;

  messageTextBox.style.position = 'absolute';
  messageTextBox.style.top = '62px';
  messageTextBox.style.left = '48px';
  messageTextBox.style.width = titleBgWidth - 80 + 'px';
  messageTextBox.style.fontSize = '11px';
  //messageTextBox.style.lineHeight = '18px';
  messageTextBox.style.color = '#ffffff';
  messageTextBox.appendChild(messageText);
  msgBox.appendChild(messageTextBox);


  // a div to contain the buttons
  var bottomRow = document.createElement('div');
  bottomRow.style.width = titleBgWidth + 'px';
  bottomRow.style.height = '19px';
  bottomRow.style.position = 'absolute';
  bottomRow.style.left = '10px';
  bottomRow.style.bottom = '59px';

  if (this.confirmAction == 'none') {

  } else {

    // add cancel link and image
    var imgCancelLink = document.createElement('a');
    imgCancelLink.setAttribute('href','#');
    imgCancelLink.onclick = closeAllWindows(parentWindow,this);

    var imgCancel = document.createElement('img');
    imgCancel.src = 'graphics/button_cancel_84x19.gif';
    imgCancel.setAttribute('width','84');
    imgCancel.setAttribute('height','19');
    imgCancel.setAttribute('alt','');
    imgCancel.style.position = 'absolute';
    imgCancel.style.top = '0';
    imgCancel.style.left = '34px';
    imgCancelLink.appendChild(imgCancel);

    // add confirm link and image
    var imgConfirmLink = document.createElement('a');
    imgConfirmLink.setAttribute('href',this.confirmAction);

    var imgConfirm = document.createElement('img');
    imgConfirm.src = 'graphics/button_continue_134x19.gif';
    imgConfirm.setAttribute('width','134');
    imgConfirm.setAttribute('height','19');
    imgConfirm.setAttribute('alt','');
    imgConfirm.style.position = 'absolute';
    imgConfirm.style.top = '0';
    imgConfirm.style.right = '32px';
    imgConfirmLink.appendChild(imgConfirm);

    bottomRow.appendChild(imgCancelLink);
    bottomRow.appendChild(imgConfirmLink);

  }

  msgBox.appendChild(bottomRow);

  // get the main body
  var docBody = document.getElementById('howf_body');
  docBody.appendChild(msgBox);

  GuitarHero.changeOpacity("hdn_message_box",25,100,50);

}

GH_Message_Window.prototype.destroy = function() {

  var docBody = document.getElementById('howf_body');
  var msgBox = document.getElementById('hdn_message_box');

  docBody.removeChild(msgBox);

  // return the background to its former state
  var bgElement = document.getElementById(this.background_id);
  bgElement.style.zIndex = this.originalZIndex;

  GuitarHero.changeOpacity(this.background_id,10,0,25);

}

function HDN_Add_Application_Window(id,bg_id,type) {

  this.attached = false;
  this.displayed = false;

  this.app_window_id = id;
  this.app_window = null;

  this.background_id = bg_id;
  this.background = null;

  this.window_type = type;

}

HDN_Add_Application_Window.prototype.retrieve = function() {

  this.app_window = document.getElementById(this.app_window_id);
  this.background = document.getElementById(this.background_id);

}

HDN_Add_Application_Window.prototype.attach = function() {

  // get the parent node
  var js_container = this.app_window.parentNode;
  var bg_container = this.background.parentNode;

  // get the main body
  var js_docBody = document.getElementById("howf_body");

  // remove it from the parent
  js_container.removeChild(this.app_window);
  bg_container.removeChild(this.background);

  // add it to the body
  js_docBody.appendChild(this.app_window);
  js_docBody.appendChild(this.background);

  this.attached = true;

}

HDN_Add_Application_Window.prototype.clearInputs = function() {

  // retrieve it if it hasn't been already
  if (this.app_window == null) {
    this.retrieve();
  }

  var inputs = this.app_window.getElementsByTagName("input");

  for (var i = 0; i < inputs.length; i++) {

    var type = inputs[i].getAttribute("type");

    // don't clear hidden inputs
    if (type != "hidden") {
      inputs[i].value = "";
    }

  }

  var textareas = this.app_window.getElementsByTagName("textarea");

  for (var i = 0; i < textareas.length; i++) {
    textareas[i].value = "";
  }

}

HDN_Add_Application_Window.prototype.show = function(width,height,app_id) {

  // retrieve it if it hasn't been already
  if (this.app_window == null) {
    this.retrieve();
  }

  // attach it if it hasn't been already
  if (!this.attached) {
    this.attach();
  }

  // store the app_id for this window
  this.application_id = app_id;

  // clear the window's inputs
  this.clearInputs();

  // see if we need to populate this window
  if (this.window_type == 'edit' && typeof this.application_id != 'undefined') {

    if (typeof hdn_dashboard_app_data[this.application_id] != 'undefined') {

      for (var i in hdn_dashboard_app_data[this.application_id]) {

        var formElement = document.getElementById(this.window_type + '_app_' + i);

        if (formElement) {

          formElement.value = hdn_dashboard_app_data[this.application_id][i];

        }

      }

    } else {

      alert("Error: Application data undefined.");

    }

  }

  // add the centering event
  this.center = centerAppWindow(this.app_window_id,width,height);

  // center the window
  this.center();

  // show it
  this.app_window.style.display = this.background.style.display = "block";
  this.displayed = true;

  // let's make it re-center when we re-size
  window.onresize = this.center;

  // want to focus on the first input in the window
  var js_children = this.app_window.getElementsByTagName("input");

  for (var i = 0; i < js_children.length; i++) {

    if (js_children[i].getAttribute("type") == "text") {

      js_children[i].focus();
      return false;

    }

  }

  return false;

}

HDN_Add_Application_Window.prototype.hide = function() {

  // retrieve it if it hasn't been already
  if (this.app_window == null) {
    this.retrieve();
  }

  // hide it
  this.app_window.style.display = this.background.style.display = "none";
  this.displayed = false;

  window.onresize = null;

  return false;

}

function closeAllWindows(parentWindow,childWindow) {

  return function() {

    childWindow.destroy();
    //parentWindow.hide();

    return false;

  }

}
