/*
 * Lightbox use jQuery library, rewrite in OOP - Require jQuery 1.2+
 * Type:	File
 * Name: 	CAOBox.js
 * Date:	2008/05/12
 * @Author:	Tuyen Cao <tuyenck@von-inc.com>
 * @Version:1.0
 * @Copyright 2008 Timnhanh.com
 */

var caobox = {		
	fadeEffectDuration : 100,
	fade:"#fade",
	light:"#light",
	
	show_popup:function(boxId)
	{	
//		jQuery('object, embed, select').css('visibility','hidden');
		jQuery('select').css('visibility','hidden');
		jQuery(this.light).html(jQuery('#'+boxId)[0].innerHTML)
		jQuery(this.light).fadeIn(this.fadeEffectDuration, function()
		{					
			 jQuery(this.light).css({display:'block'}); 
		});	

                jQuery(this.light).css({left: Math.round((TNCom_getDocumentSize(2) - jQuery(this.light).width()) / 2)+"px"});
	         if(window.scrollY)
	         {
       	             jQuery(this.light).css({top: Math.round(((TNCom_getDocumentSize(3) - jQuery(this.light).height()) / 2)+window.scrollY)+"px"});
		 } else {
       	             jQuery(this.light).css({top: Math.round(((TNCom_getDocumentSize(3) - jQuery(this.light).height()) / 2)+document.documentElement.scrollTop)+"px"});
		 }
		
		caobox.showalertlayer();	
		//caobox.setFocusElement();
//		window.scrollTo(0,0);
		window.onresize=caobox.resize_layers;
	},
	
	close_popup:function(){			
		jQuery(this.light).fadeOut(350, function(){
		jQuery(this.light).css({display:'none'}); 
			jQuery('#fade').css({display:'none'}); 
			jQuery('object, embed, select').css('visibility','');
		});				
	},
	
	showalertlayer : function(){		
		if(TNCom_getDocumentSize(0)<974) 
			 jQuery(this.fade).css({width:"974px"});
		else 
			jQuery(this.fade).css({width:TNCom_getDocumentSize(0)+"px"});
			
		jQuery(this.fade).css({height:TNCom_getDocumentSize(1)+'px'});	
		jQuery(this.fade).css({display:'block'}); 
	},
	
	resize_layers : function(){
		if(TNCom_getDocumentSize(1 ) > TNCom_getDocumentSize(3))
			 var pageHeight=TNCom_getDocumentSize(1)
		else var pageHeight=TNCom_getDocumentSize(3)
		if(TNCom_getDocumentSize(0) < 974)
			 jQuery('#fade').css({width:974 + "px"});
		else jQuery('#fade').css({width:TNCom_getDocumentSize(0) + "px"});
		jQuery('#fade').css({height:pageHeight + 'px'});
		jQuery('#light').css({left: Math.round((TNCom_getDocumentSize(2) - jQuery('#light').width()) / 2)+"px"});
		jQuery('#light').css({top:Math.round((TNCom_getDocumentSize(3) - jQuery('#light').height()) / 2)+"px"});
	},
	
	findFucusElement:function(){
		jQuery("#light").find('select').css({'visibility':'visible'});
		jQuery("#light").find('input[@type!=hidden], a, select, textarea').addClass('setFocusAble');			
		return jQuery('.setFocusAble');
	},
	
	setFocusElement:function(){
		var i = 0;
		var cntElement = 0;	
		var allFocusElement = caobox.findFucusElement();
		var len=allFocusElement.length;	
		
		for(var temJ = 0; temJ<len-1; temJ++){
			jQuery(allFocusElement[temJ]).attr('rel',temJ+1);
		}								   	
		allFocusElement[0].focus();


		allFocusElement.focus(function(){ cntElement=this.getAttribute('rel'); });
		jQuery("#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++;
					}
				}
			}
			
		}); 	
	},
	init:function(config)
	{		
		jQuery(document).ready(function()
		{	
			var masterDiv = "main_container";
			jQuery('#' + masterDiv).before('<div id="light"></div><div id="fade"></div>');
			jQuery('#light, #fade').css({'display':'none', 'position':'absolute'});
			jQuery('#light').css({'zIndex':'1002','height':'auto','overflow':'hidden'});
			jQuery('#fade').css({'zIndex':'1001','backgroundColor': ''+ config.overlayBgColor +'','opacity':'.'+ config.overlayOpacity+'','filter':'alpha(opacity='+config.overlayOpacity+')'}).click( function(){
					caobox.close_popup(); 
			});
		})		
	}
}

caobox.init({							
	overlayBgColor: "#000",
	overlayOpacity: 60
})


function TNCom_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]
}// end function

