/*
 * Js by Sofresh 2009
 * 1.0.1
 *
 * 2009/12/14 News scroll function
 */


$(document).ready(function(){
						   
	// Clear divider at the last item of navigation
	$('.nav li:last').css('background','none');
	
	var currentItem;
	$('.nav div').show().hide();
	$('.nav li').hover(
		function(){
			/*currentItem = $(this).attr('class');
			var appendLayer = $('.'+currentItem+'').html();
			$('.'+currentItem+'').append('<div>'+appendLayer+'</div>');
			$('.'+currentItem+' div').stop().fadeIn('normal');*/
			
			
		},
		function(){
			//$('.'+currentItem+' div').fadeOut('fast');
			/*$('.'+currentItem+' div').stop().animate({opacity:0}, 1000, function(){ 
				$(this).remove();
			});*/
		}
	);
	
	jQuery('ul.nav > li').hover(function() {
		jQuery('ul.nav ul').hide();
		jQuery('ul:first', this).show();
		jQuery('a:first', this).stop().addClass('hover');
	},
	function() {
		jQuery('ul:first', this).hide();
		jQuery('a:first', this).stop().removeClass('hover');
	});
	
	
	// Fix the bidi algorithm problem
	if ($('body').attr('class') == ""){
		$('p, strong, h2').attr('dir','rtl');
	}
	
	
	// News 
	if ($('#news').html() != null) initializemarquee();
});




var delayb4scroll=1000
var marqueespeed=1
var pauseit=1 
var copyspeed=marqueespeed
var pausespeed=(pauseit==0)? copyspeed: 0
var actualheight=''

function scrollmarquee(){
	if (parseInt(cross_marquee.style.top)>(actualheight*(-1)))
		 cross_marquee.style.top=parseInt(cross_marquee.style.top)-copyspeed+"px"
	else cross_marquee.style.top=parseInt(actualheight)+"px";
	
	if (parseInt(cross_marquee_copy.style.top)>(actualheight*(-1)))
		 cross_marquee_copy.style.top=parseInt(cross_marquee_copy.style.top)-copyspeed+"px"
	else cross_marquee_copy.style.top=parseInt(actualheight)+"px";
}

function initializemarquee(){
	var vmarquee_copy = "<ul id='news_copy'>"+ $('#news').html() +"</ul>";
	$('#marqueecontainer').append(vmarquee_copy);
	
	marqueeheight=document.getElementById("marqueecontainer").offsetHeight;
	
	cross_marquee=document.getElementById("news");
	actualheight=cross_marquee.offsetHeight;	
	cross_marquee.style.top=0;	
	cross_marquee_copy=document.getElementById("news_copy");	
	cross_marquee_copy.style.top=parseInt(actualheight)+"px";
	
	if (window.opera || navigator.userAgent.indexOf("Netscape/7")!=-1){
		cross_marquee_copy.style.height=marqueeheight+"px";
		cross_marquee_copy.style.overflow="scroll";
		cross_marquee.style.height=marqueeheight+"px";
		cross_marquee.style.overflow="scroll";
		return
	}

setTimeout('lefttime=setInterval("scrollmarquee()",30)', delayb4scroll)
}


function send_email(){
	if ($("#name").val() != '' && $("#email").val() != '' && $("#contact5").val() != '' && $("#message").val() != ''){
		$.post('send_email.php', {"name": $("#name").val(), "email": $("#email").val(), "tel": $("#contact3").val(), "company": $("#contact4").val(), "subject": $("#contact5").val(), "msg": $("#message").val()}, function(data){
					
			$('#contact_form').fadeOut('normal');
  			$("#thankyou").html('Thank you for contacting Worldcom International Communication, someone will be in contact with you shortly.<br><br><a href="index.html">Return to the homepage</a> or <a href="eng-contact.html">send another message</a>');
		}); 
	}
}

function orderPage(url, type)
{	
	$.post(url, { "type": type, "act" : "order"}, function(data) { 
											  $("#update_page").html(data);
										});	
}



/*
 * Lightbox use jQuery library - Require $ 1.2+
 * @Author:		Hien Nguyen <hienn.nguyen@sofresh.ca> 
 * @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 caobox = {		
	fadeEffectDuration : 900,
	fade:"#fade",
	light:"#light",
	boxID:"",
	overlayBgColor:"#000",
	overlayOpacity:"60",
	
	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
		$('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');

		// fix clone function not work in IE
		// FLV		
		var inner_boxID = $(this.boxID).html();
		/*if (boxId) 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 +">"+ embedFLV +"</div>";
			$(this.light).append(full_boxID);
		}
		else{*/
			$(this.boxID).clone().appendTo($(this.light));
		/*}*/
		
		$(this.boxID).show();
		$(this.light).fadeIn('fast', function(){
			 //$(this.light).css({display:'block'}); 
		});	

		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()) / 2);
			$(this.light).css({left: _left+ "px"});
		}		
		
		// 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"});
		}
		
		caobox.showAlertLayer();	
		caobox.setFocusElement();
		window.scrollTo(0,0);
		window.onresize=caobox.resizeLayers;
	},
	
	hide:function(){	
		//$(this.boxID).remove();
		$(this.fade).fadeOut(this.fadeEffectDuration);
		$(this.light).fadeOut(this.fadeEffectDuration, function(){
			$('object, embed, select').css('visibility','');
			$('#fade, #light').remove();
		});
	},

	
	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('normal');
	},
	
	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];
};