function roundTo(decimalpositions){
    var i = this * Math.pow(10,decimalpositions);
    i = Math.round(i);
    return i / Math.pow(10,decimalpositions);
}
Number.prototype.roundTo = roundTo;

/*********************** Gestione menu categorie in home ******************************************************/

function homecatHandle(){
	//$('#categories LI UL').hide();
	
	max = 0;
	$('#categories LI UL').each(function(){
		tmp = $(this).height();
		if (max < tmp) max = tmp;
	});
	
	$('#categories LI UL').height(max);
	
	$('#categories .toggle').toggle(function(){
		$('#categories LI UL').hide();
		$(this).attr('class','toggle plus');
		$(this).html('MOSTRA TUTTE LE CATEGORIE');
	}, function(){
		$('#categories LI UL').show();
		$(this).attr('class','toggle minus');
		$(this).html('NASCONDI LE SOTTOCATEGORIE');
	});
}

/*********************** Gestione Campi Opzionali  ******************************************************/

function optionalHandle(){
	$('#register .optional .opt').hide();
	
	$('#register .optional .toggle').toggle(function(){
		$(this).parent().find('.opt').show();
		$(this).attr('class','toggle minus');
	}, function(){
		$(this).parent().find('.opt').hide();
		$(this).attr('class','toggle plus');
	});
}

/*********************** Gestione Prezzi Preventivo ******************************************************/

function updatePrices(price){
	
	qtatot = 0;
	$('#estimate .qtatxt').each(function(i){
		qtatot = qtatot + parseFloat($(this).html());
	});
	
	$('#reference .qta').each(function(i){
		myqta = parseFloat($(this).html());
		myprice = parseFloat($(this).next('.price').html());
		if (qtatot >= myqta) price =  myprice;
	});
	
	$('#estimate .price').each(function(i){
		$(this).html(price);
		myqta = parseFloat($(this).prev('.qtatxt').html());
		mytot = myqta * price;
		$(this).next('.tot').html(mytot.roundTo(3));
	});
			
	subtot = 0;
						
	$('#estimate .tot').each(function(i){
		subtot = subtot + parseFloat($(this).html());
	});
	
	if (subtot != 0) {
		if ($('#estimate .stampa').is(':checked')) subtot = subtot + parseFloat($('#estimate .stampa').attr('value'));
		
		$('#estimate .subtot').html(subtot.roundTo(3));
		$('#estimate .qtatot').html(qtatot);
	}
	else {
		$('#estimate .subtot').html('0');
		$('#estimate .qtatot').html('0');
	}
}

function estimateHandle(){

	$('#estimate .plus').click(function(){
		value = $('#estimate .qta').html();
		inc = $('.imballo').html();
		value = parseFloat(value) + parseFloat(inc);
		
		$('#estimate .qta').html(value);
		$('#quantita').attr('value',value);
	});
	
	$('#estimate .minus').click(function(){
		value = $('#estimate .qta').html();
		dec = $('.imballo').html();
		value = parseFloat(value) - parseFloat(inc);
		
		limit = $('#reference .qta:eq(0)').html();
		
		if (value <= limit) value = limit;
		
		$('#estimate .qta').html(value);
		$('#quantita').attr('value',value);
	});
	
	$('#estimate .ok').click(function(){
		cod_prodotto = $('#id_prodotto').val();
		qta = $('#estimate .insert .qta').html();
		color = $('#estimate .insert .color option:selected').attr('value');
		size = $('#estimate .insert .size option:selected').attr('value');

		if ((qta != null) || (color!=-1) || (size!=-1)) {
			//ajax call
			$.post('/sviluppo/seritaliagadget.it/service.php', {
				mtd : 'add',
				id_prodotto : cod_prodotto,
				quantita: qta,
				taglia: size,
				colore: color
			});
		}
	});
}

/*********************** Gestione News ******************************************************/

function TickerHoverHandle(){
	$('.ticker .item').hover(function(){
		clearTimeout(TickerTimerId);
		index = $('.ticker .item').index(this);
	}, function(){
		TickerRun(index);
	});
}

function TickerShow(i){
	$('.ticker .item').hide();
	$('.ticker .item:eq('+ i +')').fadeIn('slow');
}

function TickerRun(i){
	num = $('.ticker .item').size();
	if (num > 1) {
		TickerShow(i)
		i++;
		if (i == num) i = 0;
		TickerTimerId = window.setTimeout('TickerRun('+ i +')', 6000);
	}
}

/*********************** On Document Load... ******************************************************/

$(function(){
	homecatHandle();
	estimateHandle();
	optionalHandle();
	
	// News Ticker
	TickerRun(0);
	TickerHoverHandle();
	
	max = 0;
	$('.ticker .item').each(function(){
		if ($(this).height() > max) max = $(this).height()
	});
	
	$('.ticker .item').height(max);
	
	// immagini scheda prodotto
	$('#product .views .more-images A').click(function(){
		//mysrc = $(this).find('IMG').attr('src');
		mysrc = $(this).attr('href');
		mycolor = $(this).find('IMG').attr('longdesc');
		$('#product .views .product-image IMG').attr('src',mysrc);
		$('#product .views .product-image A').attr('href',mysrc);
		
		if (mycolor != "") $('#product .views .product-image .mycolor').html('colore: '+mycolor);
		else $('#product .views .product-image .mycolor').html('');
		
		return false;
	});
	
	// Select correlazione riferimento
	$("#rif").change(function () {
		var $val = $(this).val();
		$("#id_prodotto").val($val);
		
		// $("#main > *").css("border", "3px double red");
		// $("p:last").addClass("selected");
		//class="color required" 
		$("p[id^='color_']").hide();
		
		$("#color_"+$val+" > select").attr("name", "colore");
		$("#color_"+$val+" > select").addClass("color required");
		$("#color_"+$val).show();
		

	});
	// validazione form
	$("#form-preventivo").validate();
	//outlet
	$('#outlet .desc').css('opacity','0.7');
	
			
});