jQuery(document).ready(function(){

	jQuery("a.switch_thumb").toggle(
		function() {		
			jQuery(this).addClass("swap");
	  		jQuery("ul.display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").removeClass("thumb_view");
			});			
			jQuery("#icon_thumb_detail").css('display','') ; 
			jQuery("#icon_thumb_view").css('display','none') ;
	 	}, 
	 	function () {
	  		jQuery(this).removeClass("swap");
	  		jQuery("ul.display").fadeOut("fast", function() {
				jQuery(this).fadeIn("fast").addClass("thumb_view");
			});
			jQuery("#icon_thumb_detail").css('display','none') ; 
			jQuery("#icon_thumb_view").css('display','') ;
	});
});

jQuery(document).ready(function() {
	jQuery('div.photo a').fancyZoom({scaleImg: true, closeOnClick: true});
	jQuery('#medium_box_link').fancyZoom({width:400, height:300});
	jQuery('#large_box_link').fancyZoom();
	jQuery('#flash_box_link').fancyZoom();
});

jQuery(document).ready(function() {

	jQuery('.hide').hide();

	jQuery('body').append('<div id="infoBacking"></div><div id="infoHolder" class="large"></div>');
	jQuery('#infoBacking').css({position:'absolute', left:0, top:0, display:'none', textAlign:'center', background:'', zIndex:'600'});
	jQuery('#infoHolder').css({left:0, top:0, display:'none', textAlign:'center', zIndex:'600', position:'absolute'});
	if(jQuery.browser.msie) {
		jQuery('#infoHolder').css({position:'absolute'});
	}


	jQuery('.more').mouseover(function() {
		jQuery(this).css({textDecoration:'none'});
	});
	jQuery('.more').mouseout(function() {
		jQuery(this).css({textDecoration:'none'});
	});

	jQuery('.more').click(function() {

		if (jQuery('.' + jQuery(this).attr("title")).length > 0) {
	
			browserWindow()
			getScrollXY()
	
			if (height<totalY) { height=totalY; }
	
			jQuery('#infoBacking').css({width: totalX + 'px', height: height + 'px', top:'0px', left:scrOfX + 'px', opacity:0.85});
			jQuery('#infoHolder').css({width: width + 'px', top:scrOfY + 25 + 'px', left:scrOfX + 'px'});
			source = jQuery(this).attr("title");
	
			jQuery('#infoHolder').html('<div id="info">' + jQuery('.' + source).html() + '<p class="clear"><span class="close">Close X</span></p></div>');
	
			jQuery('#infoBacking').css({display:'block'});
			jQuery('#infoHolder').show();
			jQuery('#info').fadeIn('slow');
		}
	
		jQuery('.close').click(function() {
			jQuery('#infoBacking').hide();
			jQuery('#infoHolder').fadeOut('fast');
		});

	});

	/* find browser window size */
	function browserWindow () {
		width = 0
		height = 0;
		if (document.documentElement) {
			width = document.documentElement.offsetWidth;
			height = document.documentElement.offsetHeight;
		} else if (window.innerWidth && window.innerHeight) {
			width = window.innerWidth;
			height = window.innerHeight;
		}
		return [width, height];
	}
	
	/* find total page height */
	function getScrollXY() {
		scrOfX = 0;
		scrOfY = 0;
		if( typeof( window.pageYOffset ) == 'number' ) {
			scrOfY = window.pageYOffset;
			scrOfX = window.pageXOffset;
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			scrOfY = document.body.scrollTop;
			scrOfX = document.body.scrollLeft;
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			scrOfY = document.documentElement.scrollTop;
			scrOfX = document.documentElement.scrollLeft;
		}
		totalY = (window.innerHeight != null? window.innerHeight : document.documentElement && document.documentElement.clientHeight ? document.documentElement.clientHeight : document.body != null ? document.body.clientHeight : null);

		totalX = (window.innerWidth != null? window.innerWidth : document.documentElement && document.documentElement.clientWidth ? document.documentElement.clientWidth : document.body != null ? document.body.clientWidth : null);

		return [ scrOfX, scrOfY, totalY, totalX ];
	}

	return false;
});
	
	
function updateCarrello() {

	var dataString = jQuery("#updatecart").serialize();
	
	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: dataString ,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		beforeSend: function() {
			jQuery("#button2[action='aggiornaCarrello']").attr('disabled','disabled');
			jQuery("#button2[action='aggiornaCarrello']").css({'background-color':'#bababa', 'color':'#8e8e8e'}) ;
		},
		success: function(html) {

			jQuery("#content-checkout").html(html) ;

			refresh_information_cart() ;

			refresh_cartinfo_sidebar() ;
		}
	});
}

function refresh_cartinfo_sidebar() {

	

	var modulo = jQuery("#modulo").val();
	var action = 'cartSidebarRefresh' ;
	
	var codPag = jQuery("#cod_pagina_hidden").val() ;
	var padreId = jQuery("#padre_id_hidden").val() ;

	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: 'modulo='+modulo+'&action='+action+"&cod_pagina="+codPag+"&padre_id="+padreId,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		success: function(html){
			if(eval(document.getElementById('cart_content_sidebar')))
			{
				document.getElementById('cart_content_sidebar').innerHTML=html;
			}
		}
	});
	return false;
}

function refresh_information_cart() {

	var modulo = jQuery("#modulo").val();
	var action = 'cartInfoRefresh' ;
	
	var codPag = jQuery("#cod_pagina_hidden").val() ;
	var padreId = jQuery("#padre_id_hidden").val() ;
	
	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: 'modulo='+modulo+'&action='+action+"&cod_pagina="+codPag+"&padre_id="+padreId,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		success: function(html){
			jQuery('#cart_information_span').html(html) ;
		}
	});
	return false;
}

function removeItem(codice_articolo, codice_carrello) {

	
	var modulo = "Catalogo"
	var action = 'removeItem' ;

	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: 'modulo='+modulo+'&action='+action+'&codice_articolo='+codice_articolo+'&codice_carrello='+codice_carrello,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		success: function(html) {

			jQuery("#content-checkout").html(html) ;
			

			refresh_information_cart() ;

			refresh_cartinfo_sidebar() ;
			
		}
	});
}



	
function setAttributeVal() {
	var postformflag = 1;
	// metto a zero se mancano campi.....controllo in pratica!
	
	if(postformflag) {	
		if(eval(document.getElementById('cart_information_span'))) {
			document.getElementById('cart_information_span').innerHTML = 'in elaborazione ...';
		}
		postform();
	}
}

function postform() {

	var dataString = jQuery("#shopingcartfrm").serialize();

	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: dataString,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		success: function(html){

			checkStock();

			if(eval(document.getElementById('cart_content_sidebar'))) {

				refresh_cartinfo_sidebar();
			}

			if(eval(document.getElementById('cart_information_span'))) {

				refresh_information_cart();
			}


			if(eval(document.getElementById('addtocartformspan'))) {

				/*jQuery('#addtocartformspan').html(html).hide().fadeIn(1200, function() {
					jQuery("#"+this.id+" > span").show().fadeOut(1200, function() {
					});
			    });*/
			    
			    jQuery('#addtocartformspan').html(html).hide().fadeIn(3000, function() {
			    	
			    	jQuery('#addtocartformspan > span').hide('fast') ;
			    	jQuery('#addtocartformspan > br').remove() ;
			    }) ;
			}



		}
	});
	return false;
}

function checkStock() {

	var codice_articolo = jQuery("#codice_articolo").val();
	var modulo = jQuery("#modulo").val();
	var action = 'checkStock' ;

	jQuery.ajax({
		url: './tmpl/ajax/ajax.php',
		data: 'modulo='+modulo+'&action='+action+'&codice_articolo='+codice_articolo,
		type: 'GET',
		dataType: 'html',
		timeout: 20000,
		error: function(){
			//alert('Error loading cart information');
		},
		success: function(html) {

			if(html=='unlimited') {

			}
			else {

				if(html>0) {
					//alert(html);
				}
				else {
					//window.location.href='/catalogo';
					alert('prodotto non disponibile....');
				}
			}
		}
	});
}
			
function numberonly(evt) {
    evt = (evt) ? evt : window.event
    var charCode = (evt.which) ? evt.which : evt.keyCode
    if (charCode > 31 && (charCode < 48 || charCode > 57)) {
        status = "This field accepts numbers only."
        return false
    }
    status = ""
    return true
}

function showhide_forgetpw() {
	
	if(document.getElementById('lostpassword_form').style.display=='none') {
		document.getElementById('lostpassword_form').style.display = ''
		document.getElementById('login_form_div_id').style.display = 'none';
		document.getElementById('form-reg').style.display = 'none';
		//document.getElementById('checkout_div_id').style.display = 'none';
	}
	else {
		//document.getElementById('lostpassword_form').style.display = 'none';
	}
}

function showhide_login() {
	
	if(document.getElementById('login_form_div_id').style.display=='none') {
		document.getElementById('login_form_div_id').style.display = ''
		document.getElementById('lostpassword_form').style.display = 'none';
		document.getElementById('form-reg').style.display = 'none';
		//document.getElementById('checkout_div_id').style.display = 'none';
	}
	else {
		document.getElementById('login_form_div_id').style.display = 'none';
	}
}

function showhide_registration() {

	if(document.getElementById('form-reg').style.display=='none') {
		document.getElementById('form-reg').style.display = '';
		document.getElementById('login_form_div_id').style.display = 'none';
		document.getElementById('lostpassword_form').style.display = 'none';
	}
	else {
		//document.getElementById('reg_form_div_id').style.display = 'none';
	}
}

function showhide_checkout() {
	
	if(document.getElementById('checkout_div_id').style.display=='none'){
		document.getElementById('checkout_div_id').style.display = '';
		document.getElementById('reg_form_div_id').style.display = 'none';
		document.getElementById('lostpassword_form').style.display = 'none';
		document.getElementById('login_form_div_id').style.display = 'none';
	}
	else {
		//document.getElementById('reg_form_div_id').style.display = 'none';
	}
}
