function dbug(msg){
	try {
		console.debug(msg);
	} catch(e){ /* ignore */ }
}



(function($) {

	/* center the content-bg-image vertically */
	$.fn.yCenterImage = function(){
		return this.each(function(){
			var that = this;
			var wrapper = $("#wrapper");
			$(that).height(wrapper.height()-100);

			$(window).resize(function(){
				$(that).height(wrapper.height()-100);
			});
		});
	};

	/* product rotation */
	$.fn.yCarrousel = function(opts){

		var options = $.extend(options, opts);

		return this.each(function(){

			var that = this;
			var index = 0;
			var imageCount = options.src.length;

			// generate and add arrows
			var leftArrow = $("#carrousel_left div", this);
			var rightArrow = $("#carrousel_right div", this);


			// get index
			var getIndex = function(i){
				i < 0 ? i = imageCount-1 : i = i;
				i === imageCount ? i = 0 : i = i;
				return i;
			};


			// preload
			var preload = function(i){
				var imgPrev = new Image();
				imgPrev.src = options.src[getIndex(i-1)];

				var imgNext = new Image();
				imgNext.src = options.src[getIndex(i+1)];
			};


			// rotate
			var rotate = function(i){
				var i = getIndex(i);

				$(that).css({ "background-image": "url(" + options.src[i]  +")" });
				preload(i);

				index = i;
			};


			/* show/hide arrows */
			$("#carrousel_left, #carrousel_right", this).hover(
					function(){
						$.support.opacity ? $("div", this).fadeIn() : $("div", this).show();
					},
					function(){
						$.support.opacity ? $("div", this).fadeOut() : $("div", this).hide();
					});


			$(leftArrow).click(function(){
				rotate(index-1);
				return false;
			});
			$(rightArrow).click(function(){
				rotate(index+1);
				return false;
			});

			// init preloading
			preload(index);
		});
	};

	$.fn.yContact = function(){
		return this.each(function(){
			var that = this;

			// check interest for other...
			$("#interest_other", this).click(
					function(e){
						if($(this).is(":checked")){
							$("#other_interest_input", that).fadeIn();
						}
						else {
							$("#other_interest_input", that).fadeOut();
						}
					});

			// check profession for other...
			$("#i_am select", this).change(
					function(e){
						if($("option:selected", e.currentTarget).hasClass("other")){
							$("#i_am_other_profession", that).fadeIn();
						}
						else {
							$("#i_am_other_profession", that).fadeOut();
						}
					});

			// on submit
			$(this).submit(function(){
				var that = this;
				var send = true;

				// reset error styles and all messages
				$("#i_am .text", this).removeClass("error");
				$("#error_msg, #success_msg", this).hide();

				if($("#i_am_0", this).val() === ""){
					$("#i_am_0", this).addClass("error");
					send = false;
				}
				if($("#i_am_1", this).val() === ""){
					$("#i_am_1", this).addClass("error");
					send = false;
				}
				if(! /^[A-Za-z0-9](([_\.\-]?[a-zA-Z0-9]+)*)@([A-Za-z0-9]+)(([\.\-]?[a-zA-Z0-9]+)*)\.([A-Za-z]{2,})$/.test($("#i_am_3", this).val())){
					$("#i_am_3", this).addClass("error");
					send = false;
				}

				if(send){
					var action = $(this).attr("action");
					var serializedData = $(this).serialize();

					dbug(action);
					dbug(serializedData);

					$(this).post(
							action,
							serializedData,
							function(data){
								$("#success_msg", that).show();
							});
				}
				else {
					$("#error_msg", this).show();
				}

				return false;
			});
		});
	};


	/* email / send to friend */
	$.fn.yEmail = function(){
		return this.each(function(){
			var hasBeenReset = {};

			/* clear standard text after first focus */
			$("input, textarea", this).click(function(e){
				if(hasBeenReset[e.currentTarget.id] === undefined){
					$(e.currentTarget).val("");
					hasBeenReset[e.currentTarget.id] = true;
				}
			});

			/* remove button focus */
			$(".submit", this).click(function(){
				$(this).blur();
			});

		});
	};


	/* model view */
	$.fn.yModelSwitch = function(){
		return this.each(function(){

			// calculate width and center
			var topContainer = $(this);
			var container = $("#switch", this);
			var headline = $("h2",this);
			var list = $("ul", this);
			var listItems = $("li", this);
			var listItemsWidth = parseInt(listItems.css("width")) + 11;

			var topContainerWidth = topContainer.width();
			var inactiveWidth = $(container).width();
			var activeWidth = inactiveWidth + listItems.size() * listItemsWidth;
			var inactiveMargin = (topContainerWidth - inactiveWidth) / 2;
			var activeMargin = (topContainerWidth - activeWidth) / 2;

			var setActiveMargin = function(){
				$(container).css(
						{
							"margin-left": activeMargin,
							"margin-right": activeMargin
						});
			};

			var setInactiveMargin = function(){
				$(container).css(
						{
							"margin-left": inactiveMargin,
							"margin-right": inactiveMargin
						});
			};

			setInactiveMargin();

			// hover for the switch
			$(container).hover(
					function(){
						setActiveMargin();
						$(container).width(activeWidth);
						$(list).show();

					},
					function(){
						setInactiveMargin();
						$(list).hide();
						$(container).width(inactiveWidth);
					});

			// hover for the model views
			$("a", listItems).hover(
					function(){
						$("span", this).show();
					},
					function(){
						$("span", this).hide();
					});
		});
	};

	/* hover main navigation */
	$.fn.yNavigation = function(){
		return this.each(function(){

			var isIE = !$.support.opacity;


			// first level hover
			$("#lvl_1 > li", this).each(function(i, lvl1Li){

				// calculate top and bottom bg_image width for lvl2 // set left offset for level 3
				var maxWidth = 0;
				$(".lvl_2 > ul > li", lvl1Li).each(function(){
					maxWidth < $(this).width() ? maxWidth = $(this).width() : null;

					$(".lvl_3", this).css({
						"left": maxWidth-33
					});

				});

				$(".lvl_2 > ul", lvl1Li).css({
					"width": maxWidth-22
				});

				$(".lvl_2 > .top, .lvl_2 > .bottom", lvl1Li).css({
					"width": maxWidth-62
				});


				// level 1 anchor hover
				$("> a", lvl1Li).hover(
						function(){
							$(this).addClass("hover");
							isIE ? $(".lvl_2", lvl1Li).show() : $(".lvl_2", lvl1Li).stop(false, true).fadeIn();
						},
						function(){
							$(this).removeClass("hover");
							//isIE ? $(".lvl_2", lvl1Li).hide() : $(".lvl_2", lvl1Li).fadeOut();
							$(".lvl_2", lvl1Li).stop(false, true).hide();
						});



				$(".lvl_2", lvl1Li).each(function(j, lvl2){

					// if the mouse is over a level 2 navigation element set the level 1 state correctly
					$(lvl2).hover(
							function(){
								$("> a", lvl1Li).addClass("hover");
								$(this).stop(false, true).show();

							},
							function(){
								$(this).stop(false, true).hide();
								$("> a", lvl1Li).removeClass("hover");
							});



					// level 2 hover
					$("> ul > li", lvl2).hover(
							function(){
								$("> a", this).addClass("hover");

								if($(".lvl_3", this).size() > 0){

									$(".lvl_3", lvl2).css({
										"height": 0,
										"width": 0
									});

									$("> ul, > ul > li > a", lvl2).width($("> ul", lvl2).width()-9);
									$("> ul > li > a > span.more", lvl2).css("right", "21px");

									$("> .top_right", lvl2).addClass("top_right_hover").removeClass("top_right");
									$("> .bottom_right", lvl2).addClass("bottom_right_hover").removeClass("bottom_right");

									$(".lvl_3 > ul", this).height($(".lvl_3", this).data("height"));

									// stop all other animations
									$(".lvl_3", lvl2).filter(":animated").stop(false, true);

									// set the height of the level 2 navi to the height of the lvl3 navi if smaller
									if($(".lvl_3", this).data("lvl2Smaller")){
										$("ul", lvl2).filter(":animated").stop(true, true);

										$("> ul", lvl2).animate({
											"height": $(".lvl_3", this).data("height")
										}, 400);

									}

									// resize the level 3 to full size
									$(".lvl_3", this)
										.animate({
											"height": $(".lvl_3", this).data("height"),
											"width": $(".lvl_3", this).data("width")
										});

								}

							},
							function(){
								$("> a", this).removeClass("hover");

								if($(".lvl_3", this).size() > 0){

									$("> ul, > ul > li > a", lvl2).width($("> ul", lvl2).width()+9);
									$("> ul > li > a > span.more", lvl2).css("right", "30px");

									// stop all other animations
									$(".lvl_3", lvl2).filter(":animated").stop(false, true);

									// set the height of level 2 back to normal
									$("> ul", lvl2).animate({
										"height": $("> ul > li", lvl2).size() * 20
									}, "fast");


									// resize level 3 to 0
									$(".lvl_3", this).animate({
										"height": 0,
										"width": 0
									}, "fast");

									$("> .top_right_hover", lvl2).addClass("top_right").removeClass("top_right_hover");
									$("> .bottom_right_hover", lvl2).addClass("bottom_right").removeClass("bottom_right_hover");
								}
							});

					// calculate level 3 dimensions and save them to the elements
					$(".lvl_3", lvl2).each(function(k, lvl3){

						// calculate width
						var maxWidthLvl3 = 0;

						$("li", lvl3).each(function(){
							maxWidthLvl3 < $(this).width() ? maxWidthLvl3 = $(this).width() : null;
						});

						$(".lvl_3 > .top, .lvl_3 > .bottom", lvl2).css({
							"width": maxWidthLvl3-40
						});


						// calculate height
						var maxHeightLvl3 = 0;
						var lvl2Smaller = false;

						if($(".lvl_2 > ul > li", lvl1Li).size() > $("> ul > li", lvl3).size()) {
							maxHeightLvl3 = $(".lvl_2 > ul > li", lvl1Li).size() * 20;
						}
						else {
							maxHeightLvl3 = $("> ul > li", lvl3).size() * 20
							lvl2Smaller = true;
						}

						$(lvl3).data({
							"height": maxHeightLvl3,
							"lvl2Smaller": lvl2Smaller,
							"width": maxWidthLvl3
						});

					});


				});

			});


			// set visibility to visible and display to none again (visibility hidden was used to get width and height)
			$(".lvl_3", this).css({
				"display": "none",
				"visibility": "visible"
			});

			$(".lvl_2", this).css({
				"display": "none",
				"visibility": "visible"
			});


		});
	};


	/* position left and right navigation */
	$.fn.yNavigationPos = function(){
		return this.each(function(){
			var leftNavigation = $("#site_left > *", this).first();
			var rightNavigation = $("#site_right h2", this).next();
			var firstElementContent = $("#site_content h1", this);

			var topMargin = $(firstElementContent).position().top + $(firstElementContent).height() + parseInt($(firstElementContent).css("margin-bottom"));
			if(!firstElementContent.next().is("img")){ topMargin += 4; }
			if(topMargin > 174) { topMargin = 174; }

			$(leftNavigation).css("margin-top", topMargin);
			$(rightNavigation).css("margin-top", topMargin);
		});
	};


	/* search textfield clearing */
	$.fn.ySearch = function(){
		return this.each(function(){
			var isReset = false;

			$(".text", this).focus(function(){
				if(!isReset){
					$(this).val("");
					isReset = true;
				}
			});
		});
	};


	/* send to friend layer */
	$.fn.ySendToFriend = function(){
		return this.each(function(){
			var emailForm = $("#email");
			var isHidden = true;

			$(this).click(
					function(){ //änderung mjo, damit nach versenden formular wieder hergestellt
						if (isHidden){
						  $(emailForm).show();
						  $("#sendafriend_answer").hide();
						  $("#sendafriend_content").show();					  
						} 
						else{
						  $(emailForm).hide();
						}
						isHidden = !isHidden;
						return false;
					});

			$("#close", emailForm).click(
					function(){
						$(emailForm).hide();
						isHidden = true;
						return false;
					});
		});
	};


	/* tab navigation */
	$.fn.yTabNavigation =function(){
		return this.each(function(){
			var that = this;

			/*
			$(that).hover(
					function(){
						$("ul#tabs > li > a", this).each(function(){
							if(!$(this).hasClass("active")){
								$(this).animate(
										{
											"left": "-91px"
										}
								);
							}
						});
					},
					function(){
						$("ul#tabs > li > a", this).each(function(){
							if(!$(this).hasClass("active")){
								$(this).animate(
										{
											"left": "-106px"
										}
								);
							}
						});
					}
			);
			*/

			$("ul#tabs > li", that).hover(
					function(){
						$("> a", this).each(function(){
							if(!$(this).hasClass("active")){
								$(this).animate(
										{
											"left": "0px"
										}
								);
							}
						});

					},
					function(){
						$("> a", this).each(function(){
							if(!$(this).hasClass("active")){
								$(this).animate(
										{
											"left": "-91px"
										}
								);
							}
						});
					}
			);
		});
	};

})(jQuery);

/* init */
$(document).ready(function(){
	$("#navigation").yNavigation();
	$("#tab_nav").yTabNavigation();
	$("#search").ySearch();
	$("#bg_image").yCenterImage();
	$("#wrapper_content").yNavigationPos();
	$("#model_switch").yModelSwitch();
});




