// JavaScript Document
$.noConflict();
jQuery(document).ready(function($) {
	// Hides all main images, other than the first one
	var first_image = $("#product_gallery .individual_image:nth-child(1)")
	$("#product_gallery .individual_image").not(first_image).hide();
	$("#product_gallery_videos .individual_image").hide();
	
	// Gets/sets total count of images/videos for counter
	var count_of_images = $("#product_gallery_images .individual_image").length
	var count_of_videos = $("#product_gallery_videos .thumbnails .set img").length
	$("#product_gallery_images .counter").text("1 of " + count_of_images);
	$("#product_gallery_videos .counter").text("1 of " + count_of_videos);

	// Marks the first thumbnail as active
	$("#product_gallery_images .thumbnails .set img:nth-child(1)").addClass("active");
	$("#product_gallery_videos .thumbnails .set a:nth-child(1)").children("img").addClass("active");

	// Sets the left value of each thumbnail. They are aboslutely positioned to allow the animation below to work.
	var initial_left = 0
	$("#product_gallery_images .thumbnails .set img").each(function() {
		$(this).css("left", initial_left);
		initial_left = initial_left+75
	});
	var initial_left = 0
	$("#product_gallery_videos .thumbnails .set img").each(function() {
		$(this).css("left", initial_left);
		initial_left = initial_left+75
	});

	// Moves the entire div to the first level inside body so that the overlay will work in IE, and creates the overlay div
	var gallery = $("#product_gallery")
	$("body")
		.append(gallery)
		.append("<div id='overlay'></div>");

	// Sets the left value of the div so it is centered in the window  
	var gallery_images_left = ($(window).width() - $("#product_gallery_images").width()) / 2
	var gallery_videos_left = ($(window).width() - $("#product_gallery_videos").width()) / 2
	$("#product_gallery_images").css("left", gallery_images_left+"px");
	$("#product_gallery_videos").css("left", gallery_videos_left+"px");

	// Displays the div	
	$(".wrapper_gallery div.view_images_big").click(function(event) {
			overlay_on()
			$("#product_gallery_images").show();
		})

	$(".wrapper_gallery div.view_video_big").click(function(event) {
			overlay_on();
			$("#product_gallery_videos").show();
			$("#product_gallery_videos .individual_image:nth-child(1)").show();			
			})

		function overlay_on() {
			$("#overlay")
				.css("height", $(document).height() + "px")
				.css("width", $(document).width() + "px")
				.show();
			}

	// Closes the div	
	$("#product_gallery .close").click(function(event) {
		$("#product_gallery_images, #product_gallery_videos").hide();
		// The following are to make IE happy
		// It removes the divs containing the videos, so that the audio shuts off if the user closes the window before the video is finished. 
		// Then, after the window is closed, the divs are added back.
		var main_image = $("#product_gallery #product_gallery_videos .main_image")
		$("#product_gallery #product_gallery_videos .main_image").remove();
		$("#overlay").hide();
		$("#product_gallery_videos .padding").prepend(main_image);
		})

	// Displays the correct main image based off the thumbnail clicked	
	$("#product_gallery_images .thumbnails .set img").click(function(event) {
		var thumbnail_index = $(this).index()
		var thumbnail_index_counter = thumbnail_index + 1
		var corresponding_main_image = $("#product_gallery #product_gallery_images .individual_image").get(thumbnail_index)
		$("#product_gallery .thumbnails .set img").removeClass("active");
		$(this).addClass("active");
		$("#product_gallery #product_gallery_images .individual_image").hide();
		$(corresponding_main_image).show();
		$("#product_gallery_images .counter").text(thumbnail_index_counter + " of " + count_of_images);
	  });

	$("#product_gallery_videos .thumbnails .set img").click(function(event) {
		var thumbnail_index = $(this).index()
		var thumbnail_index_counter = thumbnail_index + 1
		var corresponding_main_image = $("#product_gallery #product_gallery_videos .individual_image").get(thumbnail_index)
		$("#product_gallery .thumbnails .set img").removeClass("active");
		$(this).addClass("active");
		$("#product_gallery #product_gallery_videos .individual_image").hide();
		$(corresponding_main_image).show();
		$("#product_gallery_videos .counter").text(thumbnail_index_counter + " of " + count_of_videos);
	  });

	// Moves through the thumbnails based off clicking the right or left arrow
	var click_count_images = 0
	var total_images_thumbs = $("#product_gallery_images .thumbnails .set img").length
	$("#product_gallery_images .thumbnails .right").click(function(event) {
		if ((click_count_images+4) >= total_images_thumbs) {
			return false;
		}
		else {
			click_count_images++
			$("#product_gallery_images .thumbnails .set img")
				.animate({
					left: "-=75"
				   })			
		}
		});
	$("#product_gallery_images .thumbnails .left").click(function(event) {
		if (click_count_images <= 0) {
			return false;
		}
		else {
			click_count_images--
			$("#product_gallery_images .thumbnails .set img")
				.animate({
					left: "+=75"
				   })			
		}
		});
	var click_count_videos = 0
	var total_videos_thumbs = $("#product_gallery_videos .thumbnails .set img").length
	$("#product_gallery_videos .thumbnails .right").click(function(event) {
		if ((click_count_videos+4) >= total_videos_thumbs) {
			return false;
		}
		else {
			click_count_videos++
			$("#product_gallery_videos .thumbnails .set img")
				.animate({
					left: "-=75"
				   })			
		}
		});
	$("#product_gallery_videos .thumbnails .left").click(function(event) {
		if (click_count_videos <= 0) {
			return false;
		}
		else {
			click_count_videos--
			$("#product_gallery_videos .thumbnails .set img")
				.animate({
					left: "+=75"
				   })			
		}
	});
	// Adding google analytics to every page
	// $("<script type='text/javascript' src='/templates/Eureka_template/js/google_analytics.js'></script>").appendTo("head")
	});

// Load videos on click on thumbnail
	function LoadVideo(filepath_orig, title, description){
		var filepath = filepath_orig.trim();
		var filelength = parseInt(filepath.length) - 3;
		var extension = filepath.substring(filelength,filelength + 3);
		if (extension == 'swf'){
			swfobject.embedSWF(filepath , "flash_content", "400", "300", "9.0.0","expressInstall.swf", {}, {bgcolor: "#e7e7e7", wmode: "transparent"});
			jQuery("#individual_video .title").text(title);
			jQuery("#individual_video .description").text(description);
	}
		else if (extension == 'flv'){
			jQuery("#individual_video .title").text(title);
			jQuery("#individual_video .description").text(description);
			var flashvars = {};
			flashvars.flv = filepath;
			flashvars.kill = 0
			var params = {};
			params.menu = "false";
			params.quality = "best";
			params.scale = "noScale";
			params.wmode = "transparent";
			var attributes = {};
			swfobject.embedSWF("/templates/Eureka_template/player.swf", "flash_content", "400", "300", "9.0.0", "expressInstall.swf", flashvars, params, attributes, {id: "flash_content"});
			
		}
		else {
			jQuery("#individual_video .title").text(title);
			jQuery("#individual_video .description").text(description);
			jQuery("#flash_content").replaceWith('<iframe width="640" height="515" id="flash_content" scrolling="no" frameborder="0" src="' + filepath + '"></iframe>')
		}
	}

