var topNavTimer;
jQuery(document).ready(function($) {
	$(".dropDownNav").mouseleave(function() {
		$(this).stop(true, true).slideUp("fast");
	}).mouseenter(function() {
		clearTimeout(topNavTimer);
		topNavTimer = -1;
	});

	$("#nav .parent").mouseenter(function() {
		$("#nav .dropDownNav").stop(true, true).hide();
		var $this = $(this);
		var pos = $this.position();
		$this.next(".dropDownNav").each(function() {
			if (!($.browser.msie && $.browser.version < 7)) {
				$(this).css({
					left: pos.left + 20 + "px",
					top: pos.top + $this.outerHeight() + "px"
				});
			}
			$(this).slideDown("fast");
		});
	}).mouseleave(function() {
		var $this = $(this);
		topNavTimer = setTimeout(function() {
			$this.next(".dropDownNav").stop(true, true).slideUp("fast");
		}, 100);
	});

	$(".video").each(function() {
		var $this = $(this);
		var $img = $("img", $this);
		$this.parent().css({
			"background-image":"url(" + $img.attr("src") + ")"
		});
		$img.attr("src", ctxPath + "/images/cyclingwear/play.png");

		$this.click(function() {
			$("<div/>").attr("id", "player").css({
				width: "100%",
				height: "100%"
			}).overlay({width: "800px", height: "600px"}, function() {
				$(this).flowplayer(ctxPath + "/flash/flowplayer-3.1.5/flowplayer.swf", $this.attr("href"));
			});

			return false;
		});
	});

	$("#article").delegate(".article_image", "click", function() {
		var $this = $(this);
		var img = $(".newspic_right img");

		img.css({width: img.width() || 200 + "px", height: img.height() || 142 + "px"});
		img.attr("src", "");
		img.data("large", $(this).attr("href"));
		var src = $(this).attr("href");

		var i = new Image();
		if (src.match(/[0-9]+\.[0-9]+/)) {
			i.src = src.replace(/[0-9]+\.[0-9]+/, "200.142");
		} else {
			var dot = src.lastIndexOf(".");
			i.src = src.substring(0, dot) + ".200.142" + src.substring(dot);
		}
		$(i).load(function() {
			img.attr("src", this.src).css({width: "auto", height: "auto"});
		});
		if (i.complete) $(i).trigger("load");

		return false;
	});
	
	$("#article").delegate(".newspic_right img", "click", function() {
		var $this = $(this);
		var smSrc = $this.attr("src");
		var lgSrc = $this.data("large");

		var img = $("<img/>", {src: lgSrc });
		$this.css({width: $this.width() || 200 + "px", height: $this.height() || 200 + "px"})
		.attr("src", "");
		
		img.one("load", function() {
			img.overlay({}, function() {
				$this.attr("src", smSrc).css({width: "auto", height: "auto"});
			});
		});

		if (img[0].complete) img.trigger("load");
	});

	var loadingImage = null;
	$("#news").delegate(".side_nav a", "click", function() {
		var url = $(this).attr("href").replace(/\.html/, ".content.html");
		
		if (loadingImage == null) {
			loadingImage = $("#article .text_content").css("background-image");
		}

		$("#article .text_content").empty().css("background-image", loadingImage);
		$("#article").load(url + " #article .text_content", function() {
			$("#news .text_content").css("background-image", "none");
			$(".article_image:first").click();
		});

		return false;
	});

	$("#skus .product img").each(function() {
		// preload all the large images
		var i = new Image();
		i.src = this.src.replace(/[0-9]+\.[0-9]+/, "0.0.363.310");
	});

	var lgspeed = 200;
	$("#skus").delegate(".product img", "mouseenter", function() {
		var _this = this;
		var lg = $("#large_image");
		lg.css({
			width: lg.width(),
			height: lg.height()
		});

		var bg = lg.data("bg");
		if (!bg) {
			lg.data("bg", lg.css("background-image"));
		}

		lg.stop(true).animate({
			opacity: 0}, lgspeed, function() {
			lg.empty();
			lg.css({
				"background-image": "url(" + _this.src.replace(/[0-9]+\.[0-9]+/, "0.0.363.310") + ")"
				//"background-position": "center center"
			});
			lg.animate({opacity:1}, lgspeed);
		});
		
	});
	
	
	$("#skus").mouseleave(function() {
		var lg = $("#large_image");
		lg.animate({opacity: 0}, lgspeed, function() {
			lg.css({
				"background-image": lg.data("bg")
				//"background-position": "left center"
			});
			lg.animate({opacity: 1}, lgspeed);
		});
	});

	$("#skus img").click(function() {
		var $this = $(this);
		var smSrc = this.src;
		var lgSrc = this.src.replace(/\.[0-9]+\.[0-9]+/, "");

		var img = $("<img/>", {src: lgSrc });
		
		img.one("load", function() {
			img.overlay({className: "product_image_overlay"});
		});

		if (img[0].complete) img.trigger("load");
	});

	
	$(".article_image:first").click();
	$("#news .side_nav a:first").click();
});

jQuery(window).load(function () {
   //evenly space products
  var $ = jQuery;
  var line = $("#lineup").width();
  var count = $("#lineup .product").length;
  var EASING = 'easeOutCirc';
  var calcPadding = line/count;
  
  $(".series").each(function() {
	calcPadding = 113;
  });

  var curLeft = calcPadding / 2;

  $("#lineup .product").each(function () {
	  var $this = $(this);
	  var $img = $("img", this);
	  var w = $img.width();
	  var h = $img.height();

      var metrics = {
		lg: {
			width: w,
			height: h,
			left: curLeft - (w * .5),
			top: 60 - (h * .5)
		},
		sm: {
			width: 83,
			height: 110,
			left: curLeft - (41.5),
			top: 5
		}
	  };

      $this.data("metrics", metrics)
	  .css({
		opacity: .75,
		position: "absolute",
		top: metrics.sm.top,
		left: metrics.sm.left,
		width: metrics.sm.width,
		"z-index": 2
	  });

	  $("img", this).css({width: metrics.sm.width, height: metrics.sm.height});
	        
      $(this).mouseenter(function (e) {
          //hide small
          var d = $(this).data('metrics');

          $("img", this).stop(true).animate({
			height: d.lg.height,
			width: d.lg.width}, 100, EASING);

          $(this).css({"z-index": 9}).stop(true).animate({
			width: d.lg.width,
			left: d.lg.left,
			top: d.lg.top,
			opacity: 1}, 100, EASING);
	  });

	  $(this).mouseleave(function (e) {
		var d = $(this).data('metrics');
		
		$("img", this).animate({
			height: d.sm.height,
			width: d.sm.width}, 100, EASING);
		
		$(this).css({"z-index": 2}).animate({
			width: d.sm.width,
			left: d.sm.left,
			top: d.sm.top,
			opacity: .75}, 100, EASING);
	  });

      curLeft += calcPadding;
  });
  
  $(".series #lineup").css({
	width: $("#lineup .product").length * calcPadding + "px",
	"margin-left": 0,
	"margin-right": 0
  });

  $("#logos img").click(function() {
	var $this = $(this);
	var image = new Image();
	image.src = (this.oSrc || this.src).replace(/\.[0-9]+\.[0-9]+/, '.0.0.360.0');

	var $image = $this.clone().attr("src", image.src);

	$image.modal({width: 400});
  });
});
