var rotor = function(rotor, rotorDirection, rotorDuration, rotorInterval, rotorTouchwipe) {
	var rotor = $(rotor);

	var
		rotorWidth = rotor.width(),
		rotorArray = rotor.find(".rotorArray"),
		rotorList = rotor.find("ul"),
		rotorLength = rotor.find("li").length,
		rotorArrow = rotor.find(".rotorArrow"),
		rotorArrow_Fade = rotor.find(".rotorArrow").css("display") == "none" ? true : false,
		rotorLinear = rotor.find(".rotorLinear"),
		rotorDirection = rotorDirection ? rotorDirection : "r",
		rotorArray_Direction = false,
		rotorDuration = rotorDuration ? rotorDuration : 250,
		rotorCurrent = 0,
		rotorAnimation = false,
		rotorTimer,
		rotorInterval = rotorInterval ? rotorInterval : 3000,
		rotorTouchwipe = rotorTouchwipe ? true : false,
		rotorReady = false;
		
	if (rotorLength > 1) {
	
		rotor.addClass("rotorWaiting");
		rotorList.css({"width" : rotorWidth * rotorLength + "px"});
		if (rotorArrow_Fade) {
			rotorArrow.css({"opacity" : "0"});
		}
		
		rotor.find("li").each(function(index, element) {
			rotorLinear.append($('<em />').attr("index", index));
		});
		
		var setRotor_Linear = function(count) {
			if (rotorReady) {
				if (rotorCurrent > rotorLength - 1) {
					rotorCurrent = 0;
				} else if (rotorCurrent < 0) {
					rotorCurrent = rotorLength - 1;
				}

				rotorLinear.find("em").each(function(index, element) {
					$(element).removeClass();
					$(element).unbind();
					if (index == rotorCurrent) {
						$(element).addClass("current");
					} else {
						$(element).bind({
							mouseenter: function() {$(element).addClass("over");},
							mouseleave: function() {$(element).removeClass("over");},
							click: function() {
								if (!rotorAnimation) {
									setRotor_Animation(rotorCurrent > (1 * $(element).attr("index")) ? "l" : "r", Math.abs((1 * $(element).attr("index")) - rotorCurrent));
									rotorCurrent = $(element).attr("index");
								}
							}
						});
					}
				});
			}
		};
		
		var setRotor_Animation = function(direction, count) {
			if (rotorReady) {
				if (typeof(count) == "undefined") {
					count = 1;
				}
				if (!rotorAnimation && (direction == "l" || direction == "r")) {
					rotorAnimation = true;
					rotorDirection = direction;
					if (direction == "l") {
						rotorCurrent = (1 * rotorCurrent) - (1 * count);
						for (var i = 0; i < count; i++) {
							rotor.find("li").last().prependTo(rotorList);
						}
						rotorList.css({"margin-left" : "-" + (rotorWidth * count) + "px"});
						rotorList.animate({
							"margin-left" : "0"
						}, rotorDuration, function() {
							rotorAnimation = false;
						});
					} else {
						rotorCurrent = (1 * rotorCurrent) + (1 * count);
						rotorList.animate({
							"margin-left" : "-" + (rotorWidth * count) + "px"
						}, rotorDuration, function() {
							for (var i = 0; i < count; i++) {
								rotor.find("li").first().appendTo(rotorList);
							}
							rotorList.css({"margin-left" : "0"});
							rotorAnimation = false;
						});
					}
					setRotor_Linear(count);
				}
			}
		};
		
		var setRotor_Arrow = function(action) {
			if (rotorReady && rotorArrow_Fade) {
				rotorArrow.stop(true, false).animate({"opacity" : action ? "1" : "0"}, 150);
			}
		}
		
		rotorArray.add(rotorArrow).bind({
			mouseenter: function() {setRotor_Arrow(true);},
			mouseleave: function() {setRotor_Arrow(false);}
		});
		
		rotorArrow.each(function(index, element) {
			$(element).bind({
				mouseenter: function() {$(element).addClass("over");},
				mouseleave: function() {$(element).removeClass("over");}
			});
		});
		
		rotor.find(".rotorArrow_l").attr("title", "Previous").bind({
			click: function() {
				rotorArray_Direction = "l";
				var rotorQueue_Local = window.setInterval(function() {
					if (!rotorAnimation) {
						if (rotorArray_Direction == "l") {
							setRotor_Animation("l");
						}
						window.clearInterval(rotorQueue_Local);
					}
				}, 10);
			}
		});
		
		rotor.find(".rotorArrow_r").attr("title", "Next").bind({
			click: function() {
				rotorArray_Direction = "r";
				var rotorQueue_Local = window.setInterval(function() {
					if (!rotorAnimation) {
						if (rotorArray_Direction == "r") {
							setRotor_Animation("r");
						}
						window.clearInterval(rotorQueue_Local);
					}
				}, 10);
			}
		});
		
		if (rotorTouchwipe) {
			rotorArray.touchwipe({
				wipeLeft: function() {rotorStop(); setRotor_Animation("r");},
				wipeRight: function() {rotorStop(); setRotor_Animation("l");},
				min_move_x: 25,
				preventDefaultEvents: true
			});
		}
		
		var rotorStop = function() {
			window.clearInterval(rotorTimer);
		};
		
		var rotorAuto = function() {
			rotorStop();
			rotorTimer = window.setInterval(function() {setRotor_Animation(rotorDirection);}, rotorInterval);
		};
		
		rotor.bind({
			mouseenter: function() {rotorStop();}, mouseover: function() {rotorStop();}, mousemove: function() {rotorStop();},
			mouseleave: function() {rotorAuto();}
		});
		
		$(window).load(function() {
			rotorReady = true;
			rotor.removeClass("rotorWaiting");
			if (rotorArrow_Fade) {rotorArrow.css({"display" : "block"});}
			setRotor_Linear();
			rotorAuto();
		});
		
		return {stop: rotorStop, play: rotorAuto};
	
	}
};

var newsPrev = newsCount = false;

function setNews(id, object) {
	object.blur();
	if (newsCount > 1 && id !== newsPrev) {
		for (var i = 0; i < newsCount; i++) {
			getElement("newsHyperlink" + i).className = "";
			if (i != id) {
				$("#newsItem" + i).fadeTo(250, 0, function() {});
			}
		}
		object.className = "current";
		$("#newsItem" + id).css("visibility", "visible");
		$("#newsItem" + id).fadeTo(250, 1, function() {
			for (var i = 0; i < newsCount; i++) {
				if (i != id) {
					$("#newsItem" + i).css("visibility", "hidden");
				}
			}
		});
		newsPrev = id;
	}
	return false;
}

$(document).ready(function() {

	if ($("#cover").length && $(".moduleSearch").length) {
		$(".moduleSearch").css({"height" : $("#cover").height() + "px"});
	}
	
	var cover = rotor("#cover", null, null, null, true);
	
	//

	if ($("#rootMenu")) {
		$("#rootMenu").find(".menuItemList").each(function(index, element) {

			$(element).find("a").css("whiteSpace", "nowrap");
			if ($(element).width() < $(element).parent().find("p").width()) {
				$(element).css("width", $(element).parent().find("p").width());
			} else if ($(element).width() > 240) {
				$(element).css("width", "240px");
			} else {
				$(element).css("width", ($(element).width() + 1) + "px");//hyphenation fix
			}
			$(element).find("a").css("whiteSpace", "nowrap");

			$(element).attr("height", $(element).height());
			$(element).css({
				"clip" : "rect(" + $(element).height() + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)",
				"marginTop" : -$(element).attr("height"),
				"visibility" : "visible"
			});
		});

		var timer_rootMenu;

		$("#rootMenu").find("p").bind({
			mouseenter: function() {
				var thisElement = this;
				if ($(thisElement).parent().find(".menuItemList").length) {
					timer_rootMenu = window.setTimeout(function() {
	
						$(thisElement).parent().find(".menuItemList").each(function(index, element) {
							//width fix if changed & hyphenation fix
							$(element).css("width", "auto");
							$(element).find("a").css("whiteSpace", "nowrap");
							if ($(element).width() < $(element).parent().find("p").width()) {
								$(element).css("width", $(element).parent().find("p").width());
							} else if ($(element).width() > 240) {
								$(element).css("width", "240px");
							} else {
								$(element).css("width", ($(element).width() + 1) + "px");//hyphenation fix
							}
							$(element).find("a").css("whiteSpace", "normal");
	
							//height fix if changed
							$(element).css("height", "auto");
							if ($(element).height() != $(element).attr("height")) {
								$(element).css("height", $(element).height());
								$(element).attr("height", $(element).height());
								$(element).css({
									"marginTop" : -$(element).attr("height")
								});
							}
							$(element).clearQueue().stop().animate({
								"marginTop" : "0"
							}, {
								duration: 600,
								step: function(now, fx) {
									$(element).css({
										"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)"
									});
								}
							});
						});
					}, 100);
				}
			},
			mouseleave: function() {
				var thisElement = this;
				if ($(thisElement).parent().find(".menuItemList").length) {
					window.clearTimeout(timer_rootMenu);
					$(this).parent().find(".menuItemList").each(function(index, element) {
						$(element).stop().delay(100).animate({
							"marginTop" : -$(element).attr("height")
						}, {
							duration: 400,
							step: function(now, fx) {
								$(element).css({
									"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)",
								});
							}
						});
					});
				}
			}
		});

		$("#rootMenu").find(".menuItemList").bind({
			mouseenter: function() {
				$(this).each(function(index, element) {
					$(element).parent().find("p a").addClass("over");
					
					$(element).clearQueue().stop().animate({
						"marginTop" : "0"
					}, {
						duration: 600,
						step: function(now, fx) {
							$(element).css({
								"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)"
							});
						}
					});

				});
			},
			mouseleave: function() {
				$(this).each(function(index, element) {
					$(this).parent().find("p a").removeClass("over");

					$(element).stop().delay(100).animate({
						"marginTop" : -$(element).attr("height")
					}, {
						duration: 400,
						step: function(now, fx) {
							$(element).css({
								"clip" : "rect(" + (-now) + "px, " + $(element).width() + "px, " + $(element).height() + "px, 0px)",
							});
						}
					});

				});
			}
		});

	}
	
	//
	
	if ($("#moduleCarousel")) {
		var carouselVisible = 5;
		
		if ($("#carouselEnvelope li").length < carouselVisible) {
			$("#moduleCarousel").css("display", "none");
		} else {
			
			$("#carouselEnvelope a").css("opacity", "0.08");
			$(".carouselArrow").css("opacity", "0");
			
			$("#carouselEnvelope img").each(function(index, element) {
				$(element).css("display", "none");
				$(element).parent().css("backgroundImage", "url('" + $(element).attr("src") + "')");
				$(element).parent().attr("title", $(element).attr("alt"));
			});
				
			$(window).load(function() {
			
				$("#carouselEnvelope a").live({
					mouseover: function() {
						$(this).stop();
						grayscale.reset($(this));
						$(this).fadeTo(100, 1);
					},
					mouseout: function() {
						$(this).stop();
						grayscale($(this));
						$(this).fadeTo(100, 0.4);
					}
				});
				
				if ($("#carouselEnvelope li").length > carouselVisible) {
					$("#carouselEnvelope").jCarousel({
						btnPrev: "#carouselArrow_l",
						btnNext: "#carouselArrow_r",
						auto: 2000,
						visible: 5,
						speed: 500,
						easing: "jswing"
				    });
				}
					
				$("#carouselEnvelope a").each(function(index, element) {
					grayscale($(element));
					$(element).css("opacity", "0.08");/*IE7-8 fix*/
					$(element).fadeTo(500, 0.4);
				});
					
				if ($("#carouselEnvelope li").length > carouselVisible) {
					$(".carouselArrow").delay(450).fadeTo(250, 1, function() {
						$("#carouselLid").css("display", "none");
					});
				} else {
					$("#carouselLid").delay(550).css("display", "none");
				}
				
				$("#carouselEnvelope ul").first().touchwipe({
					wipeLeft: function() {$("#carouselArrow_r").click();},
					wipeRight: function() {$("#carouselArrow_l").click();},
				     min_move_x: 25,
				     preventDefaultEvents: true
				});
				
			});
		}
	}

	//

	if ($(".moduleCatalog")) {
		$(".moduleCatalog .childrenList").each(function(index, element) {
			if (!$(element).parent().hasClass("current")) {
				$(element).attr("height", $(element).height());
				$(element).css({
					"height" : "0",
					"overflow" : "hidden"
				});
			}
		});
		
		$(".moduleCatalog").each(function(index, element) {
			if ($(element).hasClass("minimized")) {
				
				$(element).find("> ul").css("visibility", "hidden").fadeToggle(10, function() {$(this).css("visibility", "visible");});
				
				$(element).bind({
					click: function() {
						if ($(element).find("h2").hasClass("open")) {
							$(element).find("h2").removeClass("open");
						} else {
							$(element).find("h2").addClass("open");
						}
						$(element).find("> ul").fadeToggle(250);
					}
				});
			}
		});
		
		$(".moduleCatalog > ul > li").bind({
			mouseenter: function() {
				if (!$(this).hasClass("current")) {
					$(this).addClass("over");
		
					$(this).find(".childrenList").each(function(index, element) {
						$(element).stop().animate({
							"height" : $(element).attr("height")
						}, 750, function() {
							$(element).css("height", "auto");
							if ($(element).height() != $(element).attr("height")) {
								$(element).attr("height", $(element).height());
								$(element).css("height", $(element).height());
							}
						});
					});
				}
			},
			mouseleave: function() {
				if (!$(this).hasClass("current")) {
					$(this).removeClass("over");
		
					$(this).find(".childrenList").each(function(index, element) {
						$(element).stop().animate({
							"height" : "0"
						}, 750);
					});
				}
			}
		});
	}
	
	//

	if ($("#productsCarousel0")) {
		if ($("#productsCarousel0 li").length > 1) {
			$("#arrowArray0").css({
				"opacity" : "0",
				"visibility" : "hidden"
			});
			
			var productsCarousel0_height = 0;
			
			$("#productsCarousel0 li").each(function(index, element) {
				if ($(element).height() > productsCarousel0_height) {
					productsCarousel0_height = $(element).height();
				}
			});
			
			$("#productsCarousel0 ul").css({
				"height" : productsCarousel0_height + "px",
				"overflow" : "hidden"
			});
			
			$("#productsCarousel0 li").each(function(index, element) {
				$(element).css({
					"height" : productsCarousel0_height + "px",
					"overflow" : "hidden"
				});
			});
			
			/*$("#productsCarousel0 .productArray").css({
				"height" : productsCarousel0_height + "px"
			});*/
			
			$(window).load(function() {
				$("#arrowArray0").css({
					"visibility" : "visible"
				});
				$("#arrowArray0").fadeTo(250, 1, function() {
				
					$("#productsCarousel0").jCarousel({
						btnPrev: "#arrowItem_l0",
						btnNext: "#arrowItem_r0",
						auto: 6500,
						visible: 1,
						speed: 500,
						easing: "jswing",
						beforeStart: function() {
							$("#productsCarousel0").fadeTo(200, 0.25, function() {
								$("#productsCarousel0").fadeTo(200, 1);
							})
						}
					});
					
				});
			});
		} else {
			$("#arrowItem_l0, #arrowItem_r0").css("visibility", "hidden");
		}
	}
	
	if ($("#productsCarousel1")) {
		if ($("#productsCarousel1 li").length > 1) {
			$("#arrowArray1").css({
				"opacity" : "0",
				"visibility" : "hidden"
			});
			
			var productsCarousel1_height = 0;
			
			$("#productsCarousel1 li").each(function(index, element) {
				if ($(element).height() > productsCarousel1_height) {
					productsCarousel1_height = $(element).height();
				}
			});
			
			$("#productsCarousel1 ul").css({
				"height" : productsCarousel1_height + "px",
				"overflow" : "hidden"
			});
			
			$("#productsCarousel1 li").each(function(index, element) {
				$(element).css({
					"height" : productsCarousel1_height + "px",
					"overflow" : "hidden"
				});
			});
			
			/*$("#productsCarousel1 .productArray").css({
				"height" : productsCarousel1_height + "px"
			});*/
			
			$(window).load(function() {
				$("#arrowArray1").css({
					"visibility" : "visible"
				});
				$("#arrowArray1").fadeTo(250, 1, function() {
					
					$("#productsCarousel1").jCarousel({
						btnPrev: "#arrowItem_l1",
						btnNext: "#arrowItem_r1",
						auto: 7500,
						visible: 1,
						speed: 500,
						easing: "jswing",
						beforeStart: function() {
							$("#productsCarousel1").fadeTo(200, 0.25, function() {
								$("#productsCarousel1").fadeTo(200, 1);
							})
						}
					});
					
				});
			});
		} else {
			$("#arrowItem_l1, #arrowItem_r1").css("visibility", "hidden");
		}
	}
	
	//
	
	if ($("#newsItem0")) {
		newsCount = $(".newsItem").length;
		
		for (var i = 0; i < newsCount; i++) {
			$("#newsItem" + i).css("opacity", "0");
			$("#newsItem" + i).css("visibility", "hidden");
			
			if (i == 0) {
				$("#newsItem" + i).css("opacity", "1");
				$("#newsItem" + i).css("visibility", "visible");
			} else {
				$("#newsItem" + i).css("marginTop", "-" + $("#newsItem0").height() + "px");
			}
		}
	}
	
	//
	
	if ($(".categoryArray")) {
		$(".categoryImage").find("a").each(function(index, element) {
			$(element).css("backgroundImage", "url('" + $(element).find("img").attr("src") + "')");
			$(element).find("img").css("visibility", "hidden");
		});
	}
	
	//
	
	if ($("#productOptions")) {
		$("#productOptions").find("select").each(function(index, element) {
			if ($(element).width() > 210) {//216 real
				$(element).css("width", "210px");
			}
		});
	}
	
	//

});
