// Selected li menu

if(typeof console=="undefined"||typeof console.log=="undefined")var console={log:function(){}};

	function createCookie(name,value,days) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name+"="+value+expires+"; path=/";
	}

	function readCookie(name) {
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) {
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}

// HACKS PER IE 7/8

$.ajaxSetup({
	cache: false
});

$(function() {
	
	
	if ($.browser.msie && $.browser.version == "7.0") {
		$("#claim").css('margin-left',"-980px");
		$(".core_contents").height($(".catalogo").outerHeight(true));
		$("#form-route").css('margin-left',"-" + $(window).width() + "px");
		$("#blog-contents").height(parseInt($(".blog_header").outerHeight(true))+parseInt($(".page_allegati_left").outerHeight(true)));
		$(".like_badge").css('margin-left',"-170px");
		$(window).resize(function() {
	
			$("#form-route").css('margin-left',"-" + $(window).width() + "px");
	
			
		});
		
	}
	
});

$(function(){

	var uri = location.pathname;
	
	// controllo se sono in una sezione del blog
	
	var blog = uri.split('/');
	
	if(blog[1] == 'blog') {
		
		$('.right').find('a[title=blog]').parent('li').addClass('selected');
	
	} else {				
	
		$('.right').find('a[title=blog]').parent('li').removeClass('selected');
	
	}
	
	//
		
	$('.menu').find('a[href="' + uri + '"]').parent().each(function(index){
	
		$(this).addClass('selected');
		
		$(this).parents('li.submenu').addClass('selected');
		
			$(this).parents('li.submenu').children('ul').each(function(index){
			
				$(this).children().each(function(){
				
					//console.log($(this));
				
					//if($(this).children().attr('href') != uri) $(this).children().css('color','#666666');
				
				});		
			
			});
			
	});

});

// SCROLL TOP BUTTON

$(function () {


	$(window).scroll(function(){
	
			if($(window).scrollTop() > 600 )
					$("#top_button").fadeIn();
			else
					$("#top_button").fadeOut(200);
	});
	$("#top_button").click(function(){
			$("#top_button").fadeIn(200, function(){
					$('body, html').animate({ 'scrollTop': 0 },1000);
			});
	});

});

// Tasto commenta	

$(function () {

	$("#comment").click(function(){
			$("#comment").fadeIn(200, function(){
					$('body, html').animate({ scrollTop: $("#comments").offset().top },1000);
			});
	});

});

// Newsletter

$(function(){

	$('.input_newsletter').live('click', function() {
	
		$(this).val('');
	
	});
	
	$('.input_newsletter').live('focusout', function() {
	
		if($(this).val() == '') $(this).val('indirizzo email');
	
	});

	$('.submit_newsletter').live('click', function() {
	
		var email = $('.input_newsletter').val();
	
		$.get('/newsletter_users/addUser/' + email, function(ret) {
		
			if(ret.aggiunto != 1) { 
				
				$('.input_newsletter').val('');
				$('.errors').html(ret.aggiunto.email);
				$('.errors').show(); 
				$('.errors').fadeOut(6000);
				
				
			}
			else { 
			
				$('.ok-message').html('Utente registrato con successo.'); 
				$('.ok-message').show();
				$('.ok-message').fadeOut(6000);
				
			}
		
		}, 'json');
	
	});

});		

// Tasto cerca

$("#PostInputSearch").live('click', function(){

	$(this).val('');

});

$("#PostInputSearch").live('focusout', function(){

	if($(this).val() == '') $(this).val('Cerca nel sito...');

});

$(function() {
	
	$(".fb_comments_count_zero").each(function() {
		
		$(this).parents('.comments').remove();
		
	});
	
});


$(function() {
	
	// 	Timmy Tooltips
	
	$("*[rel='timmytip']").live('mouseenter',function(e) {
		
		if ($(this).attr('data-tip-id') == undefined) {
			
			$(this).attr('data-tip-title',$(this).attr('title'));
			$(this).removeAttr('title');
			
			$(this).attr('data-tip-id','tip_' + e.timeStamp);
			
		}
	
		var timmytip = null;
		
		if ($('body').find('#' + $(this).attr('data-tip-id')).length == 0) {
				
				timmytip = $("<div></div>").addClass('timmytip')
							.text($(this).attr('data-tip-title'))
							.css('display','none')
							.css('z-index','999')
							.css('position','absolute')
							.css('left',$(this).offset().left+8)
							.css('top',$(this).offset().top-30)
							.attr('id',$(this).attr('data-tip-id'))
							.appendTo('body')
							.delay(500)
							.fadeIn(300);
							
				$(timmytip).html('<span class="arrow_tooltip"></span>' + $(timmytip).text());
							
		} else {
			
				timmytip = $('#' + $(this).attr('data-tip-id')).html('<span class="arrow_tooltip"></span>' + $(this).attr('data-tip-title')).fadeIn(300);
				timmytip.css('z-index','999')
						.css('position','absolute')
						.css('left',$(this).offset().left+8)
						.css('top',$(this).offset().top-28);
				
				
		}	
		
		if ($(this).attr('data-tipclass') != undefined) $(timmytip).addClass($(this).attr('data-tipclass'));
		
		var position = $(timmytip).offset().left + $(timmytip).width();
		
		
		if (position > $(window).width()) {
			$(timmytip).css('left','auto');
			$(timmytip).css('right','10px');
		}			
		
	});
	
	$("*[rel='timmytip']").live('mouseleave',function() {
		
		$('#' + $(this).attr('data-tip-id')).remove();
		
	});


	
});


var profiler = {
	
		marginRight: 10,
		marginBottom: 20,
		profileWidth: 180,
		profileHeight: 290
};

var currentNumBoxes = 0;

function resize_profiler() {
	

		var boxWidth = parseInt($(".catalog-column-contents").innerWidth());
		
		//console.log('grandezza contitore: ' + boxWidth);
	
		var numBoxes = Math.floor(boxWidth/(profiler.profileWidth+profiler.marginRight));

		currentNumBoxes = numBoxes;

		//console.log('numero box per riga: (' + boxWidth + '/' + (profiler.profileWidth+profiler.marginRight) + '): ' + numBoxes);

		var totalSpace = (profiler.profileWidth+profiler.marginRight)*numBoxes-profiler.marginRight;
		
		//console.log('spazio totale (larghezzabox+margine)*n - margine:' + totalSpace);
		
		var exceedWidth = boxWidth-totalSpace;
		
		//console.log('spazio in eccesso (contenitore-spazio totale): ' + exceedWidth);
		
		var marginQuote = (exceedWidth/(numBoxes-1));
		
		//console.log('quota in eccesso: ' + (marginQuote));
		
		var i = 0;

		$(".catalog-column-contents").find('.product-preview').each(function(index) {
			
			
			if (i != (numBoxes-1)) {
				
				$(this).css('margin-right',profiler.marginRight+marginQuote);

				i++;
				
			} else {

				$(this).css('margin-right','0');

				i = 0;
				


			}
		
		});
					
}

function setWidth() {
	
	
	$(".catalog-column-contents").width($("#catalog-contents").innerWidth()-$(".catalog-column-left").outerWidth()-21);
	
	
	
	resize_profiler();
}


$(function() {
	
	


	$(window).resize(setWidth);
});

$(window).load(function() {
	
	
	setWidth();
		
	
});

function preloader(opt) {
	

	if (opt == 'show') {
		
		$("#preloader").width($(window).width()).height($(document).height()).show();
		
			$("#preloader .loader").css('margin-left',$(window).width()/2);
			$("#preloader .loader").css('margin-top',$(window).scrollTop()+($(window).height()/2));
			
		$(window).resize(function() {
			
			$("#preloader").width($(window).width()).height($(document).height());
		
			$("#preloader .loader").css('margin-left',$(window).width()/2);
			$("#preloader .loader").css('margin-top',$(window).scrollTop()+($(window).height()/2));
		
		});
		
		$(window).scroll(function() {
			
			$("#preloader").width($(window).width()).height($(document).height());
			
			
			$("#preloader .loader").css('margin-left',$(window).width()/2);
			$("#preloader .loader").css('margin-top',$(window).scrollTop()+($(window).height()/2));
		
		});
		
	} else {
		
		$("#preloader").hide();
		
	}
	
}

$(function() {
	
	$("li.submenu ul").live('mouseenter',function() {
		
		$(this).parent().addClass('submenu-hover');
		
	});
	
	$("li.submenu ul").live('mouseleave',function() {
		
		$(this).parent().removeClass('submenu-hover');
		
	});
	
});

$(window).load(function() {
	
	if ($("#claim").length != 0) {

		$("#claim").height($(document).height()-$(window).scrollTop());
	}
	
});

$(window).scroll(function() {
	
	if ($("#claim").length != 0) {
		
		$("#claim").height($(document).height()-$(window).scrollTop());
		
	}
	
});

$(window).resize(function() {
	
	if ($("#claim").length != 0) {
		
		$("#claim").height($(document).height()-$(window).scrollTop());
		
	}
	
});

/* Search function */


