$(document).ready(function () {
	// Navigations-Hauptpunkte-Menu einfügen
	$('#navigation ul li.navitem').each(function() {
		var itemId = $(this).attr('id').split('navitem_main_')[1];
		$(this).mouseenter(function() { openMenu(this, 'navitem_sub_'+itemId); });
	});

	// alle Switcher der Seite herausfinden
   var switcher = $('div.switcher div.image');
   if (switcher != null && switcher.length > 0) { // wenn Switcher vorhanden
   	switcher.each(function(switcherCounter) { // jeden Switcher durchlaufen
   		var switcherCSS_ID = 'switcher-' + switcherCounter;
   		$(this).attr('id', switcherCSS_ID); // einem Switcher eine individuelle CSS-ID verpassen

   		// den Navigations-Button eine JS-Funktion verpassen
   		if ($(this).children().length > 3) {
    		$(this).prev().click(function() { showPrevious(switcherCSS_ID); });
    		$(this).next().click(function() { showNext(switcherCSS_ID); });
    	}
    	else {
    		$(this).next().css('visibility', 'hidden');
    	}
  			$(this).prev().css('visibility', 'hidden');

   		// die einzelnen Videos durchgehen und mit einer CSS-ID ausstatten
    	$(this).children().each(function(videoCounter) {
   			$(this).attr('id', switcherCSS_ID + '_video-' + videoCounter);
    		if (videoCounter >= 3) { // alle Videos nach den ersten 3 ausblenden
    			$(this).hide();
    		}
    	});
   	});
   }
});

function openMenu(navigationElement, menuCssId) {
	var subMenu = $('#'+menuCssId);
	if (subMenu.length > 0) {
		$('#navigation .submenu').each(function() {
			if ($(this).attr('id') != menuCssId) {
				$(this).fadeOut();
			}
		});
		var top	 = $(navigationElement).position().top  + parseInt($(navigationElement).parent().css('margin-top'))  + parseInt($(navigationElement).css('margin-top')) + $(navigationElement).height() - 8;
		var left = $(navigationElement).position().left + parseInt($(navigationElement).parent().css('left')) + parseInt($(navigationElement).css('margin-left')) - 10;
		subMenu.css('top',  top +'px');
		subMenu.css('left', left+'px');
		$('#'+menuCssId).fadeIn();
	}
}

function closeMenu(openMenuElement) {
	$(openMenuElement).mouseleave(function() { $(openMenuElement).fadeOut(); });
}

// Loginbox auf-/zuklappen
function animateLoginBox(showSpeed, hideSpeed) {
	if ($("div#loginform_wrapper").is(":hidden"))
    $("div#loginform_wrapper").slideDown(showSpeed);
  else
    $("div#loginform_wrapper").slideUp(hideSpeed);
}


// Bei Login-Fehler Loginbox gleich aufklappen
function openLoginBoxStatic() {
	$(document).ready(function () {
    $("div#loginform_wrapper").slideDown(1);
	});
}


function showPreviousProduct(slider_ID,productwidth) {
	if (!productwidth) productwidth = 160;
	var slider     = $('#' + slider_ID);
	var curMargin  = Number(substr(slider.css('margin-left'), 0, strrpos(slider.css('margin-left'), 'px')));
	
	return (curMargin == 0) ? false : slider.animate({ marginLeft: "+="+productwidth+"px" }, 1000 );
}


function showNextProduct(slider_ID,productwidth) {	
	if (!productwidth) productwidth = 160;
	var slider     = $('#' + slider_ID);
	var width      = Number(substr(slider.css('width'), 0, strrpos(slider.css('width'), 'px')));
	var curMargin  = Number(substr(slider.css('margin-left'), 0, strrpos(slider.css('margin-left'), 'px')));
	return (curMargin + width <= productwidth)  ? false : slider.animate({ marginLeft: "-="+productwidth+"px" }, 1000 );
}



function substr( f_string, f_start, f_length ) {
    // http://kevin.vanzonneveld.net
    // +     original by: Martijn Wieringa
    // +     bugfixed by: T.Wild
    // +      tweaked by: Onno Marsman
    // *       example 1: substr('abcdef', 0, -1);
    // *       returns 1: 'abcde'
    // *       example 2: substr(2, 0, -6);
    // *       returns 2: ''
 
    f_string += '';
 
    if(f_start < 0) {
        f_start += f_string.length;
    }
 
    if(f_length == undefined) {
        f_length = f_string.length;
    } else if(f_length < 0){
        f_length += f_string.length;
    } else {
        f_length += f_start;
    }
 
    if(f_length < f_start) {
        f_length = f_start;
    }
 
    return f_string.substring(f_start, f_length);
}

function strrpos( haystack, needle, offset){
    // http://kevin.vanzonneveld.net
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Onno Marsman
    // *     example 1: strrpos('Kevin van Zonneveld', 'e');
    // *     returns 1: 16
 
    var i = (haystack+'').lastIndexOf( needle, offset ); // returns -1
    return i >= 0 ? i : false;
}

function closeOverlayLayer() {
  $("#overlay").css('display', 'none');
  $("#countdown").css('display', 'none');
  $("#countdownflash").css('display', 'none');
}