﻿var Lightbox = Class.create();

Lightbox.fileLoadingImage = "./@images/loading.gif";		
Lightbox.fileBottomNavCloseImage = "./@images/exit.gif";
Lightbox.filePrevLinkHtml = "<img src='./images/preb.gif' border='0'/>";
Lightbox.fileNextLinkHtml = "<img src='./images/nextb.gif' border='0'/>";

Lightbox.prototype = {	
	resizeSpeed : 7,	// controls the speed of the image resizing (1:slowest and 10:fastest)
	borderSize : 10,	//if you adjust the padding in the CSS, you will need to update this variable
	activeWord:0,
	resizeDuration:0,
	wordsArray:[],
	
	initialize: function(jsObj) {	
		var _this=this;
		if(this.resizeSpeed > 10){ this.resizeSpeed = 10;}
		if(this.resizeSpeed < 1){ this.resizeSpeed = 1;}
		this.resizeDuration = (11 - this.resizeSpeed) * 0.15;
		
		if(!$('overlay'))
			Lightbox.initDom();	
	},
	
	//
	//	start()
	//	Display overlay and lightbox. If image is part of a set, add siblings to imageArray.
	//
	start: function(elem) {	
		var _this=this;	
		$('overlay').onclick=Lightbox.end;		
		Lightbox.showOverlay();
		
		// calculate top offset for the lightbox and display 
		var arrayPageSize = LightboxUtil.getPageSize();
		var arrayPageScroll = LightboxUtil.getPageScroll();
		var lightboxTop = arrayPageScroll[1] + (arrayPageSize[3] / 15);
		var lightboxLeft = (arrayPageSize[2] -400)/2;

		if($('dvClose') && (!($('dvClose').onclick)))
			$('dvClose').onclick=function() { $('word_detail').style.display='none';Lightbox.end(); return false; }
	},

	enableKeyboardNav: function() {
		var _this=this;
		document.onkeydown = function(e){
			_this.keyboardAction(e);
		} 
	},

	keyboardAction: function(e) {
		if (e == null) { // ie
			keycode = event.keyCode;
		} else { // mozilla
			keycode = e.which;
		}
		var isClose=(keycode==27);

		if(isClose){	// close lightbox
			$('overlay').click();//Lightbox.end();
		}
	}
}

Lightbox.initDom=function(){
	var objBody = document.getElementsByTagName("body").item(0);
	
	var objOverlay = document.createElement("div");
	objOverlay.setAttribute('id','overlay');
	objOverlay.style.display = 'none';
	//objOverlay.onclick = function() { Lightbox.end(); return false; }		
	objBody.appendChild(objOverlay);

}

Lightbox.disableKeyboardNav=function(){
	document.onkeydown = '';
}

Lightbox.end=function(){
    $('overlay').onclick=null;
	//if(EvalUtil)EvalUtil.stop();
	Lightbox.disableKeyboardNav();		
	new Effect.Fade('overlay', { duration: 0.2});
	LightboxUtil.showSelectBoxes();
}

Lightbox.showOverlay=function(){
	LightboxUtil.hideSelectBoxes();
	//stretch overlay to fill page and fade in
	var arrayPageSize = LightboxUtil.getPageSize();
	$('overlay').style.height =arrayPageSize[1]+'px';
	new Effect.Appear('overlay', { duration: 0.2, from: 0.0, to: 0.6 });
	//Element.show('overlay');
}

Lightbox.utilShowHtml=function(template,closeId){
	if(!$('overlay'))
		Lightbox.initDom();
	Lightbox.pageLoading=false;
	var temp=template;
	var tempClose=closeId;
	if(arguments.length<2){
		if(!$('return')){
			var pr=document.createElement('div');
			pr.setAttribute('id','return');
			pr.style.display='none';		
			document.getElementsByTagName('body')[0].appendChild(pr);			
		}
		temp='return';
		$(temp).outerHTML=template;
		tempClose='close';
	}
	//if(arguments.length>1){//self defined ele show
	if($(temp)){
		var pageSize=LightboxUtil.getPageSize();
		var pageScroll=LightboxUtil.getPageScroll();
		$(temp).style.left=parseInt(pageSize[2] / 4)+'px';
		$(temp).style.top=parseInt(pageScroll[1] + (pageSize[3] / 15))+'px';
		if($(tempClose) && (!($(tempClose).onclick))){
			if(!Lightbox.closeArr)Lightbox.closeArr=[];
			if(!(Lightbox.closeArr.include($(temp))))Lightbox.closeArr.push($(temp));
			$(tempClose).onclick=function(){//solve multi show box's conflict when close
				Lightbox.closeArr.each(function(cc){cc.style.display='none';});
				new Effect.Fade('overlay', { duration: 0.2});
				LightboxUtil.showSelectBoxes();
				return false;
			};
		}
	}//!(document.onkeydown)
	if(true){
	    document.onkeydown=function(e){
	        if (e == null) { // ie
			    keycode = event.keyCode;
		    } else { // mozilla
			    keycode = e.which;
		    }
		    var isClose=(keycode==27);
		    key = String.fromCharCode(keycode).toLowerCase();
		    if(isClose){	// close lightbox
			    $('overlay').click();
		    }
	    };
	}
	if(!($('overlay').onclick)){
	    $('overlay').onclick=function(){
	        if(Lightbox.pageLoading)return;
	        $(tempClose).click();
	        document.onkeydown=null;
	    };
	}
	Lightbox.showOverlay();
	Element.show(temp);
}

Lightbox.setLoading=function(isloading,str){
	if(isloading){
		if(!$('overlay'))
			Lightbox.initDom();
		if(!$('pageloading')){
			var pl=document.createElement('div');
			pl.setAttribute('id','pageloading');
			pl.style.display='none';
			document.getElementsByTagName('body')[0].appendChild(pl);
		}
		$('pageloading').innerHTML='';
		var img=document.createElement('img');
		img.setAttribute('src',Lightbox.fileLoadingImage);
		img.style.borderWidth='0px'
		$('pageloading').appendChild(img);
		$('pageloading').appendChild(document.createTextNode(str));
		Lightbox.showOverlay();
		//img.onload=function(){
		    Element.show('pageloading');
		    Lightbox.pageLoading=true;
		//}
	}
	else{
		Element.hide('pageloading');
		if(arguments.length<2){
		    new Effect.Fade('overlay', { duration: 0.2});
		    Lightbox.pageLoading=false;
		    LightboxUtil.showSelectBoxes();
		}
	}		
}

//Lightbox util class
var LightboxUtil={};
LightboxUtil.getPageScroll=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;
	}
	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
LightboxUtil.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;
	}

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

// ---------------------------------------------------

LightboxUtil.showSelectBoxes=function(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

LightboxUtil.hideSelectBoxes=function(){
	selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

LightboxUtil.pause=function(numberMillis) {
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

LightboxUtil.getAbsolutePos = function(el){
 	var sl = 0, st = 0;	
	if (el.scrollLeft && el.scrollTop){
	  	sl = el.scrollLeft;
		st = el.scrollTop;
 	}
 	var r = { x: el.offsetLeft - sl, y: el.offsetTop - st };
 	if(el.offsetParent){
		var tmp = LightboxUtil.getAbsolutePos(el.offsetParent);
		r.x += tmp.x;
		r.y += tmp.y;
	}
 	return r;
};
