var curFeatItem = 1;
$(function(){
	
	$("#featuredServices .iconLeft:first").click( moveFeatServicesPrev );
	$("#featuredServices .iconRight:first").click( moveFeatServicesNext );
	
	//fix scroller width
	var l = $("#featuredServices .featItem").length;
	var iW = $("#featuredServices .featItem").width() + 20; //20 is the margin
	var w = Math.ceil((iW * l ) / 2 );
	$("#featuredServices .scrollerContent:first").css({width: w+"px"});
	
	$("#featuredServices .featItem").click( showServicePage )
									.mouseenter( function(e) { var p = $(e.currentTarget); 
																if (p.hasClass("flashContainer")){ 
																	$(".img:first *:first", p).get(0).doIconHoverInAnimation(); 
																};
																var h3 = p.find("h3:first"); 
																h3.stop(true,true).clearQueue().animate({"color": h3.attr("hoverColor")}, 300 );
												})
									.mouseleave( function(e) { var p = $(e.currentTarget); 
																if (p.hasClass("flashContainer")){ 
																	$(".img:first *:first", p).get(0).doIconHoverOutAnimation(); 
																}; 
																var h3 = p.find("h3:first"); 
																h3.stop(true,true).clearQueue().animate({"color": h3.attr("orgColor")}, 300 );
												});
	
	fixFeatItemsPaginatorImages();
	
	//newsItemHover
	$("#newsBlock .newsItem").hover( newsBlockItemHoverIn, newsBlockItemHoverOut );
})

function newsBlockItemHoverIn(e)
{
	var p = $(e.currentTarget);
	var dDiv = p.find(".newsDate:first");
	dDiv.stop(true,true).clearQueue().animate({"color": dDiv.attr("hoverColor")},300);
	
	var tDiv = p.find(".newsText:first");
	tDiv.stop(true,true).clearQueue().animate({"color": tDiv.attr("hoverColor")},300);
}

function newsBlockItemHoverOut(e)
{
	var p = $(e.currentTarget);
	var dDiv = p.find(".newsDate:first");
	dDiv.stop(true,true).clearQueue().animate({"color": dDiv.attr("orgColor")},300);
	
	var tDiv = p.find(".newsText:first");
	tDiv.stop(true,true).clearQueue().animate({"color": tDiv.attr("orgColor")},300);
}

function showServicePage(e)
{
	location.href='services/' + $(this).attr("sid");	
}

function moveFeatServicesPrev(e)
{
	if (curFeatItem-1<=0) return;
	
	curFeatItem -=1;
	//get x offset
	var ofs = $("#featuredServices .featIndex:nth-child(" + curFeatItem + ")").position();
	$("#featuredServices .scrollerContent:first").stop(true,true).clearQueue().animate({"left": -ofs.left+"px"}, 300, "easeInOutQuad");
	fixFeatItemsPaginatorImages();
}

function moveFeatServicesNext(e)
{
	if (curFeatItem+1>$("#featuredServices .featIndex").length-1) return;
	
	curFeatItem++;
	//get x offset
	var ofs = $("#featuredServices .featIndex:nth-child(" + (curFeatItem+1) + ")").position();
	$("#featuredServices .scrollerContent:first").stop(true,true).clearQueue().animate({"left": -ofs.left+"px"}, 300, "easeInOutQuad");
	fixFeatItemsPaginatorImages();
}

function fixFeatItemsPaginatorImages()
{
	var pL = $("#featuredServices .featItem").length;
	
	if (pL<=4)
	{
		$("#featuredServices .iconRight").removeClass("iconRightActive").hide();
		$("#featuredServices .iconLeft").removeClass("iconLeftActive").hide();
		return;
	}
	
	if (curFeatItem==1 && pL>4)
	{
		$("#featuredServices .iconRight").addClass("iconRightActive").show();
		$("#featuredServices .iconLeft").removeClass("iconLeftActive").hide();
		
	} else if (curFeatItem==pL-3 && pL>4) {
		$("#featuredServices .iconRight").removeClass("iconRightActive").hide();
		$("#featuredServices .iconLeft").addClass("iconLeftActive").show();
	} else {
		$("#featuredServices .iconRight").addClass("iconRightActive").show();
		$("#featuredServices .iconLeft").addClass("iconLeftActive").show();
	}
	
}
