jQuery(document).ready(function(){
	
	if(jQuery('.product-collateral #specification').children('span').html()==''){
		jQuery('.product-collateral #specification').css('display','none');
		jQuery('ul#product-view-tabs #aspecification').css('display','none');
	}
	
	
	jQuery('ul#brand-dropdown li a.active').parent('li').css('display', 'block');
	
	jQuery('#links3 .cart-small').click(function(){
		jQuery('.mini-cart').addClass('on');
		return false;
	});
	
	jQuery('.mini-cart .cart-small').click(function(){
		jQuery('.mini-cart').removeClass('on');
		return false;
	});
	
	jQuery('#home_wide_ad ul.switcher li').click(function(){
		if(jQuery(this).hasClass('current')){
			return false;
		}
		else{
			var nc = jQuery(this).attr('class');
			jQuery('#home_wide_ad ul > li').removeClass('current');
			jQuery('#home_wide_ad ul.switcher > li').removeClass('current');
			jQuery(this).addClass('current');
			jQuery('#home_wide_ad ul > li.'+nc).addClass('current');
			return false;
		}
	});

	jQuery('#newest div.switcher a').click(function(){
		if(jQuery(this).hasClass('current')){
			return false;
		}
		else{
			var nc = jQuery(this).attr('class');
			jQuery('#newest div a').removeClass('current');
			jQuery('#newest ul li').removeClass('current');
			jQuery(this).addClass('current');
			jQuery('#newest ul li.'+nc).addClass('current');
			return false;
		}
	});
	
	jQuery('#home_wide_ad ul li').each(function(i){
		if(jQuery(this).parent('ul').hasClass('switcher')){
			return false;
		}
		else{
			i = i+1;
			jQuery(this).addClass('slide'+i);
			jQuery('#home_wide_ad ul.switcher > li:nth-child('+i+')').addClass('slide'+i);
		}
	});
	
	jQuery('#newest div a').each(function(i){
		i = i+1;
		jQuery(this).addClass('slide'+i);
	});
	
	jQuery('#newest ul li').each(function(i){
		i = i+1;
		jQuery('#newest ul li:nth-child('+i+')').addClass('slide'+i);
	});
	
	//Top ten rollover info
	//	jQuery('.top-ten-list ol li.listing-item, #newest ul li, .listing-type-grid li.item').hover(
	jQuery('.top-ten-list ol li.listing-item, #newest ul li').hover(
		function(){
		        jQuery(this).find('.item-desc').toggleClass('on');
		      }, 
		      function(){
		        jQuery(this).find('.item-desc').toggleClass('on');
		      }
	);
	
	// jQuery('.top-ten-list ol li.listing-item, #newest ul li.current').live('hover', (
	// 		function(){
	// 		        jQuery(this).find('.item-desc').toggleClass('on');
	// 		      }, 
	// 		      function(){
	// 		        jQuery(this).find('.item-desc').toggleClass('on');
	// 		      }
	// 	);
	
	
	
	jQuery('.header #navbar > li').hover(
		function(){
			jQuery(this).toggleClass('on');
		},
		function(){
			jQuery(this).toggleClass('on');
		}
	);

	
	//Category Page Gallery Switcher
	
	jQuery('.cat-gallery .switcher li').click(function(){
		if(jQuery(this).hasClass('current')){
			return false;
		}
		else{
			var nc = jQuery(this).attr('class');
			jQuery('.cat-gallery ul > li').removeClass('current');
			jQuery('.cat-gallery ul.switcher > li').removeClass('current');
			jQuery(this).addClass('current');
			jQuery('.cat-gallery ul > li.'+nc).addClass('current');
			return false;
		}
	});
	
	jQuery('.cat-gallery > ul > li').each(function(i){
		if(jQuery(this).parent('ul').hasClass('switcher')){
			return false;
		}
		else{
			i = i+1;
			jQuery(this).addClass('slide'+i);
			jQuery('.cat-gallery > li:nth-child('+i+')').addClass('slide'+i);
		}
	});
	
	jQuery('.cat-gallery ul.switcher > li').each(function(i){
			i = i+1;
			jQuery(this).addClass('slide'+i);
			jQuery('.cat-gallery > li:nth-child('+i+')').addClass('slide'+i);
	});

	//jQuery('#home_wide_ad ul.switcher > li:nth-child('+i+')').addClass('slide'+i);
	var firstGuts = jQuery('div.color-picker a:nth-child('+1+')').html();
	jQuery('.color-name').html(firstGuts);
	jQuery('div.color-picker span:nth-child('+1+')').addClass('current');
	jQuery('ul.color-images li:nth-child('+1+')').addClass('current');
	var colorName = jQuery('div.color-picker span a').html();
	if(colorName==null){ colorName = 'stringName';}
	var newest = lowercaseLetter(colorName);
	
	jQuery( 'div.color-picker span a').each(function(){
		var colorName = jQuery(this).html();
		var colorLower = lowercaseLetter(colorName);
		jQuery(this).html(colorLower);
		var colorGuts = jQuery(this).html();
		jQuery(this).css('background-color', colorGuts);
		
	});

	jQuery('div.color-picker span a').click(function(){
		jQuery('div.color-picker span').removeClass('current');
		jQuery(this).parent().addClass('current');
		var guts = jQuery(this).html();
        jQuery('.color-name').html(guts);
		jQuery('ul.color-images li').removeClass('current');
		jQuery('ul.color-images li').each(function(){
			if(jQuery(this).attr('class') == guts){
				jQuery(this).addClass('current');
			}
		})
		return false;
	});
		
	jQuery('ul#brand-dropdown').hover( 
		function(){
			jQuery(this).toggleClass('on');
			jQuery(this).siblings('a').css('display','none');
		}, 
		function(){
			jQuery(this).toggleClass('on');
			jQuery(this).siblings('a').css('display','inline');
		}
	);

	/*jQuery('a.enlarge-link').click(function(){
		window.open(this.href,'LargeImage','width=700,height=650,left=200,top=100');
		jQuery('.lb-background').fadeIn('medium');
		
		//window.open(this.href, 'width=500, height=500');
		return false;
	});*/
	
	//Positioning the lightbox content
	var pw = jQuery('.wrapper').width()/2 - ((jQuery('.lb-content').width())/2)-15;
	var ph = jQuery('.wrapper').height() + 25;
	var fullWidth = jQuery(window).width();
	jQuery('.lb-content').css('left', pw);

	jQuery('.lb-background').css('height', ph).css('width', fullWidth);

	//############################
	//#### Lightbox Functionality
	jQuery('.lb-show').click(function(){

		jQuery('.lb-content').css('left', pw);
		
		jQuery('.lb-background').fadeIn('medium');
		jQuery('.lb-content').fadeIn('medium');

		return false;
	});


	//## Close lightbox
	jQuery('.lb-background, .close').click(function(){
		jQuery('.lb-background, .lb-content').fadeOut('medium');
		return false;
	});
	


	/*jQuery('a.live-chat').click(function(){
		window.open(this.href,'Chatlink','width=400,height=450,left=200,top=100');
		return false;
	});*/

	jQuery('a.size-chart').click(function(){
		window.open(this.href,'SizeChart','width=400,height=450,left=200,top=100');
		return false;
	});

	jQuery('.header #navbar > li').each(function(){
		if(jQuery(this).children('div').children('ul').size()==2){
			jQuery(this).children('div').addClass('two-column');	
		}
	});
	
	jQuery('.narrow-by dt, .narrow-by dd').addClass('on');
	jQuery('.narrow-by dt').each(function (i){
		// use the loop counter argument to locate its paired match
		var match = jQuery('dd').eq(i);
		match.slideDown();
		
		if(match.children('ul').children('li').hasClass('on')){
			jQuery(this).addClass('on');
			match.slideDown();
			match.addClass('on');
		}
		// attach a toggle effect to the "dt" element
		jQuery(this).toggle(
			function (){
				// slide up the matching "dd" element
				jQuery(this).removeClass('on');
				match.slideUp();
				match.removeClass('on');
			},
			function (){
				// slide down the matching "dd" element
				jQuery(this).addClass('on');
				match.slideDown();
				match.addClass('on');
			}
		);
});

	jQuery('#sidebar ul li ul, #sidebar ul li h2').addClass('on');
	jQuery('#sidebar ul li h2').each(function (){
		jQuery(this).toggle(
			function (){
				// slide up the matching "dd" element
				jQuery(this).removeClass('on');
				jQuery(this).siblings('ul').slideUp();
				jQuery(this).siblings('ul').removeClass('on');
			},
			function (){
				// slide down the matching "dd" element
				jQuery(this).addClass('on');
				jQuery(this).siblings('ul').slideDown();
				jQuery(this).siblings('ul').addClass('on');
			});
	});
	
})

function lowercaseLetter(string){
    return string.charAt(0).toLowerCase() + string.slice(1);
}
