(function($){
	$(document).ready(function(){
		
		// Rounded Corner Subline
		$('#subline').prepend('<img id="rounded-subline" src="/assets/templates/default/images/pic_rounded-subline.png" />')
		$("#rounded-subline").css({'position':'absolute'});
		$("#rounded-subline").css({'z-index':'2000'});


		// Checkbox limit
		$("#product_list input[name=pid[]]").change(function(){

		    var max_allowed = 3;
		    var checked = $("input:checked").size();

		    if ( checked > max_allowed ) {

		        $(this).attr("checked", "");
						$('#rightCol').append('<div class="infopanel"><p><strong>Hinweis:</strong><br />Bitte wählen Sie maximal 3 Produkte für einen Vergleich aus.</p></div>');
						$('.infopanel').fadeIn().delay(2000).fadeOut('slow'); 
		    } else {
			
						$('.infopanel').fadeOut('slow');
						$('.infopanel').remove();
						//$(".filter a.more").show();
						//$(".pagination a.more").show();  
				}
					
				//if ( checked < 1 ) {
				//		$(".filter a.more").hide();
				//		$(".pagination a.more").hide();
				//}	
		});
		
		// Tab-Filter
		$("ul.tabnav li a.showAll").click(function() {
			$("ul.products li").fadeIn("fast");
			$("table.productlist tr").fadeIn("fast");
		});
		
		$("ul.tabnav li a.showCategory").click(function() {
		  var myClass = $(this).attr("id");
			$("ul.products li").hide();
			$("ul.products li."+myClass).fadeIn("fast");
			$("table.productlist tbody tr").hide();
			$("table.productlist tbody tr."+myClass).fadeIn("fast");
		});

		
		//On Hover Over
		function megaHoverOver(){
		    $(this).find(".sub").stop().fadeTo('fast', 1).show(); //Find sub and fade it in
		
		    /*(function($) {
		        //Function to calculate total width of all ul's
		        jQuery.fn.calcSubWidth = function() {
		            rowWidth = 0;
		            //Calculate row
		            $(this).find("ul").each(function() { //for each ul...
		                rowWidth += $(this).width(); //Add each ul's width together
		            });
		        };
		    })(jQuery); 

		    if ( $(this).find(".row").length > 0 ) { //If row exists...

		        var biggestRow = 0;	

		        $(this).find(".row").each(function() {	//for each row...
		            $(this).calcSubWidth(); //Call function to calculate width of all ul's
		            //Find biggest row
		            if(rowWidth > biggestRow) {
		                biggestRow = rowWidth;
		            }
		        });

		        $(this).find(".sub").css({'width' :biggestRow}); //Set width
		        $(this).find(".row:last").css({'margin':'0'});  //Kill last row's margin

		    } else { //If row does not exist...

		        $(this).calcSubWidth();  //Call function to calculate width of all ul's
		        $(this).find(".sub").css({'width' : rowWidth}); //Set Width

		    }*/
		}
		
		//On Hover Out
		function megaHoverOut(){
		  $(this).find(".sub").stop().fadeTo('fast', 0, function() { //Fade to 0 opactiy
		      $(this).hide();  //after fading, hide it
		  });
		}
		
		//Set custom configurations
		var config = {
		     sensitivity: 2, // number = sensitivity threshold (must be 1 or higher)
		     interval: 20, // number = milliseconds for onMouseOver polling interval
		     over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
		     timeout: 20, // number = milliseconds delay before onMouseOut
		     out: megaHoverOut // function = onMouseOut callback (REQUIRED)
		};

		$("ul#topnav li .sub").css({'opacity':'0'}); //Fade sub nav to 0 opacity on default
		$("ul#topnav li").hoverIntent(config); //Trigger Hover intent with custom configuration		
		
		// Last
		$('#metanav li:last-child').addClass('last');
		$('#slideshow2 .slide .box:last-child').addClass('last');
		$('.teaser .box:nth-child(3n)').addClass('last');
		$('.matrix tbody tr:last-child').addClass('last');
		$('ul.gallery li:last-child').addClass('last');
		$('#topnav li:nth-child(5n) div.sub').addClass('last');
		$('#topnav li:last-child').addClass('last');
				
		// Search-Box: Value leeren onfocus
	  $("#ajaxSearch_input").focus(function () {
			$(this).attr("value","");
	  });
	  /*
		$("#ajaxSearch_input").blur(function () {
			$(this).attr("value","Suchbegriff eingeben");
	  });
	*/

		// Tab toggeln
	  $(".tab").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
		});
		
		// Tab-Navigation		
		$(".showTab1").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab1").show();
		});
		
		$(".showTab2").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab2").show();
		});
		
		$(".showTab3").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab3").show();
		});
		
		$(".showTab4").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab4").show();
		});
		
		$(".showTab5").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab5").show();
		});
		
		$(".showTab6").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab6").show();
		});
		
		$(".showTab7").click(function () {
		  $(".tabnav li").removeClass("selected");
			$(this).parents(".tabnav li").addClass("selected");
			$(".tabgroup").hide();
			$("#tab7").show();
		});		
		
		// Colorbox
		$(".zoom").colorbox({
				transition: "elastic",
				speed: 200,
				current: "Bild {current} von {total}",
				previous: "&lt; zurück",
				next: "weiter &gt;",
				opacity: 0.75
			}
		);
		
		// Add zoom button
		$(".zoom").append("<span></span>");
	
		// Adding sliding doors to the more button via js
		$("a.moreInner").wrapInner("<span></span>");
		$("a.moreInner span span").unwrap();
		$("a.moreInner").wrap("<div style='overflow:hidden'></div>");
	
	  // Cycle
		var index = 0, hash = window.location.hash;
			if (hash) {
				index = /\d+/.exec(hash)[0];
				index = (parseInt(index) || 1) - 1; // slides are zero-based
			};
		
		$('#slideshowOuter').cycle({ 
		    fx:     'scrollHorz', 
		    speed:   300, 
		    timeout: 6000,
		    next:   '.nextSlide',
		 		prev:   '.prevSlide', 
		    pause:   1,
    		cleartype: true, 
        cleartypeNoBg: true,
 				slideExpr: '.slide',
				startingSlide: index,
				after: function(curr,next,opts) {
							window.location.hash = opts.currSlide + 1;
						}
		});
		
		$('#slideshow').cycle({ 
		    fx:     'fade', 
		    speed:   300, 
		    timeout: 6000,
		    next:   '.nextSlide',
		 		prev:   '.prevSlide', 
		    pause:   1,
    		cleartype: true, 
        cleartypeNoBg: true,
				slideExpr: '.panel',
				startingSlide: index,
				after: function(curr,next,opts) {
							window.location.hash = opts.currSlide + 1;
				}
		});
		
		$('#slideshow2').cycle({ 
		    fx:     'scrollHorz', 
		    speed:   300, 
		    timeout: 0, 
		    next:   '.nextSlide2',
		 		prev:   '.prevSlide2', 
		    pause:   1,
    		cleartype: true, 
        cleartypeNoBg: true 
		});
		
		// Akkordion
		$('.accordionButton').click(function() {
			$('.accordionButton').removeClass('on');
	 		$('.accordionContent').slideUp('normal');

			if($(this).next().is(':hidden') == true) {

				$(this).addClass('on');
				$(this).next().slideDown('normal');
			} 
		});

		$('.accordionButton').mouseover(function() {
				$(this).addClass('over');


		}).mouseout(function() {
				$(this).removeClass('over');										
		});

		$('.accordionContent').hide();
		
		
		// Scanner Wizard - Codes
		$("#form_wizard label.code").mouseover(function () {
			$(this).children("img").fadeIn('fast');
		});
		
		$("#form_wizard label.code").mouseout(function () {
			$(this).children("img").fadeOut('fast');
		});
		
		$("#form_wizard label.code input").change(function(){

		    var max_allowed = 2;
		    var checked = $("input:checked").size();

		    if ( checked > max_allowed ) {

		        $(this).attr("checked", "");
						$('#rightCol').append('<div class="infopanel"><p><strong>Hinweis:</strong><br />Bitte wählen Sie maximal 2 verschiedene Code-Typen aus.</p></div>');
						$('.infopanel').fadeIn().delay(2000).fadeOut('slow'); 
		    } else {
			
						$('.infopanel').fadeOut('slow');
						$('.infopanel').remove();
				}
		});
		
		// ImgHover
		$(".hover").imghover({suffix: "_hover"});
		$(".btn").imghover({suffix: "_hover"});

		// Survey
		$("#anregungen1").click(function () {
		  $(".anregungenText").fadeIn('fast');
		});
		$("#anregungen2").click(function () {
		  $(".anregungenText").fadeOut('fast');
		});		

	});
})(jQuery);
