
/*
 * 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 Timnhanh.com
 * uptate 22/12/2008 tridn@von-inc.com
 */
var g_html;
var typecaobox=0;
var caobox = {		
	fadeEffectDuration : 900,
	fade:"#fade",
	light:"#light",
	show_popup:function(boxId,type)
	{	
		if(g_html!=null) $('body').append(g_html);
		if(type==1) typecaobox=type;
		jQuery('object, embed, select').css('visibility','hidden');
		this.init();
		g_html=$("#"+boxId);
		$(this.light).html($("#"+boxId).html());
		$("#"+boxId).remove();
		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) + TNCom_getDocumentSize(4) - jQuery(this.light).height()) / 2)+"px"});
		caobox.showalertlayer();	
		caobox.setFocusElement();
		window.onresize=caobox.resize_layers;
	},
	close_popup:function(){	
		$('#light').remove();
		$('#fade').remove();
		$('body').append(g_html);
		g_html=null;
		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('object, embed, 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);
			}	
			// 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'); });
			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()
	{		
		jQuery(document).ready(function()
		{	
			$('body').append('<div id="light"></div><div id="fade"></div>');
			jQuery('#light, #fade').css({'display':'none', 'position':'absolute'});
			jQuery('#light').css({'zIndex':'10002','height':'auto','overflow':'hidden'});
			jQuery('#fade').css({'zIndex':'10001','backgroundColor':'#000','opacity':'0.6','filter':'alpha(opacity="60")','left':0,'top':0}).click( function(){																															 					if(typecaobox==0) caobox.close_popup(); 
			});
			
		})		
	}
}