
var setIdOper;
var setOper;
var setIdChange;
var setStringChange;
var setOpenlanguage = false;
var setOpenCartPop = false;
var speedDef = 400;
var speedSlide = speedDef;
var qtaImgBkg;

var widthBrow;
var heightBrow;
var idOpenNow;

var statMenuRight = false;
var statMenuRightHover = false;
var statAreaViewMenuId = false;
var statAreaMenuRight = false;



// funzione slide var id e slide
function viewHideLanguage(setIdOper, setOper, speedSlide){
	var idUse = setIdOper;
	switch (setOper) {
		case 0: $(idUse).slideDown(speedSlide);	break;
		case 1: $(idUse).slideUp(speedSlide); break;
	};
};


// funzione change html da id e string
function htmlChange(setIdChange, setStringChange){ var idUse = setIdChange; $(idUse).html(setStringChange); };

function viewOverlay(idOpen){
	idOpenNow = '#'+idOpen;
	$('#contOverlay').fadeIn(300);
	//$(idOpenNow).appendTo('#contOverlay');
	$(idOpenNow).fadeIn(500);
};

function closOverlay() {
	$('#contOverlay').fadeOut(300);
	//$(idOpenNow).appendTo('#contOverlay');
	$(idOpenNow).fadeOut(300);
};


//funzioni menu right
function MenuTime(){ if ( statMenuRightHover == true) { return; } 
		else if ( statAreaMenuRight == true ) {return; }
		else { hideMenuRight(); };
		
	};
	function viewMenuRight() {
	$('#menuRightId').fadeIn(300);
	statMenuRight = true;
};
function hideMenuRight() {
	$('#menuRightId').fadeOut(100);
};

var idThisBtn;
// contatti 
function OpenContatti(idThisBtn){
	
	$('.ContactBtnGall').removeClass("c_sel").addClass("ContactBtnGall");
	$('.c_sel').removeClass("c_sel").addClass("ContactBtnGall");
	
	$("#contOp"+idThisBtn).removeClass("ContactBtnGall").addClass("c_sel");

	var numTopMolt = idThisBtn;
	if(numTopMolt==2) {
		var numTopMolt1 =(numTopMolt * 35) - 70;
	} else {
		var numTopMolt1 =(numTopMolt * 35) - 105;
	}
	//alert (numTopMolt1+'    '+numTopMolt+'    '+idThisBtn);
	$('.ItemListH').fadeOut();
	
	
	
 	var idGallOpen = '#ContH'+idThisBtn;
 	$(idGallOpen).css({'margin-top' : numTopMolt1});
	$(idGallOpen).fadeIn(300);
	
};


$(document).ready(function() {


 	$('#areaViewMenuId').bind('mouseenter', function(){ viewMenuRight(); statAreaViewMenuId = true ; });
	$('#areaViewMenuId').bind('mouseleave', function(){ setTimeout("MenuTime()", 200); });
	$('#menuRightId').bind('mouseenter', function(){ statMenuRightHover = true; });
	$('#menuRightId').bind('mouseleave', function(){ statMenuRightHover = false; setTimeout("MenuTime()", 200); });
	$('a.btnViewMenu').bind('mouseenter', function(){statAreaMenuRight = true ; viewMenuRight(); });
	$('a.btnViewMenu').bind('mouseenter', function(){statAreaMenuRight = false; setTimeout("MenuTime()", 200); })
	
	// LANGUAGE SELECT
	$('#lSelItemHeader').click(function(){
		if (setOpenlanguage == false) { setIdOper = '#itemHiddenLang'; setOper = 0; viewHideLanguage(setIdOper, setOper); setOpenlanguage = true; }
		else if (setOpenlanguage == true) { setIdOper = '#itemHiddenLang'; setOper = 1; viewHideLanguage(setIdOper, setOper, 100); setOpenlanguage = false; };
	});
	$('.itemLangSel').click(function(){ setIdChange = '#lSelItemHeader'; setStringChange = $(this).html(); htmlChange(setIdChange, setStringChange); $('.inpLanguage').html('<input type="hidden" name="language" value="'+ setStringChange +'" id="inpLanguage" />'); setIdOper = '#itemHiddenLang'; setOper = 1; viewHideLanguage(setIdOper, setOper); setOpenlanguage = false; });
		
	// VISUAL POPLINK
	$('#checkBagBtnId').click(function(){
		
		if (setOpenCartPop == false) { setIdOper = '#contentCarrPop'; setOper = 0; viewHideLanguage(setIdOper, setOper, 1000); setOpenCartPop = true; }
		else if (setOpenCartPop == true) { setIdOper = '#contentCarrPop'; setOper = 1; viewHideLanguage(setIdOper, setOper, 300); setOpenCartPop = false; };
		//$('.contCarrPopScroll').jScrollPane({showArrows: true});
		
		updateCart();
		
	});
	
	// INIZIALIZZA GALLERY HOME	
	$('#galleryHomeId').scrollable({next: '#nextGalleryBtnHome', prev: '#prevGalleryBtnHome', speed: 1500, circular: false});
	
	// FUNCTION INIZIALIZZA GALLERY BKG
	//$('#bkgGalleryMoveId').scrollable({next: '#nextGalleryBtnHome', prev: '#prevGalleryBtnHome', speed: 1500, circular: true});

	$('.contScrollTempB').jScrollPane({showArrows: true});
	$('.contScrollTempB').focus();
	
	$('#vertGalleryRightId').scrollable({vertical: true, mousewheel: true , circular: true, speed: 1000}).autoscroll({ autoplay: true, interval: 5000 });
	
	// INIZIALIZZA OVERLAY LOGIN
	$('.loginPopBtnView').click(function(){
		viewOverlay('LoginPopId');
	});
	$('.closePopBtn, #contOverlay').click(function(){
		closOverlay();
	});
	
	
	$('.close_red').click(function(){
		closOverlay();
	});
	
	// INIZIALIZZA OVERLAY REGISTRAZIONE
	$('.regPopBtnView').click(function(){ 
		viewOverlay('RegPopId');
	});
	
	$('.fotoNegClickOver').click(function(){ 
		viewOverlay('TempBGallPopId');
	});
	
	$('.renderNegClickOver').click(function(){ 
		viewOverlay('TempBGallRendPopId');
	});
	
	$('#maskGalleryPop').scrollable({next: '.nextBtnOver', prev: '.prevBtnOver', speed: 1500, circular: true});
	$('#maskGalleryRendPop').scrollable({next: '.nextBtnOver', prev: '.prevBtnOver', speed: 1500, circular: true});
	
	
	// TOOLTIP GALLERIA
	$('.imgHomeBtn').mouseenter( function(){
			$('.contTooltipGallImg').hide();
			var toolView = $(this).parent('.itemImgHome').children('.contTooltipGallImg');
			var idContTollView = $(toolView).attr('id');
			var tollViewId = '#'+idContTollView;
			$(tollViewId).fadeIn(300);
	});

	$('.imgHomeBtn').bind('mouseleave', function(){
			$('.contTooltipGallImg').hide();
	});	
	
	
	/*
	try {
		loadPromo();
	}catch(err){}
	*/
	
	// SEL COLOR PAGINA PRODOTTI 
	$('#btnSelActiveCol').click(function(){
		$('#contColHidden').show(400);
	});
	$('.btnSelCol').click(function(){
		var colorSel = $(this).children('span.boxColor').attr('id');
		var nameColorSel = $(this).attr('title');
		$('#btnSelActiveCol span.boxColor').css({'background-color': ''+ colorSel +''})
		$('#btnSelActiveCol span.boxNomeColor').html(''+ nameColorSel +'')
		$('#btnSelActiveCol span.boxNomeColor').attr('title', ''+ nameColorSel +'')
		$('#colSelInp').attr('value', ''+ colorSel +'');
		$('#contColHidden').hide(100);
	});
	
	// TAGLIA
	$('.tagliaItemSel').click(function(){
		$('.tagliaItemSel').removeClass('tagliaItemSelOn');
		$(this).addClass('tagliaItemSelOn');
		var tagliaSel = $(this).html();
		$('#tagliaSelId').attr('value', ''+ tagliaSel +'');
	});
	$('.tagliaItemSelBaby').click(function(){
		$('.tagliaItemSelBaby').removeClass('tagliaItemSelOn');
		$(this).addClass('tagliaItemSelOn');
		var tagliaSel = $(this).html();
		$('#tagliaSelId').attr('value', ''+ tagliaSel +'');
	});
	
	// QUANTITA'
	$('#btnSelActiveQta').click(function(){
		$('#contQtaHidden').show(400);
	});
	$('.btnSelQta').click(function(){
		var QtaSel = $(this).children('span.QtaSel').html();
		//alert (QtaSel);
		$('#btnSelActiveQta span.QtaSel').html(''+ QtaSel +'')
		$('#quantitaSelId').attr('value', ''+ QtaSel +'');
		$('#contQtaHidden').hide(100);
		$('#quantity').val(QtaSel);
	});
	
	// GALL CONTATTI 
	$('.ContactBtnGall').click(function(){
		idThisBtn = $(this).attr('ID');
		var idThisBtn1 = idThisBtn.replace('contOp', '');
		OpenContatti(idThisBtn1);
	});
	
	$('.btnTabTaglie').click(function(){ 
		viewOverlay('tabellaTaglieId');
		$('#tabellaTaglieId').ready(function(){
			$('.contScrollOver').jScrollPane({showArrows: true});
		});
		
	});
	
	
	$('.contCarrPopScroll').jScrollPane({showArrows: true, autoReinitialise: true});	
	$('.contListProdCarr').jScrollPane({showArrows: true});
	$('.contIndStradali').jScrollPane({showArrows: true});
	$('.StoreDxLink').jScrollPane({showArrows: true});
	$('.listStoreDx').jScrollPane({showArrows: true});
	$('.contListaNegozi').jScrollPane({showArrows: true});
	$('#negozi').jScrollPane({showArrows: true});
	
	/* Search */
	$('.button-search').bind('click', function() {
		url = 'index.php?route=product/search';
		 
		var filter_name = $('input[name=\'filter_name\']').attr('value')
		
		if (filter_name) {
			url += '&filter_name=' + encodeURIComponent(filter_name);
		}
		
		location = url;
	});
	
	$('input[name=\'filter_name\']').keydown(function(e) {
		if (e.keyCode == 13) {
			url = 'index.php?route=product/search';
			 
			var filter_name = $('input[name=\'filter_name\']').attr('value')
			
			if (filter_name) {
				url += '&filter_name=' + encodeURIComponent(filter_name);
			}
			
			location = url;
		}
	});
	
	/* Ajax Cart */
	//$('#cart > .heading a').bind('click', function() {
	$('#cartTrigger').bind('click', function() {
		$('#cart').addClass('active');
		
		$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('#cartcontent').html(json['output']);
				}
				$('#cart_total').html(json['total']);
				$('#scroll-pane-carr').jScrollPane({showArrows: true});
			}
		});			
		
		$('#cart').bind('mouseleave', function() {
			$(this).removeClass('active');
		});
	});
	
	/* Mega Menu */
	$('#menu ul > li > a + div').each(function(index, element) {
		// IE6 & IE7 Fixes
		if ($.browser.msie && ($.browser.version == 7 || $.browser.version == 6)) {
			var category = $(element).find('a');
			var columns = $(element).find('ul').length;
			
			$(element).css('width', (columns * 143) + 'px');
			$(element).find('ul').css('float', 'left');
		}		
		
		var menu = $('#menu').offset();
		var dropdown = $(this).parent().offset();
		
		i = (dropdown.left + $(this).outerWidth()) - (menu.left + $('#menu').outerWidth());
		
		if (i > 0) {
			$(this).css('margin-left', '-' + (i + 5) + 'px');
		}
	});

	// IE6 & IE7 Fixes
	if ($.browser.msie) {
		if ($.browser.version <= 6) {
			$('#column-left + #column-right + #content, #column-left + #content').css('margin-left', '195px');
			
			$('#column-right + #content').css('margin-right', '195px');
		
			$('.box-category ul li a.active + ul').css('display', 'block');	
		}
		
		if ($.browser.version <= 7) {
			$('#menu > ul > li').bind('mouseover', function() {
				$(this).addClass('active');
			});
				
			$('#menu > ul > li').bind('mouseout', function() {
				$(this).removeClass('active');
			});	
		}
	}
});

$('.success img, .warning img, .attention img, .information img').live('click', function() {
	$(this).parent().fadeOut('slow', function() {
		$(this).remove();
	});
});
function updateCart() {
	$.ajax({
			url: 'index.php?route=checkout/cart/update',
			dataType: 'json',
			success: function(json) {
				if (json['output']) {
					$('.contCarrPopScroll').html(json['output']);
				}
				$('#cart_total').html(json['total']);
				setTimeout( 'reScroller()',500);
			},
			complete: function(){
				//$('.contCarrPopScroll').css("overflow","auto");
				//$('.contCarrPopScroll').jScrollPane({showArrows: true, autoReinitialise: true});
			}
		});
}
function reScroller() {
	$('.contCarrPopScroll').jScrollPane({showArrows: true});
	$('.contCarrPopScroll').css("overflow","auto");
}
function resellerLogin() {
	$("#res_error").html("");
	
	var email = $("#res_email").val();
	var password = $("#res_password").val();
	
	if(
		email =="" ||
		password ==""
	) {
		$("#res_error").html("Tutti i campi sono obbligatori");
		return;
	}	
	
	var url = "tools.php?a=checkReseller";
    url+="&email="+email;
    url+="&pass="+password;
    
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		success: function(json) {
			if (json['error']) {
				$("#res_error").html(json['error']);
				return false;
			}	
			if (json['success']) {
				window.location.reload();		
			}
		}
	});
	
}
function checkRegForm() {
	var html ="";
	$("#reg_mess").html(html);
	
	var firstname = $("#r_firstname").val();
	var lastname = $("#r_lastname").val();
	var address_1 = $("#r_address_1").val();
    
	var city = $("#r_city").val();
	var postcode = $("#r_postcode").val();
    
	var country_id = $("#r_country_id").val();
	var zone_id = $("#r_zone_id").val();
	var telephone = $("#reg_telephone").val();
	var email = $("#reg_email").val();
	var password = $("#reg_password").val();
	var confirm = $("#r_confirm").val();
	
	var company = $("#reg_company").val();
	
	if(
		firstname =="" ||
		lastname =="" ||  	
		address_1 =="" ||
		city =="" ||
		postcode =="" ||
		country_id =="" ||
		zone_id =="" ||
		telephone =="" ||
		email =="" ||
		password =="" ||
		confirm ==""
	) {
		$("#reg_mess").html("<font color='red'>All fields required<br/>Tutti i campi sono obbligatori</font>");
		return;
	}	
	
	if(password.length<5 || password.length>8) {
			$("#reg_mess").html("<font color='red'>Password:<br/>5-8 characters</font>");
			return;
	}		
	if(password!=confirm) {
		$("#reg_mess").html("<font color='red'>Password and Confirm do not match<br/>Password e Conferma non coincidono</font>");
		return;
	}
	if(postcode.length<2 || postcode.length>10) {
		$("#reg_mess").html("<font color='red'>Cap / Postcode:<br/>2-10 characters</font>");
		return;
	}
	
	if(country_id=="105" && company.length<5) {
		$("#reg_mess").html("<font color='red'>C.F. / P.IVA is mandatory for italy<br/>C.F. / P.IVA sono obbligatori per l'italia</font>");
		return;
	}
	
	
	var url = "tools.php?a=checkReg";
    url+="&email="+email;
   
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		success: function(json) {
			if (json['error']) {
				$("#reg_mess").html( "<font color='red'>"+json['error']+"</font>" );
				return false;
			}	
			if (json['success']) {
				$("#register").submit();		
			}
		}
	});
	
}
function checkResReg() {
	
	var html ="";
	$("#reg_mess").html(html);
	
	var firstname = $("#r_firstname").val();
	var lastname = $("#r_lastname").val();
	var address_1 = $("#r_address_1").val();
    
	var city = $("#r_city").val();
	var postcode = $("#r_postcode").val();
    
	var country_id = $("#r_country_id").val();
	var zone_id = $("#r_zone_id").val();
	var telephone = $("#r_telephone").val();
	var email = $("#r_email").val();
	var password = $("#r_password").val();
    

	if(
		r_firstname =="" ||
		r_lastname =="" ||  	
		r_address_1 =="" ||
		r_city =="" ||
		r_postcode =="" ||
		r_country_id =="" ||
		r_zone_id =="" ||
		r_telephone =="" ||
		r_email =="" ||
		r_password ==""		
	) {
		$("#reg_mess").html("<font color='red'>All fields required<br/>Tutti i campi sono obbligatori</font>");
		return;
	}
	
	var url = "tools.php?a=rr";
		url+="&r_nome="+r_nome;	
	    url+="&r_cognome="+r_cognome;
	    url+="&r_city="+r_city;
	    url+="&r_email="+r_email;
	    url+="&r_telefono="+r_telefono; 
	    url+="&r_ragsoc="+r_ragsoc;
	    url+="&r_piva="+r_piva;
	    url+="&r_pass="+r_pass;    
   	    url+="&r_indirizzo="+r_indirizzo;
	    url+="&r_cap="+r_cap; 
	    
	$.ajax({
		url: url,
		type: 'post',
		dataType: 'json',
		success: function(json) {
			if (json['error']) {
				$("#r_messagefield").html( "<font color='red'>"+json['error']+"</font>" );
				return false;
			}	
			if (json['success']) {
				$("#r_messagefield").html( "<font color='green'>Richiesta inviata con successo.</font>" );
				$("#r_submit").css("display","none");		
			}
		}
	});
	
	
}

function openIt(id) {
	$("#false_overlay").fadeOut();
	closeIt(window.popup);
	//alert(id);
	window.popup = id;
	var documentWidth = $(window).width();
    var documentHeight = $(window).height();
    
    var overlayWidth = $("#"+id).width();
	var overlayHeight = $("#"+id).height();
	
	var leftValue = (documentWidth-overlayWidth)/2;
    var topValue = (documentHeight-overlayHeight)/2;
    
    $("#"+id).css("top",topValue);
	$("#"+id).css("left",leftValue);
	
	$("#false_overlay").fadeIn();
	$("#"+id).fadeIn();
}
function closeIt(id) {
	$("#false_overlay").fadeOut();
	$("#"+id).fadeOut();
}
function centerOnScreen(div) {
	var documentWidth = $(window).width();
    var documentHeight = $(window).height();
    
    var overlayWidth = $(div).width();
	var overlayHeight = $(div).height();
	
	var leftValue = (documentWidth-overlayWidth)/2;
    var topValue = (documentHeight-overlayHeight)/2;
    
    $(div).css("top",(topValue/2));
	$(div).css("left",leftValue);
}
function addToCart(product_id) {
	
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information, .error').remove();
			
			if (json['redirect']) {
				location = json['redirect'];
			}
			
			if (json['error']) {
				if (json['error']['warning']) {
					$("#pop_generico_content").html( json['error']['warning'] );
					$("#pop_generico").overlay().load();
				}
			}	 
						
			if (json['success']) {
				
				/*
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');		
				$('.attention').fadeIn('slow');
				*/
				
				$("#pop_generico_content").html( json['success'] );
				$("#pop_generico").overlay().load();
				
				$('#cart_total').html(json['total']);
				
				$('#cartcontent').html(json['output']);
				$('#scroll-pane-carr').jScrollPane({showArrows: true});

				$('html, body').animate({ scrollTop: 0 }, 'slow');
				
				$('#button-cart').click();
				
			}	
		}
	});
}

function removeCart(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			updateCart();
		}
	});
}

function removeFromCart(key) {
	
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'remove=' + key,
		dataType: 'json',
		success: function(json) {
			
			window.location.replace('index.php?route=checkout/cart');
			
		}
	});
}

function removeVoucher(key) {
	$.ajax({
		url: 'index.php?route=checkout/cart/update',
		type: 'post',
		data: 'voucher=' + key,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
			
			if (json['output']) {
				
				$('#cart_total').html(json['total']);
				$('#cartcontent').html(json['output']);
				$('#scroll-pane-carr').jScrollPane({showArrows: true});

			}			
		}
	});
}

function addToWishList(product_id) {
	$.ajax({
		url: 'index.php?route=account/wishlist/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				$("#generic_popup_area").html( json['success'] );
				viewOverlay('GenericPopId');	
			}	
		}
	});
}

function addToCompare(product_id) { 
	$.ajax({
		url: 'index.php?route=product/compare/update',
		type: 'post',
		data: 'product_id=' + product_id,
		dataType: 'json',
		success: function(json) {
			$('.success, .warning, .attention, .information').remove();
						
			if (json['success']) {
				/*
				$('#notification').html('<div class="attention" style="display: none;">' + json['success'] + '<img src="catalog/view/theme/default/image/close.png" alt="" class="close" /></div>');
				$('.attention').fadeIn('slow');
				*/
				
				$("#pop_generico_content").html( json['success'] );
				$("#pop_generico").overlay().load();
				
				$('#compare_total').html(json['total']);
				
				$('html, body').animate({ scrollTop: 0 }, 'slow'); 
			}	
		}
	});
}

