// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// jQuery preload images
jQuery.preloadImages = function() {
	for(var i=0; i<arguments.length; i++) {
		$("<img>").attr("src", arguments[i]);
	}
}

// LOAD SHADOWBOX SKIN
Shadowbox.loadSkin('classic', '/shadowbox/skin');


$(document).ready(function(){
  Shadowbox.init({
        overlayColor: '#000',
        overlayOpacity: 0.9
	});
	
	// alert('jQuery loves you!');
	
	// settings
	$("div.settings_region").hover(
		function () {
			$(this).children('div.settings_region_lang').show();
			$(this).children('div.settings_region_name > a').addClass('active');
		}, 
		function () {
			$(this).children('div.settings_region_lang').hide();
			$(this).children('div.settings_region_name > a').removeClass('active');
		}
	);
	
	// language dropdown
	$('select#lang').change( function() {
		// XX
		if (language = $(this).val()) {
			url = '?lang=' + language;
			window.location = url;
		}
	});
	
	
	// homepage mouseovers
	$("#home_feature_skis img").mouseover(function(){
		rollover_path = $(this).attr("src").replace(".jpg", "_hover.gif");
		$(this).attr("src", rollover_path);
	});
	// homepage mouseouts
	$("#home_feature_skis img").mouseout(function(){
		rollover_path = $(this).attr("src").replace("_hover.gif", ".jpg");
		$(this).attr("src", rollover_path);
	});
	
	// dropdown redirect
	$("select#breadcrumb_select").change( function() {
		if (url = $(this).val()) {
			window.location = url;
		}
	});

	/* Remove newsletter's prompt value upon focus and re-add it if blank  */
	var newsletter_prompt = 'email address';
	$('input#newsletter_email').focus(function() {
		if ($(this).val() == newsletter_prompt) {
			$(this).val('');
		}
	})
	.blur(function() {
		if ($(this).val() == '') {
			$(this).val(newsletter_prompt)
		}
	});
	
	
	// product subnav hover
	$("div.product_subnav_item").hover(
		function(){
			var prodname = $(this).children().children().attr("name");
			var proddims = $(this).children().children().attr("dimensions");
			if (proddims === undefined){
				proddims = ''
			}
			$("span.product_subnav_title").html("<img src='http://assets.k2sports.com/k2skis/ftpk2skis/2010/images/red_arrow.png' alt='' /> <b>" + prodname + "</b> " + proddims);
			if (!$(this).hasClass("active")){
				$(this).css("background","#666");
			}
		},
		function(){
			$("span.product_subnav_title").html("");
			if (!$(this).hasClass("active")){
				$(this).css("background","#4a4a4a");
			}
		}
	);
	
	// series hover bubbles
	$(".prodindex_ski img").hover(
		function(){
			$(this).parent().next(".series_hover").fadeIn("fast");
		},
		function(){	
			$(this).parent().next(".series_hover").hide();
		}
	);

	// non-ski series index rollover state
	$("div.prodindex_nonski a").hover(
		function(){
			$(this).children().css('color','#c00');
		},
		function(){	
			if($(this).parent().hasClass('prodindex_nonski_womens')){
				$(this).children().css('color','#306');
			}else{
				$(this).children().css('color','#000');
			}
		}
	);
	
	// ski tabs
	$("div.ski_tab a").click(function(e){
	  e.preventDefault();
		$("div.ski_tab").removeClass('active');
		$(this).parent("div.ski_tab").addClass('active');
		$("div#ski_tab_content > div").hide();
		$("div#ski_tab_content div#" + $(this).attr('rel')).show();
		return false;
	});
	
	// helmet colorways swap
	$("#helmet_main #helmet_viewer_thumbs img").mouseover(function(){
		if ($(this).attr("class")) {
			new_path = "http://s7d5.scene7.com/is/image/TheGoods/k2skis_0910_" + $(this).attr("class") + "?$k2skis_300_height_alpha_tri_sharp$"
			// alert(new_path);
			$("div#helmet_viewer img").attr("src", new_path);
		}
	});
	
	// pole colorways swap
	$("#pole_main #pole_viewer_thumbs img").mouseover(function(){
		if ($(this).attr("class")) {
			new_path = "http://s7d5.scene7.com/is/image/TheGoods/k2skis_0910_pole-template-main?$k2skis_371_height_alpha_tri_sharp$&$pole-left=is{TheGoods/k2skis_0910_" + $(this).attr("class") + "}&$pole-right=is{TheGoods/k2skis_0910_" + $(this).attr("class") + "}&$pole-left-big=is{TheGoods/k2skis_0910_" + $(this).attr("class") + "}&$pole-right-big=is{TheGoods/k2skis_0910_" + $(this).attr("class") + "}&layer=3&src=$pole-left$&size=200,1108&pos=-60,280&flip=lr&&opac=100"
			// alert(new_path);
			$("div#pole_viewer img").attr("src", new_path);
		}
	});
	
	
	// support answer toggle
	$(".question").click(function(){
		// jQuery(this).siblings(".answer").toggle();
		$(".answer").hide();
		$(this).siblings(".answer").show();
		return false;
	});
	
	// support contact dropdown
	$("select#support_contact").change( function() {
		if (url = $(this).val()) {
			window.location = url;
		}
	});

  // replace submit buttons with submit links
  $('form input[type=submit]').each(function(index, element){
    var $this = $(this);
    var link = $("<a href='#' class='button'>"+ $this.val() +"</a>");
    $this.hide().after(link);
    link.click(function(e){
      e.preventDefault();
      link.parents('form').submit();
    })
  });

});

$(window).bind('load', function(){
	 $('.pr-snippet-read-reviews a').click(function(e){
		$('a[rel=ski_tab_reviews], a[rel=helmet_tab_reviews]').click();
	});
	
})

$(document).ready(function(){
  $('#region_selector_link').click(function(e){
    e.preventDefault();
		s = Shadowbox.open({
		  content: $(this).attr('href'),
		  width: '740px',
		  height: '430px',
		  player: 'xhr',
		  title: 'Select a region:',
		  width: '840',
		  height: '250'
		});
  });
});