/* 

Copyright (c) 2010 Vanni Valle. All rights reserved.

*/

$(document).ready(function() {
    
    //fade
    
    $(".fadeImg").hover(
        function () 
        {
            $(this).children(":first").fadeOut();
            $(this).children(":eq(1)").fadeIn();
        }, 
        function () 
        {
            $(this).children(":first").fadeIn();
            $(this).children(":eq(1)").fadeOut();	   
        }
    );

    $(".prod, .box-prod").hover(
	   function () {
	   $(this).find('.animate').fadeIn('fast');}, 
	   function () {
	   $(this).find('.animate').fadeOut('fast');}
    );
    
    $(".prod-little, #gallery li a").hover(
	   function () {
	   $(this).find('.animate-thumb').fadeIn('fast');}, 
	   function () {
	   $(this).find('.animate-thumb').fadeOut('fast');}
    );
    
    $('div.prod.disable').unbind();
    
    
    if ($.browser.msie) {
    
    }else{
    
    //fade
    
    $(".fade").hover(
	   function () {
	   $(this).fadeTo(100, 0.75);}, 
	   function () {
	   $(this).fadeTo(100, 1);}
    );
    
    }

    // Form
    
    function populateElement(selector, defvalue) {
        $(selector).each(function() {
            if($.trim(this.value) == "") {
                this.value = defvalue;
            }
        })
        $(selector).focus(function() {
            if (this.value == defvalue || this.value == "Errore: "+defvalue || 
                this.value == "Errore d'iscrizione" || this.value == "Iscrizione effettuata!") 
            {
                this.value = "";
            }
        });
        $(selector).blur(function() {
            if($.trim(this.value) == "") {
                this.value = defvalue;
            }
        });
    }
    
    var lang = $("html").attr("lang");
    
    if (lang == 'it'){
        populateElement('.search-prod input', 'Cerca...');
        populateElement('#newsletter', 'inserisci email');
        populateElement('#professione', 'inserisci categoria');
        populateElement('#search', 'Cerca prodotto, categoria, parola chiave...');
    }
    else{
        populateElement('.search-prod input', 'Search...');
        populateElement('#newsletter', 'insert email');
        populateElement('#professione', 'insert category');
        populateElement('#search', 'Search product, category, keyword...');
    }
    
    $('#nlButton').click(function(){
        if($('#professione').attr('value')=="inserisci categoria" || $('#professione').attr('value')=="Errore: inserisci categoria") 
        {
            $('#professione').attr('value', "Errore: inserisci categoria");
            return false;
        }
        if($('#newsletter').attr('value')=="inserisci email" || $('#newsletter').attr('value')=="Errore: inserisci email") 
        {
            $('#newsletter').attr('value', "Errore: inserisci email");
            return false;
        }        
        
        
		$.ajax({
			type: "POST",
			url: "subscribe/subscribe.php",
			data: "email="+$('#newsletter').attr('value')+"&professione="+$('#professione').attr('value'),
			success: function(msg){
				if(msg=="OK") $('#newsletter').attr('value', 'Iscrizione effettuata!');
				else $('#newsletter').attr('value', 'Errore d\'iscrizione'); 
				/* alert($('#tipologia').val()); */
            }
        });
		return false;
	});
    
    //change color
    $("#menu li a").not('.selected, #language, #chooseLang a').hover(
	   function () {
	   $(this).animate({color: "#fff", backgroundColor: "#7ac142"}, "fast"); 
	   }, 
	   function () {
	   $(this).stop(true, true).animate({color: "#008045", backgroundColor: "#efeff0"}, "fast", 
        function() { this.style.backgroundColor='transparent'; });
	   }
    );
    
    
    
    $("#language a").hover(
	   function () {
	   $(this).animate({color: "#fff", backgroundColor: "#7ac142"}, "fast"); 
	   }, 
	   function () {
	   $(this).stop(true, true).animate({color: "#444", backgroundColor: "#efeff0"}, "fast", 
        function() { this.style.backgroundColor='transparent'; });
	   }
    );
    
    

    $('#menu li a#language').hover(
        function () {
            $('#chooseLang').slideDown('fast');
        }, 
        function () {
            $('#chooseLang').delay(100).slideUp('fast');
        }
    );
    
    $('#chooseLang').hover(
        function () {
            $(this).stop(true, true).show();
        }, 
        function () {
            $(this).stop(true, true).slideUp('fast');
        }
    );

    
    $(".cgColor").hover(
	   function () {
	   $(this).animate({color: "#008045"}, "fast");	   
	   }, 
	   function () {
	   $(this).animate({color: "#444444"}, "fast");
	   }
    );
    
    $(".cgColor2").hover(
	   function () {
	   $(this).animate({color: "#008045"}, "fast");	   
	   }, 
	   function () {
	   $(this).animate({color: "#787878"}, "fast");
	   }
    );
    
    $(".cgColor3").hover(
	   function () {
	   $(this).animate({color: "#008045"}, "fast");	   
	   }, 
	   function () {
	   $(this).animate({color: "#858585"}, "fast");
	   }
    );
    
    $(".pagin-year ul li a").not('.highlight').hover(
        function () {
        $(this).animate({color: "#50941a"}, "fast");}, 
        function () {
        $(this).animate({color: "#A1A1A1"} , "fast");}
    );
    
    $('table tr:last-child').children('td').css('borderBottom', '0px');   
    $('table tr').children('td:last-child').css('borderRight', '0px'); 
    //$('table tr :nth-child(2n)').not('a').css('backgroundColor', '#f3f3f3'); 
    $("table tr:even").css('backgroundColor', '#f8f8f8');
    
    
    $('.line-dot').last().css('display', 'none');
    
    
    // Gestione freccia Select Box
 
    $(".select_cont").click(function(){
    // Quando utente clicca sulla freccia ...
        $(this).children(".hoverBox").show();
        return false;
    });
    
    $("body").click(function(){
    // Quando utente clicca sulla freccia ...
        $(".hoverBox").hide();
    });
    
    // Quando utente fa click il box scompare
    $(".select_cont .hoverBox").mouseup(function(){
	   $(this).hide();
    })

    // Quando utente clicca su un link , il valore viene copiato nello SPAN nell hidden
    $(".select_cont .hoverBox a").click(function(){
        var value = $(this).text();
        $(this).parent().parent().parent().children("input").val(value);
        $(this).parent().parent().parent().children(".input_val").text(value);
        return false;
    });
    

});

// Fancy-box 
    
$(function() {

    $("a.fancybox").fancybox({
		'hideOnContentClick':true,
		'titleShow':false,
		'overlayOpacity':0.9,
		'overlayColor':'#FFF',
		'padding':4,
		'type': 'iframe'
	});
	
    $("a.fancybox_cred").fancybox({
		'hideOnContentClick':true,
		'titleShow':false,
		'overlayOpacity':0.9,
		'overlayColor':'#FFF',
		'padding':4,
		'type': 'iframe',
		'width': 300
	});
	
});
