$(document).ready(function() {
	
	$('a#enlarge-button').hide();
	// Get the loccation of the page.
	// This script should only be included
	// when the products controller is called.
	current_url = location.href;
	
	$('div#video-player').css('background-color', '#000000');
	if ((current_url.indexOf('/product/')) > 0) {

		// IE6 and IE7 should have the fade effect disabled
		// as this creates a nasty 'flashing' effect, which
		// it shouldn't do.
		var ie = false;
		jQuery.each( jQuery.browser , function(i, val) {
			if (i == 'msie' && (jQuery.browser.version.substr(0, 3) == "6.0" || jQuery.browser.version.substr(0, 3) == "7.0")) {
				ie = true;
			}
		});

		// Check to see whether or not there is media
		if( $("#no-media").length == 0 ){

			// Set the initial values of the main-image area
			// The 1ist item to display in the main-image are will
			// always have the id of thumb_0
			// get id
			initial_src = $('img#thumb_0').attr('src');
			// Get the media type, this is stored as a class
			initial_type = $('img#thumb_0').attr("class");
			initial_type = initial_type.split(" ");
			initial_type = initial_type[0];
			
			// Switch the type;
			switch (initial_type) {
	
			case 'image':
				// hide the video player.
				$("div#video-player").css("display", "none");
				gallery_image(initial_src, false, ie);
				break;
	
			case 'video':
				// hide the image
				$("#main-image a").css("display", "none");
				gallery_video(initial_src, false, ie);
				break;
	
			}
	
			// Now handle clicks
			$('[id^=thumb]').click(function() {
	
				thumb_src = $(this).attr('src');
	
				// Get the media type, this is stored as a class
					thumb_type = $(this).attr("class");
					thumb_type = thumb_type.split(" ");
					thumb_type = thumb_type[0];
	
					// Switch the type;
					switch (thumb_type) {
	
					case 'image':
						gallery_image(thumb_src, true, ie);
						break;
	
					case 'video':
						gallery_video(thumb_src, true, ie, $(this).attr("id"));
						break;
	
					}
	
				});
	
		}
	
	}
		
});

// Clicked thumbnail is an image so
// add all related attributes, etc.
function gallery_image(src, effects, ie) {
	$('#main-image a').show();
	$('#main-image div#video-player').hide();
	$('#main-image div#video-player').html('');
	// $('#main-image:first-child').css("border","1px solid red")
	$('a#enlarge-button').show();
	// Create paths for other image locations.
	medium_location = src.split('small').join('med');
	large_location = src.split('small').join('large');

	// Dont do anything if the medium_location is the
	// same as src. no point in reloading the image.
	// Set links to large image.
	$('.enlarge-link').attr('href', large_location);

	// ie6 && ie7 should no use fades as it can't handle them
	// correctly.
	if (effects == true && ie == false) {

		if ($('div#video-player').css("display") != 'none') {
			$('div#video-player').slideUp( "slow",	function() {
				i = 0;
				$('#main-image a img').attr("src", medium_location).load(function() {
					if (i == 0) {
						$('#main-image a img').attr("src", medium_location);
						$('#main-image a').fadeIn();
					}
					i++;
				});
			});
		} else {

			if (medium_location != $('#main-image a img').attr("src")) {
				// replaces image
				$('#main-image a').fadeOut( "slow", function() {
					i = 0;
					$('#main-image a img').attr("src", medium_location).load(function() {
						if (i == 0) {
							$('#main-image a').fadeIn();
						}
						i++;
					});
				});
			}

		}

	} else {
		$('#main-image a img').attr("src", medium_location);

	}

}

// Clicked thumbnail is an video so
// add all related attributes, etc.
function gallery_video( src , effects , ie , id ) {

	$('a#enlarge-button').hide();
	// Remove the src of the image
	// Disable the enlarge link.
	$('#main-image a img').attr("src", "");
	$('.enlarge-link').attr('href', "");
	$('.enlarge-link').attr('disabled', 'disabled');

	// Create video location
	video_location = src.split("/");
	video_src_length = (video_location.length) - 1;
	// Create filename
	video_src_filename = video_location[video_src_length];
	video_src_filename = video_src_filename.split(".");
	
	// Video type
	thumb_type = $("img#"+id).attr("class");
	thumb_type = thumb_type.split(" ");
	thumb_type = thumb_type[1];
	
	// Create filename.
	video_src_filename[1] = thumb_type;
	video_src_filename = video_src_filename.join(".");
	video_src_filepath = 'videos/' + video_src_filename;
	video_location[video_src_length] = video_src_filepath;
	video_location = video_location.join("/");
	video_location = video_location.split("../assets").join("assets");
	
	// Host & First dir
	window_location = window.location.href
	window_location = window_location.split("http://"+location.host+'/').join("");
	window_location = window_location.split("/");
	
	// Create absolute path to video
	/*host = "http://"+location.host+'/'
	if( window_location[0] != 'product' ){
		host += window_location[0] + '/';
	}*/
	
	host = "http://"+location.host;
	
	var web_location_array = window_location;
	web_location_array.pop();
	web_location_array.pop();
	
	var web_location = '';
	for (var i = 0; i < web_location_array.length; i++) {
		web_location += '/' + web_location_array[i];
	}
	//web_location = web_location;

	
	video_location = host + web_location + '/' + video_location;
	
	// Destroy current object.
	// Create new object

	//new_object = '<script type="text/javascript">';
	//var so = new SWFObject("'+ web_location + '/site/swf/player.swf", "player", "439", "293", "9", "#000000");
	//so.addParam("wmode", "transparent");
	//so.addVariable("video", "'+video_location+'");
	//so.addVariable("autoplay", "true");
	//so.write("video-player");
	//new_object += '</script>';


	var image_location = video_location.replace(/\.[^.]+$/, ".jpg");
	
	var new_object = '<object width="439" height="293"><param name="movie" value="' + web_location + '/site/swf/player.swf"></param><param name="wmode" value="transparent"></param><param name="flashvars" value="video=' + video_location + '&thumburl='+ image_location + '"></param><embed src="'+ web_location + '/site/swf/player.swf" type="application/x-shockwave-flash" flashvars="video=' + video_location + '&thumburl=' + image_location + '" wmode="transparent" width="439" height="293"></embed></object>';	

	
	// Load in new object.
	if (effects == true && ie == false) {
		// Effects are to be used
		// the browser is not IE
		
		// If there is an image shown,
		//		hide id,
		//			load in video
		// else
		//		load in video
		if ($('#main-image a').css("display") != 'none') {
			$('#main-image a:visible').fadeOut("slow", function() {
					$('#main-image div#video-player').html("");
					$('#main-image div#video-player').html( new_object ).fadeIn("fast");
					
			});
		} else {
			$('#main-image div#video-player:visible').fadeOut("slow" ,  function(){
					$('#main-image div#video-player').html("");
					$('#main-image div#video-player').html( new_object ).fadeIn("fast");
			});
		}
		
	} else {
		$('#main-image a').hide();
		// Effects are disabled and this could be IE
		// This would be used if the first item in the
		// gallery is a video.
		$('#main-image div#video-player').html(new_object).fadeIn("fast");
	}
	
	

}

function embed_swf(video_location, web_location) {
	/*
	var so = new SWFObject(web_location + "/site/swf/player.swf", "player", "439", "293", "9", "#000000");
	so.addParam("wmode", "transparent");
	so.addVariable("video", video_location);
	so.addVariable("autoplay", "true");
	so.write("video-player");
	*/
	
	
	var flashvars = false;
	var params = {
	  menu: "false",
	  flashvars: "video=" + video_location
	};
	var attributes = {
	  id: "video-player",
	  name: "video"
	};

	swfobject.embedSWF(web_location + "/site/swf/player.swf", "video-player", "439", "293", "9.0.0", web_location + "/site/swf/expressInstall.swf", flashvars, params, attributes);
}