/*
 * Menu Dropdown with submenu
 * @author: Hien Nguyen <hien.nguyen@sofresh.ca>
 */
jQuery(document).ready(function(){
								
	jQuery('ul#nav > li').hover(function() {
		jQuery('ul:first', this).slideDown(150);
	},
	function() {
		//jQuery('ul:first', this).slideUp(0);
		jQuery('ul#nav ul').slideUp(0);
	});

	jQuery('ul#nav li li').hover(function() {
		jQuery('ul:first', this).each(function() {
		  jQuery(this).css('top', jQuery(this).parent().position().top - 10);
		  jQuery(this).css('left', jQuery(this).parent().position().left + jQuery(this).parent().width() );
		  jQuery(this).show(150);
		});
	},
	function() {
		jQuery('ul:first', this).hide(50);
		//jQuery('ul#nav ul li ul').hide(50);
	});
	imageSlideShow();
});

/*
 * Gallery slide show
 * @author: Hien Nguyen <hien.nguyen@sofresh.ca>
 */
function imageSlideShow(){
	var numImagesLoad=3;
	var speedScroll=400;
	var numPageIndex=1;
	var numItems=jQuery('.item').length;
	var numImgBlockWidth=jQuery('.item').width();
	var numImgBlockHeight=jQuery('.item').height();
	
	//jQuery(".btn_left").addClass('disable');
	if (numItems<4) jQuery('.btn_right').addClass('disable');
	
	for (var i=0; i<numItems; i++)
		jQuery('.item:eq('+i+')').css("left",(i*numImgBlockWidth));
	jQuery('.content_imglist').width(numImagesLoad * numImgBlockWidth);
	jQuery('.content_imglist').height(numImgBlockHeight);
	jQuery(".btn_right").click(function(){
		//if(numPageIndex < (numItems-2)){
		if(numPageIndex < (numItems/3)){
			jQuery(".item").animate({left:'-='+numImgBlockWidth*3+'px'}, speedScroll);
			numPageIndex++;
			jQuery(".btn_left").removeClass('disable');
		}
		//alert(numPageIndex+' '+numItems/numImagesLoad);
		//if(numPageIndex > (numItems-3)){
		if(numPageIndex >= (numItems/3)){
			jQuery(".btn_right").addClass('disable');
		}

	});
	jQuery(".btn_left").click(function(){
		if(numPageIndex>1){
			jQuery(".disable").removeClass('disable');
			jQuery(".item").animate({left:'+='+numImgBlockWidth*3+'px'}, speedScroll);
			numPageIndex--;
		}
		if(numPageIndex == 1){
			jQuery(".btn_left").addClass('disable');
		}
	});
	jQuery('.item').css({ 'visibility':'visible'});
}

/*
 * Caobox use jQuery library - Require jQuery 1.2+
 * @Author:		Hien Nguyen <hien.nguyen@sofresh.ca>
 * @Version:	1.0.4
 */
var caobox = {		
	fadeEffectDuration : 900,
	fade:"#fade",
	light:"#light",
	boxID:"",
	overlayBgColor:"#000",
	overlayOpacity:"50",
	
	show:function(boxId, overlayBgColor, overlayOpacity, overlayDisable, leftPosition, topPosition)
	{
		// Get layer Background color & opacity
		if (overlayBgColor) this.overlayBgColor = "#"+ overlayBgColor;
		if (overlayOpacity) this.overlayOpacity = overlayOpacity;
		
		// Add layer
		jQuery('body').prepend('<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',
					   		'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;		
		jQuery('object, embed, select').css('visibility','hidden');

		// fix clone function not work in IE
		// FLV		
		var inner_boxID = jQuery(this.boxID).html();
		var filetype = inner_boxID.substring(inner_boxID.length-3, inner_boxID.length);
		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 +" class='caobox-popup'>"+ embedFLV +"</div>";
			jQuery(this.light).append(full_boxID);
			jQuery(this.boxID).append('<img alt="" src="images/close.gif" width="24" height="24" class="btn_close" onclick="caobox.hide();" />');
		}
		else{
			jQuery(this.boxID).clone().appendTo(jQuery(this.light)).append('<img alt="" src="images/close.gif" width="24" height="24" class="btn_close" onclick="caobox.hide();" />');;
		}		
		
		
		jQuery(this.boxID).show();
		jQuery(this.light).fadeIn('normal');
		//jQuery(this.light).show(function(){jQuery(this.light).css({display:'block'}); });

		var _top = Math.round((getDocumentSize(3) - jQuery(this.light).height()) / 2);
		
		// Left position, default is center
		if (leftPosition){
			_left = leftPosition;
			if (leftPosition > 0) jQuery(this.light).css({left: _left+ "px"});
			else			
				jQuery(this.light).css({right: Math.abs(_left) + "px"});			
		}
		else{
			_left = Math.round((getDocumentSize(2) - jQuery(this.light).width()) / 2);
			jQuery(this.light).css({left: _left+ "px"});
		}		
		
		// Top position, default is middle
		if (topPosition){			
			if (topPosition > 0){
				_top = topPosition;
				jQuery(this.light).css({top: _top+"px"});
			}
			else{
				_top = Math.abs(topPosition);
				jQuery(this.light).css({bottom: _top+"px"});
			}
		}		
		else{
			if(_top < 0) _top = 20;		
			jQuery(this.light).css({top: _top+"px"});
		}
		
		caobox.showAlertLayer();	
		caobox.setFocusElement();
		window.scrollTo(0,0);
		window.onresize=caobox.resizeLayers;
	},
	
	hide:function(){	
		jQuery(this.boxID).remove();
		jQuery(this.fade).fadeOut('fast'); //this.fadeEffectDuration
		jQuery(this.light).fadeOut('fast', function(){
			jQuery('object, embed, select').css('visibility','');
			jQuery('#fade, #light').remove();
		});
	},
	
	showAlertLayer : function(){
		var _height=getDocumentSize(1);
		var _width =getDocumentSize(0);
		
		if(_width < 974) 
			 _width = 974;	
		
		if(jQuery(this.boxID).height() > getDocumentSize(1))
			_height=parseInt(jQuery(this.boxID).height()) + 65;		
		
		jQuery(this.fade).css({width:_width +'px'});
		jQuery(this.fade).css({height:_height +'px'});	
		//jQuery(this.fade).css({display:'block'}); 
		jQuery(this.fade).fadeIn('normal');
	},
	
	resizeLayers : function(){
		if(jQuery(this.boxID).height() > getDocumentSize(1)){
			var _height = parseInt(jQuery(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) jQuery('#fade').css({width:974 + "px"});
		else jQuery('#fade').css({width:getDocumentSize(0) + "px"});
		jQuery('#fade').css({height:pageHeight + 'px'});
		jQuery('#light').css({left: Math.round((getDocumentSize(2) - jQuery('#light').width()) / 2)+"px"});
		
		var _top=Math.round((getDocumentSize(3) - jQuery(this.light).height()) / 2);
		if(_top < 0) _top = 20;
		jQuery(this.light).css({top:_top+"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++;
						}
					}
				}			
			}); 
		}
	},
	
	unbindClickFace:function(){
		jQuery(this.fade).unbind('click');
	},
	
	init:function()
	{		
		jQuery(document).ready(function(){
			jQuery('.caobox-popup').hide();
			jQuery('body').css({'margin':'0', 'padding':'0'}); // hack browser
			jQuery('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];
};