/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var Modal_Class = {
  mCreate: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var oModal = Modal_Class.mCreate();
oModal.prototype = {

	id   : 'ith_null',
	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.id=ctrl.id;
		/*this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.activate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};*/
	},
	
	// Turn everything on - mainly the IE fixes
	activate: function(){
		if (oClient.bIE){
			this.getScroll();
			this.prepareIE('100%', 'hidden');
			this.setScroll(0,0);
			this.hideSelects('hidden');
		}
		this.displayModal("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the Modal
	prepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;

		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the Modal
	hideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	getScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	setScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayModal: function(display){
		document.getElementById('ith_modal_overlay').style.display = display;
		var modal=document.getElementById(this.id);
		modal.style.display = display;
		var form = document.getElementsByTagName('form')[0];
		var iWidth = oIThelp.mGetAttribute(modal, 'contentwidth');
		if(iWidth)
			modal.style.left = (50*(1-iWidth/form.offsetWidth))+'%';
		else
			modal.style.left=(50*(1-320.0/form.offsetWidth))+'%';
		modal.style.top='10%';
	},
	
	deactivate: function(){
		if (oClient.bIE){
			this.setScroll(0,this.yPos);
			this.prepareIE("auto", "auto");
			this.hideSelects("visible");
		}
		
		this.displayModal("none");
	}
}

/*-----------------------------------------------------------------------------------------------*/

var oModals = {
	// Onload, make all links that need to trigger a Modal active
	mInit : function() {
		lbox = oIThelp.getAllElementsByClassName(document,'ith_modal_panel');
		if (lbox.length>0) {
			oModals.addOverlay();
			var form = document.getElementsByTagName('form')[0];
			for(i = 0; i < lbox.length; i++) {
				newNode=lbox[i].cloneNode(true);
				lbox[i].parentNode.removeChild(lbox[i]);
				form.appendChild(newNode);
				newNode.modal = new oModal(newNode);
			}
		}
	},

	// Add in markup necessary to make this work.
	// Overlay holds the shadow
	// Modal is the centered square that the content is put into.
	addOverlay : function() {
		var form = document.getElementsByTagName('form')[0];
		overlay = document.createElement('div');
		overlay.id = 'ith_modal_overlay';
		overlay.className = 'ith_modal_overlay';
		form.appendChild(overlay);
	}
}

// initialize
//Event.observe(window, 'unload', Event.unloadCache, false);
oGod.mOnload ( oModals.mInit );