var _lang;
var _country;


$(document).ready(function(){  
  
	var select_language_mouseover = false;
	var t;
	
	_lang   = $('#metadata-lang').text();
  _country = $('#metadata-country').text();  
	
	show_cart_header();
	set_product_hover();
	
	$('#i_newsletter').focus(function() {
			$(this).val('');							  
		});
	
	$('#selected-country').hover(function() {				
				clearTimeout(t);	
				$(this).siblings('ul').fadeIn();																				
			}, function() {
					t = setTimeout(function() { $('#country-selection-container ul').fadeOut(); },600);	
				
			});

	$('#country-selection-container ul').hover(function() {
			 clearTimeout(t);																	
			}, function() {
				t = setTimeout(function() { $('#country-selection-container ul').fadeOut(); },600);	
			});
			
	$('.toggle-submenu').click(function(){ 
    var id = $(this).attr('id');
	    
    if ( $('#submenu'+id).is(':visible') ) {
		$(this).removeClass('selected');
    	$('#submenu'+id).fadeOut('fast');
    }
    else {		
		$(this).addClass('selected');
    	$('#submenu'+id).fadeIn('fast');
    }
    return false;
   }); 	
   
   $('#home-slideshow').innerfade({ speed: 'slow', timeout: 12000, type: 'sequence', containerheight: '414px' }); 	
	
	
	/* DISCOUNT CODE EVENT */
	
	var timeout;
	$('#discount-code').focus(function() {
							$(this).val(''); 
							$('#discode-notice').fadeOut("normal");
						}).keyup(function() {
							
							// Clear timeout if exists - the user is still typing
							if (timeout) clearTimeout(timeout);
							$('#discode-notice').fadeOut("normal");
							
							// Set the regular expr
							var rex = /[^A-Z0-9]/g;
							
							// Convert all chars to uppercase
							var val = $(this).val().toUpperCase();			
							
							// Remove any not valid characters
							val = val.replace(rex,'');
							$(this).val(val);
							
							// If the discount code length is > 4
							if ($(this).val().length > 4) {
								
								// Set a reasonable timeout (1000) to prevent ajax requests when the user is typing the password
								timeout = setTimeout(function(){ 
															  
									// Send a get request with the discount code															  
									$.getJSON(URL_PATH + "/json/check-discount/", { discount_code: val },  function(json) {
																								  
										// If the discount code is valid
										if (json.valid) {
											$('#discount-perc').empty().text(json.perc + '%');
											$('#discount-input').fadeOut("normal",function(){
												$('#discount-info').fadeIn("normal");												   
											});	
											
											// Set the discount and update the products											
																					
											if (!$('#metadata-cid').text()) {
												window.location.reload();	
											}
											else {
												$.getJSON(URL_PATH + "/json/products/", { order: $('#metadata-orderby').text(), limit: $('#metadata-limit').text(), offset: $('#metadata-offset').text(), cid: $('#metadata-cid').text(), view: $('#metadata-view').text(), ts: Date.parse(new Date()) }, function (json) {																				
													p_json = json;												
													show_products();
												});
											}
											
										}
										else {											
											$('#discode-notice').fadeIn("normal");											
										}
										
									});
								}, 700);			
							}
							
						});
	
	$('#discount-disable').click(function() {
						
						$.get(URL_PATH + "/json/disable-discount/", { dd: 1 }, function() {
							
							$('#discount-code').val('');
							$('#discount-info').fadeOut("normal",function(){
												$('#discount-input').fadeIn("normal");												   
											});	
						
							if (!$('#metadata-cid').text()) {
								window.location.reload();	
							}
							else {
								$.getJSON(URL_PATH + "/json/products/", { order: $('#metadata-orderby').text(), limit: $('#metadata-limit').text(), offset: $('#metadata-offset').text(), cid: $('#metadata-cid').text(), view: $('#metadata-view').text(), ts: Date.parse(new Date()) }, function (json) {																				
									p_json = json;												
									show_products();
								});
							}
																						
						});			  
																	
	
						return false;			   
						
						});
	
    
});


set_product_hover = function() {
	$('.product').hover(function() {
				$(this).css('cursor','pointer');
				$(this).find('.prod-descr').css('backgroundColor','#181c1e');
						 
			}, function() {
				$(this).css('cursor','auto');
				$(this).find('.prod-descr').css('backgroundColor','#2a3135');	
				
			});	
};

show_cart_header = function() {
	   $.getJSON(URL_PATH + "/json/cart/", { ts: Date.parse(new Date()) }, function (json) {			
			if (json) {
				$('#mill-cart').empty();
				
				$('#mill-cart').createAppend(							
							'ul', { id: 'mill-cart-ul' }, [ ]
							);															   
				
				$.each(json, function(i, n) {                
					$('#mill-cart-ul').createAppend(							
							'li', { className: 'cart-item' }, [
							'img', { src: URL_PATH + '/product_images/1/' + n.img + '.jpg', width: '55', height: '69', alt: n.name + ' | ' + n.size, title: n.name + ' | ' + n.size }, , 
							'br', , , 'p', , n.qty + ' x ' + format_price(n.price,true)						
							]
						);				  
				});
				
				$('.cart-item').css('cursor','pointer').click(function() { document.location.href = URL_PATH + '/' + _country + '/' + _lang +'/cart/' } ).hover(function() { $(this).children('img').css('border-color','#ffc935') }, function() { $(this).children('img').css('border-color','#E2E2E2') } );									
				$('#mill-cart-ul').jcarousel();						
			}
			else {
				$('#mill-cart').empty();	
			}
		
	});		
}



format_price = function(price, big_decimals) {
	var minus_sign =''; 
	if (price < 0) {
		minus_sign = '- ';
		price = Math.abs(price);
	}
	
	price = number_format(price); 
		
	return ( minus_sign + '€' + price );
	
	
};


/* Made by Mathias Bynens <http://mathiasbynens.be/> */
function number_format(a, b, c, d) {
 b = (b) ? b : 2;
 c = (c) ? c : ',';
 d = (d) ? d : '.';
 
 a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
 e = a + '';
 f = e.split('.');
 if (!f[0]) {
  f[0] = '0';
 }
 if (!f[1]) {
  f[1] = '';
 }
 if (f[1].length < b) {
  g = f[1];
  for (i=f[1].length + 1; i <= b; i++) {
   g += '0';
  }
  f[1] = g;
 }
 if(d != '' && f[0].length > 3) {
  h = f[0];
  f[0] = '';
  for(j = 3; j < h.length; j+=3) {
   i = h.slice(h.length - j, h.length - j + 3);
   f[0] = d + i +  f[0] + '';
  }
  j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
  f[0] = j + f[0];
 }
 c = (b <= 0) ? '' : c;
 return f[0] + c + f[1];
};


define = (function(){
	function toString(name, value){
		return	"const " + name + "=" + (
			/^(null|true|false|(\+|\-)?\d+(\.\d+)?)$/.test(value = String(value)) ? value : '"' + replace(value) + '"'
		)
	};
	var	define, replace;
	try{
		eval("const e=1");
		replace = function(value){
			var	replace = {"\x08":"b","\x0A":"\\n","\x0B":"v","\x0C":"f","\x0D":"\\r",'"':'"',"\\":"\\"};
			return	value.replace(/\x08|[\x0A-\x0D]|"|\\/g, function(value){return	"\\"+replace[value]})
		};
		define = function(name, value){
			var	script = document.createElement("script");
			script.type = "text/javascript";
			script.appendChild(document.createTextNode(toString(name, value)));
			document.documentElement.appendChild(script);
			document.documentElement.removeChild(script);
		}
	}
	catch(e){
		replace = function(value){
			var	replace = {"\x0A":"\\n", "\x0D":"\\r"};
			return	value.replace(/"/g, '""').replace(/\n|\r/g, function(value){return replace[value]})
		};
		define = this.execScript ?
		function(name, value){
			execScript(toString(name, value), "VBScript");
		}:
		function(name, value){
			eval(toString(name, value).substring(6));
		}
	};
	return	define;
})();

define("URL_PATH", '');