/*-----------------------

Title : Lightbox Main Js Class
Author : Zia
URL : http://arbisoft.com/

Created : May 3, 2007
Modified : May 5, 2007

------------------------- */


var lightbox = Class.create();
lightbox.noofinstances = 0;
lightbox.prototype = {
	boxid:'',
	parentDialog: null,
	loaded:0,
	yPos: 0,
	xPos: 0,
	loadFiles:Array(),
	loadedFiles:'',
	dimensions:Array(0,0),
	UA:Array('Explorer','7'),
	title:'',
	isActive:false,
	mychildren:Array(),
	cacheIt :false,
	canMaximize:false,
	callbackfunc:'',
	HTMLContent:'',
	background:'',

	initialize: function(ctrl,title,parent) {
		lightbox.noofinstances += 1;
		this.boxid = ctrl;
		this.title = title;
		this.parentDialog = parent;
		bod 				= document.getElementsByTagName('body')[0];
		overlay 			= document.createElement('div');
		overlay.id		= 'overlay_'+this.boxid;
		overlay.className = 'overlay';
		lb					= document.createElement('div');
		lb.id				= 'lightbox_'+this.boxid;
		lb.className 	= 'lightbox';

		if(this.parentDialog==null)
		{
			bod.appendChild(overlay);
			$('overlay_'+this.boxid).setStyle({ zIndex: lightbox.noofinstances+5000});
		}

		bod.appendChild(lb);
		$('lightbox_'+this.boxid).setStyle({ zIndex: lightbox.noofinstances+6000});
		Event.observe(document, 'mousedown', this.startMoveDialog.bind(this), false);
		Event.observe(document, 'mouseup', this.endMoveDialog.bind(this), false);
		Event.observe(document, 'mousemove', this.moveDialog.bind(this), false);

	},
	startMoveDialog: function(evt){
		var obj = Event.element(evt);
		if(obj != null && (obj.className == 'titlebar' || obj.className == 'titlebutton'))
		{
			if(obj.className == 'titlebar')
			var arrids = obj.id.split("_");
			else
			var arrids = obj.parentNode.id.split("_");
			this.movingdialog_id = arrids[1];

			this.dragIt = true;
			this.dialog_tx = parseInt($('lightbox_'+this.movingdialog_id).style.left+0);
			this.dialog_ty = parseInt($('lightbox_'+this.movingdialog_id).style.top+0);

			this.dialog_x = (this.UA[0] == 'Explorer') ? event.clientX : evt.clientX;
			this.dialog_y = (this.UA[0] == 'Explorer') ? event.clientY : evt.clientY;
			return false;
		}

	},
	endMoveDialog: function(){
		this.dragIt =false;
	},
	moveDialog: function(evt){
		if(this.dragIt==true) {
			$('lightbox_'+this.movingdialog_id).style.left = (this.UA[0] == 'Explorer') ? this.dialog_tx + event.clientX - this.dialog_x : this.dialog_tx + evt.clientX - this.dialog_x;
			$('lightbox_'+this.movingdialog_id).style.top  = (this.UA[0] == 'Explorer') ? this.dialog_ty + event.clientY - this.dialog_y : this.dialog_ty + evt.clientY - this.dialog_y;
			return false;
		}
	},

	activate: function(){
		if(this.isActive)
		return;
		if (this.UA[0] == 'Explorer' && parseInt(this.UA[1]) <= 6){
			if(this.parentDialog == null){
				this.getScroll();
				this.prepareIE('100%', 'hidden');
				this.setScroll(0,0);
			}
			//if(this.loaded == 0)
			this.hideSelects('hidden');
		}

		this.displayLightbox("block");
		this.isActive=true;

	},

	// Ie requires height to 100% and overflow hidden or else you can scroll down past the lightbox
	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 lightbox
	hideSelects: function(visibility){
		var bodyselects = document.getElementsByTagName('select');
		var dialogNodeList = $('lightbox_'+this.boxid).getElementsByTagName('select');
		var nodes = $A(dialogNodeList);

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

			var alreadynodelist = true;
			nodes.each(function(node){
				if(node === bodyselects[i])
				{
					alreadynodelist = false;
					return ;
				}
			});
			if(alreadynodelist)
			bodyselects[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);
	},

	displayLightbox: function(display){
		if(this.parentDialog==null)
		{
			$('overlay_'+this.boxid).style.display = display;
			var bodyheight1 = document.body.scrollHeight;
			var bodyheight2 = document.body.offsetHeight;
			var overleyheight = (bodyheight1>bodyheight2)?bodyheight1:bodyheight2;
			//no need of overlay?
			//$('overlay_'+this.boxid).setStyle({ "display": display,"height":overleyheight+'px'});

		}

		if(display != 'none' && this.loaded == 0) {
			if(this.HTMLContent=='')
			this.loadInfo();
			else
			this.setHTMLContent(this.HTMLContent);
		}
		else if(display != 'none' && this.loaded == 1){
			$(this.boxid).style.display = display;
			setTimeout(this.callbackfunc,500);
		}
		else if(display == 'none' && this.cacheIt == false)
		{
			this.isActive=false;
			this.maximized = false;
			if($(this.boxid) != null)
			Element.remove(this.boxid);
		}
		else
		{
			$(this.boxid).style.display = display;
			this.isActive=false;
		}

	},

	// Begin Ajax request based off of the href of the clicked linked
	loadInfo: function() {
		var myAjax = new Ajax.Request(
		this.content,
		{method: 'post', parameters: "",evalScripts: true, onComplete: this.processInfo.bindAsEventListener(this)}
		);

	},
	setHTMLContent: function(contents){
		info = "<div id='"+ this.boxid +"' class='popup_body'><div class='closebutton'";
		if(this.canMaximize)
		info += " style='width:34px'><div id=\""+this.boxid+"_maxbutton\" class='maxbutton' title='Maximize/Restore' onclick=\"javascript:eval('"+this.boxid+".maximize()')\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div>&nbsp;";
		else
		info += " style='width:12px'>";
		info += "<a href=\"javascript:eval('"+this.boxid+".deactivate()')\" class='black_link' title='Close'>X</a></div><div class='titlebutton'></div><div class='titlebar' id='titlebutton_"+this.boxid+"'>"+this.title+"</div><div class='dialogcontent' id='maincont"+ this.boxid +"'>" + contents + "</div><div class='dialog_footer'><div class='footer_right'></div><div class='footer_left'></div><div class='footer_cont'>&nbsp;</div></div></div>";

		contai 			= document.createElement('div');
		contai.innerHTML = info;
		$('lightbox_'+this.boxid).appendChild(contai);
		this.setInitPosition();
		$("maincont"+ this.boxid).setStyle({'overflowX': 'hidden','overflowY': 'auto'});
		if(this.background != '')
		$("maincont"+ this.boxid).setStyle({'backgroundColor': this.background});
		//cursor problem FF
		//$("lightbox_"+ this.boxid).setStyle({'position':'absolute'});
		$('lightbox_'+this.boxid).style.display = 'block';

		if(this.cacheIt)
		this.loaded = 1;
		if(this.loadExtFile())
		setTimeout(this.callbackfunc,500);
	},
	setHorizontalScroll: function()
	{
		if(arguments[0]==true)
		$("maincont"+ this.boxid).setStyle({'overflowX': 'auto'});
		else
		$("maincont"+ this.boxid).setStyle({'overflowX': 'hidden'});
		$(this.boxid).style.width = $(this.boxid).offsetWidth;
	},
	processInfo: function(response){
		this.setHTMLContent(response.responseText);
	},
	setInitPosition: function()
	{

		var contwidth = $('lightbox_'+this.boxid).offsetWidth;
		var contheight = $('lightbox_'+this.boxid).offsetHeight;
		var availwidth = (this.dimensions[0]>0)?this.dimensions[0]:this.getViewportWidth();
		var availheight = (this.dimensions[1]>0)?this.dimensions[1]:this.getViewportHeight();
		$("maincont"+ this.boxid).setStyle({'height': availheight + 'px'});
		$(this.boxid).setStyle({'width': availwidth + 'px'});
		//if(this.UA[0] == 'Explorer')
		$('lightbox_'+this.boxid).setStyle({'top': Math.round((this.getViewportHeight()-availheight)/2)+this.getScrollTop()+'px'});
		//else
		//$('lightbox_'+this.boxid).setStyle({'top': Math.round((this.getViewportHeight()-availheight)/2)+'px'});
		$('lightbox_'+this.boxid).setStyle({'left': Math.round((this.getViewportWidth()-availwidth)/2)+this.getScrollLeft()+'px'});

	},
	maximize: function()
	{
		if(this.maximized)
		this.restore();
		else{
			$('lightbox_'+this.boxid).setStyle({'left': 0,'top':0});
			var scrolltop = (document.documentElement.scrollHeight || document.body.scrollHeight);
			$("maincont"+ this.boxid).setStyle({'height': scrolltop-20 + 'px'});
			//$("maincont"+ this.boxid).setStyle({'height': this.getViewportHeight()+scrolltop-20 + 'px'});
			var boxwidth = (this.UA[0] == 'Firefox')?this.getViewportWidth()-20:this.getViewportWidth();
			$(this.boxid).setStyle({'width': boxwidth + 'px'});
			$(this.boxid+"_maxbutton").className = "restorebutton";
			this.maximized = true;
		}


	},
	restore: function()
	{
		this.setInitPosition();
		$(this.boxid+"_maxbutton").className = "maxbutton";
		this.maximized = false;
	},
	// Load any required files
	loadExtFile: function (){

		if (!document.getElementById)
		return false;
		for (i=0; i<this.loadFiles.length; i++){
			var file=this.loadFiles[i];
			var fileref="";
			if (this.loadedFiles.indexOf(file)==-1){
				if (file.indexOf(".js")!=-1){ //If object is a js file
					fileref=document.createElement('script');
					fileref.setAttribute("type","text/javascript");
					fileref.setAttribute("src", file);
				}
				else if (file.indexOf(".css")!=-1){ //If object is a css file
					fileref=document.createElement("link");
					fileref.setAttribute("rel", "stylesheet");
					fileref.setAttribute("type", "text/css");
					fileref.setAttribute("href", file);
				}
			}
			if (fileref!=""){
				document.getElementsByTagName("head")[0].appendChild(fileref);
				this.loadedFiles+=file+" " ;//Remember this object as being already added to page
			}
		}
		return true;
	},
	getViewportHeight: function() {
		if (window.innerHeight!=window.undefined) return window.innerHeight;
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
		if (document.body) return document.body.clientHeight;

		return window.undefined;
	},

	getViewportWidth: function() {
		if (window.innerWidth!=window.undefined) return window.innerWidth;
		if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth;
		if (document.body) return document.body.clientWidth;
	},
	getScrollTop: function() {
		if (self.pageYOffset)
		{
			return self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
		{
			return document.documentElement.scrollTop;
		}
		else if (document.body)
		{
			return document.body.scrollTop;
		}
	},

	getScrollLeft:function() {
		if (self.pageXOffset)
		{
			return self.pageXOffset;
		}
		else if (document.documentElement && document.documentElement.scrollLeft)
		{
			return document.documentElement.scrollLeft;
		}
		else if (document.body)
		{
			return document.body.scrollLeft;
		}
	},
	btnChangeHoverClass:function() {
		var container = arguments[0];
		if(arguments[1] == 'over')
		{

			container.className = 'normal_button_center_hover';
			var childs =container.childNodes;
			for(var i=0;i<childs.length;i++)
			{
				if(childs[i].className == "normal_button_a")
				childs[i].className = "normal_button_a_hover";
			}
			var childs =container.parentNode.childNodes;
			for(var i=0;i<childs.length;i++)
			{
				if(childs[i].className == "normal_button_left")
				childs[i].className = "normal_button_left_hover";
				else if(childs[i].className == "normal_button_right")
				childs[i].className = "normal_button_right_hover";
			}
		}
		else
		{
			container.className = 'normal_button_center';
			var childs =container.childNodes;
			for(var i=0;i<childs.length;i++)
			{
				if(childs[i].className == "normal_button_a_hover")
				childs[i].className = "normal_button_a";
			}
			var childs =container.parentNode.childNodes;
			for(var i=0;i<childs.length;i++)
			{
				if(childs[i].className == "normal_button_left_hover")
				childs[i].className = "normal_button_left";
				else if(childs[i].className == "normal_button_right_hover")
				childs[i].className = "normal_button_right";
				else if(childs[i].className == "normal_button_a_hover")
				childs[i].className = "normal_button_a";

			}

		}

	},

	deactivate: function(){
		for(var i=0;i<this.mychildren.length;i++)
		{
			if(eval(this.mychildren[i]+".isActive"))
			eval(this.mychildren[i]+".displayLightbox('none')");
		}
		this.displayLightbox("none");

		if (this.UA[0] == 'Explorer' && parseInt(this.UA[1]) <= 6){
			if(this.parentDialog == null)
			{
				this.setScroll(0,this.yPos);
				this.prepareIE("auto", "auto");
			}
			this.hideSelects("visible");
		}
	}
}