jQuery(document).ready(function(){


	// Homepage functions
	
	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;
	});
	
	// Masthead function
	jQuery('#al_loginForm p:not(.login-remember) label, #al_checkout_loginForm p:not(.login-remember) label').hide();
	jQuery('#al_loginForm input[type=text], #al_loginForm input[type=password]').each(function() {
		var title = jQuery(this).siblings('label').text();
		//alert('This is the title: '+title);
		if(jQuery(this).val() === '') {
			jQuery(this).val(title);
		}

		jQuery(this).focus(function() {
			if(jQuery(this).val() === title) {
				jQuery(this).val('').addClass('focused');
			}
		});

		jQuery(this).blur(function() {
			if(jQuery(this).val() === '') {
				jQuery(this).val(title).removeClass('focused');
			}
		});
	});
	
	jQuery('#al_checkout_loginForm input[type=text], #al_checkout_loginForm input[type=password]').each(function() {
		var title = jQuery(this).siblings('label').text();
		//alert('This is the title: '+title);
		if(jQuery(this).val() === '') {
			jQuery(this).val(title);
		}

		jQuery(this).focus(function() {
			if(jQuery(this).val() === title) {
				jQuery(this).val('').addClass('focused');
			}
		});

		jQuery(this).blur(function() {
			if(jQuery(this).val() === '') {
				jQuery(this).val(title).removeClass('focused');
			}
		});
	});
	
	jQuery('#al_login p.login-submit input').click(function(event){
		jQuery('#al_login p.error').hide();
	});

	// Product page function
	
	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>';
		}
	        		
	});
	
	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 {	
	
		
		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, .page-styling h3, .page-styling h2, .page-styling h3, .wpsc_buy_button, .firstcol a, input.make_purchase, span.checkout-login', {
		hover:true,
		textShadow: '#000 1px 1px'
	});
	
	Cufon.replace('#home-about-us h1', {
		hover:true,
		textShadow: '#f7ddaf 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");
	
	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');
	});


	// Remove unecessary dom elements
	jQuery("p:empty").remove();
	
	
	/*jQuery('body').keyup(function(e) {
	    if (e.keyCode == 13) {
	       alert('You hit enter');
	    }
	});*/
	
	
});

// Return Id of string //
function getID(strID){
 return strID.replace(/[^0-9]/gi, '');
}
