$(document).ready(function(){

// link targets
// nb rel may contain multiple space-separated values, e.g. rel="external nofollow"
	$("a[rel~='top']").attr("target", "_top");
	$("a[rel~='blank']").attr("target", "_blank");
	$("a[rel~='external']").attr("target", "_blank");
	$("a.url-warn").click(function(event) {
		if(confirm("You are being taken to an external website:\n\n" + $(this).attr("href") + "\n\nBircham Gallery cannot be held responsible for the contents of this website.\n\nContinue?")) {
			return true;
		} else {
			event.preventDefault();
			return false;
		}
	});

// button press
	$(".button, button").mousedown(function() {
		$(this).addClass("button-on");
	}).mouseup(function() {
		$(this).removeClass("button-on");
	}).mouseleave(function() {
		$(this).removeClass("button-on");
	});

// homepage exhibition lead
	$("#lead-current strong").hide();
	$("#lead-current span").css("padding-bottom", "8px"); // for IE7
	$("#lead-current").fadeTo(0, 0.9).parent("#banner").mouseenter(function() {
		$("#lead-current").fadeTo(200, 1).find("strong").slideDown(175);
	}).mouseleave(function() {
		$("#lead-current").fadeTo(500, 0.9).find("strong").slideUp(400);
	});

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

// popup windows with settings
// if popup on an image link doesn't work, add the popup classname to the space() function
// popup window may only have one classname!

// if settings seem to be ignored, check that no classnames other than than spaceball or js-enabled-popup are applied to the link.
// if so, remove them or add code to replace them on window.open().
	var popupLinkConfig = [];
	popupLinkConfig["popup"] = "width=500, height=500, scrollbars=yes, resizable=yes, menubar=no";
	popupLinkConfig["pci-popup"] = "width=700, height=500, scrollbars=yes, resizable=yes, menubar=no";
	popupLinkConfig["purchase-print-order-popup"] = "width=700, height=500, scrollbars=yes, resizable=yes, menubar=yes";
	popupLinkConfig["terms-popup"] = "width=500, height=500, scrollbars=yes, resizable=yes, menubar=yes";
	popupLinkConfig["z-code-popup"] = "width=500, height=500, scrollbars=yes, resizable=no, menubar=no";
	//	var catalogueZoomPopupHeightDesired = parseInt($(this).find("span").eq(1).attr("class"), 10) + 110;
	//	var catalogueZoomPopupHeight;
	//	if(catalogueZoomPopupHeightDesired > screen.height - 100) { catalogueZoomPopupHeight = screen.height - 100; }
	//	else catalogueZoomPopupHeight = catalogueZoomPopupHeightDesired;
	//popupLinkConfig["catalogue-zoom-popup"] = "width=" + (parseInt($(this).find("span").eq(0).attr("class"), 10) + 81) + ", height=" + catalogueZoomPopupHeight + ", scrollbars=yes, resizable=yes, menubar=no, left=" + ((screen.width / 2) - ((Math.round($(this).find("span").eq(0).attr("class")) + 81) / 2));
	
	$("a.popup, a.pci-popup, a.purchase-print-order-popup, a.terms-popup, a.z-code-popup").each(function(i) {
		// add popup icon class for suitable popups
		// exclude certain other links where icon looks ugly
		if($(this).hasClass("pci-popup") == false) {
			$(this).addClass("js-enabled-popup");
		}
	}).live('click', function(event) {
		event.preventDefault();
		newWin = window.open($(this).attr("href"), $(this).attr("target"), popupLinkConfig[$.trim($(this).attr("class").replace("spaceball", "").replace("js-enabled-popup", ""))]);
		newWin.focus();
	});


// link confirmations
	$(".basket-empty a").click(function() {
		if(!confirm("Empty basket - sure?")) {
			return false;
		}
	});
	$(".wish-list-delete-list a").click(function() {
		if(!confirm("Permanently delete your wish list - sure?")) {
			return false;
		}
	});
	$("a.confirm").click(function() {
		if(!confirm("Are you sure? This is irreversible...")) {
			return false;
		}
	});

// basket delivery options table
$("#basket-delivery-options").hide();
$("#basket-delivery-options").before("<p><a href=\"#\" id=\"show-delivery-options\">Show Delivery Options</a></p>");
$("#show-delivery-options").toggle(function() {
		$("#basket-delivery-options").fadeIn();
	},
	function() {
		$("#basket-delivery-options").fadeOut();
	}
);

// purchase page
	if($("#content.purchase-page").length) {
		
		$(".js-remove").remove();
		
		$("#copy_cardholder").click(function() {
			$("#delivery_address").val($("#cardholders_name").val() + '\n' + $("#cardholders_address").val()); 
			$("#delivery_postcode").val($("#cardholders_postcode").val());
			$("#delivery_address_country").attr("selectedIndex", $("#cardholders_address_country").attr("selectedIndex"));
			$("#delivery_address_country").change();
		});
		
		// page load/re-load
		if($("#delivery_yes").attr("checked")) {
			$("#collection-block, #total-without-delivery, #non-uk-delivery-note, #total-with-international").hide();
			$("#delivery-address-block, #total-with-delivery").show();
		}		
		if($("#delivery_no").attr("checked")) {
			$("#collection-block, #total-without-delivery").show();
			$("#delivery-address-block, #total-with-delivery, #non-uk-delivery-note, #total-with-international").hide();
		}
		if($("#delivery_yes_free").attr("checked")) {
			$("#collection-block, #total-with-delivery, #non-uk-delivery-note, #total-with-international").hide();
			$("#delivery-address-block, #total-without-delivery").show();
		}
		if($("#delivery_international").attr("checked")) {
			$("#collection-block, #total-with-delivery, #total-without-delivery").hide();
			$("#delivery-address-block, #non-uk-delivery-note, #total-with-international").show();
		}
		
		// radio clicks		
		$("#delivery_yes").click(function() {
			if($(this).attr("checked")) {
				$("#collection-block").fadeOut("normal", function() {
					$("#delivery-address-block").fadeIn();
				});
				$("#total-without-delivery, #non-uk-delivery-note, #total-with-international").hide();
				$("#total-with-delivery").show();
			}
		});
		$("#delivery_yes_free").click(function() {
			if($(this).attr("checked")) { 
				$("#collection-block").fadeOut("normal", function() {
					$("#delivery-address-block").fadeIn();
				});
				$("#total-with-delivery, #non-uk-delivery-note, #total-with-international").hide();
				$("#total-without-delivery").show();
			}
		});
		$("#delivery_no").click(function() {
			if($(this).attr("checked")) {
				$("#delivery-address-block").fadeOut("normal", function() {
					$("#collection-block").fadeIn();
				});
				$("#total-with-delivery, #non-uk-delivery-note, #total-with-international").hide();
				$("#total-without-delivery").show();
			}
		});
		$("#delivery_international").click(function() {
			if($(this).attr("checked")) {
				$("#collection-block").fadeOut("normal", function() {
					$("#delivery-address-block").fadeIn();
				});
				$("#total-with-delivery, #total-without-delivery").hide();
				$("#non-uk-delivery-note, #total-with-international").show();
			}
		});
	}
	
	
// pinball effect
	$(".pinball-scoop").live("mouseover", function() {
		$(this).addClass("pinball-on");
	}).live("mouseout", function() {
		$(this).removeClass("pinball-on");
	});

// enlarge artwork image
/*
	if($(".artwork-nav-enlarge").length > 0) {
		var original_width = 0;
		var original_height = 0;
		var new_width = $(".artwork-nav-enlarge span").eq(0).attr("class");
		var new_height = $(".artwork-nav-enlarge span").eq(1).attr("class");
		var new_src = $(".artwork-nav-enlarge").attr("href");
		var preloader = new Image();
				
		$(".artwork-nav-enlarge").click(function() {
			if($(".artwork-nav-enlarge").hasClass("enlarged")) {
				$(".artwork-nav-enlarge").removeClass("enlarged");
				$("#artwork-img img").animate({'width':original_width, 'height':original_height}, "normal", function() {
					$("#artwork-img").parent().addClass("two-column");
					$("#purchase, #artwork-title").fadeIn("normal", function() {
						if($("#z-marker").length == 0) {
							respace();
						}
					});
				});
			} else {
				
				if(original_width == 0) { original_width = $("#artwork-img img").width(); }
				if(original_height == 0) { original_height = $("#artwork-img img").height(); }
				
				$("#purchase, #artwork-title").fadeOut("normal");
				
				preloader.src = new_src;
				preloader.onload = function() {
					preloader.src = "";
					$("#artwork-img").parent().removeClass("two-column");
					$("#artwork-img img").animate({'width':new_width, 'height':new_height}, "normal", function() {
						$(".artwork-nav-enlarge").addClass("enlarged");
						if($("#z-marker").length == 0) { respace(); }
					}).attr("src", new_src);			
				}
			}
			return false;
		});
	}
*/

// catalogue lightbox
$(function() {
	var catalogueZoomPopupWidth = parseInt($(".catalogue-zoom-popup").find("span").eq(0).attr("class"), 10);
	//var catalogueZoomPopupHeight = parseInt($(".catalogue-zoom-popup").find("span").eq(1).attr("class"), 10);
	
	function lightboxPosition() {
		$("#lightbox-overlay").css(
			{
				width: $(window).width() + "px", 
				height: $(document).height() + "px"
			}
		);
		$("#lightbox-content").css(
			{
				left: parseInt(($(window).width() - catalogueZoomPopupWidth) / 2, 10) + "px"
			}
		);
	}
		
	$(".catalogue-zoom-popup").live("click", function(event) {
	
		event.preventDefault();
		$("body").append('<div id="lightbox-overlay" class="lightbox-sundry"></div><div id="lightbox-content" class="lightbox-sundry"></div>');
		
		// show the overlay
		$("#lightbox-overlay").css(
			{
				background: "#000", 
				position: "absolute", 
				top: 0, 
				left: 0, 
				"z-index": 99999999,
				opacity: 0
			}
		).fadeTo(750, 0.7).click(function() {
			$(".lightbox-sundry").fadeOut(300, function() {
				$(this).remove();
			});
		});	
		
		// load the lightbox content
		$("#lightbox-content").css(
			{
				background: "#fff",
				position: "absolute",
				top: "24px", 
				"z-index": 999999999,
				padding: "24px",
				cursor: "pointer",
				display: "none"
			}
		).load($(".catalogue-zoom-popup").attr("href") + " #zoom-container", function() {
			$("#lightbox-content #back").remove();
			$("#lightbox-content img").load(function() {
				$(this).css({"margin-bottom":"14px"});
				$("#lightbox-content").fadeIn(300, function() {
					if($("#z-marker").length == 0) {
						respace();
					}
				});
			});
		}).click(function() {
			$(".lightbox-sundry").fadeOut(300, function() {
				$(this).remove();
			});
		});

		lightboxPosition();
		
		$(window).resize(function() {
			lightboxPosition();
		});
		
	});
});



// provenance truncator
$(function(){

	// The height of the content block when it's not expanded
	var adjustheight = 80;

	// don't truncate unnecessarily
	if($("#provenance").height() < adjustheight + 64) { 
		return; 
	}

	// The "more" link text
	var moreText = "show more";
	// The "less" link text
	var lessText = "show less";

	$("#provenance").wrapInner('<div class="more-block" />');

	// Sets the .more-block div to the specified height and hides any content that overflows
	$("#provenance .more-block").css('height', adjustheight).css('overflow', 'hidden');

	// The section added to the bottom of the "more-less" div
	$("#provenance").append('<span class="continued">[&hellip;]</span> <a href="#" class="adjust"></a>');

	$("a.adjust").text(moreText);

	$(".adjust").toggle(function() {
			$(this).parents("div:first").find(".more-block").css('height', 'auto').css('overflow', 'visible');
			// Hide the [...] when expanded
			$(this).parents("div:first").find(".continued").css('display', 'none');
			$(this).text(lessText);
		}, function() {
			$(this).parents("div:first").find(".more-block").css('height', adjustheight).css('overflow', 'hidden');
			$(this).parents("div:first").find(".continued").css('display', 'block');
			$(this).text(moreText);
	});
});


// art market
$(".art-market-collapsed-info").hide().before('<a href="#" class="art-market-toggle">More information</a>');
$(".art-market-toggle").click(function(event) {
	event.preventDefault();
	$(this).hide().next("dl").slideDown(300);
});


// workaround for php's max_file_uploads setting
// disable all empty file fields on submit to avoid empty fields being counted
$("form").submit(function(){
    $('input:file[value=""]').attr("disabled", "disabled");
});
$('input:file').change(function(event) {
	if($('input:file[value!=""]').length == 20) {
		window.alert("You now have the maximum number of files allowed for uploading. Please upload these 20 files before uploading any more.");
		$('input:file[value=""]').attr("disabled", "disabled");
		event.preventDefault();
		return false;
	}
});



// spacer
// .complete property doesn't seem to work so run over all images on window load.
// do not run on image load or variable heights may cause phantom spacers all a-over the place!
function space(what) {
	if($(what).hasClass("spaced")) {
		 return;
	}
	
	$(what).addClass("spaced");
	
	var w = $(what).width();
	var h = $(what).height();
	var o = $(what).position(); // need to use position and append internally to offset parents for pinball effect to work
	var ball;
	
	if($(what).parent("a").length) {
		ball = $(what).parent("a").clone(true).empty();
	} else {
		ball = $("<div />");
	}
	
	$(ball).css(
		{
			display:"block", 
			width: w + "px", 
			height: h + "px", 
			position: "absolute", 
			top: o.top + "px", 
			left: o.left + "px",
			"z-index": "2"
		}
	).addClass("spaceball");
	
	$(what).offsetParent().append(ball);
}

function respace() {
	$(".spaceball").remove();
	$("img.spaced").removeClass("spaced");
	$("img").each(function() {
		space($(this));
	});
}

if($("#z-marker").length == 0) {
	$(window).load(function() {
		$("img").each(function() {
			space($(this));
		});
	});
	$(window).resize(function() {
		respace();
	});
}

});
