// JavaScript Document

function haeckchen(){

var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
var imageheight = 150;
var imagewidth = 345;
var positionX = ((width / 2) - (imagewidth / 2));
var positionY = ((height / 2) - (imageheight / 2));
var id = "#haeckchen";
$("#haeckchen").clone(true).insertAfter("#haeckchen").css("left",positionX).css("top", positionY).fadeIn(200, function(){$(this).fadeOut(2600, function(){$(this).css("left", 9999);});});
}

function falsch(){

var width = window.innerWidth || (window.document.documentElement.clientWidth || window.document.body.clientWidth);
var height = window.innerHeight || (window.document.documentElement.clientHeight || window.document.body.clientHeight);
var imageheight = 122;
var imagewidth = 311;
var positionX = ((width / 2) - (imagewidth / 2));
var positionY = ((height / 2) - (imageheight / 2));
var id = "#haeckchen";
$("#falsch").clone(true).insertAfter("#falsch").css("left",positionX).css("top", positionY).fadeIn(200, function(){$(this).animate({opacity: 1.0}, 1000,function(){$(this).fadeOut(600, function(){$(this).css("left", 9999);});});});
}

function basket_overlay(){
$("#sidebar_basket_overlay").clone(true).insertAfter("#sidebar_basket_overlay").css("left",-15).css("top", -20).fadeIn(200, function(){$(this).fadeOut(2600, function(){$(this).css("left", 9999);});});
}



function ajaxquery (name,variation,anzahlvariationen){
	
	//Variablendeklaration
	 var selector = 'form[name='+name+']';
	 var hiddenInputs = 'form[name='+name+'] input:hidden';
	 var allradios = 'form[name='+name+'] input:radio:checked';
	 var allcheckboxes = 'form[name='+name+'] input:checkbox';
	 var alltext = 'form[name='+name+'] input:text';
	 var allselect = 'form[name='+name+'] select option:selected';
	 var anzahl = 'form[name='+name+'] input[name=anzahl]';
	 var string='';
	 var index = 0;
	 
	 $(hiddenInputs).each(
		function(i){

			string += $(this).attr('name');
			string += '=';
			string += $(this).val();
			string += '&';

	 });
	 
	 $(allradios).each(
		function(i){
			index++;
			string += $(this).attr('name');
			string += '=';
			string += $(this).val();
			string += '&';

	 });
	 
	 $(allcheckboxes).each(
		function(i){
			
			index++;
			string += $(this).attr('name');
			string += '=';
			string += $(this).val();
			string += '&';

	 });
	 
	 $(allselect).each(
		function(i){
			
			if($(this).val() != 0){
			  index++;
			  string += $(this).attr('name');
			  string += '=';
			  string += $(this).val();
			  string += '&';
			}

	 });
	 
	  $(alltext).each(
		function(i){
			if($(this).val() != "" && $(this).attr('name') != "anzahl"){
			  index++;
			  string += $(this).attr('name');
			  string += '=';
			  string += $(this).val();
			  string += '&';
			}

	 });
	 
	 string += $(anzahl).attr('name');
	 string += '=';
	 string += $(anzahl).val();
	 string += '&';
	 string += index;
	 
	 //Überprüfung ob Artikelvariation vorhanden
	 
	 //Artikelvariation nicht vorhanden
	 if(variation=='0'){
	 
	 haeckchen();
	 
	  $.ajax({
		  type: "POST",
		  url: "warenkorb_load.php",
		  data: (string),
		  dataType: "html",
		  success: function(html){
						$("#sidebar_basket_box").replaceWith(html);
						 basket_overlay()
		  			}
		});

	 }
	
	//Artikelvariation vorhanden
	else{

		//Überprüfen on eine Variation angewählt
		if(anzahlvariationen == index){
			
		   haeckchen();
		   
			$.ajax({
				type: "POST",
				url: "warenkorb_load.php",
				data: (string),
				dataType: "html",
				 success: function(html){
						$("#sidebar_basket_box").replaceWith(html);
						basket_overlay()
		  			}
			  });
		}
		else{
			falsch();
		}
	 }

}

$(document).ready(function(){
	//eraes the submitbutton if js is on
	$('.absendenButton').hide();	
	$('.absendenButtonArtikel').hide();	
	$('.inputButtonHide').hide();
});

