//$ = jQuery
(function ($) {

	pathUrl = "http://www.dallia.ch";

	function getUrlParam(name) {
		  var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
		  var regexS = "[\\?&]"+name+"=([^&#]*)";
		  var regex = new RegExp(regexS);
		  var results = regex.exec(window.location.href);
		  if (results == null)
		    return "";
		  else
		    return results[1];
	};
	
	function addToCart(productId, realProductId) {
		var params = {product_id: productId,real_product_id: realProductId, quantity: $('input[name=quantity]').attr('value'), redirect: $('input[name=redirect]').attr('value')};
		$('option:selected').each( function(i, e) {
			$(params).attr($(e).parent().attr('name'), parseInt($(e).attr('value'), 10) + $('select').children().length * (productId - realProductId));
		});
		$.ajax({
			url:	'index.php?route=module/cart/callback',
			type:	'POST',
			//contentType:	'multipart/form-data',
			data:	params,
			success: function (html) {
				$('#module_cart .middle').html(html);
			},
			complete: function () {
				var image = $('#image').offset();
				var cart  = $('#module_cart').offset();

				$('#image').before('<img src="' + $('#image').attr('src') + '" id="temp" style="position: absolute; top: ' + image.top + 'px; left: ' + image.left + 'px;" />');

				params = {
					top : cart.top + 'px',
					left : cart.left + 'px',
					opacity : 0.0,
					width : $('#module_cart').width(),
					height : $('#module_cart').height()
				};

				$('#temp').animate(params, 'slow', false, function () {
					$('#temp').remove();
				});
			}
		});
	};


	$(document).ready(function() {
		//various template functions

		//$('#add_to_cart').removeAttr('onclick');
		
		$('#footer .div6 a').click(function (e) {
			e.preventDefault;
			$('html, body').animate({
				scrollTop: '0'
				}, 1000
			);
		});

		$('#add_to_cart').click(function (e) {
			addToCart($('input[name=product_id]').attr('value'),$('input[name=product_id]').attr('value'));
			e.preventDefault();
		});

		$('#add_sample_to_cart').click( function() {
			$('#add_sample_to_cart').attr("disabled","disabled");
			addToCart(parseInt($('input[name=product_id]').attr('value'), 10) + 1,$('input[name=product_id]').attr('value'));
		});
		
		
		$('#searchSubmit').click( function() {
			e = jQuery.Event ('keydown'); 
     		e.keyCode = 13;
      		//then trigger an event
      		if($('#search input').attr('value')!="" && $('#search input').attr('value')!="Recherche :" && $('#search input').attr('value')!="Suche:")
     			$('#search input').trigger(e); 
		});
		
		$('#filter_keyword').click( function() {
			if($('#filter_keyword').attr('value')=="" || $('#filter_keyword').attr('value')=="Recherche :" || $('#filter_keyword').attr('value')=="Suche:")
				$('#filter_keyword').attr('value','');
		});
		
		$('#filter_keyword').keydown(function(e) {
			$('#filter_keyword').css("color",'#000000');
		});


		$('#filter_keyword').keydown(function(e) {
			if (e.keyCode == 13) {
				if($('#filter_keyword').attr('value')!="" && $('#filter_keyword').attr('value')!="Recherche :" && $('#filter_keyword').attr('value')!="Suche:"){
					url = pathUrl+'/recherche';
	
					var filter_keyword = $('#filter_keyword').attr('value')
	
					if (filter_keyword) {
						url += '&keyword=' + encodeURIComponent(filter_keyword);
					}
	
					var filter_category_id = $('#category_id').attr('value');
	
					if (filter_category_id) {
						url += '&category_id=' + filter_category_id;
					}
	
					location = url;
				}
			}
		});

		$('.cartRemove').live('click', function () {
			$(this).removeClass('cartRemove').addClass('cart_remove_loading');
			$.ajax({
				type: 'post',
				url: 'index.php?route=module/cart/callback',
				dataType: 'html',
				data: 'remove=' + this.id,
				success: function (html) {
					$('#module_cart .middle').html(html);
					if (getUrlParam('route').indexOf('checkout') != -1) {
						window.location.reload();
					}
				}
			});
		});

		$('a.confirm.size').click( function() {
			var message = 'Veuillez vérifier que vous avez bien commandé des tailles Suisse\r\n\r\nVoulez-vous continuer ?';
			if ($('body.de').length != 0)
				message = ' Bitte überprüfen Sie, dass sie Schweizerische Grösse bestellt\r\n\r\nWeiter?';
			return confirm(message);
		});

		//rooter > Checking on which page we are
		//rooter > Homepage
    	if($("body.common__home").length!=0) {
    		 $('#slider').nivoSlider({
		        effect:'fade', // Specify sets like: 'fold,fade,sliceDown'
		        pauseTime:5000, // How long each slide will show
		        directionNav:false, // Next & Prev navigation
		        controlNav:false, // 1,2,3... navigation
		        keyboardNav:false, // Use left & right arrows
		        manualAdvance:false, // Force manual transitions
		        captionOpacity:1, // Universal caption opacity
		        beforeChange: function(){}, // Triggers before a slide transition
		        afterChange: function(){}, // Triggers after a slide transition
		        slideshowEnd: function(){}, // Triggers after all slides have been shown
		        lastSlide: function(){}, // Triggers when last slide is shown
		        afterLoad: function(){} // Triggers when slider has loaded
		    });
    	}
    	else if($("body.checkout__cart").length!=0) {
    		$('.cartTable .cartRemoveBut').each(function() {
	   			$(this).click(function () {
					$(this).removeClass('cartRemoveBut').addClass('cart_remove_loading');
					$(this).siblings('input[type=checkbox]').attr('checked','checked');
					$('#cartSubmit').trigger('click');
				});
			});
			$('#cartCheckout').click( function() {
				var message = 'Veuillez vérifier que vous avez bien commandé des tailles Suisse\r\n\r\nVoulez-vous continuer ?';
				if ($('body.de').length != 0)
					message = ' Bitte überprüfen Sie, dass sie Schweizerische Grösse bestellt\r\n\r\nWeiter?';
				if(confirm(message))
	      			window.location=$('#cartCheckout').attr("rel");	     							
			});
       	}
    	else if($("body.product__product").length!=0) {
    		$('.jqzoom').jqzoom({
    			zoomType: 'standard',
    			lens:true,
    			preloadImages: false,
    			alwaysOn:false,
    			title:false,
    			xOffset:40,
    			zoomHeight: 305,
    			zoomWidth: 369
    		});
    	}
    	else if($("body.checkout__shipping").length!=0) {
    		$('#changeAddress').click( function() {
	      		window.location=$('#changeAddress').attr("rel");     							
			});
			$('#shippingBack').click( function() {
	      		window.location=$('#shippingBack').attr("rel");     							
			});
    	}
    	else if($("body.checkout__payment").length!=0) {
    		$('#changeAddress').click( function() {
	      		window.location=$('#changeAddress').attr("rel");     							
			});
			$('#paymentButton').click( function() {
	      		window.location=$('#paymentButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.checkout__confirm").length!=0) {
    		$('#confirmBack').click( function() {
	      		window.location=$('#confirmBack').attr("rel");     							
			});
			$('#paymentButton').click( function() {
	      		window.location=$('#paymentButton').attr("rel"); return false;	     							
			});
			$('#checkoutPostFinance').click( function() {
	      		$.ajax({
					type: 'GET',
					url: 'index.php?route=payment/postfinance/confirm',
					success: function() {
						$('#checkout-form').submit();
					}
				});
				return false;
			});
    	}
    	else if($("body.checkout__success").length!=0) {
    		$('#successButton').click( function() {
	      		window.location=$('#successButton').attr("rel");     							
			});
    	}
    	else if($("body.account__logout").length!=0) {
    		$('#successButton').click( function() {
	      		window.location=$('#successButton').attr("rel");     							
			});
    	}
    	else if($("body.account__forgotten").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__edit").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__address__insert").length!=0 || $("body.account__address__update").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__address").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
			$('#activeButton').click( function() {
	      		window.location=$('#activeButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__newsletter").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__history").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.account__invoice").length!=0) {
    		$('#backButton').click( function() {
	      		window.location=$('#backButton').attr("rel"); return false;	     							
			});
    	}
    	else if($("body.product__search").length!=0) {
			
			$('#contentSearch button').click( function() {
	      		if($('#keyword').attr('value')!="" && $('#keyword').attr('value')!="Recherche :" && $('#keyword').attr('value')!="Suche:")
	      			{
	      			$('#search input').attr('value',$('#keyword').attr('value'));
	      			e = jQuery.Event ('keydown'); 
	     			e.keyCode = 13;
	     			$('#search input').trigger(e);
	      			}
	     							
				});
			
			$('#keyword').click( function() {
				if($('#keyword').attr('value')=="" || $('#keyword').attr('value')=="Recherche :" || $('#keyword').attr('value')=="Suche:")
					$('#keyword').attr('value','');
			});
			
			$('#keyword').keydown(function(e) {
				$('#keyword').css("color",'#000000');
			});
	
	
			$('#keyword').keydown(function(e) {
				if($('#search input').attr('value')!=""){
					if (e.keyCode == 13) {
						$('#search input').attr('value',$('#keyword').attr('value'));
						e = jQuery.Event ('keydown'); 
		     			e.keyCode = 13;
		      			$('#search input').trigger(e);
					}
				}
			});
    	}
	});

})(jQuery);
