var indexLoopTime = "8s";
var api2;//header media

$(function(){
	var homeMediaItemNum = $("#panel2 > div > .imgitem").length;
	if(homeMediaItemNum<=3){
		//$("#leftNavBtn").css("display","none");	
		//$("#rightNavBtn").css("display","none");
		//alert(homeMediaItemNum);
	}
	
	/* Carousel plugin */
	$("#trigger2").switchable("#panel2 > div > .imgitem", {
		triggerType: "click",
		effect: "scroll",
		steps: 1,
		delay: .1, // 100ms
		speed:.7,
		visible: 1
	}).carousel();
		
	api2 = $("#trigger2").switchable();
	//add by runbao
	 api2.beforeSwitch(function(){
			
			  if( $("#imgitems").children().length==$("#trigger2").children().length)
			  {
				
				$("#imgitems").append('<div class="imgitem" style="position: relative; left: 0px;">'+$("#imgitems > div").eq(0).html()+'</div>');
				$("#imgitems").append('<div class="imgitem" style="position: relative; left: 0px;">'+$("#imgitems > div").eq(1).html()+'</div>');
				$("#imgitems").append('<div class="imgitem" style="position: relative; left: 0px;">'+$("#imgitems > div").eq(2).html()+'</div>');
			
				  }
			
			 });/**/
	//end by runbao
	$("#next2").click(function(){
			api2.prev();
			//AutoMediaMoveRight(true);
		});
		$("#prev2").click(function(){
			api2.next();
			//AutoMediaMoveLeft(true);
		});
		//auto move function
//		AutoMediaMoveLeft(true);

	//AutoMediaMoveLeft();
	/*$("#imgitems").mouseenter(function(){
		//alert("over");
		$('body').stopTime ('moveToLeft');
		$('body').stopTime ('moveToRight');
		
	}).mouseleave(function(){
		//alert("out");
		if(isMove=='left')
		{
			AutoMediaMoveLeft(false);
			}else{
				AutoMediaMoveRight(false);
				}
		
	});*/
	
	$(".indexrealtem").click(function(e) {
		var linkUrl = $(this).children().first().val();
        if(linkUrl){
			var open_type = $(this).children().first().attr('thistype');
			if(open_type=="1"){
				window.open(linkUrl);
			}else{
				window.location.href = linkUrl;
			}
		}
    });
	$(".indexrealtem").hover(
	  function () {
		$(this).find('.indexrealtemcover').first().show();
	  },
	  function () {
		$(this).find('.indexrealtemcover').first().hide();
	  }
	);
});
var isMove='left';

function AutoMediaMoveLeft(flog){
	    isMove='left';
		//alert('testmove');
		$('body').stopTime ('moveToLeft');
		$('body').stopTime ('moveToRight');
		if(flog)
		api2.next();
    	$('body').everyTime('8s','moveToLeft',function(){
														api2.next();
														//$("#prev2").click();
														$("#animation").css("visibility","visible");
														$("#animation").animate({
														left: '960px'
														}, 700,null,function(){
														   $("#animation").css("left","0px");
														   $("#animation").css("visibility","hidden");
														 });
														});
	}
	function AutoMediaMoveRight(flog){
		isMove='right';
		$('body').stopTime ('moveToLeft');
		$('body').stopTime ('moveToRight');
		if(flog)
		api2.prev();
    	$('body').everyTime('8s','moveToRight',function(){
														api2.prev();
														//$("#prev2").click();
														$("#animation").css("visibility","visible");
														$("#animation").animate({
														left: '960px'
														}, 700,null,function(){
														   $("#animation").css("left","0px");
														   $("#animation").css("visibility","hidden");
														 });
														});
	}

function projectSearchIndex(){
	var country = $('#country').val();
	var sectors = $('#sectors').val();
	var region_name='';
	$("#country option").each(function(index, element) {
		if($(this).val() == country){
			region_name=$(this).attr("region_name");
		}
	});
	window.location.href="projects?country="+country+"&sectors="+sectors+"&region="+region_name;
}


////////////add by zhezhe
function prev2Click(){
	if(api2.getIndex()==$("#trigger2").children().length-3)
	{
		//$("#trigger2 > a").eq(0).click();
		//$("#leftNavBtn").css("display","none");
		//return;
	}
	//$("#leftNavBtn").css("display","block");
	api2.next();
	//AutoMediaMoveLeft();
}
 

