// JavaScript Document
// Copyright (c) 2010 Hen's Teeth Network. All rights reserved.
$(function(){

	//
	// Handle Hierloom and Acrostic Popups
	//
	var flagHasHierloom = (-1 != $("#item-content h1").html().indexOf('eirloom')) ? true : false;
	var flagHasAcrostic = (-1 != $("#item-content h1").html().indexOf('crostic')) ? true : false;
	
	if (flagHasHierloom)
	{
		$("#heirloom-popup a").colorbox();
		$("#heirloom-popup").show();
	}
	
	if (flagHasAcrostic)
	{
		$("#acrostic-popup a").colorbox();
		$("#acrostic-popup").show();
	}

	$("#main-item-enlarge-image").hover(function(){
			this.src="/PDGCommTemplates/HTN/images/item_image_enlarge.jpg";
		},
		function(){
			this.src="/PDGCommTemplates/HTN/images/item_image_enlarge_glass.jpg";
	});
	
	
	//
	// For "historical terms", convert them into links for the appropriate page
	//
	var aHistoricalPeriods = [
		{'name': 'georgian', 
		 'keywords': 'georgian', 
		 'template': 'KYG_georgian.html'},		 
		{'name': 'victorian',
		 'keywords': 'victorian', 
		 'template': 'KYG_victorian.html'},		 
		{'name': 'edwardian',
		 'keywords': 'edwardian', 
		 'template': 'KYG_edwardian.html'},		 
		{'name': 'art nouveau',
		 'keywords': 'art nouveau', 	 
		 'template': 'KYG_artn.html'},
		{'name': 'arts and crafts',
		 'keywords': 'arts and crafts', 
		 'template': 'KYG_artc.html'},		 
		{'name': 'art deco',
		 'keywords': 'art deco', 
		 'template': 'KYG_artd.html'},		 
		{'name': 'retro', 
		 'keywords': 'retro',
		 'template': 'KYG_retro.html'}
	];

	$(aHistoricalPeriods).each(function(){
		var sSearch = "(" + this.name + ")";
		var sReplace = "<a class='history_link' href='/cgi-bin/commerce.cgi?search=action&keywords=" + 
			escape(this.keywords) + "&key=" + $("#key").val() +
			"&perpage=200&template=PDGCommTemplates/HTN/" + this.template + "'>$1</a>";
		var re = new RegExp(sSearch, "gi");
		$("#item-content p.main-item-content").html($("#item-content p.main-item-content").html().replace(re, sReplace))
	});	
	
	//
	// Handle click events on thumbnails
	//
	$(".secondary-item-image").hover(function(){
		$("#main-item-image").attr('src', $(this).attr('id'));
		$("#main-item-enlarge-image").show();
	});

/* removed for ticket 278
	// The last image isn't part of the gallery, so hide the enlarge image
	$(".secondary-item-image:last").hover(function(){
		if ($(".secondary-item-image:first").attr('alt') != $(".secondary-item-image:last").attr('alt'))
		{
			$("#main-item-enlarge-image").hide();
		}
	});
*/

	// Add BIG images to DOM
	$("#slideshow-images").html(aSlideshow.join(''));

/* removed for ticket 278
	// The last one (with a ruler) doesn't have a big image so remove it.
	if ($(".secondary-item-image:first").attr('alt') != $(".secondary-item-image:last").attr('alt'))
	{
		$("#slideshow-images img:last").remove();  
	}
*/
	
	//
	// Attach slideshow to BIG images that were added above
	// We're using the very cool and versitile colorbox jQuery plugin - the tool used by jQuery Ninjas
	//
	
	/* updated by edg 7/13/11 to use innerWidth and innerHeight rather than width and height.
	    Images are 600x600 and colorbox adds a 10px margin all around, so I made the settings 620x620.*/
	    
	$("#slideshow-images img").colorbox(
		{rel:"image-gallery", 
		 transition:"none", 
		 current: "{current} of {total}", 
		 //width:"610px", 
		 //height:"625px", 
		 innerWidth:"620px",
		 innerHeight:"620px",
		 href: function(){return $(this).attr('src'); }
		}
	);
	
	//
	// Handle click event on magnifying glass to launch the slideshow
	//
	$("#main-item-enlarge-image").click(function(){
		// We need to start with the image currently displayed, so grab it's src attribute
		$("#slideshow-images img[name='" + $("#main-item-image").attr('src') + "']").click();
		return false;
	});

}); // end of file

