var slideShowInterval = null;
// Main navigation dropdowns
var closetimer = 0;
var navitem = 0;

function nav_open() {  
	nav_canceltimer();
	if (jQuery(this) != navitem) {
	   nav_close();
   	navitem = jQuery(this);
		jQuery(this).find('ul').css('visibility', 'visible');
	   jQuery(this).children("a").addClass('over_state');
	}
}

function nav_close() {  
	if(navitem) {
		navitem.find('ul').css('visibility', 'hidden');
		navitem.children("a").removeClass('over_state');
	}
}

function nav_timer() {
	closetimer = window.setTimeout(nav_close, 500);
}

function nav_canceltimer() {  
	if(closetimer) {  
		window.clearTimeout(closetimer);
      closetimer = null;
	}
}

// Preload rollover images (from css)
jQuery.preloadImages = function() {
	for (var i = 0; i<arguments.length; i++) {
		jQuery("<img>").attr("src", arguments[i]);
	}
}

jQuery.preloadImages(
    '/images/hdr/topnav_gsgcnwi_over.gif',
    '/images/hdr/topnav_register_over.gif',
    '/images/hdr/topnav_login_over.gif',
    '/images/hdr/topnav_my_account_over.gif',
    '/images/hdr/topnav_privacy_policy_over.gif',
    '/images/hdr/topnav_help_over.gif',
    '/images/hdr/topnav_shopping_cart_over.gif',
    '/images/hdr/nav_council_info_over.png',
    '/images/hdr/nav_gs_info_over.png',
    '/images/hdr/nav_support_us_over.png',
    '/images/hdr/nav_cookies_over.png',
    '/images/hdr/nav_shop_over.gif',
    '/images/main/btn_donate_over.gif',
    '/images/main/btn_join_over.gif',
    '/images/main/btn_pitch_in_over.gif',
    '/images/store/btn_remove_over.gif'
);



// Slideshow
var curr_slide = 1;
var switching = false;
var num_slides = 0;

function slideshowLink(num) {
	clearInterval(slideShowInterval); 
	setSlideshow(num);
}

function setSlideshow(num) {
	if (num > num_slides) num = num_slides;
	if (num != curr_slide && !switching && num != 0) {
		switching = true;
		jQuery('#slideshow_link'+curr_slide).removeClass('selected');
		jQuery('#slideshow_link'+curr_slide).attr('src', jQuery('#slideshow_link'+curr_slide).attr('src').replace('_over', '_off'));
		jQuery('#slideshow_link'+num).addClass('selected');
		jQuery('#slideshow_link'+num).attr('src', jQuery('#slideshow_link'+num).attr('src').replace('_off', '_over'));

		jQuery(jQuery('#slideshow_items .slideshow_item')[curr_slide-1]).css('z-index', '1');
		jQuery(jQuery('#slideshow_items .slideshow_item')[num-1]).css('z-index', '9');
		jQuery(jQuery('#slideshow_items .slideshow_item')[num-1]).fadeIn(1000, function(){
			jQuery(jQuery('#slideshow_items .slideshow_item')[curr_slide-1]).hide();
			curr_slide = num;
			switching = false;
		});
	}
}

function slideShow() {
	num = curr_slide+1;
	if (num > num_slides) num = 1;
	setSlideshow(num);
}

function buildSlideButtons(num) {
	jQuery('<a href="#" onclick="slideshowLink(1);return false;"><img id="slideshow_link1" class="rollover selected" src="images/main/btn_1_over.png" alt="1" /></a>').appendTo('#slideshow_btns');
	for (var i=2; i < num + 1; i++) {
		jQuery('<a href="#" onclick="slideshowLink('+i+');return false;"><img id="slideshow_link'+i+'" class="rollover" src="images/main/btn_'+i+'_off.png" alt="'+i+'" /></a>').appendTo('#slideshow_btns');
	}
}

function delayUpdateShoppingCartNumber() {
	setTimeout('updateShoppingCartNumber()', 500);
}

function updateShoppingCartNumber() {
  jQuery('.add_to_cart_btn').click(delayUpdateShoppingCartNumber);
  jQuery('.product_item .productAttributes').each(function(){
		jQuery(this).closest('.product_item').find('.add_to_cart_btn').click(popupAttributes);
  });
  var cartsummary = jQuery('.cartSummaryItem').html();
  if (cartsummary) {
	  var cartnum = cartsummary.substr(0, cartsummary.indexOf(" "));
	  if (!isNaN(cartnum)) {
	  		jQuery('.cart_num').html("("+parseFloat(cartnum)+")");
	  } else {
			jQuery('.cart_num').html("(0)");
	  }
  }
}

jQuery(function() {
	jQuery('#header > div > ul > li').mouseover(nav_open);
   jQuery('#header > div > ul > li').mouseout(nav_timer);

	if (jQuery('#maincontent').next().is('h1')) {
		jQuery('#maincontent').next().addClass('first_child');
	} else if (jQuery('#maincontent').next().children().eq(0).is('h1')) {
		jQuery('#maincontent').next().children().eq(0).addClass('first_child');
	}

	if (jQuery('#product_cross_sell').length) {
		if (jQuery('#product_cross_sell').html().indexOf('This product has no related products') != -1) {
			jQuery('#product_cross_sell').css('display', 'none');
		}
	}
	
	if (jQuery('#sidebar').length && jQuery('.browse-panel').length) {
		jQuery('#sidebar').append(jQuery('.browse-panel'));
		if (jQuery('.browse-sale').length) {
			if (jQuery('.browse-sale .count').html().indexOf('(0)') == -1) jQuery('.browse-sale').before('<h6>Sale Items</h6>');
			else jQuery('.browse-sale').css('display', 'none');
		}
		jQuery('.browse-panel').html(jQuery('.browse-panel').html().replace('less than', 'Up to'));
		jQuery('.browse-panel').html(jQuery('.browse-panel').html().replace('Sub Catalogs', 'Sub Categories'));
		if (!jQuery('#store_product').length) jQuery('.browse-panel').css('display', 'block');
	}

	if (jQuery('#slideshow_items').length) {
		num_slides = jQuery('#slideshow_items .slideshow_item').size();
		if (num_slides > 5) num_slides = 5;
		buildSlideButtons(num_slides);
		jQuery(jQuery('#slideshow_items .slideshow_item')[0]).show();
		slideShowInterval = setInterval("slideShow()", 7000);
	}

  // Rollover any images with class="rollover"
  jQuery('.rollover').hover(function() {
		if (!jQuery(this).hasClass('selected')) {
	      this.src = this.src.replace("_off","_over");
		}
  }, function() {
		if (!jQuery(this).hasClass('selected')) {
	      this.src = this.src.replace("_over","_off");
		}
  });

  // Preload rollover images (from page)
  jQuery('.rollover').each(function(){
		var img = new Image();
      img.src = this.src.replace("_off","_over");
  });
  
  // Add "first" and "last" class to main navigation dropdown items
  jQuery('#header ul ul li:first-child').each(function() {
  		jQuery(this).addClass('first');
		jQuery(this).mouseout(function(){jQuery(this).addClass('first')});
  });

  jQuery('#header ul ul li:last-child').each(function() {
  		jQuery(this).addClass('last');
		jQuery(this).mouseout(function(){jQuery(this).addClass('last')});
  });

  // Add "first" class to first subnav items
  jQuery('#subnav ul li:first-child').each(function() {
  		jQuery(this).addClass('first');
		jQuery(this).mouseout(function(){jQuery(this).addClass('first')});
  });
  
  if (jQuery('h1').next().is('h2')) {
  		jQuery('h1').next().addClass('h2_after_h1');
  }

  // Fix selected issue
  jQuery('.selected').each(function(){
  	  var thiselm = jQuery(this);
	  if (!thiselm.find('li.selected').length) {
		  thiselm.children('a').addClass('selected');
     } else {
		  thiselm.children('a').addClass('selected_parent');
	  }
  });
  
  // Get number of items from shopping cart
  updateShoppingCartNumber();
  
  jQuery('body').css('background', '#fff url(/images/main/bg.jpg) top left repeat');

	jQuery.fn.textNodes = function()
	{
	  return $(this).contents().filter(function(){ return this.nodeType == 3 || this.nodeName == "BR" ; });
	}
	
	jQuery('img.[class*=patch]').each(function() {
		jQuery(this).wrap('<div class="full_image"></div>');
		jQuery(this).parent().append('<div class="'+jQuery(this).attr("class")+'"></div>');
	});
	
	jQuery('img.[class=left_image]').each(function() {
		jQuery(this).wrap('<div class="left_image"></div>');
	});

	jQuery('img.[class=right_image]').each(function() {
		jQuery(this).wrap('<div class="right_image"></div>');
	});

	jQuery('img.[class=vertical_right_image]').each(function() {
		jQuery(this).wrap('<div class="vertical_right_image"></div>');
	});

	jQuery('img.[class=vertical_left_image]').each(function() {
		jQuery(this).wrap('<div class="vertical_left_image"></div>');
	});

	if (jQuery('.store_breadcrumb').length && jQuery('#breadcrumb').length) {
		jQuery(".store_breadcrumb a[href='/products']").remove();
		jQuery(".store_breadcrumb a[href='/age-group']").remove();
		var storebreadcrumb = jQuery('.store_breadcrumb').html();
		jQuery('#breadcrumb').html(storebreadcrumb.replace(' : ','').replace(/ : /g,'<img src="/images/main/vr_breadcrumb.gif" alt="">'));
	}
	
	jQuery('span.email').each(function() {
		jQuery(this).wrap("<a></a>");
		var address = jQuery(this).text().replace(" at ", "@");
		jQuery(this).parent().attr('href', 'mailto:' + address);
		jQuery(this).text(address);
	});
	
	jQuery('.product_item .productAttributes').each(function(){
		jQuery(this).closest('.product_item').find('.add_to_cart_btn').click(popupAttributes);
	});
});

$(window).load(function(){
  jQuery('#subnav a.selected').each(function(){
		var curheight = jQuery(this).height();
		if (curheight > 24 && curheight < 40) jQuery(this).addClass('selected2Lines');
		else if (curheight > 40) jQuery(this).addClass('selected3Lines');
  });
});

(function($) {
	$.fn.customFadeIn = function(speed, callback) {
		$(this).fadeIn(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
	$.fn.customFadeOut = function(speed, callback) {
		$(this).fadeOut(speed, function() {
			if(jQuery.browser.msie)
				$(this).get(0).style.removeAttribute('filter');
			if(callback != undefined)
				callback();
		});
	};
})(jQuery);

function addShippingChoice() {
	if (jQuery('#ShippingOptions option:selected').text().indexOf('Pick up at store') != -1) {
		if (jQuery('#catshopbuy').attr('href').indexOf('&SO=1') == -1)	jQuery('#catshopbuy').attr('href', jQuery('#catshopbuy').attr('href')+'&SO=1');
	} else {
		if (jQuery('#catshopbuy').attr('href').indexOf('&SO=1') != -1) jQuery('#catshopbuy').attr('href', jQuery('#catshopbuy').attr('href').replace('&SO=1',''));
	}
}

var attrElm = "";
var attrId = "";
var unitsElm = "";
var unitsId = "";
function popupAttributes() {
	var elm = jQuery(this);
	jQuery('.attribute_options').html("");
	jQuery('.attribute_options').html(elm.closest('.product_item').html());
	if (attrElm != "") {
		attrElm.attr('id', attrId);
	}
	if (unitsElm != "") {
		unitsElm.attr('id', unitsId);
	}
	attrElm = jQuery(elm.closest('.product_item').find('.productAttributes')[0]);
	attrId = attrElm.attr('id');
	unitsElm = jQuery(elm.closest('.product_item').find('input.[id*=Unit]')[0]);
	unitsId = unitsElm.attr('id');
	
	attrElm.attr('id', "");
	unitsElm.attr('id', "");
	jQuery('.attribute_options .productAttributes').attr('id', attrId);
	jQuery('.attribute_options .item_add .productTextInput').attr('id', unitsId);

	jQuery('.attribute_options .add_to_cart_btn').click(function() {
		delayUpdateShoppingCartNumber();
		if (attrId != "" && jQuery('#'+attrId) != jQuery('.attribute_options .productAttributes')) {
			jQuery('#'+attrId).attr('id', "");
		}
		if (unitsId != "" && jQuery('#'+unitsId) != jQuery('.attribute_options .item_add .productTextInput')) {
			jQuery('#'+unitsId).attr('id', "");
		}
//		attrClose();
	});
	
	jQuery('.attribute_options .rollover').each(function() {
			this.src = this.src.replace("_over","_off");
	});

	// Rollover any images with class="rollover"
	jQuery('.attribute_options .rollover').hover(function() {
		this.src = this.src.replace("_off","_over");
	}, function() {
		this.src = this.src.replace("_over","_off");
	});
	jQuery('.attribute_popup').css({ "left": (elm.closest('.product_item').position().left - 51) + "px", "top":(elm.closest('.product_item').position().top - 10)+ "px"});
	jQuery('.attribute_popup').show();
}

function attrClose() {
	if (attrElm != "") {
		jQuery('.attribute_options .productAttributes').attr('id', "");
		attrElm.attr('id', attrId);
	}
	if (unitsElm != "") {
		jQuery('.attribute_options .item_add .productTextInput').attr('id', "");
		unitsElm.attr('id', unitsId);
	}
	jQuery('.attribute_popup').hide();
}

