/* ----- Taylor Wimpey jQuery ------------------------------------------------------------- */
/* ----- PH 2011 -------------------------------------------------------------------------- */
/* ----- enjoy, but please don't rip things off outright ---------------------------------- */

$(document).ready(function() {
	
	initDocument();
	
});

function initDocument() {

	if($("#slideshow").exists()) {
		initSlideshow();
	}

}

//////////----- CORE ------------------------------------------//////////
//---------------------------------------------------------------------//
jQuery.fn.exists = function() {
	return jQuery(this).length > 0;
}


//////////----- GENERAL SLIDESHOW -----------------------------//////////
//---------------------------------------------------------------------//
function initSlideshow() {	

	var imgW = 368;
	var posX = 0; //Starting x pos
	var imgCount = $("#slideshow-set ul li").length;
	var showW = imgW*imgCount;
	var showing = 1;

	$("#slideshow-set ul").css({ "width": showW+"px" });

	//$(".slideshow.l .loader").remove();

	$("#slideshow-controls .next a").bind("click", function() { //Next image
		if(showing<imgCount) {
			$("#slideshow-set ul").animate({
				"left": "-="+imgW+"px"
			}, 600);
			showing++;
			$("#slideshow-indicators a").removeClass("current");
			$("#slideshow-indicators li.img"+showing+" a").addClass("current");
			$("#slideshow-controls .previous").show();
			if(showing==imgCount) {
				$("#slideshow-controls .next").hide();
			}
		}
		return false;
	});

	$("#slideshow-controls .previous a").bind("click", function() { //Previous image
		if(showing>1) {
			$("#slideshow-set ul").animate({
				"left": "+="+imgW+"px"
			}, 600);
			showing--;
			$("#slideshow-indicators a").removeClass("current");
			$("#slideshow-indicators li.img"+showing+" a").addClass("current");
			$("#slideshow-controls .next").show();
			if(showing==1) {
				$("#slideshow-controls .previous").hide();
			}
		}
		return false;
	});

	//SLIDESHOW CONTROLS
	$("#slideshow-controls .previous").hide();
	$("#slideshow").bind("mouseover", function() {
		$("#slideshow-controls").fadeIn(300);
	}).bind("mouseleave", function() {
		$("#slideshow-controls").fadeOut(300);
	});
	
	//SLIDESHOW INDICATORS
    if(imgCount>1) {
		$("#slideshow-controls").after("<ul id=\"slideshow-indicators\"></ul>");
		var slideshowNavX = (imgW - ((13+3)*imgCount))/2; //(Page width x slideshow indicators (width + margin) x no. of nav items) /2
		$("#slideshow-indicators").css({ "margin-left": slideshowNavX+"px" });
		for(i=1;i<=imgCount;i++) {
			$("#slideshow-indicators").append("<li class=\"img"+i+"\"><a href=\"#Image"+i+"\">"+i+"</a></li>");
		}
		$("#slideshow-indicators li:first-child a").addClass("current");		

		$("#slideshow-indicators a").each(
			function() {
				var thisImage = $(this).text();
				var targetX = (thisImage-1)*imgW;
				$(this).bind("click", function() {
					if(showing!=thisImage) {
						$("#slideshow-set ul").animate({
							"left": "-"+targetX+"px"
						}, 600);
						showing = thisImage;
						$("#slideshow-indicators a").removeClass("current");
						$(this).addClass("current");
						if(showing==1) {
							$("#slideshow-controls .previous").hide();
						} else {
							$("#slideshow-controls .previous").show();
						}
						if(showing==imgCount) {
							$("#slideshow-controls .next").hide();
						} else {
							$("#slideshow-controls .next").show();
						}
					}
					return false;
				});
			}
		);
	}
	
}
