//includeCSS('/ve/res/css/customBox.css');

function disableBodyScroll() {
 var b = document.body;
 var win = getWindowSize();
 var temp = win.st;
 b.style.overflow = 'hidden';
 if( document.all ) {
  document.all[1].style.overflow = 'hidden';
 } else {
  b.style.marginTop = (temp*-1) + 'px';
 }
}

function enableBodyScroll() {
 var b = document.body;
 var temp = b.style.marginTop;
 if( document.all ) {
  document.all[1].style.overflow = '';
 }
 b.style.overflow = 'auto';
 if( !document.all ) {
  b.style.marginTop = 0 + 'px';
  temp = parseInt(temp)*-1;
  window.scrollTo(0,temp);
 }
}



function keepCenter(id) {
 var item = document.getElementById(id);
 var win = getWindowSize();
 item.style.left = ( win.w / 2 ) - ( item.clientWidth / 2 ) + win.sl + 'px';
 item.style.top = ( win.h / 2 ) - ( item.clientHeight / 2 ) + win.st + 'px';
}


var CustomPopup = {

 open: function( id, modal, custom, dragable, title, frameURL, popupObject ) {
  var popup = document.getElementById(id);
  var header = title;
  var body = '';
  var footer = '';
  if ( popup != null ) {   // id exists?
   body = popup.innerHTML;
  } else {
   custom = true;
  }  // end id exists?
  if ( modal != null ) {   // begin modal
   this.createShroud(modal);
  }   // end modal
  if ( frameURL != null ) {   // begin iframe
   body = '<iframe src="' + frameURL + '" frameborder="0" width="100%"></iframe>';
  }   // end iframe
  else if ( custom == true && popup != null ) { // fix for repeat custom div
   if ( popup.className == 'customBox' ) {
    body = popup.getElementsByTagName('DIV')[4].innerHTML;
   }
  } // end fix

  if ( popupObject ) {  // custom object
   for( var i in popupObject ) {
    if( i.match( "action_.*" ) != null ) {
     var action = i.substring( 7 );
     var actionName = action;
     if ( popupObject.buttons ) {
      actionName = popupObject.buttons[action][0];
     }
     //footer += '<input type="button" value="' + actionName + '" class="button" onclick=\'CustomPopup.execute( "' + action + '" );CustomPopup.close("' + id + '")\' />';
     footer += '<input type="button" value="' + actionName + '" class="button" onclick=\'CustomPopup.execute( "' + action + '" );\' />';
    }
   }
   this.CurrentPopup = popupObject;
   if ( custom != true ) {
    var buttonholder = newElement('div', popup, 'popupButtons', 'popupButtons');
    buttonholder.innerHTML = footer;
   }
   if ( popupObject.body ) {
    body = popupObject.body;
   }
   if ( popupObject.attributes ) {

   }
   // ************************************************************************
   /*var buttons = buttonholder.getElementsByTagName('INPUT');   // begin all buttons same width
   var maxwidth = 0;
   for ( var i = 0; i < buttons.length; i++ ) {
    maxwidth = Math.max(buttons[i].clientWidth, maxwidth);
   }
   for ( var i = 0; i < buttons.length; i++ ) {
    buttons[i].style.width = maxwidth;
   }   // end all buttons same width     */
   // ************************************************************************
  } else {  // end custom object
   // create title bar for non custom
  }

  if ( custom == true ) { // begin custom
   if ( popup != null ) {
    popup.parentNode.removeChild(popup);
   }
   this.createCustomBox(id, header, body, footer);
   popup = document.getElementById(id);
  } // end custom
  popup.style.display = 'block';
  popup.style.zIndex = '1001';
  popup.style.position= 'absolute';
  CustomPopup.resize(id);
  if ( dragable != true || modal != null ) {  // hold center
   window.onresize = function() { keepCenter( id ); };
  } else if ( dragable == true ) {  // enable move
   if ( custom != true ) {
    popup.style.cursor = 'move';
    popup.setAttribute('onmousedown', 'MouseMover.newmover(this)');
   } else {
    document.getElementById(id+'Titlebar').style.cursor = 'move';
    document.getElementById(id+'Titlebar').onmousedown = function () { MouseMover.newmover(this.parentNode.parentNode.parentNode.parentNode); };
   }
  } // end move or hold center
  if ( popupObject ) {  // custom object
   if ( popupObject.onOpen ) {
    popupObject.onOpen();
   }
  }
 },

 resize: function( id ) {
  var popup = document.getElementById(id);
  var b = document.body;
  var w = popup.style.width = 200;
  var h = popup.clientHeight;
  var bh = b.clientHeight;
  var bw = b.clientWidth;
  while( ((h/w) > (bh / bw)) && (w < ((4/5) * bw)) ) {
   w += 10;
   popup.style.width = w + 'px';
   h = popup.clientHeight;
  }
  var win = getWindowSize();
  popup.style.left = ( win.w / 2 ) - ( popup.clientWidth / 2 ) + win.sl + 'px';
  popup.style.top = ( win.h / 2 ) - ( popup.clientHeight / 2 ) + win.st + 'px';
 },

 execute: function( action ) {
  eval( "this.CurrentPopup.action_" + action + "()" );
 },

 close: function( id ) {
  var popup = document.getElementById(id);
  popup.style.display = 'none';
  if ( document.getElementById('modal_shroud') ) {
   enableBodyScroll();
   document.getElementById('modal_shroud').style.display = 'none';
   document.getElementById('modal_shroudFrame').style.display = 'none';
  }
  window.onresize = '';
 },

 createCustomBox: function(id, header, body, footer) {
  var custom = newElement('div', document.body, id, 'customBox');
  custom = newElement('div', custom, null, 'm');
  var h = newElement('div', custom, null, 'h');
  h = newElement('div', h, null, 'h2');
  h = newElement('div', h, id+'Titlebar', null);
  var b = newElement('div', custom, null, 'b');
  var f = newElement('div', custom, null, 'f');
  h.innerHTML = '<a href=\'javascript:CustomPopup.close("' + id + '")\'>&nbsp;</a>' + header;
  b.innerHTML = body;
  f.innerHTML = footer;
 },

 createShroud: function(opacity) {
  var b = document.body;
  if ( !document.getElementById('modal_shroud') ) {
   newElement('div', b, 'modal_shroud', 'modalShroud');
  }
  if ( !document.getElementById('modal_shroudFrame') ) {
   newElement('iframe', b, 'modal_shroudFrame', 'modalShroudFrame');
  }
  var shroud = document.getElementById('modal_shroud');
  var frame = document.getElementById('modal_shroudFrame');
  disableBodyScroll();
  var win = getWindowSize();  
  shroud.style.filter = 'progid:DXImageTransform.Microsoft.alpha(opacity=' + opacity + ')';
  shroud.style.opacity = opacity * 0.01;
  shroud.style.top = win.st + 'px';
  shroud.style.left = win.sl + 'px';
  shroud.style.display = 'block';
  shroud.style.width = screen.availWidth + 100 + 'px';
  shroud.style.height = screen.availHeight + 100 + 'px';
  shroud.style.backgroundColor = '#000';
  shroud.style.position = 'absolute';
  shroud.style.zIndex = '1000';
  frame.style.filter = 'progid:DXImageTransform.Microsoft.alpha(opacity=0)';
  frame.style.opacity = '0';
  frame.style.top = win.st + 'px';
  frame.style.left = win.sl + 'px';
  frame.style.display = 'block';
  frame.style.width = screen.availWidth + 100 + 'px';
  frame.style.height = screen.availHeight + 100 + 'px';
  frame.style.position = 'absolute';
  frame.style.zIndex = '5';
  //disableBodyScroll();
 }

};


var VE_Alert = {
 action_OK: function() { }
};


var VE_Confirm = {
 action_OK: function() { },
 action_Cancel: function() { }
};


function veAlert( text, title ) {
 if ( !title ) { title = "Attention!"; }
 VE_Alert.body = text;
 CustomPopup.open('ve_alert', 0, true, true, title, null, VE_Alert);
}


function veConfirm( text, title, okaction ) {
 if ( !title || title == null ) { title = "Please Confirm..."; }
 VE_Confirm.body = text;
 VE_Confirm.action_OK = function() { okaction(); };
 CustomPopup.open('ve_confirm', 0, true, true, title, null, VE_Confirm);
}