jQuery(document).ready(function(){
	
	// Homepage functions
	if(jQuery('body').hasClass('home')){
		
		jQuery('#home-about-us h1 a, #about-close').click(function(event){
			event.preventDefault();
			jQuery('#home-about-us .hidden').slideToggle('slow');
			jQuery('#home-about-us').toggleClass('bgswap');
			jQuery('#about-close').toggleClass('hidden');
			if(jQuery('#about-close').hasClass('hidden')){
				jQuery('#about-close').fadeIn('slow');
			} else {
				jQuery('#about-close').fadeOut('fast');
			}
		});
	
		jQuery('#home-gallery-container').cycle({
			fx: 'fade',
			pager: '#home-gallery-nav',
			timeout: 0,
			
			pagerAnchorBuilder: function(idx, slide) {
	        //var src = jQuery(slide).eq(0).attr('id');
	        var src = jQuery(slide).attr('rel');
	        //return '<a href="#" title="' + src + '"></a>';
	        
	        return '<a href="#" class="home-gallery-thumb" title="View this image"><img width="54px" height="30px" src="'+src+'" alt="Gallery thumbnail image" /></a>';
			}
		        		
		});
	
		jQuery('#hero-container').cycle({
			fx: 'fade',
			delay: 3000,
			timeout: 8000,
			pager: '#hero-nav',	
			//pause: '1',	
			pagerAnchorBuilder: function(idx, slide) {
			                        var src = jQuery(slide).eq(0).text();
			                        var trimsrc = jQuery.trim(src);
			                        return '<a href="#" title="'+trimsrc+'"></a>';
			}       		
		});
	
	}
	
	jQuery('.product-feature-item').click(function(){
		var thishref = jQuery('.product-feature-inner h2 a', this).attr('href');
		window.location.href = (thishref);
	    return false;
	});
	
	jQuery('.feature-nugget').click(function(){
		var thishref = jQuery('h3 a', this).attr('href');
		window.location.href = (thishref);
	    return false;
	});
	
	
	// Product page function
	if(jQuery('#features-gallery-container').size() > 0){
		
		jQuery('#features-gallery-container').cycle({
			fx: 'fade',
			pager: '#features-gallery-nav',
			timeout: 0,
			
			pagerAnchorBuilder: function(idx, slide) {
			                        //var src = jQuery(slide).eq(0).attr('id');
			                        var src = jQuery(slide).attr('rel');	        		                        
			                        //return '<a href="#" title="' + src + '"></a>';
			                        
			                        return '<a href="#" class="features-gallery-thumb" title="View this image"><img width="54px" height="30px" src="'+src+'" alt="Gallery thumbnail image" /></a>';
			}
		        		
		});
		
	}
	
	if(jQuery('#image-gallery-container').size() > 0){
		
		jQuery('#image-gallery-container').cycle({
			fx: 'fade',
			pager: '#image-gallery-nav',
			timeout: 0,
			
			pagerAnchorBuilder: function(idx, slide) {
			                        //var src = jQuery(slide).eq(0).attr('id');
			                        var src = jQuery(slide).attr('rel');	        		                        
			                        //return '<a href="#" title="' + src + '"></a>';
			                        
			                        return '<a href="#" class="image-gallery-thumb" title="View this image"><img width="45px" height="27px" src="'+src+'" alt="Gallery thumbnail image" /></a>';
			}
		        		
		});
	}
	
	
	jQuery("#tabs").tabs({ fx: { opacity: 'toggle' } });
	//jQuery("#tabs").tabs("option", "fx", { opacity: 'toggle' });
	
	jQuery("ul.ui-tabs-nav li").click( function(event) {
		event.preventDefault();
	});
		
	if(jQuery.browser.msie){
	} else {	
	// Replace the 'buy now 'input button with a span to make cufon work
		//jQuery('.wpsc_buy_button').each(function(){
		    //jQuery(this).hide().after('<span class="wpsc_buy_button">').next('span.wpsc_buy_button').text(this.value).click(function(){
		        //jQuery(this).prev('input.wpsc_buy_button').click();
		    //});
		//});
		
		Cufon.replace('.browsersub', {
		hover:true,
		textShadow: '#000 -1px -1px'
	});
		    
    // Replace the 'checkout login' input button with a span to make cufon work
	
	/*	jQuery('#wpsc_shopping_cart_container p.login-submit input').each(function(){
		
		    jQuery(this).hide().after('<span class="checkout-login">').next('span.checkout-login').text(this.value).click(function(){
		        jQuery(this).prev('p.login-submit input').click();
		    });
	    
		});*/
	}
	
	Cufon.replace('.fontsub, #reply-title, .form-submit input, .page-styling h3, .page-styling h2, .page-styling h3, .wpsc_buy_button, .firstcol a', {
		hover:true,
		textShadow: '#000 1px 1px'
	});
	
	Cufon.replace('.nav li', {
		hover:true,
		textShadow: '#000 -1px -1px'
	});

	jQuery("div.accessory:even").addClass("accessory-left");
	jQuery("div.accessory:odd").addClass("accessory-right");
	
	if(jQuery('body').hasClass('category')){
		var multiplecats = jQuery(".subcat-container").size();
		//alert(multiplecats);
		// Toggle categories on category.php
		if(multiplecats > '0'){
			jQuery('.cat-item').not('.cat-item:first').click(function(event){
				if(jQuery(this).hasClass('current-cat')){
				 //alert('Yes');
				} else {
				event.preventDefault();
				var lastClass = jQuery(this).attr('class').split(' ').slice(-1);
				var subcatclass = 'sub-'+lastClass;
				//alert(subcatclass);
				jQuery(".subcat-container").not(".sub"+lastClass).slideUp();
				jQuery(".sub"+lastClass).slideDown();
				jQuery('.cat-item').removeClass('current-cat');
				jQuery(this).addClass('current-cat');
				}
			});
			jQuery('.cat-item:first').click(function(event){
				jQuery('.subcat-container').each(function(){
					jQuery(this).slideDown();
				});
				jQuery('.cat-item').removeClass('current-cat');
				jQuery(this).addClass('current-cat');
			});
			jQuery('.cat-item a').click(function(event){
				event.preventDefault();
			});
		}
		
		jQuery("ul.sub-sub-nav li").click(function(event){
			event.preventDefault();
			if(jQuery(this).hasClass('allprod')){
				var suballid = getID(jQuery(this).attr('id'));
				var subcontainer = jQuery(this).parent('ul').parent('.subcat-container').attr('class').split(' ').slice(-1);
				//alert('This is the sub category id: '+suballid);
				jQuery('.subcat-item-'+suballid+' .product-container, .subcat-item-'+suballid+' .product-container').slideDown();
				jQuery('.'+subcontainer+' ul.sub-sub-nav li').removeClass('current-cat');
				jQuery(this).addClass('current-cat');
			} else {
				var subcontainer = jQuery(this).parent('ul').parent('.subcat-container').attr('class').split(' ').slice(-1);
				//alert('This is the parent category: '+subcontainer);
				var subsubid = getID(jQuery(this).attr('id'));
				jQuery('.'+subcontainer+' .product-container,.'+subcontainer+' .product-container-main').not('.prod-cat-'+subsubid).slideUp();
				jQuery('.prod-cat-'+subsubid).slideDown();
				jQuery('.'+subcontainer+' ul.sub-sub-nav li').removeClass('current-cat');
				jQuery(this).addClass('current-cat');
			}
		});
		
		jQuery("ul.sub-sub-nav li a").click(function(event){
			event.preventDefault();
		});
	}
	
	var cartempty = jQuery(".shopping-cart-wrapper p.empty").size();
	//alert(cartempty);
	if(cartempty > 0  || jQuery('body').hasClass('pagename-checkout')){
		jQuery("#sideshoppingcart").hide();
	}
	
	
	jQuery('span.emptycart').click(function(event){
		jQuery("#sideshoppingcart").delay(2000).fadeOut('slow');
	});
	
	jQuery('#sideshoppingcart h3').click(function(event){
		jQuery.get('http://'+document.domain+'/cartsession.php');
		var bodyid = jQuery('body').attr('id')
		if(bodyid == 'cartdrawn'){
			jQuery("#sideshoppingcart").animate({
				width: 234
			}, 500, function(){
				jQuery("#shoppingcartcontents").slideDown(function(){
					jQuery('body').removeAttr('id');
				});
			});
		} else {
			jQuery("#shoppingcartcontents").slideUp(function(){
				jQuery("#sideshoppingcart").animate({
					width: 45
				}, 500, function(){
					jQuery('body').attr('id', 'cartdrawn');
				});
			});
		}
	});
	
	jQuery('.wpsc_buy_button').click(function(event){
		var bodyid = jQuery('body').attr('id')
		if(bodyid == 'cartdrawn'){
			jQuery("#sideshoppingcart").delay(500).fadeIn('slow');
			jQuery("#sideshoppingcart").animate({
				width: 234
			}, 500, function(){
				jQuery("#shoppingcartcontents").slideDown(function(){
					jQuery('body').removeAttr('id');
				});
			});
			jQuery.get('http://'+document.domain+'/cartsession.php');
		} else {
			jQuery("#sideshoppingcart").delay(500).fadeIn('slow');
		}
	});

	

	
	jQuery(".equalheights").equalHeights();
	
	// Toggle the feature items
	jQuery("ul.feature-list li").click(function(event){
		event.preventDefault();
		var featureid = getID(jQuery(this).attr('id'));
		jQuery(".feature-item").not("#item-"+featureid).slideUp(300, function(){
			jQuery("#item-"+featureid).slideDown(600);
		});
		jQuery('ul.feature-list li').removeClass('current-cat');
		jQuery(this).addClass('current-cat');
	});
	
	
	jQuery('.pagename-transaction-results .page-styling').wrapInner('<p class="trans"/>');
	// Remove unecessary dom elements
	jQuery("p:empty").remove();
	
});

// Return Id of string //
function getID(strID){
 return strID.replace(/[^0-9]/gi, '');
}
