/*
  Serener.org
  Copyright (C) 2011 by Systemantics, Bureau for Informatics

  Systemantics GmbH
  Am Lavenstein 3
  52064 Aachen
  GERMANY

  Web:    www.systemantics.net
  Email:  hello@systemantics.net

  Permission granted to use the files associated with this
  website only on your webserver.

  Changes to these files are PROHIBITED due to license restrictions.
*/



jQuery.isPositionFixedSupported = function () {
	  var container = document.body;
	  if (document.createElement &&
	      container && container.appendChild && container.removeChild) {
	      var el = document.createElement("div");
	      if (!el.getBoundingClientRect) {
	          return null;
	      }
	      el.innerHTML = "x";
	      el.style.cssText = "position:fixed;top:100px;";
	      container.appendChild(el);
	      var originalHeight = container.style.height, originalScrollTop = container.scrollTop;
	      container.style.height = "3000px";
	      container.scrollTop = 500;
	      var elementTop = el.getBoundingClientRect().top;
	      container.style.height = originalHeight;
	      var isSupported = elementTop === 100;
	      container.removeChild(el);
	      container.scrollTop = originalScrollTop;
	      return isSupported;
	  }
	  return null;
	};

$(function() {
	$(window).resize(function() {
		var w = 0;
		$("#menu li")
			.css("width", "auto")
			.each(function() {
				w += $(this).width();
			});
		var windowWidth = Math.max(Math.min($(window).width(), parseInt($(".content").css("max-width"))), parseInt($(".content").css("min-width")));
		$("#menu li").each(function() {
			$(this).width($(this).width()+(windowWidth-w-375)/($("#menu li").length-1));
		}).css("visibility", "visible");
		$(".fullwidth").each(function() {
			$(this).css("width", windowWidth-parseInt($(this).css("margin-left"))-parseInt($(this).css("margin-right")));
		});
		$("#video").height($("#video").width()*9/16)
	}).resize();
	
	if ($.isPositionFixedSupported() || ($.browser.msie && parseInt($.browser.version)==7)) {
		$(window).scroll(function() {
			$("#menu").css("left", -$(this).scrollLeft());
		});

		var body = $.browser.webkit ? $("body") : $("html,body");
		var scrollDirection = 1;
		$(window)
			.bind("autoscroll", function() {
				if ($(".home").length==0) {
					return;
				}
				$(window).data("autoscrolling", true);
				var currentScrollTop = $(body).scrollTop();
				if (currentScrollTop==$(window).data("prevScrollTop")) {
					$(body).scrollTop(currentScrollTop+scrollDirection);
					if ($(body).scrollTop()==currentScrollTop) {
						scrollDirection = scrollDirection*-1;
					}
				}
				$(window).data("prevScrollTop", $(body).scrollTop());
				window.setTimeout(arguments.callee, 50);
			})
			.data("prevScrollTop", $(body).scrollTop());
		window.setTimeout(function() {
			if ($(window).data("autoscrolling")) {
				return;
			}
			$(this).trigger("autoscroll");
		}, 10000);
	}
	
	$("#product_details .colors").each(function() {
		var images = $(".image img", this);
		images.hide().eq(0).show();
		var selectors = $(".selectors div", this);
		selectors.click(function() {
			images.hide().eq(selectors.index(this)).show();
		});
	});

	$(".checkbox .box").hover(
		function() {
			$(this).closest(".item").addClass("hover");
		},
		function() {
			$(this).closest(".item").removeClass("hover");
		}
	);
	
	$("#product_like .box, #product_like a").click(function() {
		var item = $(this).closest(".item");
		var url = $("a", item).attr("href");
		$(".label", item).load(url);
		item.addClass("checked");
		return false;
	});

	$("#product_comments .box").click(function() {
		var next = $(this).closest(".item")
			.toggleClass("checked")
			.next().toggle();
		if (next.is(":visible")) {
			$("html,body").animate({scrollTop: next.offset().top-$(window).height()/2}, 750);
		}
	});

	$("#product_comment_form form").ajaxForm({
		beforeSubmit: function(formData, jqForm, options) {
			$("*", jqForm).removeClass("error");
			for (var i in formData) {
				if (!$.trim(formData[i].value)) {
					$("[name="+formData[i].name+"]", jqForm).addClass("error");
				}
			}
			return $(".error", jqForm).length==0;
		},
		target: "#product_comment_form"
	});

	$(".interior .caption").wrapInner("<div></div>");
	$(".interior area").hover(
		function() {
			$(this).closest(".interior").find(".caption").append("<span>"+$(this).attr("alt")+"</span>");
		},
		function() {
			$(this).closest(".interior").find(".caption span").remove();
		}
	);
	
	$("#menu-logo").click(function() {
		location.href = $("#menu li a").eq(1).attr("href");
	});

	$("a[href^=http://]").click(function() {
		this.blur();
		window.open(this.href);
		return false;
	});
});

$(window).load(function() {
	$(this).resize();
	$("#pattern").each(function() {
		var pattern = $(this);
		var message = $("#message");
		if ($.isPositionFixedSupported() || ($.browser.msie && parseInt($.browser.version)==7)) {
			if (message.length) {
				var showDuration = 50;
				pattern
					.delay(1000)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(1500)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(50)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(500)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(2000)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(200)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(50)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(50)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(50)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration*2)
					.queue(function(next) { $(this).show(); next(); })
					.delay(50)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration*2)
					.queue(function(next) { $(this).show(); next(); })
					.delay(300)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration)
					.queue(function(next) { $(this).show(); next(); })
					.delay(150)
					.queue(function(next) { $(this).hide(); next(); })
					.delay(showDuration*24)
					.queue(function(next) { $(this).show(); next(); })
					.delay(1500)
					;
			}
			pattern
				.delay(500)
				.queue(function() {
					pattern.click();
				});
			pattern.click(function() {
				if (pattern.is(":animated")) {
					return;
				}
				message.remove();
				$(window).trigger("autoscroll");
				pattern.stop(true)
				if ($.browser.msie) {
					pattern.remove();
				} else {
					pattern.fadeOut(5000, "easeOutCubic");
				}
			});
		} else {
			message.remove();
			pattern.hide();
		}
	});
});

