$(document).ready(function() {
	$('.select_stockist_country').change(function () {
		$('.clicked').val("country");
		$("#stockist_form").submit();
	});
	$('.select_stockist_city').change(function () {
		$('.clicked').val("city");
		$("#stockist_form").submit();
	});

/*
	var gal_h = $("#gallery > img").height();
	$("#gallery_items").css("min-height", gal_h+"px");
*/

$('.product-list-item a img').hover(function() {
	var img = $(this).attr('rel');
	var org = $(this).attr('src');
	if(img != ''){
		$(this).attr('src', img);
		$(this).attr('rel', org);
	}
}, function() {
	var img = $(this).attr('rel');
	var org = $(this).attr('src');
	if(img != ''){
		$(this).attr('src', img);
		$(this).attr('rel', org);
	}
});

	$(".twitter").tweet({
		join_text: "auto",
		username: "OntourClothing",
		avatar_size: 0,
		count: 1,
		auto_join_text_default: "", 
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "Loading tweets..."
	});

	/* all - cart scroll */
	$(".cart-down").click(function() {
    cartitems = $(this).parent().find(".cart-items");
    wrapper = $(this).parent().find(".cart-items .cart-items-wrapper");
    max = 0 - (wrapper.height() - cartitems.height());
    if(wrapper.position().top > max) {
      wrapper.animate({top: '-=111'}, 200);
    }
    event.stopPropagation();
	});

	$(".cart-up").click(function() {
    wrapper = $(this).parent().find(".cart-items .cart-items-wrapper");
    if(wrapper.position().top < 0) {
      wrapper.animate({top: '+=111'}, 200);
    }
    event.stopPropagation();
	});

  /* all - make mini cart a dialog */
  $.tools.overlay.addEffect("slideDown", function(position, done) {
    this.getOverlay().css(position).slideDown(this.getConf().speed, done);
  },function(done) {
    this.getOverlay().hide(0, done);
  }); 
  $(".showCart").overlay({
    close: false,
    closeOnClick: true,
    closeOnEsc: true,
    fixed: true,
    left: 'inherit',
    top: 0,
    mask: '#000',
    target: '#cart-small'
  });
  $("#cart-small").css("right","0");
  
  $(".continue-shopping").click(function () {
    $(".close").click();
  });
  
  /* all - check mini cart when product has been added */
  if($(".added").length != 0) {
    $(".showCart").click();
  }
  
  /* all - terms and conditions popup */
  $(".term").popupWindow({ 
    height: 500, 
    width: 800, 
    centerBrowser: 1 ,
    scrollbars: 1
  });
  
  /////////////////////////////////////////////////////

	/* homepage - annotate */
  /*
  $('#bg').addAnnotations(function(annotation){
    return $(document.createElement('span')).
      addClass('note').html(annotation.position);
  },[
      {x: 0.4875, y: 0.3246, position: '<div class="info"><img src="upload/products/mj08.jpg" alt="Jacket / shelter">Jacket / shelter</div>'}, 
      {x: 0.48, y: 0.4, position: '<div class="info"><img src="upload/products/mj08.jpg" alt="Jacket / shelter">Jacket / shelter</div>'}
    ]
  );
  $(".note").hover(function() {
    var h = $(this).find(".info").height();
    $(this).find(".info").css("top", -(h+50))
    $(this).find(".info").show();
  }, function() {
    $(this).find(".info").hide();    
  });
  */

  /////////////////////////////////////////////////////
  
  /* register - form check */
  $("#register-form").validate({
    rules: {
      fname: "required",
      lname: "required",
      street: "required",
      zipcode: "required",
      city: "required",
      phone: "required",
      agree: "required",
      password: "required",
      passwordcheck: {
        equalTo: "#formPassword"
      },
      email: {
        required: true,
        email: true
      },
      altstreet: {
        required: "#formdifship:checked"
      },
      altzipcode: {
        required: "#formdifship:checked"
      },
      altcity: {
        required: "#formdifship:checked"
      }
    }
  });

  /////////////////////////////////////////////////////

  /* product list - info popup */
  $(".product-list-item").mouseover(function(){
    $(this).find(".product-list-item-info").show();
  }).mousemove(function(kmouse){
    hoogte = $(this).find(".product-list-item-info").height();
    $(this).find(".product-list-item-info").css({left:kmouse.pageX-30, top:kmouse.pageY-(hoogte+10)});
  }).mouseout(function(){
    $(this).find(".product-list-item-info").hide();
  });
  
  /* product list - click 
  $(".product-list-item").click(function () {
    var url = $(this).find("a").attr("href");
    window.location.href = url;
  });
  */
  
  /* gallery item - click */
  $(".gallery_item").click(function () {
    var url = $(this).find("a").attr("href");
    window.location.href = url;
  });
    
  /////////////////////////////////////////////////////
	
	/* product detail - size check before submitting */
	$("#product-detail form").submit(function() {
    if($(this).find(".size").val() == 0) {
      alert("Please choose your size first.");
      return false;
    }
	});
	
	/* product detail - color select */
  $("select.color").change(function() {
    var url = $(this).find("option:selected").attr("title");
    window.location.href = url;
  });

  /* product detail - no margin on last image */
  $("#product-detail #images #small img").last().css("margin-bottom", "0");
  
  /* product detail - tabs */ 
  $("#tabs li").last().find("a").css("border-right", "0");
  $("#tabs #sizechart table tr").last().find("td").css("border-bottom", "0")
  function tab(selector) {
    $("#tabs .btn_"+selector).click(function () {
      $("#tabs li a").removeClass("current");
      $(this).addClass("current");
      $("#tabs div").hide();
      $("#tabs #"+selector).show();
    });
  }
  tab("delivery");
  tab("sizechart");
  tab("productcare");
  tab("returns");

  /* product detail - image zoom */
  $('#images #small a').click(function () {
    $('img.big-image').attr('src', $(this).attr('href'));
    $('a.big-image').attr('href', $(this).attr('href'));
    return false;
  });
  $('#images img.zoom_icon').click(function () {
  	$(this).parent().find("a").click();
  });
  $('a[rel="zoom"]').colorbox();
  
  /////////////////////////////////////////////////////
	
  /* checkout - auto update */
  $("#formcountry").change(function() {
    $("#type").attr("value", "reload");
		$("form").validate().cancelSubmit = true;
		$("#checkout-btn").click();
  });
  $(".btn_small").click(function() {
    $("#type").attr("value", "reload");
		$("form").validate().cancelSubmit = true;
		$("#checkout-btn").click();
  });
	  
  /* checkout - payment type class */
  $('.payment input').change(function() {
    var id = $(this).attr('id');
    $('.payment label').removeClass('current');
    $('label[for='+id+']').addClass('current');
  });
  
  /* checkout - different shipping addres toggle */
  if(!$("#formdifship").is(':checked')) {
		$("#difShipBlock").hide();
	}
  $("#formdifship").change(function() {
    $("#difShipBlock").slideToggle();
  });
  
  
  /* shipping-info - language select box submit */
  $(".shipping-info-select").change(function() {
    $(this).parent().submit();
  });
  
  /* form validate */
  $("#continue-without-registration").validate({
    rules: {
      fname: "required",
      lname: "required",
      street: "required",
      zipcode: "required",
      phone: "required",
      city: "required",
      email: {
        required: true,
        email: true
      },
      altstreet: {
        required: "#formdifship:checked"
      },
      altzipcode: {
        required: "#formdifship:checked"
      },
      altcity: {
        required: "#formdifship:checked"
      }
    }
  });
  
  $("#new-customer").validate({
    rules: {
      fname: "required",
      lname: "required",
      street: "required",
      zipcode: "required",
      city: "required",
      phone: "required",
      password: "required",
      passwordcheck: {
        equalTo: "#formPassword"
      },
      email: {
        required: true,
        email: true
      },
      altstreet: {
        required: "#formdifship:checked"
      },
      altzipcode: {
        required: "#formdifship:checked"
      },
      altcity: {
        required: "#formdifship:checked"
      }
    }
  });
  
  $("#login").validate({
    rules: {
      email: "required",
      password: "required"
    }
  });
  $("#my-account").validate({
    rules: {
      agree: "required"
    }
  });
});
