/**
 * Script relativi al carrello
 * 
 * @author Domenico Renna
 * @return
 */


var cart_off = true;
jQuery(document).ready(function() {
	jQuery('#advanced').click(function() {
		if(this.hasClass('show_off')) {
			jQuery('#advanced').removeClass('show_off');
			jQuery('#advanced').addClass('show_on');
			jQuery('#advanced').html('Ricerca semplice');
			jQuery('.cart').hide();
			jQuery('#advanced_param').fadeIn("slow");
			jQuery('#author').focus();
			modalCart('.search_cart',true);
		} else {
			jQuery('#advanced').removeClass('show_on');
			jQuery('#advanced').addClass('show_off');
			jQuery('#advanced').html('Ricerca con pi&ugrave; criteri');
			jQuery('.cart').show();
			jQuery('#advanced_param').hide();
			jQuery('#keywords').focus();
			modalCart('.search_cart',false);
		}
	});
	
	jQuery('.show_cart').click(function() {
		if(jQuery('.cart_elem').length<1) return;
		if(cart_off) {
			jQuery('.show_cart').removeClass('show_off');
			jQuery('.show_cart').addClass('show_on');
			jQuery('.show_cart').html('Nascondi tutto');
			jQuery('.cart_elem').fadeIn("slow");
			if(jQuery('.cart_elem').length>1) {
				var h = (jQuery('.cart_elem').length*90);
				jQuery('#prod_list').height(h+'px');
			} else jQuery('#prod_list').height('80px');
			
			var ch = (jQuery('.search_cart .content').height()+jQuery('.cart_elem').length*80-90);
			jQuery('.search_cart .content').height(ch+'px');
			jQuery('.search_cart .content').css('background','url("'+siteurl+'/theme/css/images/bg_cerca_big.jpg") repeat-y top left');
			cart_off = false;
			modalCart('.search_cart',true);
		} else{
			jQuery('.show_cart').removeClass('show_on');
			jQuery('.show_cart').addClass('show_off');
			jQuery('.show_cart').html('Visualizza tutto');
			cart_off = true;
			jQuery('.cart_elem').each(function(index) {
				if(jQuery(this).attr('selected')!='selected') jQuery(this).hide();
			});
			jQuery('#prod_list').height('80px');
			jQuery('.search_cart .content').css('background','url("'+siteurl+'/theme/css/images/bg_cerca.jpg") no-repeat top left');
			jQuery('.search_cart .content').height('368px');
			modalCart('.search_cart',false);
		}
	});
	
	jQuery('.modal').click(function() {
		jQuery('.show_cart').removeClass('show_on');
		jQuery('.show_cart').addClass('show_off');
		jQuery('.show_cart').html('Visualizza tutto');
		cart_off = true;
		jQuery('.cart_elem').each(function(index) {
			if(jQuery(this).attr('selected')!='selected') jQuery(this).hide();
		});
		jQuery('#prod_list').height('80px');
		jQuery('.search_cart .content').css('background','url("'+siteurl+'/theme/css/images/bg_cerca.jpg") no-repeat top left');
		jQuery('.search_cart .content').height('368px');
		
		jQuery('#advanced').removeClass('show_on');
		jQuery('#advanced').addClass('show_off');
		jQuery('#advanced').html('Ricerca con pi&ugrave; criteri');
		jQuery('#advanced_param').hide();
		jQuery('.cart').show();
		
		modalCart('.search_cart',false);
	});
});


//Aggiungere un elemento al carrello via AJAX
function delAllCartItems(url,block_id) {
	jQuery.post(url, { action: "block:"+block_id+"-DelAllProducts" }, function(data) {
		//jQuery('#prod_list').html("");
		jQuery('.cart_elem').remove();
		jQuery('.voidcart').show();		
		jQuery('#prod_list').height('80px');
		jQuery('.search_cart .content').css('background','url("'+siteurl+'/theme/css/images/bg_cerca.jpg") no-repeat top left');
		jQuery('.search_cart .content').height('368px');
		jQuery('.show_cart').removeClass('show_on');
		jQuery('.show_cart').addClass('show_off');
		jQuery('.show_cart').html('Visualizza tutto');
		jQuery('.total').html(data.total);
		cart_off = true;
		modalCart('.search_cart',false);
		setPz(data.pz);
	});
}

//Aggiungere un elemento al carrello via AJAX
function addCartItem(url,block_id,id,qta) {
	jQuery.getJSON(url, { action: "block:"+block_id+"-AddProduct", id: id, qta: qta }, function(data) {
		jQuery('#prod_list').height('80px');
		jQuery('.voidcart').hide();
		if(data.mode == 'append') {
			jQuery('#prod_list').prepend(data.html);
			jQuery('.cart_elem').removeAttr('selected');
			jQuery('.cart_elem').hide();
			jQuery('#cart_elem_'+data.id).attr('selected','selected');
			jQuery('#cart_elem_'+data.id).fadeIn("slow");
			jQuery.growlUI(data.title, 'Prodotto <b>inserito</b> nel carrello!'); 
		}
		if(data.mode == 'increm') {
			var qtc = jQuery('#qta_prod_'+data.id);
			qtc.attr("value",parseInt(qtc.attr("value"))+1);
			jQuery('#cart_elem_'+data.id+' .total_prod').html(data.html);
			
			jQuery('.cart_elem').removeAttr('selected');
			jQuery('#cart_elem_'+data.id).attr('selected','selected');
			jQuery('.cart_elem').hide();
			jQuery('#cart_elem_'+data.id).fadeIn("slow");
			jQuery.growlUI(data.title, 'Prodotto <b>aggiunto</b> al carrello!'); 
		}
		setPz(data.pz);
		jQuery('.total').html(data.total);
	});
}

//Rimuovere un elemento al carrello via AJAX
function delProduct(url,block_id,id) {
	jQuery.getJSON(url, { action: "block:"+block_id+"-RemoveProduct", id: id }, function(data) {
		jQuery('.cart_elem').removeAttr('selected');
		jQuery('#cart_elem_'+data.selected_id).attr('selected','selected');
		jQuery('#cart_elem_'+data.selected_id).fadeIn("slow");
		jQuery('#cart_elem_'+id).empty().remove();
		if(data.selected_id==0) jQuery('.voidcart').show();
		jQuery('.total').html(data.total);
		if(!cart_off) {
			var h = (jQuery('.cart_elem').length*90);
			if(jQuery('.cart_elem').length>0) {
				jQuery('#prod_list').height(h+'px');
				jQuery('.content').height((jQuery('.content').height()-90)+'px');
			} else {
				jQuery('.show_cart').html('Visualizza tutto');
				cart_off = true;
				modalCart('.search_cart',false);
			}
		}
		setPz(data.pz);
		jQuery.growlUI(data.title, 'Prodotto <b>rimosso</b> dal carrello!');
	});
}

function isNumber(field,url,block_id,id) {
	var re = /^[0-9-'.'-',']*jQuery/;
	if (!re.test(field.value)) {
		field.value = field.value.replace(/[^0-9-'.'-',']/g,"");
	}
	jQuery.getJSON(url, { action: "block:"+block_id+"-SetProductQta", id: id, qta: field.value }, function(data) {
		jQuery('#cart_elem_'+data.id+' .total_prod').html(data.html);
		
		//jQuery('.cart_elem').removeAttr('selected');
		//jQuery('.cart_elem').hide();
		//jQuery('#cart_elem_'+data.id).attr('selected','selected');
		//jQuery('#cart_elem_'+data.id).fadeIn("slow");
		jQuery('.total').html(data.total);
		setPz(data.pz);
	});
}

function modalCart(elem,status) {
	if(jQuery.browser.msie && parseInt(jQuery.browser.version)<8) return;
	if(status) {
		jQuery('.modal').show();
		jQuery(elem).css('background','url("'+siteurl+'/theme/css/images/ombra_cerca_alpha.jpg") no-repeat top left');
	} else {
		jQuery('.modal').hide();
		jQuery(elem).css('background','url("'+siteurl+'/theme/css/images/ombra_cerca.jpg") no-repeat top left');
	}
}

function setPz(n) {
	if(!n) {
		jQuery('#pz_cart').hide();
		jQuery('#pz_cart strong').html("");
	}
	else {
		jQuery('#pz_cart').show();
		jQuery('#pz_cart strong').html(n);
	}
}

function removeSerchParam(elem,key) {
	jQuery(elem).remove();
	if(key=="genere") jQuery(".genere option").removeAttr("selected");
	else jQuery('#'+key).attr('value',"");
	
	jQuery('.res_search .loader').css('visibility','visible');
	jQuery('#adv_search').submit();
}




