// JavaScript Document
// +++ update_176 ---
$(document).ready(function(){
	$('#cartInfoBubble').hide();
	$('.productDetail').click(function() {
		$('#cartInfoBubble').hide();
	})
	
	// +++ update_176 +++
	// Hauptmenu
	$(".scrollerBox div.scrollable").scrollable({
		size: 1,  
		hoverClass: 'hover',
		clickable: false
	}).navigator(".navi").mousewheel();
	// --- update_176 ---


	// +++ update_135 +++
	//////////////////////////////
	// Warenkorb Slider
	//////////////////////////////
	// +++ update_181 +++
	$(".slider-box .inner").scrollable({
		size: 7
	});
	// ---update_181 ---
	
	//////////////////////////////
	// Bastelanleitung
	//////////////////////////////
	// +++ update_216 +++
	if ($('div').is('.slider-box-bastel') == true ){
		var apiSlider = $(".slider-box-bastel .inner").scrollable({ 
			size: 1
		}).navigator({api: true});
	
		apiSlider.onSeek(function() { 
			var stepIndex = this.getIndex();
			showHide(stepIndex);
		});
	}
	// ---update_216 ---	
	
	//////////////////////////////
	// Produktdetailseite - Last view products
	//////////////////////////////
	if ($('div').is('.slider-box-last_viewed') == true ){
		var apiSlider = $(".slider-box-last_viewed .inner").scrollable({ 
			size: 1,
			vertical: true
		});
	}	
	
	
	var cartOpen = false;												 
	var btnCart = true;												 
	var btnWish = false;
	
	sliderClose = function(){
		if (cartOpen == true){
			$('#cartSlider').animate({"bottom": "-=100px"}, "slow");
			$('.btnSlider').css('background-position','0 0')
			cartOpen = false;
		}
	}

	sliderOpen = function(setTimer){
		if (cartOpen == false && setTimer == 'timer'){
			$('#cartSlider').animate({"bottom": "+=100px"}, "slow", function(){setTimeout("sliderClose()", 1000);});
			$('.btnSlider').css('background-position','0 -30px');
			cartOpen = true;
		}else	if (cartOpen == false){
			$('#cartSlider').animate({"bottom": "+=100px"}, "slow");
			$('.btnSlider').css('background-position','0 -30px');
			cartOpen = true;
		}
	}
	
	// Auf- / Zuslider
	$(".btnSlider").click(function () {
		if (cartOpen == true){
			$('#cartSlider').animate({"bottom": "-=100px"}, "slow");
			$(this).css('background-position','0 0')
			cartOpen = false;
		}else{
			$('#cartSlider').animate({"bottom": "+=100px"}, "slow");
			$(this).css('background-position','0 -30px')
			cartOpen = true;
		}
	});

// Button fuer Wishlist
	$(".btnWish").click(function () {
		if (btnCart == true){
			$('#slider-wish').addClass('activ');
			$('#slider-cart').removeClass('activ');
			btnCart = false;
			btnWish = true;
		}
		sliderOpen();
	});
	
	// Button fuer Warenkorb
	$(".btnCart").click(function () {
		if (btnWish == true){
			$('#slider-cart').addClass('activ');
			$('#slider-wish').removeClass('activ');
			btnCart = true;
			btnWish = false;
		}
		sliderOpen();
	});
	
	//Aus Warenkorb loeschen
	$('.delete').click(function (id) {
					
		// +++ update_140 +++		
		// Enth�lt Zieladresse fuer Ajax-Funktion inkl. Variablen
		action = $(this).attr('title');
		
		// Loeschen per Ajax aufrufen
		$.post(action, function(data){
			reloadCart();			
		});	
		// --- update_140 ---
								 
		$(this).parent().animate({
			marginTop: "150px",
			opacity: 0
		}, "slow", "swing", function(){
			$(this).remove();
		});
		
		setTimeout("sliderClose()", 500);
	});

	deleteCartItem = function() {
		$('.delete').click(function (id) {
									 
			// +++ update_140 +++		
			// Enth�lt Zieladresse f�r Ajax-Funktion inkl. Variablen
			action = $(this).attr('title');
			
			// Loeschen per Ajax aufrufen
			$.post(action, function(data){
				reloadCart();									
			});	
			// --- update_140 ---
									 
			$(this).parent().animate({
				marginTop: "150px",
				opacity: 0
			}, "slow", "swing", function(){
				$(this).remove();
			});
			api.reload().prev();
			
			setTimeout("sliderClose()", 500);
		});
	}
	
	/////////////////////////////
	// END: Warenkorb Slider
	//////////////////////////////
	// --- update_135 ---

	// Dropdown-Menu
//	$(".dropDown a").click(function() {
//		$(this).next().slideToggle('normal');
//		if (this.flip==1) {
//			$(this).css('background-position','3px 9px');
//			this.flip = 0;
//		} else {
//			$(this).css('background-position','3px -21px');
//			this.flip = 1;
//		}
//	}).next().hide();

	// +++ update_201 +++
	// Navi
	// original
	/*
	$('#navi ul li').hover(function(){
		n = $(this).find('div');
		n.css('display','none');
		t = setTimeout("naviDown(n)", 500);
	},
	function () {
  	clearTimeout(t); 
		$(this).find('div').css('display','none');
	});
	
	naviDown = function(){
		n.css('display','block');
	}
	*/
	
	// +++ update_135 +++
	// NAVI TIMEOUT
	
	// +++ update_365 +++
	var x = 0;
	// --- update_365 ---
	
	$('#navi ul li[class^="nav"]').hover(function(){
		tab = $(this).find('a:first');
		n = $(this).find('div:first');
		t = setTimeout(function(){naviDown(n,tab);}, 500); 
		clearTimeout(x); 
	
	},function(){
		clearTimeout(t); 
		x = setTimeout(function(){naviUp(n,tab);}, 500); 									
	});

	function naviDown(n,tab){
		$('#navi ul li[class^="nav"]').each(function(){
			$(this).find('div:first').hide();
			if(!$(this).find('a:first').hasClass('active')){
				$(this).find('a:first').css('background-position','0 0');
			}
		});
		tab.css('background-position','0 -54px');
		n.show();
	}
	
	function naviUp(n,tab){
		if(!tab.hasClass('active')){
			tab.css('background-position','0 0');
		}
		n.hide();
	}
	// --- update_135 ---
	///
	
	// --- update_201 ---

	//Formfields
	// +++ update_222 +++
	$("input[type='text'], input[type='password'], textarea").focus(function () {
		$(this).addClass('activ');
	});
	$("input, textarea, select").blur(function () {
		$(this).removeClass('activ');
	});
	// --- update_222 ---

	// Suche
	$(".search02, .search03").hover(function(){
		$(this).find('div:first:hidden').css({visibility: "visible",display: "none"}).show(400);
	},function(){
		$(this).find('div:first').hide('fast');
	});
	
	// NEUE Suche
	// +++ update_274 +++
	$(".search02").click(function(){
		$(this).find('a').toggleClass('active');
		$("#co-search").toggle();
		hideIntelliSearch();
	});
	$(".search03").click(function(){
		$(this).find('a').toggleClass('active');
		$("#th-search").toggle();
		hideIntelliSearch();
	});
	
	$('input[name=keywords]').click(function(){
		$('#intelliSearchResult').show();																		 
	});
	
	hideIntelliSearch = function(){
		if( $(".search02").find('a').hasClass("active") || $(".search03").find('a').hasClass("active") ){
			$('#intelliSearchResult').hide();	
		}else{
			$('#intelliSearchResult').show();	
		}
	};
	// --- update_274 ---
	
	$("#co-search a span.c-hover").click(function() {
		if($(this).hasClass("active") == true ){
			$(this).removeClass("active");
		}else{
			$(this).addClass("active");
		}
	});

	// Accordion
	// +++ update_185 +++
	// +++ update_222 +++
	var myURL = document.location.toString();
	if (myURL.match('#')) { // the URL contains an anchor
	  // click the navigation item corresponding to the anchor
	  var myAnchor = myURL.split('#')[1];
	  myAnchor = myAnchor - 1;
	  //alert(myAnchor);
	 }
	 else {
		var myAnchor = 0;
	 }
	
	$(".showhide h3").eq(0).removeClass("minus");
	$('.boxContentSH').eq(0).hide();
	$(".showhide h3").eq(myAnchor).addClass("minus");
	$('.boxContentSH').eq(myAnchor).show();
	// --- update_222 ---

	$(".showhide h3").click(function() {
		if (this.flip==1 || $(this).hasClass("minus")) {
			$(this.parentNode).find('.boxContentSH').hide();
			$(this).removeClass("minus");
			this.flip = 0;
		} else {
			$(this.parentNode).find('.boxContentSH').show();
			$(this).addClass("minus");
			this.flip = 1;
		}
	});
	// --- update_185 ---
	
	//Farbwaehler
	var color;
	$(".btnColor, .cPad a").click(function(){
		$('#colorSelect').slideToggle('normal');
		if (color==1) {
			$('.btnColor').css('background-position','left top');
			color = 0;
		} else {
			$('.btnColor').css('background-position','left -24px');
			color = 1;
		}
	}).next().hide();


	// Farb-Plaetchen Austauschen
	$(".cPad a").click(function() {
		var pTitle = $(this).attr("title");
		var pStyle = $(this).find('span').attr("style");
		var pImg = $(this).find('span').html();
		
		$('#color-selected').find('a').attr({
			title: pTitle
		});
		$('#color-selected a span').replaceWith('<span style=" '+ pStyle + '">' + pImg + '</span>');
	});
	
	// Tooltipp
	$(".tt a").hover(function() {
			// Beschreibung der Farbe steht im title-Attribut des a-Tags !!!
			var title = $(this).attr("title");

			if ($.browser.msie) {
				$(this).after('<div class="tooltip"><p>'+title+'</p></div>');
				$(this.parent).find(".tooltip").stop(true, true).show().animate({top: "-33"}, "fast");
			}
			else {
				$(this).after('<div class="tooltip"><p>'+title+'</p></div>');
				$(this.parent).find(".tooltip").stop(true, true).animate({opacity: "show", top: "-33"}, "fast");
			}
		}, 
		function() {
			if ($.browser.msie) {
				$(this.parent).find(".tooltip").remove();
			}
			else {
				$(this.parent).find(".tooltip").remove();
			}
	});		
	

	// TABS 
	$("ul.tabs").tabs("div.panes > div"); 
	$("ul.tabs-search").tabs("div.panes-search"); 		

	// +++ update_190 +++
	openTab = function(tabIndex) {
		var api = $("ul.tabs").tabs();
		api.click(tabIndex);
	}
	// --- update_190 ---


	// +++ update_90 +++
	// Script in js.php des Produktes ausgelagert
	// --- update_90 ---

	// Alle Checkboxen selektieren
	checkAll = (function( id, pID ){
		 $( "#" + pID + " :checkbox").attr('checked', $('#' + id).is(':checked'));
	});


	// +++ update_99 +++
	b_changeValue = (function( id ){
		 
		 // Wert lesen
		 val = $("#" + id).val();
		 		 
		 // Setzen
		 if (val == 0)
		 {
			 $("#" + id).val(1);
		 }
		 else
		 {			 
			 $("#" + id).val(0);
		 }
	});
	// --- update_99 ---
	//+++ update_379 +++ 
	changeJsButtons();
	changeLoginOptions();
	searchTab();
	changeColorCheckboxes();
	changeColorProductDetail();
	//--- update_379 --- 
});

//+++ update_379 +++ 
function changeColorProductDetail() {
	if($("#js_color").length != 0){
		$("#noJs_color").hide();
		$("#js_color").show();
	}
}
function changeColorCheckboxes() {
	var num = 1;
	$("#co-search .cPad").each(function(index,value) {
		var neHref = $("#co-search #color_"+num).html();
		$(this).find('a').attr('href',neHref);
		num++;
	});
}
function changeLoginOptions() {
	if($("#load_dologinCustomer").length != 0 && $("#load_doregCustomer").length != 0) {
		$("#dologinCustomer").html($("#load_dologinCustomer").html());
		$("#doregCustomer").html($("#load_doregCustomer").html());
		//+++ update_394 +++
		$("#doregGuest").html($("#load_doregGuest").html());
		//--- update_394 ---
	}
}
function changeJsButtons() {
	/* Add2 Cart*/
	$("input[name=useJS]").val(1);
	/*Wishlist Button*/
	if($(".addWishlistProduct").attr('href') != undefined) {
		var href = $(".addWishlistProduct").attr('href');
		var href = href.replace(/&useJS=0/,"");
		$(".addWishlistProduct").attr('href',href);
	}

	/*Top Nav*/
	for (var i = 1; i <= 5; i++) {
		$("#navi .nav0"+i+" > a").attr('href','javascript:void(0)');
	}
}

function searchTab() {
	$("li.search01 a").attr('href','javascript:void(0)');
	$("li.search02 a").attr('href','javascript:void(0)');
	$("li.search03 a").attr('href','javascript:void(0)');
	
	$("li.search03>a").click(function() {
		$("li.search01 a").removeClass('active');
		$("li.search02 a").removeClass('active');
		$("#co-search").hide();
		
	});
	$("li.search02>a").click(function() {
		$("li.search01 a").removeClass('active');
		$("li.search03 a").removeClass('active');
		$("#th-search").hide();
	});
	$("li.search01>a").click(function() {
		$("li.search02 a").removeClass('active');
		$("li.search03 a").removeClass('active');
		$("#th-search").hide();
		$("#co-search").hide();
	});
}
//--- update_379 --- 

//+++ update_394 +++
function setVisibility(contId) {
	if(contId == "loginRegistered") {	
		$('#loginUnregistered').hide();
		$('#loginRegistered').show();
		$('#loginGuest').hide();
	} 
	else if(contId == "loginGuest") {
		$('#loginUnregistered').hide();
		$('#loginRegistered').hide();
		$('#loginGuest').show();			
	} 
	else {	
		$('#loginUnregistered').show();
		$('#loginRegistered').hide();
		$('#loginGuest').hide();			
	}
}
//--- update_394 ---

// +++ update_266 +++
function winOpen (url, w, h) {
	param = 'width='+ w +',height='+h+',status=no,scrollbars=auto,resizable=no';
	fenster = window.open(url, "fenster1", param);
	fenster.focus();
}
// --- update_266 ---

function showHide(e){
	$('div.step').hide();
	$("div#step"+e+"").show();
}	

function showMasterSlaveMessage() {
	$('#cartInfoBubble').show();
}

priceCalcSet = (function(formname) {
	 // Gesamtpreis
	 total = 0;

	 // Checkboxen durchlaufen
	 $('#'+formname+' input:checkbox').each(function() {
		
		// Checkbox des Sets und markiert ermitteln 
		if(this.name.match(/check\[.*/) && this.checked) {
			
			// ID extrahieren, um entsprechende Elemente anzusprechen				
			strstart = this.name.indexOf('[');
			strend = this.name.indexOf(']');

			p_id = this.name.substring(strstart+1, strend);
			
			// Preis (unformatiert, ungerundet)
			p_price = $("input[name='price_plain["+p_id+"]']").val();
			
			// Preis runden
			p_price = Math.round(p_price*100) / 100;
			total = total + (p_price * $("input[name='qty["+p_id+"]']").val());
		}
	 });
	 $('#price_total').text(makePrice(total));
});
	 
	 
// Preis formatieren
makePrice = (function(input) {
	// zur Sicherheit nochmal runden
	output = Math.round(input*100)/100;
	// von Zahl mit . in Wert mit , und W�hrung umwandeln
	var tmp = String(output);	
	var tmp = tmp.split(".");
	
	// Pr�fen, ob 2stellig, sonst ne 0 dran h�ngen
	if (tmp[1])
	{
		var comma_length = tmp[1].length;
		
		if (comma_length < 2)
		{
			tmp[1] = String(tmp[1]) + '0';
		}
	}
	else
	{
		tmp[1] = '00';
	}
	
	//output = '&euro; '+tmp[0]+','+tmp[1];
	output = tmp[0]+','+tmp[1];
	
	return output;
});


// +++ UPDATE basePrice +++
// +++ 13.01.2012
//
$(document).ready(function(){
	setOldPrice();
})

function setOldPrice() {
	$('.old').each(function(){
		var star = $(this).parent().find('.priceInfoStart');
		var salePrice = $(this).parent().find('.sale');
		star.appendTo(salePrice);
	})
}

