$(document).ready(function(){
	
	// ------ MEDIA ---------
	
	// for each link to an mp3, remove it and add music player to show-media div...

	$("#content a[href*='.mp3']").each(function(){
		var embedcode = '<embed width="172" height="68" type="application/x-shockwave-flash" wmode="transparent" pluginspage="http://www.macromedia.com/go/getflashplayer" quality="high" flashvars="song=' + $(this).attr("href") + '" src="/scripts/flash/music_player.swf"/>'
		$(this).parent().hide().prevAll(".show-media:first").prepend(embedcode);
	});

	// for each youtube vid, remove it and add it to the nearest show-media div above it...
	
	var curVid = 0;
	$("div.video").each(function(){
		curVid++;

		// select the YT player in this div
		var myObject = $(this).children(":first");

		// get width and height of YT player
		var myWidth = parseInt(myObject.attr("width"));
		var myHeight = parseInt(myObject.attr("height"));

		// hide the div and set ID for fancybox to point to
		$(this).hide().attr("id","video"+curVid);

		if ($("body#previous").length) { // if we're on a previous shows page...
			// select previous show-media div and add a link to the start of it pointing to our div
			$(this).prevAll(".show-media:first").prepend("<p class='video_link'><a class='video_link video_link"+curVid+"' href='#video"+curVid+"'>Watch video</a></p>");
		} else { // otherwise add it to the images div
			$("#images").prepend("<p class='video_link'><a class='video_link video_link"+curVid+"' href='#video"+curVid+"'>Watch video</a></p>");
		}

		// add fancybox to this link
		$("a.video_link"+curVid).fancybox({
			'hideOnContentClick': false,
			'overlayShow':	true,
			'overlayOpacity': .8,
			'overlayColor': "#000",
			'zoomOpacity':true,
			'frameWidth':myWidth,
			'frameHeight':myHeight
		});
	});
	
	/* ----- SHOP ------ */
	
	if ($("body#shop").length) {

		// if we've got the country saved in cookie, and it's worldwide, change handling_cart values to relect that
		if ($.cookie('country')=="country_worldwide") {
			$("input[name='handling_cart']").attr("value","5.00");
		}

		$("input[alt='Add to cart']").click(function() {
					
			if ($.cookie('country')==null) {
				// close any open country panels
				$("div.country_check").parent().parent().remove();

				$(this).parent().parent().parent().after('<tr><td colspan="3" class="country_check_td"><div class="country_check" style="display:none"><h3>Please select postage and packaging</h3><ul><li><a class="country_uk">Post to UK address (free P&amp;P)</a></li><li><a class="country_worldwide">Post to worldwide address (&pound;5 P&amp;P per order)</a></li></ul><p>Or <a class="country_cancel">cancel</a></p></div></td></tr>');
				$("a.country_cancel").click(function() {
					$("div.country_check").slideUp(400, function() {
						$(this).parent().parent().remove();
					});
				});
				// when we choose a country
				$("a.country_uk, a.country_worldwide").click(function() {
					// set handling cart values accordingly
					if ($(this).is(".country_uk")) {
						$("input[name='handling_cart']").attr("value","0");
					} else {
						$("input[name='handling_cart']").attr("value","5.00");
					}
					
					// save cookie for next time
					$.cookie('country',$(this).attr("class"));
					
					// submit the Add to cart button above
					$(this).closest("tr").prev().find("form").submit();
					
				});
				
				// finally, display the panel
				$(".country_check").slideDown();
				return false;
			}
		});
	
	}
	
});
