var nowPsroductIndex = 0;
var nowBodyBgImgIndex = -1;
var linkAdvIndex = 0;
var totalLinkAdv = 0;
var linkAdvTimeout = null;
var linkAdvActive = true;

$(document).ready(function(){
	setTimeout(setBodyBackground,2000);
	
	var activeID = 'linkAdvBox';
	var activeObj = $('#'+activeID);
	
	if (activeObj.length > 0)
	{
		totalLinkAdv = $('.wade_index_banner_submenu > div').length;
		
		if ($('#'+activeID+' > table > tbody > tr > td').length > 4)
		{
			setLinkAdvBtn(0, 0);
			activeSmallAdver(activeID, 1);
		}
	}
	
});

function activeSmallAdver(activeID, newIndex)
{
	linkAdvTimeout = setTimeout(function(){
		exeLinkAdvCarousel(activeID, newIndex);
	},6000);
}

function exeLinkAdvCarousel(activeID, newIndex)
{
	setLinkAdvBtn(linkAdvIndex, newIndex);
	
	var newLeft = 0-940*newIndex;
	//newLeft = (newLeft <= (0-$('#'+activeID+' > table:first').width()+940)) ? 0 : newLeft;
	
	$('#'+activeID).animate({left:newLeft},1000,function(){
		var nextIndex = newIndex + 1;
		nextIndex = (nextIndex == totalLinkAdv) ? 0 : nextIndex;
		linkAdvIndex = newIndex;
		linkAdvActive = true;	
		activeSmallAdver(activeID, nextIndex);
	});
}

function setLinkAdvBtn(lastIndex, indexInt)
{
	var btnStr = 'wade_index_banner_submenu';
	var btnObj = $('.'+btnStr);
	$($($('.'+btnStr+' > div')[lastIndex]).children('a').children('img')).attr('src','images/icon_dodo.jpg');
	$($($('.'+btnStr+' > div')[indexInt]).children('a').children('img')).attr('src','images/icon_dodo2.jpg');
}

function linkAdvBtnClick(indexInt)
{
	if (linkAdvActive)
	{
		linkAdvActive = false;
		
		if (linkAdvTimeout)
		{
			clearTimeout(linkAdvTimeout);
		}
		
		indexInt = indexInt / 4;
		exeLinkAdvCarousel('linkAdvBox', indexInt);
	}
}

function activeNewsCate(newsIndex, titleStr, otherIndex, otherTitle)
{
	$('#newsCateBtn'+otherIndex).html('<a href="javascript:activeNewsCate('+otherIndex+',\''+otherTitle+'\','+newsIndex+',\''+titleStr+'\');" class="link_top_03"><span>'+otherTitle+'</span></a>');
	$('#newsCateBtn'+newsIndex).html('<span style="background-color:#6fba2c"><a href="javascript:activeNewsCate('+newsIndex+',\''+titleStr+'\','+otherIndex+',\''+otherTitle+'\');" class="link_top_02">'+titleStr+'</a></span>');
	$('#newsContent').animate({top:0-(newsIndex*164)},1000);
}

function activeProduct(proIndex)
{
	document.getElementById('productBtn'+nowPsroductIndex).src = 'images/index_content_30.png';
	document.getElementById('productBtn'+proIndex).src = 'images/index_content_28.png';
	$('#productContent').animate({left:0-(parseInt(proIndex)*283)},1000);
	nowPsroductIndex = proIndex;
}

function setBodyBackground()
{
	nowBodyBgImgIndex += 1;
	if (!document.getElementById('bodyBgImg'+nowBodyBgImgIndex))
	{
		nowBodyBgImgIndex = 0;
	}
	
	$('#bodyBackground').fadeOut(1000,function(){
		var imgSrc = 'url(\''+document.getElementById('bodyBgImg'+nowBodyBgImgIndex).value+'\')';
		$('#bodyBackground').css('backgroundImage',imgSrc);
		
		if (document.getElementById('bodyBgLink'+nowBodyBgImgIndex).value == '')
		{
			$('#bodyBackground').css('cursor','auto');
			$('#bodyBackground').unbind('click');
		}else{
			$('#bodyBackground').css('cursor','pointer');
			$('#bodyBackground').click(function(event){
				location.href = document.getElementById('bodyBgLink'+nowBodyBgImgIndex).value;
			});
		}
		
		$('#bodyBackground').fadeIn(1000,function(){
			setTimeout(setBodyBackground,5000);
		});
	});
}
