/**
 * Funktionen der Originalsprache werden überschrieben
 */
function setConfig() {
	var	messages = [];
	// de
	messages[0] = {
		note: 'Hinweis',
		quantity: 'Bitte achten Sie auf die St&uuml;ckzahlen. Sie kaufen nicht automatisch einen kompletten Radsatz!',
		country: 'Bitte Land w&auml;hlen',
		deliveryCountry: 'Momentan versenden wir nur nach Deutschland.',
		image_outside: 'Vorderansicht der Fuchsfelge',
		image_inside: 'Hinteransicht der Fuchsfelge',		
		image_car: 'Fuchsfelgen am Fahrzeug'
				
	};
	// en
	messages[1] = {
		note: 'Please note',
		quantity: 'Please check your quantity. You do not automatically order a complete set of wheels.',
		country: 'Select your country',
		deliveryCountry: 'Currently, we are only delivering to Germany.',
		image_outside: 'Front view of the Fuchsfelge',
		image_inside: 'Rear view of the Fuchsfelge',		
		image_car: 'Fuchsfelgen on Porsche'		
	};
	msg = messages[conf.lang];
}

	function jqZoom() {
		var options = {};
		$('.jqZoom').jqzoom(options);
		$('.jqZoom:last').css('margin-right','0px');
		
		// Bildunterschriften
		$('div.com-product-images a.jqZoom').each(function() {
			var path = $(this).attr('href');
			/*var filename = path.replace('uploads/tx_commerce/','');
			filename = filename.replace('_',' ');
			filename = filename.replace('.jpg','');		*/	
			var Ausdruck = /Vorderansicht/;
			Ausdruck.test(path);
			
			if(path.search(/Vorderansicht/) != -1) {
				var filename = msg.image_outside;	
			} else if(path.search(/Hinteransicht/) != -1) {
				var filename = msg.image_inside;	
			} else if(path.search(/Fahrzeug/) != -1) {
				var filename = msg.image_car;	
			}
			/*
			var filename = RegExp.$1;
			filename = filename.replace(/_/g,' '); */
			var newCaption = $('<p class="small"/>').html(filename);
			$('div.com-product-images div.clear').before(newCaption);
		});
		$('div.com-product-images p.small:last').css('margin-right','0px');
		$('div.com-product-images p.small:first').css('clear','left');		
	}

	// Shopfunktionen
	var alertClosed = false;
	function displayAmountWarn() {
		
		$('form.com-article-basket').submit(function(){
			var result = true;
			$(this).find('.com-input-qty[value!=""]').each(function(){
				if($(this).val() % 2 != 0) {
					result = false;	
				}													
			});										 
			if(!result && !alertClosed) {
				displayAlert(msg.quantity,
							 function(){
								 alertClosed = true;
		   					     $('form.com-article-basket').submit();
							 });
				return false;
			}
			return true;
		});
	}

	function checkFahrzeugTyp() {

		$('#fahrzeugtyp').change(function(){
			var str = $('#fahrzeugtyp option:selected').attr('title');
			if(str != '') {
				displayAlert(str,function() {$('#alert').hide(); $('form.com-category-form').submit();});
			}
		});
	}

	function displayAlert(message, callback) {
		$.modal("<div id='alert'><h1>"+ msg.note +"</h1><p>"+message+"</p></div>", {onClose: callback});
	}

	function checkCountryChosen() {
		$('p#checkout_link a').click(function(){
			var chosen = ($('select[name="tx_commerce_pi1[delArt]"]').val() != '90');
			if(!chosen) {
				displayAlert(msg.country);	
			}
			return chosen;
		});
	}

	function checkCountryDEU() {
		if($('select#billing-country').length > 0) {
			var form = $('select#billing-country').parents('form');
			form.submit(
				function() { 
					if($('select#billing-country').val() != 'DEU') {
						displayAlert('Momentan versenden wir nur nach Deutschland.');
						return false;
					} else
						return true;
				});	
		}
		if($('select#delivery-country').length > 0) {
			var form = $('select#delivery-country').parents('form');
			form.submit(
				function() { 
					if($('select#delivery-country').val() != 'DEU') {
						displayAlert('Momentan versenden wir nur nach Deutschland.');
						return false;
					} else
						return true;
				});	
		}	
	}

	function init() {
		setConfig();
		jqZoom();
		displayAmountWarn();
		checkFahrzeugTyp();
		checkCountryChosen();
		$('#searchword').placeholder();
		// vorübergehend
		checkCountryDEU();
	}

	$(init);
	
	$.fn.placeholder = function() {
	    return this.each(function() {
	        var oldval = $(this).val();
	        
	        $(this).focus(function() {
	            if($(this).val() == oldval) {
	                $(this).val('');
	            }
	        });
	        $(this).blur(function() {
	            if($(this).val() == '') {
	                $(this).val(oldval);
	            }
	        });        
	    });
	}
