(function($){
    var cache=[];
    $.preloadImages = function(){
        var args_len = arguments.length;
        for (var i=args_len; i--;){
            var cacheImage = document.createElement('img');
            cacheImage.src = arguments[i];
            cache.push(cacheImage);
        }
    }
})(jQuery);

function backgroundResize(){
    $('#background img').load(function(){

        // Unique id container
        $(this).parent().css('visibility', 'hidden');
        $(this).parent().css('overflow', 'hidden');


        var coef = $('#wrapper').width()/this.width;
        $(this).attr('width', coef * this.width * 1.1);
        $(this).attr('border', 0);
        $(this).css({
            'left': -Math.round( ($(this).width() - $(this).parent().width())/2 ),
            'top':0
        });

        $(this).parent().css('visibility', 'visible');

    }).each(function(){
        if(this.complete || (jQuery.browser.msie && parseInt(jQuery.browser.version) == 6))
            $(this).trigger("load");
    });
}

// backgroundResize();
$(function(){
    // Precaricamento immagine bg
    var imgPath = $('#background img').attr('src');
    $.preloadImages(imgPath);

    var $dialog = $('<div></div>');


    /* Bg Resize - Position */
    $(window).bind('resize', function(){
        backgroundResize();
    });
    backgroundResize();

    $('a.newsletterMenuLink').bind('click', function(e){
        $dialog.load(this.href).dialog({
            modal:true,
            resizable: false,
            position: 'top'
        });
        e.preventDefault();
    });


    /* Le form nel layout */
    $('body').ajaxComplete(function(event, xhr, options){
        if($dialog.dialog('isOpen')){
            $('.ui-dialog form').bind('submit', function(e){
                var form = jQuery(this);
                jQuery.ajax({
                    type: 'post',
                    url: this.action,
                    data: jQuery(this).serialize(),
                    success: function(data, status, xhr){
                        /* chiudo il dialog */
                        // $dialog.dialog('destroy');
                        /* ricarico il contenuto del tab selezionato */
                        // $('.content-tabs').tabs('load', $('.content-tabs').tabs('option','selected'));
                        $('.ui-dialog-content').html(data);
                        
                    }
                });
                e.preventDefault();
                return false;
            });

        } /* endif */
    });
    /* TABS */
    $( "#tabs" ).tabs({
        ajaxOptions: {
            error: function( xhr, status, index, anchor ) {
                $( anchor.hash ).html(
                    "Couldn't load this tab. We'll try to fix this as soon as possible. " +
                    "If this wouldn't be a demo." );
            }
        }
    });
    
    /* Scrollbars per i div con overflow: auto */
    $('.panel').scrollbar({
        color: '#EB1C97',
        bg: '#000000' /* track */
    });
    
    // Caricamento Ajax
    $('#loading').hide().ajaxStart(function() {
        $(this).show();
    }).ajaxStop(function() {
        $(this).hide();
        checkForErrorBoxes();
    });
    
    
    /* ERRORI INPUT */
    function checkForErrorBoxes(){
        $('.error-message').each(function(index, el){
            var errorMessageBox = $(el);
            errorMessageBox.hide();
            var inputElement = errorMessageBox.prev('input');
            inputElement.after('<a class="displayMessage element'+index+'"><img src="/img/display_error32.gif"/></a>');
            $('.element'+index).bind('click', function(e){
                $dialog.html($(el).text()).dialog({
                    modal: true,
                    resizable: false
                });
                e.preventDefault();
                return false;
            });
        });
    }
});
