$(document).ready(function(){
	$('A[rel="external"]').click( function() { window.open($(this).attr('href')); return false; });
	$('A[rel="pdf"]').click( function() { window.open($(this).attr('href')); return false; });

	$("div#product-info").append('<a class="back" href="#" title="Go back to your previous page">Back</a>');
	$("a.back","div#product").click(function(){ history.back(); return false; });

	if($("#products").size() && $("#product-images").size())
	{
		// get the urls and preload them
		links = $('a','#product-images');
		$.each(links,function(i){
			this.preload = new Image();
			this.preload.src = this.href;
		});
		$("#product-images > ul.thumbs > li:first").addClass('active');
		// swap the main image and set classes when clicked
		$("#product-images > ul.thumbs > li").click(function(){
			target = $("#product-images > img");
			link = $('a',this);
			swapImage(target,link);
			$(".active", "#product-images > ul.thumbs").removeClass('active');
			$(this).addClass('active');
			return false;
		});
	}
});

function swapImage(target,link)
{
	// fadeout target, change src and alt, fade back in
	$(target).fadeOut("fast",function(){
		$(target).attr('src',$(link).attr('href'));
		$(target).attr('alt',$(link).attr('title'));
		$(target).fadeIn("fast");
	});
}

function showImages()
{
	$("#product-images > img").fadeIn('fast',function(){
		$("#product-images > ul.thumbs").slideDown();
	});
}


jQuery.fn.expandable=function(options){options=$.extend({more:'more »',less:'« hide',length:20,greedy:true},options||{});return this.each(function(){var $this=$(this),text=$this.text(),truncated=text.substr(0,options.length),after=text.substr(options.length);if(!text){return;}
truncated=(options.greedy)?truncated+(after.match(/.*?[\s]/)||[''])[0]:truncated.match(/.*[\s]/)[0];truncated=truncated.substr(0,truncated.length-1);$this.data('expandable',{full:text,truncated:truncated});var $less=$('<a/>').text(options.less).attr('href','#less').addClass('expand').click(function(){$(this).appendTo('body').hide();$this.text($this.data('expandable').truncated+' ').append($more.show());return false;});var $more=$('<a/>').text(options.more).attr('href','#more').addClass('expand').click(function(){$(this).appendTo('body').hide();$this.text($this.data('expandable').full+' ').append($less.show());return false;});$less.click();});};