$(function(){
	
	$('div.picker').picker().ieHeightFix();
	$('ul.products').ieHeightFix();
	$('div.option_info').optionInfo();
	
	$('#faqs').faqShowHide();
	
	$('#gallery, #ad_sponsors ul').cycleFade();
	
	$('.zoom').colorbox({opacity:.7});
	$('.overlay, .zoom').colorbox({
		opacity:.7,
		onComplete: function(){ $('.tabs').tabs(); }
	});

	$("#sagepay_checkout_options").each(function(){
		var container = $(this);
		var hiddenForms = container.find(">div")
		
		var checkoutChooser = $('<ul id="checkout_chooser"></ul>').insertBefore(container);
		
		hiddenForms.each(function(){
			var div = $(this);
			$('<li><label><input type="radio" name="checkoutchooser" />'+div.find("h2").text()+'</label></li>').appendTo(checkoutChooser).click(function(){
				hiddenForms.hide();
				div.show();
			});
		});
		
		container.find("h2").remove();
		container.find(">div").hide();
		
	});
	
	//Simple image gallery on product detail page
	$("#suit_gallery_small a").click(function() {
		var imageSrc = $(this).find("img").attr("src");
		$("#suit_gallery_large img#large_image").attr('src',imageSrc);
		$("#suit_gallery_large .zoom").attr('href',imageSrc);
	  return false;
	});
	
	//Clicking on the large image opens an overlay window
	/*$('#suit_gallery_large a').click(function() {
		var imageLargeSrc = $(this).find("img#large_image").attr("src");
		$('#suit_image_overlay').fadeIn('fast', function() {
			var img = new Image();
			$(this).prepend(img);
			$(img).fadeIn();
		  $(img).attr({
			  'src': imageLargeSrc,
			  'alt': ''
			});
		});
		return false;
	});
	
	$('#suit_image_overlay .close').click(function() {
		$('#suit_image_overlay').hide();
		$('#suit_image_overlay img').remove();
		return false;
	});*/

});


$.fn.picker = function(){
	return this.each(function(){

		var radios = $('input',this).css({position:'absolute',left:'-9999px'});
		radios.filter(':checked').parent().addClass('checked');
		radios.parent().click(function(){
			$(this).addClass('checked').siblings().removeClass('checked');
			$(this).children('input').attr('checked',true);
		}).hover(function(){
			$(this).addClass('over');
		},function(){
			$(this).removeClass('over');
		});
		
		// image border
		$('>label>img',this).each(function(){
			$('<span class="border" />').css({
				position: 'absolute',
				height: $(this).height()-8+'px',
				width: $(this).width()-8
			}).insertBefore(this);
		});
		
	});
}

$.fn.optionInfo = function(){
	var infos = this;
	return this.each(function(){
		var info = $(this).append('<img src="/images/bg_option_info.png" alt="" class="pointer" />');
		var head = info.prev('h3,span.item');
		var info_button = $('<a href="#"><img src="/images/icon_info.png" alt="More information" /></a>').appendTo(head);
		var close_button = $('<a href="#"><img src="/images/icon_close.png" alt="Close" /></a>').appendTo(info);

		var infoHeight = info.outerHeight();
		info.hide().css({
			position: 'absolute',
			marginLeft: (info_button.position().left-16) + 'px',
			marginTop: -(infoHeight+head.outerHeight(true)+8) + 'px'
		});

		info_button.click(function(){
			var showing = info.is(':visible');
			infos.hide();
			if (!showing) info.show();
			return false;
		});
		close_button.click(function(){ info.fadeOut(); return false; });

	});
}

$.fn.faqShowHide = function(){
	return this.each(function(){

		$('>li',this).each(function(){
			var item = $(this).addClass('closed');
			var content = $('>div',this).hide();
			$('>h2',this).click(function(){
				if (item.is('.closed')) {
					item.removeClass('closed').addClass('open');
					content.show();
				} else {
					item.removeClass('open').addClass('closed');
					content.slideUp();
				}
			});
		});
		
		if (document.location.hash.length>0) $(document.location.hash).click();
		
	});
}

$.fn.cycleFade = function(){
	return this.each(function(){

		var lis = $('>li',this);
		lis.eq(0).show();
		lis.not(':first').hide();

		window.setInterval(function(){
			var current = lis.filter(':visible');
			var next = (current.next().length>0) ? current.next() : lis.eq(0);
			current.fadeOut('slow');
			next.fadeIn('slow');
		},4000);

	});
}

$.fn.tabs = function(){
	return this.each(function(){
		$('a',this).click(function(){
			$(this).parent().addClass('on').siblings().removeClass('on')
			$('#'+this.href.split('#')[1]).show().siblings('div').hide();
			return false;
		}).eq(0).click();
	});
}

// IE6+7 don’t properly implement clearing, so here’s a height equalisation function
$.fn.ieHeightFix = function(){
	return this.each(function(){
		if ($.browser.msie && $.browser.version<8) {
			var items = $(this).children();
			var line_length = Math.ceil(items.length/items.filter('.row_leader').length);
			var start = 0;
			while (start<items.length) {
				var row = items.slice(start,start+line_length+1);
				var height = row.map(function(){
					return $(this).height();
				}).get().sort(function(a,b){return (a-b);}).pop();
				row.height(height);
				start+=(line_length+1);
			}
		}
	});
}


