spotLightAniAvailable = false;

myPos = 0;
function animatePortfolio(){
	if(myPos==2){
		myPos=0
	}else{
		myPos++
	}
	imgHeight = $('#aniContainter').children('div').children('div:first').children('img').height();
	$('#portfolioAni #aniContainter #slider .aniHolder .details').slideUp(300,function(){
	$('#aniContainter #slider').animate({top:-(myPos*imgHeight)},500,function(){
		$('#portfolioAni #aniContainter #slider .aniHolder .details').slideDown(300);
	});
	});
	
	$('#portfolioAniBtns ul li').each(function(){
		if($(this).index()==myPos){
			$(this).children('a').addClass('selected');
		}else{
			$(this).children('a').removeClass('selected');
		}
	});
}

//------------MAIN_HOMEPAGE_ANIMATION--------------
value=0;

function animateHeader(){
	clearInterval(spotLightAni);
	$('#imgSet .aniSet:last').fadeOut(1300, function(){
		$(this).prependTo($('#imgSet'));
		$(this).fadeIn(0);
		setButton();
	});
	
	spotLightAni = setInterval("animateHeader()", 5000);
}

function checkHeader(){
	if(value<=(-((totalImges)*995))){
		value=0;
		$("#imgSet").css('left',"0");	
	}
}

function animateHeaderByBtn(btnIndex){
clearInterval(spotLightAni);

$('#imgSet .aniSet').each(function(){
if($(this).attr('rel')==btnIndex){
	toMove=$(this).nextAll().not(':last');
	toFade=$(this).nextAll().last();
	allImages= $(this).nextAll();
	}
});
	toMove.hide();
		toFade.fadeOut(1000, function(){
			$(allImages.get().reverse()).each(function(){
				$(this).prependTo($('#imgSet')).show(0);
				//clearInterval(spotLightAni);
			});

	});

spotLightAni = setInterval("animateHeader()", 5000);

}

function setButton(){
	$("#aniBar li a").removeClass('selected');
	activeImg = $('#imgSet .aniSet:last').attr("rel");
	$("#aniBar li").each(function(){
		if(($(this).index()+1)==activeImg){
			$(this).children("a").addClass("selected")
		}
	});
}
	



function buildButtons(){
	$('#imgSet img').each(function(i){
		i++
		$("<li><a href='#'><span class='invisible'>"+i+"</span></a></li>").appendTo($("#aniBar"));
	});
		$("#aniBar li:first a").addClass('selected');
		
		
	$('.imgContainer #featuredSet .aniSet').each(function(i){
		$("<li><a href='#'><span class='invisible'>"+i+"</span></a></li>").appendTo($(".imgViewerBtn"));
	});
	$(".imgViewerBtn li:first a").addClass('selected');
	

		
	$("#aniBar li a").click(function(e){
		
		if($(this).hasClass('selected')){
		}else{
			clearInterval(spotLightAni);
			$("#aniBar li a").removeClass('selected');
			$(this).addClass('selected');
			btnIndex=$(this).parent().index()+1;
			animateHeaderByBtn(btnIndex);	
		}
		e.preventDefault();

	});
	
}

$(document).ready(function(){

$("a.cat_title").click(function(){
	new_target = $(this).attr('alt');
		if(new_target!='clients'){
			$('.cat_sections').slideUp();
			$('#'+new_target).slideDown();
		}
});

$("li.newsyear").click(function(){
	yearid = $(this).attr('alt');
	alreadytest = $("ul#"+yearid).css('display');
		if(alreadytest!='block'){
			$("ul.years").slideUp();
			$("ul#"+yearid).slideDown();
		}
});


$("#search").focus(function(){
	if($(this).val()=="Search"){
			$(this).val("");
	}						
});

$("#search").blur(function(){
	if($(this).val()==""){
			$(this).val("Search");
	}						
});

$("a").click(function(e){

	if($(this).attr("href")=="#"){
	e.preventDefault();
	}
});

$('.scrollable').jScrollPane();


$(".homePod .imgViewerBtn").each(function(){
	var myOldWidth = $(this).width()/2;
	var myParentWidth = $(this).parent().width();
	var myNewWidth = (myParentWidth/2)-myOldWidth;
	$(this).css({marginLeft: myNewWidth-26});
});

//$("img").click(function(){
//	var offset = $(this).position();
//	alert(offset.left);
//});

//------------MAIN_HOMEPAGE_ANIMATION--------------

if($('#imgSet .aniSet').size()>1){
	totalImges = $("#imgSet .aniSet").size();
	$('#imgSet .aniSet').each(function(i){
		//$(this).css({zIndex:totalImges-i});
		$(this).prependTo('#imgSet').attr("rel",1+i);
	});
	buildButtons();
	
}

$('#imgSet').hover(function(){
	clearInterval(spotLightAni);
},function(){
	if($('#imgSet .aniSet').size()>1 && spotLightAniAvailable==true){
	spotLightAni = setInterval("animateHeader()", 5000);
	}
});


//------------TESTIMONIALS_HOMEPAGE_ANIMATION--------------

aniValue=0;
totalFeatured = $(".imgContainer #featuredSet .aniSet").size();
if($('.imgContainer #featuredSet .aniSet').size()>1){
	
	$("#featuredSet").width(totalFeatured*290)
}


$("#featProdHomePrev").click(function(e){
	e.preventDefault();
	animateFeatured("left");
});

$("#featProdHomeNext").click(function(e){
	e.preventDefault();
	animateFeatured("right");
});

function animateFeatured(direction){
	if(direction=="right"){
		if(aniValue!=(totalFeatured-1)){
			aniValue++;
		}
	}else{
		if(aniValue!=0){
			aniValue--
		}
	}
	
	$("#featuredSet").stop(true,true).animate({left: -(aniValue*290)},1000,"easeInOutQuint");
		$(".imgViewerBtn li a").removeClass("selected");
		$(".imgViewerBtn li").each(function(){
			if($(this).index()==aniValue){
				$(this).children("a").addClass("selected");
			}
	});
}

$("#featProdHome .imgViewerBtn li a").click(function(e){
	aniValue = $(this).parent().index();
	
	$("#featuredSet").stop(true,true).animate({left: -(aniValue*290)},1000,"easeInOutQuint");
		$(".imgViewerBtn li a").removeClass("selected");
		$(".imgViewerBtn li").each(function(){
			if($(this).index()==aniValue){
				$(this).children("a").addClass("selected");
			}
	});
	
	e.preventDefault();
});


//-------------------------------------------------
//--------------ACCORDION_ANIMATION----------------

$(".accordion ul li a").not(".accordion ul li ul li a").click(function(e){
	$(this).siblings("ul").slideToggle(300);
	e.preventDefault();
});

$('#portfolioAniBtns ul li a').hover(function(){
	$('#portfolioAniBtns ul li a').not($(this)).removeClass('selected');
	$(this).addClass('selected');
	myPos = $(this).parent().index();
	imgHeight = $('#aniContainter').children('div').children('div:first').children('img').height();
	$('#aniContainter #slider').css({top:-(myPos*imgHeight)})
});


$('#portfolioAni').hover(function(){
	clearInterval(aniPortfolio);
},
function(){
	aniPortfolio = setInterval("animatePortfolio()", 5000);
});

if($('#portfolioAni').length){
	aniPortfolio = setInterval("animatePortfolio()", 5000);
}

	
});


$(window).load(function(){
	if($('#imgSet img').size()>1){
		spotLightAniAvailable = true;
		spotLightAni = setInterval("animateHeader()", 1000);
	}

});

animation_speed = 700; //------How long the animation takes

animation_delay = 8000; //------The time length between each animation


$(document).ready(function(){
				
	direction="-="
		
	$('.imgSet span').each(function(i){
		$('<li><a href="#"></a></li>').appendTo('.imgViewerBtn');
	});
	
	$('.imgSet span:first').clone().appendTo('.imgSet');
	
	$('.imgViewerBtn li:first a').addClass('selected');
	
	button_width = $('.imgViewerBtn').width();
	$('.imgViewerBtn').css('margin-left', (718/2)-button_width);
	
	ani_total = $('.imgSet span').size();
	set_width = $('.imgSet span:first').width();
	max_ani = (set_width*ani_total)-set_width;
	
	$('.imgSet').width(set_width*ani_total);
	
	if(ani_total>1){
		$('<a href="#" class="next"><span class="invisible">N</span></a>').appendTo(".imgViewer");
		$('<a href="#" class="prev"><span class="invisible">P</span></a>').appendTo(".imgViewer");
	}
		
	$('.imgViewerBtn li a').live('click',function(e){
		clearInterval(ani_interval);
		
		my_index = $(this).parent().index()
		
		c = $(this).parent().index()+1;
		
		ani_value = my_index*set_width
		
		$('.imgViewerBtn li a').removeClass('selected');
		$(this).addClass('selected');
		
		$('.imgSet').animate({left: -ani_value}, animation_speed);
		
		e.preventDefault();
	});
	
	$('.imgViewer a.next, .imgViewer a.prev').live('click',function(e){
		
		clearInterval(ani_interval);
		
		if($(this).hasClass('next')){
			if(c<ani_total-1){
				direction="-="
				animate_featured();
			}
		}else{
			if(c>1){
				c=c-2;
				direction="+="
				animate_featured();
			}
		}
		
		e.preventDefault();
	});
	
		ani_interval = setInterval ("animate_featured()", animation_delay);
	
	$('.imgViewer').hover(function(){
		clearInterval(ani_interval);
	},function(){
		ani_interval = setInterval ("animate_featured()", animation_delay);
		direction="-="
	});
});

c=1;

function animate_featured(){
	if(c==(ani_total-1)){
		c=1;
	}else{
		c++
	}
		
	$('.imgSet').animate({left: direction+set_width}, animation_speed,function(){
		$('.imgViewerBtn li a').removeClass('selected');
		$('.imgViewerBtn li:nth-child('+c+') a').addClass('selected');
		
		if($('.imgSet').css('left')==-max_ani+'px'){
			$('.imgSet').css('left',0);
		}
	});
}
