var IE = document.all?true:false;

function getPath(){
	var langs = ['en', 'tc', 'sc'];
	var currentLang = langs[0];
	for(x in langs){
		if(location.href.search("/"+langs[x]+"/")!=-1){
			currentLang = langs[x];
		}
	}
	return [location.href.substr(0, location.href.search('/'+currentLang+'/')), currentLang];
}

var temp = getPath();
var currentPath = temp[0];
var currentLang = temp[1];
var currentPathLang = temp[0] + '/' + temp[1] + '/';

var mouseover_tid = [];
var mouseout_tid = [];
var currentMenu = '';
var currentMenuSection = '';

var menuOnOver = 0;


var imageName = '';
var selectedImage = '';
var deselectedImage = '';
var link = '';

function deBug(str) {
	if($('div#deBug').length<=0){
		$('body').append('<div id="deBug"></div>');
	}
	$('div#deBug').append(str + '<br />');
}

$(document).ready(function(){
	
	id_sequence = content_id.split(',');
	if($('.promo_img').children().length <=4){
		$("#a_left").css("visibility", "hidden");
		$("#a_right").css("visibility", "hidden");
	}
	btnOver();
	if($('#promo_showcase').length != 0) {
		try{
		$(".promo_thumb").scrollable({circular: false, mousewheel: true, speed: 600}).navigator().autoscroll({
			interval: 8000		
		});
		}catch(err){
		}
	}
	if($('#car').length != 0) {
		carPosition();
	}
	
	subMenuStart();	
	subMenu();
	
	selectedImage = $('#promo_showcase .promo_img a:eq(0)').css({'z-index':'1','display':'block'});
	$('#promo_showcase .promo_thumb a:eq(0)').addClass('selected');
	$('#main_zone > div:eq(0)').css('position','relative');
	$('#promo_showcase .promo_top .frame > a').attr('href', $('#promo_showcase .promo_img a:eq(0)').attr('href'));
	
})

$(window).resize(function() {
	if($('#car').length != 0) {
		carPosition();
	}
});

function promoGallery(me) {
	var length = $('#promo_showcase .promo_img').length;
	var thumb = $(me).children('img').attr('src').replace('_thumb', '');
	
	$('#promo_showcase .promo_thumb .items div > a').removeClass('selected');
	
	$('#promo_showcase .promo_img img').each(function(index) {
		imageName = $(this).attr('src');
		if(thumb == imageName) {
			selectedImage = $(this).parent();
			link = $(this).parent('a').attr('href');
			$('#promo_showcase .promo_img a').removeClass('selected');
			selectedImage.addClass('selected');
			$('#promo_showcase .promo_top .frame > a').attr('href', link);
		}
	});
	
	if($(selectedImage).css('z-index') != '1') {
		$(selectedImage).css({'z-index':'1','display':'block'});
		$('#promo_showcase .promo_img a:not(.selected)').css('z-index','2').fadeOut('slow').length;
		$('#promo_showcase .promo_top .frame > a').attr('href');
		$(me).addClass('selected');
	}
	
}



var currentSelectedMenu = false;

function subMenuStart() {
	currentMenuSection = 'menu_'+id_sequence[0];
	currentSubMenuSection = 'submenu_'+id_sequence[1];
	$('#menu > li.'+currentMenuSection+' > a').addClass('selected');
	
	if( $('#menu li.'+currentMenuSection+' > .submenu').length > 0) {
		$('#menu li.'+currentMenuSection+' > .submenu li.'+currentSubMenuSection+' > a').addClass('selected');
		$('#menu li.'+currentMenuSection+' .submenu').fadeIn();
		$('.submenu_bg').fadeIn();
	}
}
function subMenuReset() {
	currentMenuSection = 'menu_'+id_sequence[0];
	currentSubMenuSection = 'submenu_'+id_sequence[1];

	$('#menu li > .submenu').css('z-index',-1);
	$('#menu li > .submenu').hide();
	$('#menu li > a').removeClass('selected');
	$('.submenu_bg').stop(true, true).fadeOut();
}

function subMenu() {
	currentMenuSection = 'menu_'+id_sequence[0];
	currentSubMenuSection = 'submenu_'+id_sequence[1];
	
	$('#menu > li').mouseenter(function() {
		currentMenu = $(this).attr('class');
		if(currentMenu != currentMenuSection) {
			subMenuReset();
		}
		//deBug('current Menu: ' + currentMenu);
		//deBug('Section: ' + currentSubMenuSection);
		
		currentSelectedMenu = true;
		
		$(this).children('a').addClass('selected');
		if( $(this).children('.submenu').length > 0) {
			$('#menu li.'+currentMenuSection+' > .submenu li.'+currentSubMenuSection+' > a').addClass('selected');
			$(this).children('.submenu').css('z-index',2);
			$('.submenu_bg').stop(true, true).fadeIn();
			$(this).children('.submenu').fadeIn();
		}
	}).mouseleave(function() {
		currentSelectedMenu = false;
	if(currentMenu != currentMenuSection) {
				subMenuReset();
		setTimeout(function ()
		{
			if(currentSelectedMenu == false) {
				subMenuStart();
			}
		}, 800);
	}
		
		
	});
}

function carPosition() {
	var window_w = $(window).width();
	var car_x = $('#car').offset().left;
	var car_w = $('#car').width();
	var car = car_w + car_x;
	var extra = car - window_w;
	
	if(window_w >= 980) {
		$('#car').css('width', (car_w - extra) );
	} else {
		$('#car').css('width', '190px' );
	}
}

function btnOver() {
	$('img.btn_over').mouseover(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('.g','_over.g').replace('.j','_over.j'));
    }).mouseout(function(){
		$(this).attr('src', $(this).attr('src').toString().replace('_over.g','.g').replace('_over.j','.j'));
    });
	
}

function AdjustHeight()
	{
		for (var i=0;i<$(".product_list").length;i=i+4)
	{
		//alert($(".tag_container:eq("+i+")").html());
		var maxheight_top=0;
		var maxheight_cell=0;
		var maxheight_btn=0;
		for (var j=0;j<4;j++)
		{
			if ((i+j)>=$(".product_list").length)
				break;
			if ($(".product_list:eq("+(i+j)+") .img_top").height()>maxheight_top)
				maxheight_top=$(".product_list:eq("+(i+j)+") .img_top").height();
			
			if ($(".product_list:eq("+(i+j)+") .content_cell").height()>maxheight_cell)
				maxheight_cell=$(".product_list:eq("+(i+j)+") .content_cell").height();	
				
			if ($(".product_list:eq("+(i+j)+") .content2_btn").height()>maxheight_btn)
				maxheight_btn=$(".product_list:eq("+(i+j)+") .content2_btn").height();	
		}
		
		for (var j=0;j<4;j++)
		{
		$(".product_list:eq("+(i+j)+") .img_top").height(maxheight_top);
		$(".product_list:eq("+(i+j)+") .content_cell").height(maxheight_cell);
		$(".product_list:eq("+(i+j)+") .content2_btn").height(maxheight_btn);
		}
	}
	}
	
function AdjustHeight2()
	{
		for (var i=0;i<$(".maxh").length;i=i+2)
	{
		//alert($(".tag_container:eq("+i+")").html());
		var maxheight_h=0;
		for (var j=0;j<2;j++)
		{
			if ((i+j)>=$(".maxh").length)
				break;
			if ($(".maxh:eq("+(i+j)+") .maxh_h").height()>maxheight_h)
				maxheight_h=$(".maxh:eq("+(i+j)+") .maxh_h").height();	
		}
		
		for (var j=0;j<2;j++)
		{
		$(".maxh:eq("+(i+j)+") .maxh_h").height(maxheight_h);
		}
	}
	}
function jump(secNum)//'sec Num,sub Sec Num'
	{
		//find the href from the tree
		//jump to the href
		new_href=get_href(secNum, rootMenu);
		if (new_href!='')
			window.location.href=new_href;
	}
	
	function get_href(secNum, parentMenu)
	{
		href_found=''
		for (var i=0;i<parentMenu.childs.length;i++)
		{
			var m=parentMenu.childs[i];
			if (m.contentId==secNum)
				return (currentPathLang+m.href);
			if (m.childs.length>0)
				href_found=get_href(secNum, m);
			if (href_found!='')
				return href_found;
		}
		return '';
	}
	
	
		$(window).load(function (){
			adjust_width();
	});	
	
		$(window).resize(function (){
			adjust_width();
	});	
	
	function adjust_width()
	{
		if (($.browser.msie&& /MSIE 6\.0/i.test(window.navigator.userAgent)&& !/MSIE 7\.0/i.test(window.navigator.userAgent))){
				if ($(window).height()<parseInt($('body').css('min-height')))
				{
					$('body').css('height', $('body').css('min-height'))
				}
				else if ($('body').css('height')==$('body').css('min-height'))
					$('body').css('height','100%');
				if ($(window).width()<parseInt($('body').css('min-width')))
				{
					$('body').css('width', $('body').css('min-width'))
					if ($('.top_flash').length>0)
					$('.top_flash').css('width', $('body').css('min-width'))
				}
				else if ($('body').css('width')==$('body').css('min-width'))
				{
					$('body').css('width','100%');
					if ($('.top_flash').length>0)
					$('.top_flash').css('width', '100%')
				}
			}
	}
	
	function jumpPopup(langnum)
	{
		var url;
		if (langnum==1)
			url='http://webordering.kfchk.com/KFC/KFC/Index.action?request_locale=EN_US';
		else if (langnum==2)
			url='http://webordering.kfchk.com/KFC/KFC/Index.action?request_locale=zh_TW';
			
			window.open(url,'new_popup','scrollbars=yes,width=850,height=600,resizable=no');
	}
	
	
	function jumpBanner(bannernum)
	{
		if (bannernum==1)
			location.href=currentPathLang+'new/index.html';
			
		else if  (bannernum==2)
			location.href=currentPathLang+'menu/am_plate_deluxe.html';

		else if  (bannernum==3)
			{
				url='http://www.facebook.com/kfchk';
				window.open(url,'new_popup6','scrollbars=yes,width=880,height=600,resizable=yes');
			}
			
		else if  (bannernum==4)
			
				url=currentPath+'/filemanager/pdf/online_order_offer_20110707.pdf';
				window.open(url,'new_popup6','scrollbars=yes,width=880,height=600,resizable=yes');
	}
	
	global_stores=['http://www.kfc.com.cn','http://www.kfchk.com/','http://www.kfc.co.in/','http://www.kfcindonesia.com','http://www.kfc.co.jp/','http://www.kfckorea.com/','http://www.kfc.com.my','http://www.kfcpakistan.com/','http://www.kfc.ph/','http://www.kfc.com.sg/','http://www.kfcclub.com.tw/','http://www.kfc.co.th/','http://www.kfcturkiye.com/','http://www.kfcvietnam.com/','http://www.kfc.ca/','http://www.kfc.com.mx/','http://www.kfc-pr.com/','http://www.kfc.com/','http://www.kfc.cz/','http://www.kfc.fr/','http://www.kfc.de/','http://www.kfc.gr/','http://www.kfc-malta.com/','http://www.kfc.nl/','http://www.kfc.pl/','http://www.kfc.ro/','http://www.kfc.co.uk/','http://www.kfc.com.au/','http://www.kfc.co.nz/'];
	
	function jumpPlace(place_num)
	{
		window.open(global_stores[place_num-1],'new_popup');
	}
	
	function clickFacebook() 
	{
		url='http://www.facebook.com/pages/KFC-Foodmania/37702709081';
		window.open(url,'new_popup1');
	}
	
	function tvcHover(arg){
		if(arg){
			document.getElementById("btn_tvc").style.backgroundPosition='left bottom';
		}else{
			document.getElementById("btn_tvc").style.backgroundPosition='left top';
		}
	}

