$(document).ready(function () {

    /*
     * INICIALITZACIONS
     */

     /* slimbox */
     /*if (!/android|iphone|ipod|series60|symbian|windows ce|blackberry/i.test(navigator.userAgent)) {
	jQuery(function($) {
		$("a[rel^='lightbox']").slimbox({}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	});
    }*/

    $('#menu ul li').hover(
        function() {
            $(this).find('div.desplegable:first').css({visibility: 'visible', display: 'none'}).slideDown();
        },
        function () {
            $(this).find('div.desplegable:first').hide(0, function () {
                $(this).find('div.desplegabl:first').css({visibility: 'hidden'});
            });
        }
    );

});

/*
 * TIMERS DIAPOSITIVES AUTOMÀTIQUES
 */

(function ($) {

    var tempCanviFoto;
    var tempAmagaText;

    $.activarTimers = function() {
        tempCanviFoto = window.setInterval("$.canviFoto()", 6000);
    };

    $.clickCanviFoto = function(num) {
        
        clearTimeout(tempCanviFoto);
        clearTimeout(tempAmagaText);

        var seguent = $(".destacat_visible");
        var desti = "";

        $('.destacat_text').css('top', '239px');
        
        switch(num)
        {
            case 1:
                seguent = $('#destacat_1');
                desti = '0px';
                break;
            case 2:
                seguent = $('#destacat_2');
                desti = '77px'
                break;
            case 3:
                seguent = $('#destacat_3');
                desti = '154px'
                break;
        }

        seguent.css('z-index', '15');

        $(".destacat_visible").fadeOut('slow', function() {
            $(".destacat_visible").css({'z-index': '10', 'display': 'block'}).removeClass('destacat_visible');
            seguent.addClass('destacat_visible').css('z-index', '20');
            tempCanviFoto = window.setInterval("$.canviFoto()", 6000);
            $('.destacat_visible .destacat_text').animate({top: '-=70px'}, 'slow');
            tempAmagaText = window.setInterval("$.amagarText()", 3000);
        });
        $('#destacat_mini_req').animate({top: desti}, 'slow');
    };

    $.canviFoto = function() {

        clearTimeout(tempCanviFoto);
        clearTimeout(tempAmagaText);

        var seguent = $(".destacat_visible").next();
        var act_id = $(".destacat_visible").attr('id')
        var desti = '+=77px'

        if ( act_id == "destacat_3" ) {
            seguent = $('#destacat_1');
            desti = '0px'
        }

        seguent.css('z-index', '15');
        $('.destacat_text').css('top', '239px');

        $(".destacat_visible").fadeOut('slow', function() {
            $(".destacat_visible").css({'z-index': '10', 'display': 'block'}).removeClass('destacat_visible');
            seguent.addClass('destacat_visible').css('z-index', '20');
            tempCanviFoto = window.setInterval("$.canviFoto()", 6000);
            $('.destacat_visible .destacat_text').animate({top: '-=70px'}, 'slow');
            tempAmagaText = window.setInterval("$.amagarText()", 3000);
        });
        $('#destacat_mini_req').animate({top: desti}, 'slow');
    };

    $.amagarText = function() {
        clearTimeout(tempAmagaText);
        $('.destacat_visible .destacat_text').animate({top: '+=70px'}, 'slow');    
    }

})(jQuery);


/*
 * PRECÀRREGA DE LES IMATGES
 */

(function ($) {
    $.preLoadImages = function(imageList,callback) {
        var pic = [], i, total, loaded = 0;
        if (typeof imageList != 'undefined') {
            if ($.isArray(imageList)) {
                total = imageList.length; // used later
                    for (i=0; i < total; i++) {
                        pic[i] = new Image();
                        pic[i].onload = function() {
                            loaded++; // should never hit a race condition due to JS's non-threaded nature
                            if (loaded == total) {
                                    if ($.isFunction(callback)) {
                                            callback();
                                    }
                            }
                        };
                        pic[i].src = imageList[i];
                    }
            }
            else {
                pic[0] = new Image();
                pic[0].onload = function() {
                    if ($.isFunction(callback)) {
                            callback();
                    }
                };
                pic[0].src = imageList;
            }
        }
        pic = undefined;
    };
})(jQuery);


/*
 * CANVI DEL MENÚ SEGONS SECCIÓ
 */

(function($) {
    $.canviOpcioMenu = function(seccio) {
        $(".actiu").removeClass('actiu');
        $(seccio).addClass('actiu');
    }
})(jQuery);

/*
 * CLICK BOTÓ SUBSCRIURE'S LLISTA
 */

$(function() {
    $("#boto_llista").click(function() {
        var adresa = document.getElementById('input_llista').value;
        
        if (adresa.match(/\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/)) 
        { 
            $.ajax({  
                type: "POST",  
                url: "bin/enviar_correu.php",  
                data: ({adresa: adresa}),  
                success: function() {  
                    $('#box_llista_correu p').fadeOut('slow', function() {
                        $('#box_llista_correu h3').after('<p class="msg_correcte">Subscripció correcta</p>');
                        $(".msg_correcte").slideDown('slow');
                        temps = window.setInterval(function() {
                            $(".msg_correcte").slideUp('slow', function() {
                                $(".msg_correcte").detach();
                                document.getElementById('input_llista').value = "";
                                $('#box_llista_correu p').fadeIn('slow');
                            });
                            clearTimeout(temps);            
                        }, 2000);
                    }); 
                }  
            });  
            return false;
        }
        else if (adresa.length >= 1 && adresa != "Correu electrònic") 
        {
            Error("Introdueix una adreça electrònica de format correcte");
        }
        else if (adresa.length == 0 || adresa == "Correu electrònic")
        {
            Error("Introdueix una adreça electrònica");
        }
    });
    
    function Error(m) {
        var temps = 0;
        var codi = $('#box_llista_correu').html();
        $('#box_llista_correu p').fadeOut('slow', function() {
            $('#box_llista_correu h3').after('<p class="msg_error">'+ m + '</p>');
            $(".msg_error").slideDown('slow');
            temps = window.setInterval(function() {
                $(".msg_error").slideUp('slow', $(".msg_error").detach());
                $('#box_llista_correu p').fadeIn('slow');
                clearTimeout(temps);            
            }, 2000);
        });
    };
});
