/*
* Client: Dulwich Picture Gallery
* JS: FADE IN/OUT and DELAY REDIRECT
* Author: Reading Room (NG)
 */


function fadeDelay() {
	var t = 1200;
	
	//if the referrer is in a section not the same as the current page, then
	var ref = document.referrer;
	var refSplit = ref.split("/")[4];
	
	var section = $("body").attr("id");
	
	//path / id inconsistency fixes! Not nice code I know... I'll clean up.
	if(refSplit == "the_gallery.aspx" || refSplit == "the_gallery") {
		refSplit = "gallery";
	}
	
	if(refSplit == "exhibitions.aspx") {
		refSplit = "exhibitions";
	}
	
	if(refSplit == "whats_on" || refSplit == "whats_on.aspx") {
		refSplit = "events";
	}
	
	if(refSplit == "education.aspx") {
		refSplit = "education";
	}
	
	if(refSplit == "support_us" || refSplit == "support_us.aspx") {
		refSplit = "support";
	}
	
	if(refSplit == "shop.aspx") {
		refSplit = "shop";
	}
	
	if(refSplit == "contact_us.aspx" || refSplit == "contact_us") {
		refSplit = "contact";
	}
	
	if(refSplit != section) { //if the destination page is not in the same section as the refering page, then fade in on arrival
		$("#wrapCont, #footer").css('visibility','visible').hide().fadeIn(1000);
	}
	else { //if not, just do nothing except override the css hiding the content
		$("#wrapCont, #footer").css('visibility','visible').show();
	}
	
	//if destination page is in different section to current page, then
	$("body#home a, body#exhibitions a:not([@href*=exhibitions]), body#collection a:not([@href*=collection]), body#gallery a:not([@href*=the_gallery]), body#events a:not([@href*=whats_on]), body#education a:not([@href*=education]), body#support a:not([@href*=support]), body#shop a:not([@href*=shop]), body#contact a:not([@href*=contact_us]), body#friends a:not([@href*=friends])").click(function () {
		if($(this).attr("title").match("opens new window") != null)
	    	{
			if ($(this).attr("title").match("opens new window").length > 0)
	        	{
	            	return false;
	        	}
	    	}
		var linkHref = $(this).attr("href"); //Store the href
		var linkRel = $(this).attr("rel");

		if(linkHref.indexOf("search_results.aspx") == -1 && linkHref.indexOf("mailto:") == -1 && linkHref != "#non" && linkHref.indexOf("artwork_detail.aspx") == -1 && (linkRel != "external")) {
		//if(linkHref != "/search/" && linkHref != "#non") {
			$(this).attr("href","#non"); //Replace the href
			$("#wrapCont, #footer, #largeimage").fadeOut(t); //Fade out content
			$("#searchBox").fadeOut(800); //Fade out content
			setTimeout(function(){ //Delay page refresh
				document.location.href = linkHref;
			}, t);
		}
	});
};

function printIcon() {
	$("#footer").prepend('<a class="print" href="javascript:window.print();" title="Print this page"><img src="/images/design/print.gif" alt="" /> Print page</a>');
};

function extLinks() {
	$('a[rel="external"],a[title*="opens new window"]').each(function() {
		$(this).click(function() {
			window.open($(this).attr('href'));
			return false;
		});
	});
};

$(function() {
	fadeDelay();
	printIcon();
	extLinks();
});