$(function(){




// SMOOTH SCROLLING ------------------------------------------------------------------------------------------------------

    $('a[href*=#]').click(function() {
    
    if (location.pathname.replace(/^\//,'') == this.pathname.replace(/^\//,'') 
        && location.hostname == this.hostname) {
        
            var $target = $(this.hash);
            
            $target = $target.length && $target || $('[name=' + this.hash.slice(1) +']');
            
            if ($target.length) {
            
                var targetOffset = $target.offset().top - 40;
                
                $('html,body').animate({scrollTop: targetOffset}, 1500);
                    
                return false;
                
            }
            
        }
        
    });
	
	
	
	$(document).ready(function(){
		$("#agbs").click(function(event){
			event.preventDefault();
			$("#agbtext").slideToggle();
		});
		
// Merkzettel ------------------------------------------------------------------------------------------------------
			
			$('#merkzettel .items').slideToggle("fast");
			$('#merkzettel #btn_close').toggleClass("closed");
			$('#btn_close').click(function(){
				$('#merkzettel .items').slideToggle("fast");
				$('#merkzettel #btn_close').toggleClass("closed")
				return false;
			});
			
//scrollables	 ------------------------------------------------------------------------------------------------------				
			$(".scrollable").scrollable({items:".items"}).navigator({navi:".slider-prod-menu", naviItem: ".prod-menu-item", activeClass:"selected"});	 				
			$(".slider-prod-menu").scrollable({items:".prod-menu-items", next:".btn_forward", prev:".btn_back"});
			$("#slideshow-container").scrollable({items:".category-items", vertical:true}).navigator({navi:"#kategorien",naviItem: "li", activeClass:"selected"}); 	
			
// autoclear input fields ------------------------------------------------------------------------------------------------------
			$('.autoclear').autoclear();		
			

// Infos		-----------------------------------------------------------------------------------------------------	
			$('.moreinfo').click(function(){
				$(this).parents(".item").find('.infos').fadeIn(200);
				return false;
			});
			$('.infos .btn-close').click(function(){
				$(this).parents(".item").find('.infos').fadeOut(200);
				return false;
			});			

// Produkt-Varianten Bilder Vergrößern		 ------------------------------------------------------------------------------------------------------
			$(".prod_pic").click(function(){
				var maxheight = 290;
				var maxwidth = 420;
				if($(this).data("big") == "" || $(this).data("big") == undefined){ 	
					$(this).data("big","1");
					$(this).data("position" ,$(this).position());
					$(this).css({"position":"absolute","top":$(this).data("position").top,"left":$(this).data("position").left,"z-index":"2000"});
					var proportion = (maxwidth/$(this).width()) * $(this).height();	
					$(this).data("width",$(this).width());
					$(this).data("height",$(this).height());
					if( proportion > maxheight){
						var width = (maxheight / $(this).height()) * $(this).width();
						$(this).animate({"height":maxheight,"width":width,"top":"0px","left":"0px"},"fast");					
					}else{
						var height = (maxwidth / $(this).width()) * $(this).height();
						$(this).animate({"width":maxwidth,"height":height,"top":"0px","left":"0px"},"fast");
					}
				}
				else{
					$(this).data("big","");					
						$(this).animate({"height":$(this).data("height"),"width":$(this).data("width"),"top":$(this).data("position").top,"left":$(this).data("position").left},"fast",function(){					
							$(this).css({"position":"relative","z-index":"0","top":"0","left":"0"});	
						});	
				}											
			});

// zur Merkliste hinzufügen ------------------------------------------------------------------------------------------------------
			
			$(".btn_merkliste").click(function(){		
				if($('#merkzettel .items').css("display")=="none"){
					$('#merkzettel .items').slideToggle("fast");
					$('#merkzettel #btn_close').toggleClass("closed");
				}		
				/*var index = $(this).parents(".item").find('.prod_big');
				var accociate = $(this).parents(".kategorie").find(".prod-menu-item:eq("+index+")");
				accociate.css("border","1px solid #333");*/				
				var bigpic = $(this).parents(".item").find('.prod_big img');
				var product = $(this).parents(".item").find('h6').html();
				
				var item = $('<div class="item"></div>');
				var pic = item.append('<img src="'+bigpic.attr("src")+'" />');
				var pic = item.append('<div class="form"><div class="delete"></div><input name="product" type="hidden" value="'+product+'" /><input name="Sondergeschmack" type="checkbox" value="" /><label>Sondergeschmack</label><select name="Bedruckung" size="1"><option>- Bedruckung -</option><option>keine Bedruckung</option><option>nicht sicher</option><option>schwarz/wei&szlig;</option><option>1-3c</option><option>1-4c</option></select><input name="Lieferdatum" type="text" class="autoclear" value="Lieferdatum" /><input  class="autoclear"  name="Menge" type="text" value="Menge" /></div>');
				

				var top = bigpic.offset().top - $("#merkzettel").offset().top;
				var left = bigpic.offset().left - $("#merkzettel").offset().left;
				item.css({"display":"none","position":"relative"});				 
				var item2 = $("#merkzettel .items").append(item);	
				var totop = item2.offset().top - $("#merkzettel").offset().top;
				var toleft = item2.offset().left- $("#merkzettel").offset().left;
				
				item.css({"display":"block","position":"absolute","top":top,"left":left});
				item.find("img").css({"height":"340px"});
				
				item.find("img").animate({"height":"130px"},"slow");
				item.animate({"top":totop,"left":toleft},"slow",function(){
					item.css({"position":"relative","top":"0","left":"0"});		
					checkCount();	
				});
				
				item.find('.autoclear').autoclear();
				$('.item .form .delete').click(function(){
					$(this).parent().parent().remove();
					checkCount();
				});
				
			});
			
		});    

//  Send Mail --------------------------------


		$("#btn_anfrage2").click(function(){
			var valid = '';
			var isr = ' wird ben&ouml;tigt.';
			var name = $("[name=name]").val();
			var mail = $("[name=mail]").val();
			var telefon = $("[name=telefon]").val();
			var text = $("[name=text]").val();
			var uv = $('[name=uv]').attr('checked') == true ? "ja" : "nein";
			
			var items = $('<div></div>');
			$('#merkzettel .items .item').each(function(index){
				var prod_name = $(this).find('[name=product]').val();
				var prod_geschmack = $(this).find('[name=Sondergeschmack]').attr('checked') == true ? "ja" : "nein";
				var prod_bedruckung = $(this).find('[name=Bedruckung]').val();
				var prod_lieferdatum = $(this).find('[name=Lieferdatum]').val();
				var prod_menge = $(this).find('[name=Menge]').val();
				var pic = $(this).find('img').attr("src");
				items.append('Produkt: ' + prod_name + ',,Sondergeschmack: ' + prod_geschmack + ',,Bedruckung: ' + prod_bedruckung + ',,Lieferdatum: ' +  prod_lieferdatum + ',,Menge: ' + prod_menge + '||'+ pic +';;');
			});
			produkte = items.html();
			var valid_name=true;
			var valid_tel=true;
			var valid_mail=true;
			if (name.length<1 || name == "Name / Firma") {
				valid_name=false;
			};
			if (!mail.match(/^([a-z0-9._-]+@[a-z0-9._-]+\.[a-z]{2,4}$)/i)) {
				valid_mail=false;
			};
			if (telefon.length<1 || telefon == "Telefon") {
				valid_tel=false;
			};			
			
			if(!valid_name) $('[name=name]').animate({'borderLeftWidth':'4px','width':'253px','backgroundPosition': '-4px'},"slow")
			else $('[name=name]').animate({'borderLeftWidth':'0px','width':'257px','backgroundPosition': '0px'},"slow");			
			if(!valid_tel) $('[name=telefon]').animate({'borderLeftWidth':'4px','width':'253px','backgroundPosition': '-4px'},"slow")
			else $('[name=telefon]').animate({'borderLeftWidth':'0px','width':'257px','backgroundPosition': '0px'},"slow");			
			if(!valid_mail) $('[name=mail]').animate({'borderLeftWidth':'4px','width':'253px','backgroundPosition': '-4px'},"slow")
			else $('[name=mail]').animate({'borderLeftWidth':'0px','width':'257px','backgroundPosition': '0px'},"slow");	
		
			if(valid_tel && valid_mail && valid_name){
				var datastr ='name=' + name + '&mail=' + mail + '&telefon=' + telefon + '&text=' + text + '&uv=' + uv + "&items=" + produkte;
				$("#btn_anfrage2").css({"background-position":"0px -99px","cursor":"default"});
				setTimeout("send('"+datastr+"')",2000);
			}else {	
			}
		
			return false;
		});    
});
		
		function checkCount(){
			var count = $('#items .item').size();
			if(count<=0){ 
				count = "Es befinden sich zur Zeit keine Produkte auf der Merkliste";
				$('#merkzettel .items').slideUp("fast");
				$('#merkzettel #btn_close').attr("class","closed");
			}
			if(count==1) count = "Es befindet sich zur Zeit 1 Produkt auf der Merkliste";
			if(count>=1) count = "Es befinden sich zur Zeit "+count+" Produkte auf der Merkliste";
			$('#status').html(count);
		}
		function send(datastr){
				$.ajax({
				type: "POST",
				url: "mail.php",
				data: datastr,
				cache: false,
	 			success: function(html){
					$("#btn_anfrage2").css("background-position","0px -132px");
					setTimeout('$("#btn_anfrage2").css({"background-position":"","cursor":"pointer"})',2000);
				}
				});
		}
