/*
 * 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.1
 * @Copyright 2008 AttMp3.com
 */

var caobox = {		
	fadeEffectDuration : 900,
	fade:"#fade",
	light:"#light",
	
	show_popup:function(boxId)
	{	
		jQuery('object, embed, 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"});
		jQuery(this.light).css({top: Math.round((TNCom_getDocumentSize(3) - jQuery(this.light).height()) / 2)+"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;	
		
		if (len>0){
			for(var temJ = 0; temJ<len-1; temJ++){
				jQuery(allFocusElement[temJ]).attr('rel',temJ+1);
			}								   	
			if ( allFocusElement[0].style.display == 'none') // fix focus on hidden element
				allFocusElement[1].focus();
			else
				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 = "uni_wrapper";
			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+')','left':0}).click( function(){
					//caobox.close_popup(); 
			});
		})		
	}
}

caobox.init({							
	overlayBgColor: "#000",
	overlayOpacity: 60
})