$.ajaxSetup({
    cache: false
});

$(document).ready(function(){
	if ($(".text.success").length > 0){
		$.post('/ajax', {"cleanProducts" : "true"});
	}
	$("#border").css({"height": $("#contentContainer").height()+20});
	$("#contentContainer a.lBox").lightBox();
	$("#contentContainer").load(function(){
		$("#border").css({"height": $("#contentContainer").height()+20});
	});
	$("#searchTerm").focus(function(){
		if($(this).attr("value")=="Suche..."){
			$(this).attr("value","");
		}
		$(this).css("border","1px solid #8a3ea9");
	}).blur(function(){
		$(this).css("border","1px solid #ffffff");
	});
	
	$(".login").click(function(){
		$( "#dialogBox" ).dialog( "open" );
	});
	
	$( "#dialogBox" ).dialog({
		autoOpen: false,
		closeText: "[x]",
		modal: true
	});
	$("#loginForm").submit(function(){
		$("#fail").remove();
		$.ajax({
			  url: "/loginCheck",
			  data: $(this).serializeArray(),
			  type: "POST",
			  statusCode: {
							401: function() {
						    	alert('logged out');
						  	}
						  },
			  error: function(msg){
							  alert(msg.status);
					if (msg.status == 401){
		            	 //location.reload();
		             }
		      }, 
			  success: function(msg){
		    	  if (msg.indexOf("denied") != -1){
		    		  $("#loginForm").append($("<div style='color: red' id='fail'>Benutzername oder Passwort falsch</div>"));;
		    	  }
		    	  else{
		    		if ( $.browser.msie ){
					$("#loginForm").remove();
					self.location.href=location.pathname+"";	
				} 
				else{
					location.reload();
		    		}  
			}
		     }
		});
		return false;
	});
	
	$(".logout").click(function(){
		$.ajax({
			  url: "/loginCheck",
			  data: {'mgnlLogout':'true'},
			  type: "POST",
			  statusCode: {
							401: function() {
						    	alert('logged out');
						  	}
						  },
			  error: function(msg){
							  alert(msg.status);
					if (msg.status == 401){
		            	 location.reload();
		             }
		      }, 
			  success: function(msg){
		    	  location.reload();
		    	  //alert(msg);
		     }
			});
		return false;
	});
	
	$(".productref").click(function(){
		if ($(this).attr("id") != "selectedVar"){
			$(".productref img").css({"border":"none", "padding":"4px"});
			$(this).children("img").css({"border": "1px solid #8a3ea9", "padding":"3px"});
			$(".productref").attr("id", "");
			$(this).attr("id", "selectedVar");
			$("#productDetail > *").remove();
		}
		$("#productDetail").load($(this).attr("href").replace("#", "")+"?rdr=0"+" #productDetail > *", function(){
			$(".take").click(function(){
				var $link = $(this);
				link=$(this).attr("href").replace("#", "");
				links=link.split("&");
				//alert(links);
				$.post('/ajax', {"id" : links[0], "var" : links [2], "pos": links[1]}, function(data, status){
					takenClick();
				});
			});
			$(".taken").click(function(){
				var $link = $(this);
				link=$(this).attr("href").replace("#", "");
				links=link.split("&");
				//alert(links);
				$.post('/ajax', {"id" : links[0], "var" : links [2], "pos": links[1], "removeItem": "true"}, function(data){
					takenClick();//$link.innerText(data);
				});
			});
			$(".take").tooltip({delay: 0, 
			    showURL: false ,
			    track: true,
			    left: 10,
			    bodyHandler: function() { 
					return $("<div><strong>"+$('#prodHelp').html()+"</strong></div>");
			    }
			});
			
			$(this).fadeIn();
			$("#border").css({"height": $("#contentContainer").height()});
		});
	});
	
	$(".productref:not(#selectedVar)").mouseover(function(){
		if ($(this).attr("id")!="selectedVar")
		$(this).children("img").css({"border": "1px solid gray","padding": "3px"});
	}).mouseout(function(){
		if ($(this).attr("id")!="selectedVar")
		$(this).children("img").css({"border": "none", "padding": "4px"});
	});
	
	$(".productref").tooltip({delay: 0, 
		    showURL: false ,
		    track: true,
		    left: 10,
		    bodyHandler: function() { 
				return $("<div><img src='/docroot/imgs/loader.gif' /></div>").load($(this).attr("href").replace("#", "")+"?rdr=0"+" #description #subTitle, #description #dNr, #pass", function(){
					
				});
		    }
	});
	
	$(".page").click(function(){
		$(".page").css({"font-weight":"normal"});
		$(this).css({"font-weight":"bold"});
		var nIndex = $(this).index()-1;
		var realHeight = $(".productNavPages:eq("+nIndex+")").height();
		var oldHeight = $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height();
		var oldIndex = $(".productNavPages:visible").index();
		$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
		if ($(".productNavPages:eq("+nIndex+")").height() < $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height()){
			$(".productNavPages:eq("+nIndex+")").css({"height":"164"});
			$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500, function(){});//.css({"display":'none'});
			$(".productNavPages:eq("+(nIndex)+")").fadeIn(500, function(){
				$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
				$(".productNavPages:eq("+nIndex+")").animate({"height":realHeight},function(){$("#border").css({"height": $("#contentContainer").height()+20});});
				$("#border").css({"height": $("#contentContainer").height()+20});
			});
		}
		else{
			if ($(".productNavPages:eq("+nIndex+")").height() > $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height()){
				$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").animate({"height":realHeight},function(){
					$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500);//.css({"display":'none'});
					$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
					$(".productNavPages:eq("+nIndex+")").fadeIn(500, function(){
						$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
						$(".productNavPages:eq("+oldIndex+")").css({"height":oldHeight});
						$("#border").css({"height": $("#contentContainer").height()+20});
					});
					$(".page").css({"font-weight":"normal"});
					$(".page:eq("+nIndex+")").css({"font-weight":"bold"});
				});
			}
			else{
				$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500);//.css({"display":'none'});
				$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
				$(".productNavPages:eq("+nIndex+")").fadeIn(500, function(){
					$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
				});
				$(".page").css({"font-weight":"normal"});
				$(".page:eq("+nIndex+")").css({"font-weight":"bold"});
			}
		}
		
		$("#border").css({"height": $("#contentContainer").height()+20});
		return false;
	}); 
	
	$(".nextPage").click(function(){
		$(".productNavPages:visible").index();/*.css({"display":'none'});*/
		if ($(".productNavPages:eq("+($(".productNavPages:visible").index()+1)+")").size() != 0){
			var nIndex = ($(".productNavPages:visible").index()+1);
			$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
			var realHeight = $(".productNavPages:eq("+nIndex+")").height();
			if ($(".productNavPages:eq("+nIndex+")").height() < $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height()){
				$(".productNavPages:eq("+nIndex+")").css({"height":"164"});
			}
			$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500, function(){ 
				$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
				$(".productNavPages:eq("+nIndex+")").animate({"height":realHeight}, function(){
					$("#border").css({"height": $("#contentContainer").height()+20});
				});
				
			});//css({"display":'none'});
			$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
			$(".productNavPages:eq("+nIndex+")").fadeIn(500, function(){
				$("#border").css({"height": $("#contentContainer").height()+20});
			});
			$(".page").css({"font-weight":"normal"});
			$(".page:eq("+nIndex+")").css({"font-weight":"bold"});
		}
		$("#border").css({"height": $("#contentContainer").height()+20});
		return false;
	}); 
	
	$(".prevPage").click(function(){
		$(".page").css({"font-weight":"normal"});
		$(".productNavPages:visible").index();
		if ($(".productNavPages:eq("+($(".productNavPages:visible").index()-1)+")").size() != 0){
			var nIndex = ($(".productNavPages:visible").index()-1);
			$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
			var realHeight = $(".productNavPages:eq("+nIndex+")").height();
			var oldHeight = $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height();
			if ($(".productNavPages:eq("+nIndex+")").height() > $(".productNavPages:eq("+$(".productNavPages:visible").index()+")").height()){
				$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").animate({"height":realHeight},function(){
					$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500);//.css({"display":'none'});
					$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
					$(".productNavPages:eq("+nIndex+")").fadeIn(500, function(){
						$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
						$(".productNavPages:eq("+(nIndex+1)+")").css({"height":oldHeight});
						$("#border").css({"height": $("#contentContainer").height()+20});
					});
					$(".page").css({"font-weight":"normal"});
					$(".page:eq("+nIndex+")").css({"font-weight":"bold"});
				});
			}
			else{
				$(".productNavPages:eq("+$(".productNavPages:visible").index()+")").fadeOut(500);//.css({"display":'none'});
				$(".productNavPages:eq("+nIndex+")").css({"position":"absolute", "top":"0", "left":"0"});
				$(".productNavPages:eq("+nIndex+")").fadeIn(500, function(){
					$(".productNavPages:eq("+nIndex+")").css({"position":"static"});
					$("#border").css({"height": $("#contentContainer").height()+20});
				});
				$(".page").css({"font-weight":"normal"});
				$(".page:eq("+nIndex+")").css({"font-weight":"bold"});
			}
		}
		$("#border").css({"height": $("#contentContainer").height()+20});
		return false;
	}); 
	
	if (window.location.hash){
		var selectedEl = $("a[href='"+window.location.hash+"']");
		if (selectedEl.length > 0){
			var ndx = selectedEl.parent().index();
			$(".page:eq("+(ndx)+")").click();
			selectedEl.click();
		}
		
		$("#border").css({"height": $("#contentContainer").height()});
		/*$("#productDetail").load(window.location.hash.replace("#", "")+" #productDetail > *", function(){$(this).fadeIn();
		$("#border").css({"height": $("#contentContainer").height()});});*/
		
	}
	
	$(".take").click(function(){
		link=$(this).attr("href").replace("#", "");
		links=link.split("&amp;");
		//alert(links);
		$.post('/ajax', {"id" : links[0], "var" : links [1]}, function(data){
			//alert(data);
		});
		return false;
	});
	$(".take").tooltip({delay: 0, 
	    showURL: false ,
	    track: true,
	    left: 10,
	    bodyHandler: function() { 
			return $("<div><strong>"+$('#prodHelp').html()+"</strong></div>");
	    }
	});
	
	$(".help").tooltip({delay: 0, 
	    left: 10,
	    showURL: false ,
	    bodyHandler: function() { 
			return $('<div style="max-width:200px">'+$('#help').html()+'</div>');
	    }
	});
	
	addOrderFunc();
	getTotal();
	/** Order Workaround */
	$("#otherProductsLabel").html($("label[for='otherProducts']").text());
	$("#otherProductsValue").change(function(){
		$("#otherProducts").value = $(this).value;
	});

	$("#border").css({"height": $("#contentContainer").height()});
});


function getTotal(){
	$.post('/ajax', {"total" : true}, function(data){
		if (data != null){
			$('.total').html(data);
		}
	});
}



function addOrderFunc(){

	$(".orderRow").unbind("mouseover").mouseover(function(){
		$(this).css({"background-color":"#f1d7ef"});
	}).mouseout(function(){
		$(this).css({"background-color":"#ffffff"});
	});
	
	$(".addinfo").unbind("change").change(function(){
		var params = $(this).attr("name").split(":");
		var el = $(this).parent().parent();
		var index = $(this).parent().parent().index();
		$.post('/ajax', {"index" : index, "param" : params[0], "value":$(this).attr("value")}, function(data){
			if (data != null){
				ppm = "";
				p = "";
				var prices = data.split("<br />");
				if (prices.length == 1){
					ppm = prices[0];
					p=prices[0]
				}
				else{
					ppm = prices[0];
					p=prices[2]
				}
				el.find('.price').html(p);
				el.find('.pricepm').html(ppm);
				getTotal();
			}
		});
	});
	
	$(".conf").change(function(){
		alert("die Preise sind nur für Produkte\n ohne Konfektionierung gültig");
	});
	
	$(".remove").unbind("click").click(function(){
		params = $(this).attr("name").split(":");
		var el = $(this).parent().parent();
		var index = el.index();
		$.post('/ajax', {"index" : index, "remove" : "true"},function(data){
			if (data.indexOf("removed") != -1){
				/*$(this+":parent:parent").remove();*/
				el.remove();
				getTotal();
			}
			if (data.indexOf("empty") != -1){
				location.reload();
			}
		});
	});
	$(".remove").tooltip({delay: 0, 
	    showURL: false ,
	    track: true,
	    left: 10,
	    bodyHandler: function() { 
			return $("<div><strong>Dieses Produkt löschen</strong></div>");
	    }
	});
	
	$(".copy").tooltip({delay: 0, 
	    showURL: false ,
	    track: true,
	    left: 10,
	    bodyHandler: function() { 
			return $("<div><strong>Weitere Konfiguration für<br /> dieses Produkt einfügen</strong></div>");
	    }
	});
	
	$(".copy").unbind("click").click(function(){
		params = $(this).attr("name").split(":");
		var el = $(this).parent().parent();
		var index = el.index();
		$.post('/ajax', {"index" : index, "copy" : "true"},function(data){
			if (data.indexOf("added") != -1){
				var clone = el.clone();
				clone.insertAfter(el);
				addOrderFunc();
				getTotal();
				/*$(this+":parent:parent").remove();*/
				//el.remove();
			}
		});
	});
	
	$(".orderButton").click(function(){
		var passed=true;
		var allInputs = $(".addinfo");
		for (i = 0; i < allInputs.length; i++){
			try{
				if (allInputs.get(i).value == ""){
					allInputs.get(i).focus();
					alert("Alle Felder ausfüllen");
					passed=false;
					return passed;
				}
			}
			catch (e){
				//alert(i+" "+(allInputs.length-1));
				//return;
			}
		}
		if (passed)
		$("#order").submit();
	});
}


function takenClick(){
	$("#selectedVar").click();
}

