// JavaScript Document

$.fn.image = function(src, f){
    return this.each(function(){
      $("<img />").appendTo(this).each(function(){
         this.src = src;
         this.onload = f;
      });
    });
}

$(document).ready(function(){
	var old_input_val = '';
	
	$("input[type=text], input[type=password], textarea").css({
		border: '1px dotted #BE9E56',
		background: 'url(templates/dwersteg/images/suchebg.jpg) repeat-x'
	});
	
	$("input[type=text], input[type=password]").focus(function(){
		old_input_val = $(this).val();							  
		$(this).val('');
	});
	$("input[type=text], input[type=password]").blur(function(){
		if ($(this).val()==''){
			$(this).val(old_input_val);
		}
	});
	
	// Quicksearch
	
	$("#search-box .close-button").click(function(){
		$("#search-box").fadeOut(100);											  
	});
		
	$("#search-box").hover(function(){}, function(){
		$(this).fadeOut(100);											  
	});
						   
	$("#magnifying-glass").hover(function(){
		$("#search-box").fadeIn(500);										  
	},function(){
		
	});
	
	// Account
	
	$("#account-box").hover(function(){}, function(){
		$(this).fadeOut(100);											  
	});
						   
	$("#login").click(function(){
		$("#account-box").fadeIn(500);										  
	},function(){
		
	});
	
	// Replace Header
	
	$(".render-text").each(function(){
		var text = $(this).html();
		var max_width = $(this).parent().width();
		var font_size = $(this).css('font-size');
		var image = 'templates/dwersteg/scripts/imgheader.php?msg='+text+'&size='+font_size+'&width='+max_width+'&font=fonts/Amplifip.ttf&pad=5';
		if (jQuery.browser.msie && jQuery.browser.version < 7) {
			$(this).html('<img src="'+image+'" alt="'+text+'" />');
			$(this).find('img').attr({
				src: 'templates/dwersteg/blank.gif'						 
			}).css({
				filter: 'progid:DXImageTransform.Microsoft.AlphaImageLoader(src="'+image+'")'		
			});
		} else {
			$(this).html('<img src="'+image+'" alt="'+text+'" />');
		}
	});
	
	$(".description .short-desc").each(function(){
		if ($(this).html().length>160) {
			$(this).html($(this).html().substring(0, 160)+'[..]');
		}
	});
	
	$('.item-box .image .cart-image').hover(function(){
		$(this).next('.cart-image-popup').fadeIn(250);					  
	},function(){
		$(this).next('.cart-image-popup').fadeOut(250);		
	});
	
	// Center Product-Image
	if ($("#product-info").length>0){
		var image_height = $("#product-info #image-1 img").height();
		var image_width = $("#product-info #image-1 img").width();
		var image_container_height = $("#product-info #product-content").height();
		var image_center_y = (image_container_height-image_height)/2;
		
		if (image_height<image_container_height && image_width==200) {
			$("#product-info #image-1 img").css({marginTop: image_center_y});
		}
	}
    
    // Literpreis
    if ($("#buy-box").length>0){
        var price = $(this).find('.price').html();
        var re = new RegExp('[0-9]+,[0-9]{1,2}');
        var m = re.exec(price);
        
        if (m!=null) {
            price = m+'';
            price = parseFloat(price.replace(',', '.'));
                 
            //alert(price);
                    
            var liter = $('.description').html();
            re = RegExp('([0-9]{1,2},[0-9]{1,2}).*l');
            m = re.exec(liter);
                        
            if (m!=null){
                liter = m[1]+'';
                liter = parseFloat(liter.replace(',', '.'));
                                
                //alert(liter);
                
                price = price * ( 1 / liter);
                price = Math.round(price * 100) / 100;
                price = price+'';
                price = price.replace('.', ',');
                test = price.split(',');
                
                var additional_null = '';
                if (test[1].length<2) {
                    additional_null = '0'
                }
                
                $(this).find('.price').after('<div class="price" style="font-size:8px;margin:5px 0px;padding:0px;line-height:10px;"><span class="indent">&euro;/l</span>'+price+additional_null+' &euro;</div>')
            }
        }
    }
	
	// Checkout
	
	$(".method-input-fields").hide();
	$(".payment-module").each(function(){
		$(this).find("input[type=radio]:first").click(function(){
			var check_content = $(this).closest(".payment-module").find(".method-input-fields").html();
			check_content = check_content.replace(" ", "");
			
			$(".payment-module .open").slideUp(500, function(){
				$(this).removeClass('open');															 
			});	
				
			if (check_content.length>50){
				$(this).closest(".payment-module").find(".method-input-fields").slideDown(500, function(){
					$(this).addClass('open');																						
				});		
			}
		});								   
	});
	
	$(".checkout-box .agb").hide();
	$(".checkout-box .agb").prev(".title").find("a").click(function(){
		if ($(".checkout-box .agb").hasClass("open")) {
			$(".checkout-box .agb").slideUp(500, function(){
				$(this).removeClass("open");											  
			});	
		} else {
			$(".checkout-box .agb").slideDown(500, function(){
				$(this).addClass("open");												
			});	
		}
		
		return false;																
	});
	
	var check_age = '';
	$.post("templates/dwersteg/scripts/ajax.php", { action: "getOldEnough" }, function(result){
		check_age = result;	
		
		if (check_age!='true') {
			$("#mature-overlay").fadeIn(250);
			$("#mature-background").css({
				opacity: 0.75							
			});
			$("#submit-age").submit(function(){
				var day = $(this).find("input[name=day]").val();
				var month = $(this).find("input[name=month]").val();
				var year = $(this).find("input[name=year]").val();
				var currentTime = new Date()
				var currentYear = currentTime.getFullYear();
				
				if (day!="" && month!="" && year!="") {
				
					day = parseInt(day);
					month = parseInt(month);
					year = parseInt(year);
					
					if (year<(currentYear-18)) {
						$.post("templates/dwersteg/scripts/ajax.php", { action: "setOldEnough" });
						$("#mature-overlay").fadeOut(1000, function(){
							$(this).remove();										   
						});
					} else {
							
					}
				
				}
				
				return false;								 
			});	
		}
	});
	
	function contactform()
	{
		$("#kontaktformular .contact_text, #kontaktformular .contact_textarea").focus(function(){
			$(this).addClass('focus');
		});	
		
		$("#kontaktformular .contact_text, #kontaktformular .contact_textarea").blur(function(){
			$(this).removeClass('focus');
		});	
		
		var language = $("#kontaktformular label:first").html();
		
		if (language=='Vorname:<em>*</em>') {
			var mailformplus = $("#kontaktformular").validate({
				rules: {
					Vorname: "required",
					Nachname: "required",
					"E-Mail-Adresse": "required email",
					Nachricht: "required",
					"Telefon-Nr": "number"
				},
				messages: {
					Vorname: "Bitte geben Sie Ihren Vornamen ein.",
					Nachname: "Bitte geben Sie Ihren Nachnamen ein.",
					"E-Mail-Adresse": {
						required: "Bitte geben Sie Ihre E-Mail-Adresse ein.",
						email: "Ihre Eingabe muss vom Format name@meinedomain.de sein."
					},
					Nachricht: "Bitte geben Sie eine Nachricht ein.",
					"Telefon-Nr": {
						number: "Sie d&uuml;rfen nur Zahlen eingeben."	
					}
				},
				//errorLabelContainer: "#messageBox",
				//wrapper: "li",
				errorPlacement: function(error, element) {
					error.appendTo(element.parent("td").find(".messageBox"));
				},
				errorClass: "invalid"
			});
		} else {
			var mailformplus = $("#kontaktformular").validate({
			rules: {
				Vorname: "required",
				Nachname: "required",
				"E-Mail-Adresse": "required email",
				Nachricht: "required",
				"Telefon-Nr": "number"
			},
			messages: {
				Vorname: "Please enter your first name.",
				Nachname: "Please enter your surname.",
				"E-Mail-Adresse": {
					required: "Please enter your E-Mail-Address.",
					email: "Your input must be like yourname@yourdomain.com."
				},
				Nachricht: "Please enter a message.",
				"Telefon-Nr": {
					number: "You are only allowed to enter numbers."	
				}
			},
			//errorLabelContainer: "#messageBox",
			//wrapper: "li",
			errorPlacement: function(error, element) {
				error.appendTo(element.parent("td").find(".messageBox"));
			},
			errorClass: "invalid"
		});	
		}
	}
	
	contactform();
});