if (top.location != self.location) {
    top.location.href="index.php?content=kasse"
}

function reload_form(){
	document.kontakt_form.update.value=2; 
	document.kontakt_form.submit();
}

function suche() {
  if(document.search.search_form.value.length < 3){
  	 alert ("Bitte mindestens 3 Zeichen eingeben.");
  	 return false;
  	} else {
  	  return true;
  	}
}

function PopUp(picture){
neu = window.open(picture,'picture_view','toolbar=0,location=0,status=0,directories=0,scrollbars=yes,resizable=no,menubar=0,width=620,height=358'); 
neu.focus();
}

function hinweis_aus() {
  document.getElementById("hinweis").style.visibility = "hidden";
}

var filter_changed=false;

function activateButtons(){
	 $('input').checkBox({replaceInput:true});
	 $('.rollover').hoverswap();
	 $("#button_filter_ok").click(function(){
	  var inhalte = $(".filter_checkbox").serialize();
	  var typ = $("#filter_typ").val();
	  var session_id = $("#session_id").val();
	  $.ajax({
	    type: "POST",
	    url: "/lib/js_functions.php",
	    async: false,
	    data: "function=setFilter&session_id="+session_id+"&typ="+typ+"&"+inhalte,
	    success: function(data_str){
	    	 var data = eval('('+data_str+')');
	    	 filter_changed=true;
	    	 $.fancybox.close();
	    }
	  });
	 	 return false;
	 	});
}


function closeFilter(){
	if(filter_changed){
//	 $("#box_filter").load("box_filter.php", function(){
//	  	activateButtons();
//	 	  activateFilterBox(); 	 
//	 });	 
//	 $("#box_content").html("<div style='width:100%; padding:50px 0px 50px 0px; text-align:center'><img src='images/loader.gif' border=0></div>");
	 var content = $("#content").val();
	 var id_typ = $("#id_typ").val();
	 var id_utyp = $("#id_utyp").val();
	 var hersteller = $("#hersteller").val();
	 var region = $("#region").val();
	 var session_id = $("#session_id").val();
//	 $("#box_content").load("artikel.php?content="+content+"&id_typ="+id_typ+"&id_utyp="+id_utyp);
//	 $("#box_regionen").load("box_regionen.php?content="+content+"&id_typ="+id_typ+"&id_utyp="+id_utyp);
//   var lastindex = location.href.lastIndexOf("/")+1;
//   if(lastindex == 0)lastindex = location.href.length;
//   alert(location.href.slice(0, lastindex));
//   if(location.href.search(/artikel/) == -1)location.href="/artikel/Wein,1-1/";
//   else location.href=location.href.slice(0, lastindex);
	   var lastPage=$("#last_page").val();
	   var lastindex = 0;
	   if(lastPage.search(/\.html/) > -1){
	   	 lastindex = lastPage.lastIndexOf("/")+1;
	   }     
	 	 if(lastindex == 0)lastindex = lastPage.length;
	 	 if(lastPage != "" && lastPage != "/home.html" && lastPage != "/")location.href=lastPage.slice(0, lastindex);
	 	 else location.href="/artikel/Wein,1-1/";
	 

	}
//	if($("#content").val()=="artikel")location.reload();
//	else {
//	  var content = $("#content").val();
//	  var id_typ = $("#id_typ").val();
//	  var id_utyp = $("#id_id_utyp").val();
//	  var hersteller = $("#hersteller").val();
//	  var region = $("#region").val();
//	  var session_id = $("#session_id").val();
//	 $("#box_regionen").load("box_regionen.php?content="+content+"&id_typ="+id_typ+"&id_utyp="+id_utyp+"&hersteller="+hersteller+"&region="+region);
//	}
}

function activateFilterBox(){
	 	$("a.link_suchfilter").fancybox({
	 	'zoomSpeedIn': 300,
	 	'zoomSpeedOut': 300,
	 	'frameWidth': 594,
	 	'frameHeight': 300,
	 	'overlayShow': true,
	 	'hideOnContentClick' : false,
	 	'onComplete' : activateButtons,
	 	'onCleanup' : closeFilter
	 	});	 
	 	
	 	$("a.link_suchfilter_rebsorten").fancybox({
	 	'zoomSpeedIn': 300,
	 	'zoomSpeedOut': 300,
	 	'frameWidth': 594,
	 	'frameHeight': 720,
	 	'overlayShow': true,
	 	'centerOnScroll':	false,
	 	'hideOnContentClick' : false,
	 	'onComplete' : activateButtons,
	 	'onCleanup' : closeFilter
	 	});	 
	 	
	 $("#button_filter_reset").click(function(){
 
	  var session_id = $("#session_id").val();
	  $.ajax({
	    type: "POST",
	    url: "/lib/js_functions.php",
	    async: false,
	    data: "function=resetFilter&session_id="+session_id,
	    success: function(data_str){
	    	 var data = eval('('+data_str+')');
	    	 filter_changed=true;
	    	 closeFilter();
	    }
	  });


	 	 return false;
	 	});
	 
}

var timeoutId;
var boxWarenkorbTimeOutID = 0;
var warenkorbTimeOutID = 0;


function init(){
		$(".fancy").fancybox();
		$(".box_partner_inner").cycle({
				fx:'fade'
		})
		
		
		$('input[name="menge"]').bind("keydown",function(event){	
			if( (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode==13 || event.keyCode == 8 || event.keyCode == 46 || (event.keyCode >= 37 && event.keyCode <= 40) ) return true;
			else return false
		});
		
		
		$(".datenbox_line_right").click(function(){
			$(this).mouseOver();
		});
		
		$("#box_warenkorb").parent().delegate(".form_menge_small", "keydown", function(event){
			if( (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode==13 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 16 || event.keyCode == 46 || (event.keyCode >= 37 && event.keyCode <= 40) ) return true;
			else return false
		});
		
		$("#warenkorb").parent().delegate(".form_menge", "keydown", function(event){
			if( (event.keyCode >= 96 && event.keyCode <= 105) || (event.keyCode >= 48 && event.keyCode <= 57) || event.keyCode==13 || event.keyCode == 8 || event.keyCode == 9 || event.keyCode == 16 || event.keyCode == 46 || (event.keyCode >= 37 && event.keyCode <= 40) ) return true;
			else return false
		});
		
		$('input[name="menge"]').keyup(function(){
			if(parseInt($(this).attr("max")) > 0){
				if(parseInt($(this).attr("max")) < parseInt($(this).val())){
					$(this).css({"background-color":"#8D0000","color":"#ECE3B6"});
					return false;
				} else {
					$(this).removeAttr("style");
				}
			}			
		});
		
		$(".formular_artikel_uebersicht").submit(function(){
			var data = $(this).serializeArray();
			if($("#hinweis").length < 1)$("body").append("<div id='hinweis'></div>");
			$("#hinweis").fadeOut(0);
			$("#hinweis").load($(this).attr("action")+ " #hinweis",data,function(){
				$("#hinweis").fadeIn(500);
				$("#box_warenkorb").load("/box_warenkorb.php #box_warenkorb");
				$('#hinweis input[value="weiter shoppen"]').click(function(){
					$("#hinweis").fadeOut(500, function(){
						$(this).remove();
					});
					return false;
				});
			});
			return false;
		});
		
		$("#box_warenkorb").parent().delegate(".form_menge_small", "keyup", function(e){
			
			if(boxWarenkorbTimeOutID){
				clearTimeout(boxWarenkorbTimeOutID);
				boxWarenkorbTimeOutID = 0;
			}			
			
			if(parseInt($(this).attr("max")) > 0){
				if(parseInt($(this).attr("max")) < parseInt($(this).val())){
					$(this).css({"background-color":"#8D0000","color":"#ECE3B6"});
					return false;
				} else {
					$(this).removeAttr("style");
				}
			}			
		
			if($(this).val()=="" || parseInt($(this).val())==0){
				clearTimeout(boxWarenkorbTimeOutID);
				return;
			}
			boxWarenkorbTimeOutID = window.setTimeout(function(){sendBoxWarenkorb(e.target)},1000);
		});
		
		$("#warenkorb").parent().delegate(".form_menge", "keyup", function(e){
			
			if(warenkorbTimeOutID){
				clearTimeout(warenkorbTimeOutID);
				warenkorbTimeOutID = 0;
			}
						
			if(parseInt($(this).attr("max")) > 0){
				if(parseInt($(this).attr("max")) < parseInt($(this).val())){
					$(this).css({"background-color":"#8D0000","color":"#ECE3B6"});
					return false;
				} else {
					$(this).removeAttr("style");
				}
			}

			if($(this).val()=="" || parseInt($(this).val())==0){
				clearTimeout(boxWarenkorbTimeOutID);
				return;
			}
			warenkorbTimeOutID = window.setTimeout(function(){sendWarenkorb(e.target)},1000);
			
		});		
}

function sendBoxWarenkorb(target){
	if(boxWarenkorbTimeOutID){
		clearTimeout(boxWarenkorbTimeOutID);
		boxWarenkorbTimeOutID = 0;
	}
	var form = $(target).parents("form");
	var data = form.serialize();
	if(parseInt($(target).val()) > parseInt($(target).attr("max")))return;
	$.ajax({
	  type: 'POST',
	  url: form.attr("action"),
	  data: data,
	  success: function(){
	  	$("#box_warenkorb").load("/box_warenkorb.php #box_warenkorb", data,function(){
	  		if($("#"+$(target).attr("id")).val() != $(target).val()){
	  			$("#"+$(target).attr("id")).val($(target).val());
	  			$("#"+$(target).attr("id")).parents("form").submit();
	  		}
	  		$("#"+$(target).attr("id")).caret($(target).caret().start,$(target).caret().start);
	  		if($("#warenkorb").length > 0)$("#warenkorb").load("/warenkorb.php?content=warenkorb #warenkorb_content");
	  	});
	  }
	});
	
}

function sendWarenkorb(target){
	if(warenkorbTimeOutID){
		clearTimeout(warenkorbTimeOutID);
		warenkorbTimeOutID = 0;
	}
	var form = $(target).parents("form");
	var data = form.serializeArray()

	console.log($(target).val());
	if(parseInt($(target).val()) > parseInt($(target).attr("max")))return;
	$("#warenkorb").load("/warenkorb.php?content=warenkorb #warenkorb_content", data,function(){
		$("#box_warenkorb").load("/box_warenkorb.php #box_warenkorb");
	  $("#"+$(target).attr("id")).caret($(target).caret().start,$(target).caret().start);	
	});	
}



function startTabMenue(){
//  tab menue______________________________________________________
  //Get all the LI from the #tabMenu UL
  $('#tabMenu > li').click(function(){
        
    //perform the actions when it's not selected
    if (!$(this).hasClass('selected')) {    

    //remove the selected class from all LI    
    $('#tabMenu > li').removeClass('selected');
    
    //After cleared all the LI, reassign the class to the selected tab
    $(this).addClass('selected');
    
    //Hide all the DIV in .boxBody
    $('.boxBody div').slideUp('1500');
    
    //Look for the right DIV index based on the Navigation UL index
    $('.boxBody div:eq(' + $('#tabMenu > li').index(this) + ')').slideDown('1500');
    
   }

  }).mouseover(function() {

    //Add and remove class, Personally I dont think this is the right way to do it, 
    //if you have better ideas to toggle it, please comment    
    $(this).addClass('mouseover');
    $(this).removeClass('mouseout');   
    
  }).mouseout(function() { 
    
    //Add and remove class
    $(this).addClass('mouseout');
    $(this).removeClass('mouseover');    
    
  });

  
  //Mouseover with animate Effect for Category menu list  :)
  $('.boxBody #category li').mouseover(function() {

    //Change background color and animate the padding
    $(this).css('backgroundColor','#888');
    $(this).children().animate({paddingLeft:"20px"}, {queue:false, duration:300});
  }).mouseout(function() {
    
    //Change background color and animate the padding
    $(this).css('backgroundColor','');
    $(this).children().animate({paddingLeft:"0"}, {queue:false, duration:300});
  });  
	
  //Mouseover effect for Posts, Comments, Famous Posts and Random Posts menu list.
  $('.boxBody li').click(function(){
    window.location = $(this).find("a").attr("href");
  }).mouseover(function() {
    $(this).css('backgroundColor','#888');
  }).mouseout(function() {
    $(this).css('backgroundColor','');
  });  	
//  tab menue ende_________________________________________________

}


$(document).ready(function(){
	$('.rollover').hoverswap({
});

	var imgloader = new Image();
  imgloader.src= "/images/loader.gif";
	activateFilterBox();
//  _______________________________________________________________


  startTabMenue();
  init();
});



$("#box_content").ajaxError(function(event, request, settings){
   $(this).append("<li>Error requesting page " + settings.url + "</li>");
 });
 

