﻿//AutoComplete
Type.registerNamespace("PH.CMS.Player");

PH.CMS.Player._interval = 0;
 
PH.CMS.Player.Show = function(type, code) {
  if(type == 2) {
    //Open in new window.
    return;
  }
    
  if(!this._back) {    
    this._back = document.createElement("div");
    this._back.className = 'dialog_back';
    document.forms[0].appendChild(this._back);
  }

  if(!this._panel) {    
    this._panel = document.createElement("div");
    this._panel.className = 'dialog_panel';
    document.forms[0].appendChild(this._panel);
  }
  
  while(this._panel.childNodes.length > 0) this._panel.removeChild(this._panel.firstChild);

  var iIndex = code.toLowerCase().indexOf('width="');
  if(iIndex > 0) {
    iIndex += 7;
    this._width = parseInt(code.substr(iIndex, code.indexOf('"', iIndex) - iIndex)) + 40;
  }  
  
  var iIndex = code.toLowerCase().indexOf('height="');
  if(iIndex > 0) {
    iIndex += 8;
    this._height = parseInt(code.substr(iIndex, code.indexOf('"', iIndex) - iIndex)) + 40;
  }
  
  this._code = code;
  
  this._back._newA = 80;
  
  this.SetAlpha(this._back, 0);
  
  this._panel._alpha = 100;
  this._panel.innerHTML = "";
  this._panel.style.width = "1px";
  this._panel.style.height = "1px";
  
  if(this._interval == 0) {
    this._interval = setInterval("PH.CMS.Player.Animate();", 40);
  }
}

PH.CMS.Player.Close = function() {
  this._back._newA = 0;
  
  while(this._panel.childNodes.length > 0) this._panel.removeChild(this._panel.firstChild);
  
  if(this._interval == 0) {
    this._interval = setInterval("PH.CMS.Player.Animate();", 40);
  }
}

PH.CMS.Player.Animate = function() {
  if(this._back._newA > 0) {
      this.SetAlpha(this._back, this._back._alpha - ((this._back._alpha - this._back._newA) / 4));
      
      var iWidth = parseInt(this._panel.style.width);
      var iHeight = parseInt(this._panel.style.height);

      if(iWidth <= this._width - 1) {
        iWidth -= Math.round((iWidth - this._width) / 3);
      } else if(iHeight <= this._height - 1) {
        iHeight -= Math.round((iHeight - this._height) / 3);
      } else {
        clearInterval(this._interval);
        this._interval = 0;

        var inner = document.createElement("div");
        inner.innerHTML = this._code;
        this._panel.appendChild(inner);
        
        var button = document.createElement("a");
        button.className = "btn";
        button.innerHTML = "Close";
        button.href = "javascript:void(0);";
        button.onclick = PH.CMS.Player._closeButton;
        this._panel.appendChild(button);
      }
      
      if(iWidth >= this._width - 1) iWidth = this._width;
      if(iHeight >= this._height - 1) iHeight = this._height;

      this._panel.style.width = iWidth + 'px';
      this._panel.style.height = iHeight + 'px';
      this.Position();
  } else {
      this.SetAlpha(this._back, this._back._alpha - (this._back._alpha / 3));
      this.SetAlpha(this._panel, this._panel._alpha - (this._panel._alpha / 3));
      
      if(this._back._alpha == 0) {
        clearInterval(this._interval);
        this._interval = 0;

        document.forms[0].removeChild(this._panel);
        document.forms[0].removeChild(this._back);
          
        this._panel = undefined;
        this._back = undefined;
      }
  }
}

PH.CMS.Player.Position = function() {
  var iWidth = parseInt(this._panel.style.width);
  var iHeight = parseInt(this._panel.style.height);
  var iWindowWidth  = (window.innerHeight) ? window.innerWidth : document.documentElement.clientWidth;
  var iWindowHeight = (window.innerHeight) ? window.innerHeight : document.documentElement.clientHeight;
  
  this._panel.style.left = Math.round((iWindowWidth - iWidth) / 2) + 'px';
  this._panel.style.top = Math.round((iWindowHeight - iHeight) / 2) + 'px';
}

PH.CMS.Player.SetAlpha = function(obj, percent) {
    if(percent > 99) percent = 100;
    if(percent < 1) percent = 0;

    obj._alpha = percent;
    obj.style.filter = 'alpha(opacity=' + percent + ')';
    obj.style.MozOpacity = (percent/100);
    obj.style.opacity = (percent/100);
} 

PH.CMS.Player._closeButton = function() {
  PH.CMS.Player.Close();
}

PH.CMS.Player._onresize = function() {
  PH.CMS.Player.Position();
  if(PH.CMS.Player._oldresize) PH.CMS.Player._oldresize();
}

PH.CMS.Player._oldresize = window.onresize;
window.onresize = PH.CMS.Player._onresize;

//.NET AJAX
if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded(); 
