// $.urlParam = function(name){
//  var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
//  return results[1] || 0;
// }

$(document).ready(function() { 

  // menu hovers
  if( $("li[id*='menu_']").size() > 0 ) {
    $("li[id*='menu_']").find("a")
      .mouseover(function() {
        var src=$(this).find("img").attr("src").replace(".jpg","_hover.jpg");
        $(this).find("img").attr("src",src);
      })
      .mouseout(function() {
        var src = $(this).find("img").attr("src").replace("_hover","");
        $(this).find("img").attr("src",src);
      });
  }

  // product image switcher
  if( $("#product #product_images").size() > 0 ) {
    var feature_img = $('#product #product_images #primary_image');
    var f_src = feature_img.find("img").attr("src");
    var secondary_imgs = $('#product #product_images .secondary_image');
    secondary_imgs.find("a")
      .click(function() {
		$("#loading_div").show();
		var c_w = $('#primary_image').width();
		var c_h = $('#primary_image').height();
		var i_w = $('#loading_image').width();
		var i_h = $('#loading_image').height();
		$("#loading_div").css({
			"top": c_h/2-i_h/2,   
			"left": c_w/2-i_w/2   
		}); 
        var tmp_m_src = $(this).find("img").attr("src");
        var tmp_src = tmp_m_src.replace("/small_image/","/");
		//alert(tmp_src);
        // swap main image with large version of hovered image
        $("#primary_image_display").attr("src",tmp_src);
		$("#primary_image_display").load(function(){
				$("#loading_div").hide();
		});
		
      })
      
  }

  // slideshow script settings (if .jq_slideshow present)
  if( $('.jq_slideshow').size() > 0 ) {
    $('.jq_slideshow').cycle({ fx: 'fade' });
  }

  if( $('.search_form input.standout').size() > 0) { 
    if( $('.search_form input.standout').val()=='') {
      $('.search_form input.standout').val('SEARCH BY KEYWORD');
    }
  }

// =================
// = SHOPPING CART =
// =================

  // remove item from shopping cart
  if( $('#cart').size() > 0 ) {
    // remove item from cart
    $('.remove').click(function(){
      var id = this.id.replace(/remove_/,'');
      Shopify.removeItem(id);
      // Shopify.removeItem(id, remove_item_callback);
    });
    
    // update cart
    $('.update').click(function() {
      $('#checkout').submit();
    });
  }

});

// ========================
// = SHOPPING CART PLUGIN =
// ========================

Shopify.onCartUpdate = function(cart) {
  // alert('There are now ' + cart.item_count + ' items in the cart.');
  location.reload();
};  


// function remove_item_callback(data){
//   if(data['item_count']==0){
//     console.log(this);
//     Shopify.onCartUpdate(this);
//   }
// //  {"items":[],"requires_shipping":false,"total_price":0,"attributes":null,"item_count":0,"note":null,"total_weight":0}
// }

