$(document).ready(function(){

	if ( $("body").hasClass("home") ) {
		$('#latest-news ol li:gt(0)').hide();
		$('#latest-news ol').cycle({ 
			delay:  2000, 
			speed:  500
		});
	}
	
	if ( $("body").hasClass("product") ) {
		// pdv tabs
		var body = $("#product .body").first();
		body.remove();
		var spec = $("#product .item-info");
		spec.remove();
		var revs = $("#product #customer-reviews");
		revs.remove();
		
		var tabhtml='\
			<div class="tabs-box">\
				<ul class="tabs">\
					<li class="current desc"><a href="#desc">Description</a></li>';
		if($.trim(spec.html())!=''){
			tabhtml+='\
					<li class="spec"><a href="#spec">Specification</a></li>';
		}
		tabhtml+='\
					<li class="revs"><a href="#revs">Customer Reviews</a></li>\
				</ul>\
				<div id="desc" class="tab-conts"><div class="body">'+ body.html() +'</div></div>';
		if($.trim(spec.html())!=''){
			tabhtml+='\
				<div id="spec" class="tab-conts"><table class="nc item-info">'+ spec.html() +'</table></div>';
		}
		tabhtml+='\
				<div id="revs" class="tab-conts"><div id="customer-reviews">'+ revs.html() +'</div></div>\
			</div>';
		$("#product .inner-box .favourites").after(tabhtml);
			
		
		$(".tab-conts").hide();
		$("#desc").show();
		
		$(".tabs a").click(function(){
			$(".tabs li").removeClass("current");
			$(this).parents(".tabs-box").find(".tab-conts").hide();
			$("#"+$(this).parent().attr("class")).show();
			$(this).parent().addClass("current");
			return false;
		});
		
		if ( $("#spec table").html() == "" ) $(".tabs .spec, #spec").remove();
		if ( $("#revs #customer-reviews").html() == "" ) {
			$(".tabs .revs, #revs").remove();
		} else {			
			if ( window.location.hash.match(/customer\-reviews/) )
				$(".revs a").click();
		}
		
		$("form .rating .select-Enum").after('<span id="stars-cap"></span>').stars({
			inputType: "select",
			cancelShow: false,
			captionEl: $("#stars-cap")
		});
		
		if ( $("#product .images").length ) {
			$("#product .images").append('<ul></ul>');
			var $imgs = $("#product .images img");
			$imgs.each(function(i){
				var $img = $(this);
				$(this).attr("id","item"+i);
				$('<li class="item'+i+'"><a href="#item'+i+'" class="item'+i+'"><img style="width:100%;height:100%;" src="'+ $(this).attr("src") +'" /></a></li>')
					.appendTo("#product .images ul");
					
			});
			$("#product .images ul a")
				.click(function(){
					$imgs.css("z-index",1);
					$("#"+$(this).attr("class")).css("z-index",10);
					return false;
				})
				.mouseover(function(){
					$imgs.css("z-index",1);
					$("#"+$(this).attr("class")).css("z-index",10);
				});
			$("#product .images #item0").css("z-index",10);
		}	
	}
	
	if ( $("#checkout").length ) {
		$("#checkout .billing-address ol").hide().parents("fieldset").find(".msg")
			.html('<a id="adr-toggle" href="#adr-toggle-link">Click here to add a billing address if different from delivery address.</a>');
			
		$("#adr-toggle").click(function(){
			$("#checkout .billing-address ol").show();
			$(this).parent().hide();
			return false;
		});
		
		$(".nolabel select")
			.change(function(){
				if( $(this).val() != '' ) {
					$(this).parents("fieldset").find("li:not(.nolabel)").hide();
				} else {
					$(this).parents("fieldset").find("li:not(.nolabel)").show();
				}
			}).change();
	}
	
	if ( $("#primary #search").length ) {
		$(' <small><a id="search-toggle" href="#search-toggle">Toggle search form on/off</a></small>').appendTo("#search legend").click(function(){
			if ($("#search ol").is(":visible")) {
				$("#search ol, #search fieldset.submit").hide();
			} else {
				$("#search ol, #search fieldset.submit").show();
			}
			return false;
		}).click();
	}
	
});

