/**
 *
 *   jsDialog
 *   Copyright (c) 2008. by MASSVision, http://massvision.net
 *   Author: Mladen Mijatov
 *
 **/

/**
 * Global jd_Dialog object
 * @var object
 */
var js_Dialog = new Object();

/**
 * show/hide timer
 * @var integer
 */
js_Dialog.Timer = 0;

/**
 * animation speed, bigger number slower
 * @var integer
 */
js_Dialog.Speed = 10;

/**
 * minimum width and height
 * @var array
 */
js_Dialog.MinDimensions = [250, 150];

/**
 * Creates dialog with given parameters
 *
 * @param string id
 * @param array dimension [width, height]
 * @param string backImage
 * @param array dimCorner [left, top, right, bottom]
 */
js_Dialog.Create = function(id, dimension, backImage, dimCorner) {
   var obj = document.getElementById(id);
   var page_size = this.GetPageSize();
   var page_scroll = this.GetScroll();

   // fix dimensions of dialog
   if (dimension[0] < this.MinDimensions[0]) dimension[0] = this.MinDimensions[0];
   if (dimension[1] < this.MinDimensions[1]) dimension[1] = this.MinDimensions[1];

   // set basic attributes
   with (obj.style) {
      display = 'block';
      width = dimension[0] + 'px';
      height = dimension[1] + 'px';

      position = 'absolute';
      left = (Math.round((page_size[0] - dimension[0]) / 2)) + 'px';
      top = (page_scroll + Math.round((page_size[3] - dimension[1]) / 2)) + 'px';

      zIndex = 100000;
   }
   obj.style.opacity = 0;
   obj.style.visibility = 'hidden';
   this.SetTransparency(id, 0);

   // retrieve content of dialog
   content = obj.innerHTML;
   obj.innerHTML = '';

   // create container elements
   var obj_container    = document.createElement('div');
   var obj_leftTop      = document.createElement('div');
   var obj_leftBottom   = document.createElement('div');
   var obj_rightTop     = document.createElement('div');
   var obj_rightBottom  = document.createElement('div');
   var obj_content      = document.createElement('div');

   // global container
   with (obj_container.style) {
      display = 'block';
      width = '100%';
      height = '100%';

      position = 'relative';
   }

      // left top container
      with (obj_leftTop.style) {
         display = 'block';
         width = dimCorner[0] + 'px';
         height = (dimension[1] - dimCorner[3]) + 'px';

         position = 'absolute';
         top = '0px';
         left = '0px';

         overflow = 'hidden';

         backgroundImage = 'url('+backImage+')';
         backgroundPosition = 'top left';
         backgroundRepeat = 'no-repeat';
      }

      // left bottom container
      with (obj_leftBottom.style) {
         display = 'block';
         width = (dimension[0] - dimCorner[2]) + 'px';
         height = dimCorner[3] + 'px';

         position = 'absolute';
         bottom = '0px';
         left = '0px';

         overflow = 'hidden';

         backgroundImage = 'url('+backImage+')';
         backgroundPosition = 'bottom left';
         backgroundRepeat = 'no-repeat';
      }

      // right top container
      with (obj_rightTop.style) {
         display = 'block';
         width = (dimension[0] - dimCorner[0]) + 'px';
         height = dimCorner[1] + 'px';

         position = 'absolute';
         top = '0px';
         right = '0px';

         overflow = 'hidden';

         backgroundImage = 'url('+backImage+')';
         backgroundPosition = 'top right';
         backgroundRepeat = 'no-repeat';
      }

      // right bottom container
      with (obj_rightBottom.style) {
         display = 'block';
         width = dimCorner[2] + 'px';
         height = (dimension[1] - dimCorner[1]) + 'px';

         position = 'absolute';
         bottom = '0px';
         right = '0px';

         overflow = 'hidden';

         backgroundImage = 'url('+backImage+')';
         backgroundPosition = 'bottom right';
         backgroundRepeat = 'no-repeat';
      }

   // content container
   with (obj_content.style) {
      display = 'block';
      width = (dimension[0] - (dimCorner[0] + dimCorner[2])) + 'px';
      height = (dimension[1] - (dimCorner[1] + dimCorner[3])) + 'px';

      position = 'absolute';
      top = dimCorner[1]+'px';
      left = dimCorner[0]+'px';

      backgroundImage = 'url('+backImage+')';
      backgroundPosition = '-'+dimCorner[0]+'px -'+dimCorner[1]+'px';
      backgroundRepeat = 'no-repeat';
   }
   obj_content.innerHTML = content;

   obj.appendChild(obj_container);
   obj_container.appendChild(obj_leftTop);
   obj_container.appendChild(obj_leftBottom);
   obj_container.appendChild(obj_rightTop);
   obj_container.appendChild(obj_rightBottom);
   obj_container.appendChild(obj_content);
}

/**
 * Shows dialog with given ID
 *
 * @param string id
 */
js_Dialog.Show = function(id) {
   var obj = document.getElementById(id);

   // show element
   obj.style.visibility = 'visible';

   // stop current action
   if (this.Timer !== 0)
      clearInterval(this.Timer);

   // set show interval
   this.Timer = setInterval('js_Dialog.OnTimer("' + id + '", 1);', this.Speed);
}

/**
 * Shows dialog relative to obj
 *
 * @param string id
 * @param object obj
 * @param array modifiers [x,y]
 */
js_Dialog.ShowRelative = function(id, obj, modifiers) {
   this.PositionRelative(id, obj, modifiers);
   this.Show(id);
}

/**
 * Before showing dialog is placed in center of browser window
 *
 * @param string id
 */
js_Dialog.ShowCenter = function(id) {
   this.PositionCenter(id);
   this.Show(id);
}

/**
 * Hides dialog with given ID
 *js_Dialog_SetTransparency
 * @param string id
 */
js_Dialog.Hide = function(id) {
   // stop current action
   if (this.Timer !== 0)
      clearInterval(this.Timer);

   // set hide interval
   this.Timer = setInterval('js_Dialog.OnTimer("' + id + '", -1);', this.Speed);
}

/**
 * Moves dialog with specified id to center of browser window
 *
 * @param string id
 */
js_Dialog.PositionCenter = function(id) {
   var obj = document.getElementById(id);
   var obj_dim = this.GetDimensions(obj);
   var page_size = this.GetPageSize();
   var page_scroll = this.GetScroll();

   obj.style.left = (Math.round((page_size[0] - obj_dim[0]) / 2)) + 'px';
   obj.style.top = (page_scroll + Math.round((page_size[3] - obj_dim[1]) / 2)) + 'px';
}

/**
 * Positions dialog with specified id relativly to given object.
 * Modifiers specify offset from bottom right corner of specified object
 *
 * @param string id
 * @param object obj
 * @param array modifiers [x,y]
 */
js_Dialog.PositionRelative = function(id, obj, modifiers) {
   var dialog = document.getElementById(id);
   var obj_dim = this.GetDimensions(obj);
   var obj_pos = this.GetPosition(obj);

   dialog.style.left = (obj_pos[0] + obj_dim[0] - modifiers[0]) + 'px';
   dialog.style.top = (obj_pos[1] + obj_dim[1] - modifiers[1]) + 'px';
}

/**
 * OnTimer event
 *
 * @param string id
 * @param integer direction 1, -1
 */
js_Dialog.OnTimer = function(id, direction) {
   var obj = document.getElementById(id);
   var transparency = obj.style.opacity * 100;

   // increase or decrease transparency
   if (direction == 0) direction = 1;
   transparency = transparency + (5 * direction);

   // roundup transparency
   if (transparency < 0) transparency = 0;
   if (transparency > 100) transparency = 100;

   // if we finished showin stop the timer
   if (transparency == 0 || transparency == 100) {
      clearInterval(this.Timer);
      this.Timer = 0;
   }

   // hide element for real
   if (transparency == 0)
      obj.style.visibility = 'hidden';

   this.SetTransparency(id, transparency);
}

/**
 * Sets object transparency level
 *
 * @param string id
 * @param integer percent
 */
js_Dialog.SetTransparency = function(id, percent) {
   var obj = document.getElementById(id);

   // change opacity of the object
   with(obj.style) {
      opacity = (percent / 100);
      KhtmlOpacity = (percent / 100);
      filter = "alpha(opacity=" + percent + ")";
   }
}

/**
 * Returns array with page width, height and window width, height
 * Core code from - quirksmode.org
 * Edit for Firefox by pHaez
 *
 * @return array [pageWidth,pageHeight,windowWidth,windowHeight]
 */
js_Dialog.GetPageSize = function () {
   var xScroll, yScroll;

   if (window.innerHeight && window.scrollMaxY) {
      xScroll = document.body.scrollWidth;
      yScroll = window.innerHeight + window.scrollMaxY;
   } else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
      xScroll = document.body.scrollWidth;
      yScroll = document.body.scrollHeight;
   } else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
      xScroll = document.body.offsetWidth;
      yScroll = document.body.offsetHeight;
   }

   var windowWidth, windowHeight;
   if (self.innerHeight) { // all except Explorer
      windowWidth = self.innerWidth;
      windowHeight = self.innerHeight;
   } else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
      windowWidth = document.documentElement.clientWidth;
      windowHeight = document.documentElement.clientHeight;
   } else if (document.body) { // other Explorers
      windowWidth = document.body.clientWidth;
      windowHeight = document.body.clientHeight;
   }

   // for small pages with total height less then height of the viewport
   if(yScroll < windowHeight){
      pageHeight = windowHeight;
   } else {
      pageHeight = yScroll;
   }

   // for small pages with total width less then width of the viewport
   if(xScroll < windowWidth){
      pageWidth = windowWidth;
   } else {
      pageWidth = xScroll;
   }

   return [pageWidth,pageHeight,windowWidth,windowHeight];
}

/**
 * Returns array with x,y page scroll values.
 * Core code from - quirksmode.org
 *
 * @return integer
 */
js_Dialog.GetScroll = function() {
   var yScroll;

   if (self.pageYOffset) {
      yScroll = self.pageYOffset;
   } else if (document.documentElement && document.documentElement.scrollTop){    // Explorer 6 Strict
      yScroll = document.documentElement.scrollTop;
   } else if (document.body) {// all other Explorers
      yScroll = document.body.scrollTop;
   }

   return yScroll;
}

/**
 * Retrieves object dimensions
 *
 * @param object obj
 * @return array
 */
js_Dialog.GetDimensions = function(obj) {
   if (obj.clientWidth)
      return [obj.clientWidth, obj.clientHeight]; else
      return [obj.width, obj.height];
}

/**
 * Get object absolute position
 *
 * @param object obj
 * @return array
 */
js_Dialog.GetPosition = function(obj) {
   var curX = 0;
   var curY = 0;

   // find position using offsetParent
   // NOTE: IE skips objects with positon: relative
   if (obj.offsetParent) {
      do {
         curX += obj.offsetLeft;
         curY += obj.offsetTop;
      } while (obj = obj.offsetParent);
   }
   return [curX, curY];
}
