jQuery.fn.delay = function(time,func){
	this.each(function(){
		setTimeout(func,time);
	});	
	return this;
};
jQuery(document).ready(function(){
	//Remove outline from links
	jQuery("a").click(function(){
		jQuery(this).blur();
	});	
	// Common Function
	var clearOthers = function(){
		jQuery(".dropdownbox").css({display: 'none'});
		jQuery(".tn_topbg").css({display: 'none'});
		jQuery(".tn_dropdown").removeClass('tn_onact');	
		jQuery(".tn_loginbox").removeClass('tn_onact');
		jQuery(".tx-newloginbox-pi1","#topContainer").css({display: 'none'});
	}
	//Effect when hover
	//-- Main Items --
	jQuery("a.tier1").hover(
		function(){
			clearOthers();
			jQuery("#topContainer ul li.tn_dropdown").children("a").removeClass('tn_onact');
			jQuery("#mask").css({display: 'block'}).stop().animate({opacity: '0.8'},{queue:false, duration:200});			
			jQuery(this).next("div").stop().animate({opacity: '1', height: '268px'},{queue:false, duration:400, easing: "easeInOutCirc"});
			return false;
		},
		function(){		
			jQuery(this).next("div").stop().animate({opacity: 'hide', height: '0'},{queue:false, duration:200, easing: "easeInOutCubic"});
			jQuery("#mask").stop().animate({opacity: 'hide'},{queue:false, duration:200});
			return false;
		}
	);
	jQuery("a.f-tier1").hover(
		function(){
			clearOthers();
			jQuery("#topContainer ul li.tn_dropdown").children("a").removeClass('tn_onact');
			jQuery("#mask").css({display: 'block'}).stop().animate({opacity: '0.8'},{queue:false, duration:200});			
			jQuery(this).next("div").stop().animate({opacity: '1', height: '268px'},{queue:false, duration:400, easing: "easeInOutCirc"});
			return false;
		},
		function(){		
			jQuery(this).next("div").stop().animate({opacity: 'hide', height: '0'},{queue:false, duration:200, easing: "easeInOutCubic"});
			jQuery("#mask").stop().animate({opacity: 'hide'},{queue:false, duration:200});
			return false;
		}
	);
	//-- Dropdown Area --
	jQuery(".divmenu").hover(
		function(){
			jQuery(this).prev("a").addClass('selected');
			jQuery(this).stop().css({opacity: '1', height: '268px'});
			jQuery("#mask").stop().css({opacity: '0.8'});
			return false;
		},
		function(){	
			jQuery(this).prev("a").removeClass('selected');
			jQuery("#mask").stop().animate({opacity: 'hide'},{queue:false, duration:200});
			jQuery(this).stop().animate({opacity: 'hide'},{queue:false, duration:200});			
			return false;
		}
	);
	//-- Header Navigation --	
	jQuery(".tn_dropdown","#topContainer").each(function(){
		var ddbox_width = jQuery(this).parent().width();		
		jQuery(this).next("ul").addClass("dropdownbox").css({'width': ddbox_width-3});
		jQuery(this).next("div").addClass("dropdownbox").css({'width': ddbox_width-3});
	});
	jQuery(".tn_dropdown","#topContainer").click(function(){
		if (jQuery(this).next(".dropdownbox").is(":hidden")) {
			clearOthers();	
			jQuery(this).prev("span.tn_topbg").css({display: 'block'});
			jQuery(this).addClass('tn_onact');			
			jQuery(this).next(".dropdownbox").slideDown('fast');
			return false;
		}
		else {
			jQuery(this).prev("span.tn_topbg").css({display: 'none'});
			jQuery(this).removeClass('tn_onact');
			jQuery(this).next(".dropdownbox").slideUp('fast');
			return false;
		}
	});
	//-- Login Box --
	var lb_state =  jQuery.cookie('loginbox');	
	var lb = jQuery(".tn_loginbox","#topContainer");
	if (lb_state == 'on'){
		lb.prev(".tn_topbg").css({display: 'block'});
		lb.addClass('tn_onact');			
		lb.next(".tx-newloginbox-pi1").css({display: 'block'});		
	};	
	jQuery(".tn_loginbox","#topContainer").click(function(){												  
		if (jQuery(this).next(".tx-newloginbox-pi1").is(":hidden")) {
			clearOthers();	
			jQuery(this).prev(".tn_topbg").css({display: 'block'});
			jQuery(this).addClass('tn_onact');
			jQuery(this).next(".tx-newloginbox-pi1").slideDown('fast');			
			jQuery.cookie('loginbox', 'on');			
			return false;
		}
		else {
			jQuery(this).prev(".tn_topbg").css({display: 'none'});
			jQuery(this).removeClass('tn_onact');
			jQuery(this).next(".tx-newloginbox-pi1").slideUp('fast');
			jQuery.cookie('loginbox', 'off');
			return false;
		}
	});
	jQuery("#logout_submit").click(function(){
		jQuery.cookie('loginbox', 'off');								
		jQuery("#logout").submit();
		return false
	});
	//-- Languages Selector --	
	var convert2ISOcountry = function(a){
		switch (a) {
			case 'en': {a = 'global'; break};
			case 'au': {a = 'at'; break};
			case 'ge': {a = 'ch'; break};
			case 'io': {a = 'in'; break};
			case 'sp': {a = 'sg'; break};
			case 'vi': {a = 'vn'; break};
			case 'ar': {a = 'ae'; break};
			case 'zh-cn': {a = 'cn'; break}			
		};
		return a;
	};
	var l_item = jQuery(".tx-srlanguagemenu-pi1","#topContainer").children("div");
	l_item.each(function(){
		var l_flag = jQuery(this).children("a").children("img");
		var langtitle = l_flag.attr("alt");
		l_flag.remove();
		langtitle = convert2ISOcountry(langtitle);		
		l_flag.attr("title",langtitle);
		jQuery(this).children("a").addClass(langtitle);			
	});
	//-- Search Box --	
	var df_searchtext = jQuery(".bg_search",".tx-macinasearchbox-pi1").attr("value");
	jQuery(".btn_search",".tx-macinasearchbox-pi1").attr("value","GO!");
	jQuery(".bg_search",".tx-macinasearchbox-pi1").focus(function(){
		if (jQuery(this).attr("value") == df_searchtext){
			jQuery(this).attr("value","")
		};
		return false;
	});
	jQuery(".bg_search",".tx-macinasearchbox-pi1").blur(function(){
		if (jQuery(this).attr("value") == ''){
			jQuery(this).attr("value",df_searchtext)
		};
		return false;
	});
	jQuery(".btn_search",".tx-macinasearchbox-pi1").hover(
		function(){jQuery(this).css({cursor: 'pointer', color: '#ccc'})},
		function(){jQuery(this).css({cursor: 'default', color: '#fff'})}
	);	
	//-- Out Link notify --
	var ol = jQuery(".outlink");
	var href_val = "";		
	ol.click(function(){
		href_val = jQuery(this).attr("href");
		jQuery("#notify").css({display: 'block'}).stop().animate({opacity: '0.8'},{queue:false, duration:200});		
		jQuery(this).delay(1000,function(){location.href = href_val});
		return false;
	});
});
//-- Black Mask --
jQuery(window).bind("load", function() { 
	var d = jQuery(document).height();	
	jQuery("#mask").css({'height': d});
	jQuery("#notify").css({'height': d});	
	return false;
});
	