/*
 * Lightbox use jQuery library - Require $ 1.2+
 * @Version:	1.0.4

Usage: 	caobox.show('boxID', 'overlayColor', 'overlayOpacity', 'overlayDisable', 'leftPosition', 'topPosition')
Exp:	caobox.show('testID', '000', '50', '', '10', '10')
Arguments:
	divID : [char] Div’s id
	overlayColor : [hex number] Used to define the overlay background color.  ‘000‘ (black) is default
	overlayOpacity : [int 0~100] Used to define the overlay opacity. ‘50‘ is default.
	overlayDisable : ['false'] Used to disable click on overlay to close popup. null is default
	leftPosition : [int] The padding CSS information used in the left position of image box. null mean center is default. [int]<0 is padding-right position
	topPosition : [int] The padding CSS information used in the top position of image box. null mean middle is default. [int]<0 is padding-bottom position
*/
var inner_boxID ;
var cloneTemp;
var caobox = {		
	fadeEffectDuration : 0,
	fade:"#fade",
	light:"#light",
	boxID:"",
	overlayBgColor:"#B4B5A6",
	overlayOpacity:"80",
	
	show:function(boxId, overlayBgColor, overlayOpacity, overlayDisable, leftPosition, topPosition, callback)
	{
		// Get layer Background color & opacity
		if (overlayBgColor) this.overlayBgColor = "#"+ overlayBgColor;
		if (overlayOpacity) this.overlayOpacity = overlayOpacity;
		
		// Add layer
		$('body').prepend('<div id="light"></div><div id="fade"></div>');		
		$('#light, #fade').css({'display':'none', 'position':'absolute'});
		$('#light').css({	'zIndex':'10002',
							'height':'auto'/*, 'overflow':'hidden'*/});
		$('#fade').css({	'zIndex':'10001',
					   		'left':'0',
							'top':'0',
					   		'backgroundColor': ''+ this.overlayBgColor +'',
					   		'opacity':'.'+ this.overlayOpacity+'',
							'filter':'alpha(opacity='+ this.overlayOpacity +')'
						}).click(	function(){
										//if (overlayDisable) return true;
										//else caobox.hide();
									});
	
		// Show layer
		this.boxID='#'+boxId;		
		$('object, embed, select').css('visibility','hidden');
		$('object, embed').css('display','none');

		
		var autodectect = boxId.substring(boxId.length-3, boxId.length);
		
		// Auto dectect Ajax loading
		if ((autodectect=="tml")||(autodectect=="php")){
			$(this.light).load(boxId, function(){});
		}
		else{		
			//inner_boxID = $(this.boxID).html();
			var oClone = $(this.boxID).clone();
			$(this.boxID).remove();
			$(this.boxID+'_hidden').remove();
			oClone.appendTo($(this.light));
			//cloneTemp = $(this.boxID).parent().clone().html();
			
			//$(this.light).append(cloneTemp);

			//if (
			//$(this.boxID).parent().siblings().find('.lightbox').attr('id');
			
			//$('.lightbox:last').html('');
			
			
			/*if (boxId) var filetype = inner_boxID.substring(inner_boxID.length-3, inner_boxID.length);*/
		// FLV		
		//		if (filetype == "flv"){
		//			var embedFLV = '<object height="400" width="420" type="application/x-shockwave-flash" data="'+inner_boxID+'&bufferTime=3&autoStart=false">'
		//				+'<param value="true" name="allowfullscreen"/>'
		//				+'<param value="high" name="quality"/>'
		//				+'<param value="'+inner_boxID+'" name="src"/></object>';		
		//			var full_boxID = "<div id="+ boxId +">"+ embedFLV +"</div>";
		//			$(this.light).append(full_boxID);
		//		}
		//		else{
		//			$(this.boxID).clone().appendTo($(this.light));
		//		}
		}
		
		//$(this.boxID).show();
		//$(this.light).fadeIn('slow', function(){
			 //$(this.light).css({display:'block'}); 
		//});
		
		
		//$(this.light).fadeIn( this.fadeEffectDuration, function(){	
		$(this.light).slideDown('0', function(){		
			//callback
			if (callback){
				var callbackFunction = callback;
				window[callbackFunction]();				
			}
		});
		

		//var _top = Math.round((getDocumentSize(3) - $(this.light).height()) / 2);
		
		// Left position, default is center
		/*if (leftPosition){
			_left = leftPosition;
			if (leftPosition > 0) $(this.light).css({left: _left+ "px"});
			else			
				$(this.light).css({right: Math.abs(_left) + "px"});			
		}
		else{
			_left = Math.round(getDocumentSize(2) - $(this.light).width());
			_left = Math.round((getDocumentSize(2) - 945) / 2);
			$(this.light).css({left: _left+ "px"});
		}	*/	
		_left = Math.round((getDocumentSize(2) - $(this.light).width())/2);
		
		// Top position, default is middle
		/*if (topPosition){			
			if (topPosition > 0){
				_top = topPosition;
				$(this.light).css({top: _top+"px"});
			}
			else{
				_top = Math.abs(topPosition);
				$(this.light).css({bottom: _top+"px"});
			}
		}		
		else{
			if(_top < 0) _top = 20;		
			$(this.light).css({top: _top+"px"});
		}*/
		$(this.light).css({left: _left});
		$(this.light).css({top: "32px"});
		
		$('#light object, #light embed, #light select').css({visibility:"visible", display:"block"});
		caobox.showAlertLayer();
		
		//alert($('.lightbox').attr('id'));
		//$('body').children().find('#'+boxId).html('');
		
		//caobox.setFocusElement();
		window.scrollTo(0,0);
		window.onresize=caobox.resizeLayers;
	},
	
	hide:function(){
		//$(this.fade).fadeOut(this.fadeEffectDuration);
		$(this.fade).hide();
		
		//$(this.light).fadeOut(this.fadeEffectDuration, function(){
		$(this.light).hide(function(){
			$('object, embed, select').css('visibility','visible');
			$('object, embed, select').css('display','');
			$('#fade, #light').remove();
			
		});
		
		var lightboxID = $(this.boxID).attr('id');
		$('#'+lightboxID+':last').html(inner_boxID);		
		returnBoxID = $('.lightbox').attr('id');
		var oClone = $(this.boxID).clone();
		$('#light').remove();
		var boxhidden = this.boxID.substr(1)+'_hidden';
		$('body').append('<div id="'+boxhidden+'" style="display: none;"></div>');
		oClone.appendTo($('#'+boxhidden));
		
		
		
/*		cloneTemp = $(this.boxID).parent().html();
		$(this.boxID).parent().html('');
		$('body').append('<div style="display:none">'+cloneTemp+'</div>');
*/		
		
		
	},
	
	showAlertLayer : function(){
		var _height=getDocumentSize(1);
		var _width =getDocumentSize(0);
		
		if(_width < 974) 
			 _width = 974;	
		
		if($(this.boxID).height() > getDocumentSize(1))
			_height=parseInt($(this.boxID).height()) + 65;		
		
		$(this.fade).css({width:_width +'px'});
		$(this.fade).css({height:_height +'px'});	
		
		//$(this.fade).css({display:'block'}); 
		//$(this.fade).fadeIn( this.fadeEffectDuration);
		$(this.fade).show();
	},
	
	resizeLayers : function(){
		if($(this.boxID).height() > getDocumentSize(1)){
			var _height = parseInt($(this.boxID).height());
			var pageHeight = _height;
		}
		else{
			if (getDocumentSize(1) > getDocumentSize(3)){ var pageHeight=getDocumentSize(1)}
			else{ var pageHeight=getDocumentSize(3);}
		};
		
		if(getDocumentSize(0) < 974) $('#fade').css({width:974 + "px"});
		else $('#fade').css({width:getDocumentSize(0) + "px"});
		$('#fade').css({height:pageHeight + 'px'});
		$('#light').css({left: Math.round((getDocumentSize(2) - $('#light').width()) / 2)+"px"});
		
		var _top=Math.round((getDocumentSize(3) - $(this.light).height()) / 2);
		if(_top < 0) _top = 20;
		$(this.light).css({top:_top+"px"});
	},
	
	findFucusElement:function(){
		$("#light").find('object, embed, select').css({'visibility':'visible'});
		$("#light").find('input[@type!=hidden], a, select, textarea').addClass('setFocusAble');			
		return $('.setFocusAble');
	},
	
	setFocusElement:function(){
		var i = 0;
		var cntElement = 0;	
		var allFocusElement = caobox.findFucusElement();
		var len=allFocusElement.length;	
		
		if (len>0){
			for(var temJ = 0; temJ<len-1; temJ++){
				$(allFocusElement[temJ]).attr('rel',temJ+1);
			}
			
			// fix focus on 1st hidden element
			if ( allFocusElement[0].style.display == 'none') 
				allFocusElement[1].focus();
			else
				allFocusElement[0].focus();
	
	
			allFocusElement.focus(function(){ cntElement=this.getAttribute('rel'); });
			$("#light").keydown(function(event){									 
				if(event.shiftKey){
					if(event.keyCode == 9){					
						if (i!=cntElement) i=cntElement;
						i--;			
						if (i == 0) {
							i = len-1;
							allFocusElement[i].focus();
							return false;
						}
					}
				}
				else{
					if(event.keyCode == 9){
						if (i != cntElement) i = cntElement;
						if (i == len-1){
							i=0;
							allFocusElement[0].focus();						
							return false;
						}
						else{
							i++;
						}
					}
				}			
			}); 
		}
	},
	
	unbindClickFace:function(){
		$(this.fade).unbind('click');
	},
	
	init:function()
	{		
		$(document).ready(function(){
			$('.caobox-popup').hide();
			$('body').css({'margin':'0', 'padding':'0'}); // hack browser
			$('img.caobox-popup').css({ 'border':'5px solid #000'});
		})		
	}
};
caobox.init({							
});

// Get ducument size
// @param int 0 ~ 3
// @return array [width of current page] [height of current page] [width of window] [height of window]
function getDocumentSize(val){
	var xScroll,yScroll,value;
	
	if(window.innerHeight&&window.scrollMaxY){
		xScroll=window.innerWidth+window.scrollMaxX;
		yScroll=window.innerHeight+window.scrollMaxY;
	}
	else if(document.body.scrollHeight>document.body.offsetHeight){
		xScroll=document.body.scrollWidth;
		yScroll=document.body.scrollHeight;
		}
		else{
			xScroll=document.body.offsetWidth;
			yScroll=document.body.offsetHeight;
		}
	
	var windowWidth,windowHeight;
	
	if(self.innerHeight){
		if(document.documentElement.clientWidth){
			windowWidth=document.documentElement.clientWidth;
		}
		else{
			windowWidth=self.innerWidth;
		}
		windowHeight=self.innerHeight;
	}
	else if(document.documentElement&&document.documentElement.clientHeight){
			windowWidth=document.documentElement.clientWidth;
			windowHeight=document.documentElement.clientHeight;
		}
		else if(document.body){
			windowWidth=document.body.clientWidth;
			windowHeight=document.body.clientHeight;
		}
	
	if(yScroll<windowHeight){
		pageHeight=windowHeight;
	}
	else{
		pageHeight=yScroll
	}
	
	if(xScroll<windowWidth){
		pageWidth=xScroll
	}
	else{
		pageWidth=windowWidth
	}
	
	arrayPageSize=new Array(pageWidth, pageHeight, windowWidth, windowHeight);
	return arrayPageSize[val];
};
