

$(document).ready(function() {

	if(document.getElementById("detail_image")) {
		var $target = $("#detail_image")[0];
		var $target_src = strrev($target.src);
		var $target_img = strrev($target_src.substring(0,$target_src.indexOf("/")));
		var $identifier = $target_img.substring(0,$target_img.indexOf("_"))
		var $root_src = strrev($target_src.substring($target_src.indexOf("/"),$target_src.length));
	}

	// Attach image swapping functions to portfolio details
	$("#detail_list a").click(function() {
		$target.src = $root_src + $identifier + "_" + this.rel + "_large.jpg";
		$("#detail_list a").removeClass("here");
		$(this).addClass("here");
		return false;
	})
	.end()

	// Make main ep logo swap on rollover
	$("#logo-ep").hover(
		function () {
			$(this).attr({src: website_url + "site_images/logo_ep_over.gif"});
		},
		function () {
			$(this).attr({src: website_url + "site_images/logo_ep_faint.gif"});
		}
	);

	// Make links with the class 'popup' open in a new window
	$("a.popup").attr({target: "_blank", title: "Opens in a new window"});


	// Add a 'Print this page' button to the page controls
	if (window.print) {
		$("#pagecontrols").append(" | <a href=\"#\" onclick=\"printpage(); return false;\">Print this page</a>");
	}


	// Ajax to add project selection links to homepage
	$("#home_case_study").load("/content/ajax_list_projects");


	// Attach the hover event to 'button' links
	$(".button").hover(
		function () {
			$(this).css("backgroundPosition","0px -36px");
		},
		function () {
			$(this).css("backgroundPosition","0px 0px");
		}
	);


	// Function to load new project onto homepage
	updateProject = function(item_url) {
		if(item_url != "") {
			$("#home_case_study .grab").before("<div id=\"loading\"><img src=\"site_images/ajax-loader.gif\" alt=\"Loading content...\" /></div>");
			$.ajax({
				type: "GET",
				url: "/content/ajax_get_project/" + item_url,
				success: function(data) {
					if(data != "") {
						$("div#case_study_details").html(data);
					}
					$("#loading").remove();
					$("#home_case_study li a").each(function() {
						if(this.rel == item_url) {
							$(this).attr({href: "javascript:updateProject('')"});
							$(this).addClass("here");
						} else {
							$(this).attr({href: "javascript:updateProject('" + this.rel + "');"});
							$(this).removeClass("here");
						}
					});
				}
			});
		}
	};


	// Attach AJAX calls to portfolio sector list
	$("#sector-nav li a").each(function() {
		var $theURL = $(this).attr("href");
		var $newURL = $theURL.substring($theURL.indexOf("portfolio")+9,$theURL.length);
		$(this).attr("href", "javascript:updatePortfolioList('" + $newURL + "');");

	});


	// Function to load new portfolio list
	updatePortfolioList = function(list_url) {
		if(list_url != "") {
			$("ul.portfolio").before("<div id=\"loading\"><img src=\"site_images/ajax-loader.gif\" alt=\"Loading content...\" /></div>");

			$.ajax({
				type: "GET",
				url: "/content/portfolio/ajax_portfolio_list" + list_url,
				success: function(data) {
					if(data != "") {
						$("div#portfolio-list").html(data);
					}

					//$("#loading").remove();

					//$("#home_case_study li a").each(function() {
					//	if(this.rel == item_url) {
					//		$(this).attr({href: "javascript:updateProject('')"});
					//		$(this).addClass("here");
					//	} else {
					//		$(this).attr({href: "javascript:updateProject('" + this.rel + "');"});
					//		$(this).removeClass("here");
					//	}
					//});
				}
			});

		}
	};




	getTest = function(test_id, direction){
		$.ajax({
			type: "GET",
			url: "/content/ajax_get_testimonial_list/?test_id=" + test_id + "&direction=" + direction,
			success: function(data) {
				if(data != "") {
					data = parseInt(data);
					$("#home_testimonial").load("/content/ajax_get_testimonial/?entry_id=" + data);
				}
			}
		});
	}



	// Homepage testimonial ajax
	$('#home_testimonial').each(
		function() {
			$block_q = $(this).children('blockquote');
			$test_id = $block_q.attr("id");
			$test_id = $test_id.substring(12, $test_id.length)
			$("<p class=\"test-pag\"><a href=\"javascript:getTest('" + $test_id + "','prev');\">Previous</a> | <a href=\"javascript:getTest('" + $test_id + "','next');\">Next</a></p>").insertAfter('#home_testimonial h2');
		}
	);





});



var highlightSector = function() {
	if(activeSectors.length>0) {
		for(var a=0; a<activeSectors.length; a++) {
			if(activeSectors[a] != "") {
				var theCat = document.getElementById("cat_" + activeSectors[a]);
				addClass(theCat, "here");
			}
		}
	}
};



var sendToTop = function() {
	window.location = String(window.location).replace(/\#.*$/, "") + "#page_top";
};


addLoadEvent(highlightSector);



