﻿/*
* www.vedovato.it
*
* home page
*
*@author andrea.bovo@sevenlab.it
*/

var isHome = 1;
var index = 0
var colors = ["#ffffff", "#ffffff", "#ffffff"];

$(document).ready( function(){

    $('#main').addClass('loadingCenter');

    $("#focus")
        .find("div.box")
        .find("h3")
        .hover(function () {
            $(this).fadeTo(500, .5 );
        },
        function () {
            $(this).fadeTo(500, 1.0 ); 
        });
});

$(window).load(function () {

    $('#main').removeClass('loadingCenter');

    $("#box-home")
        .show()
        .delay( 1000 )
        .fadeTo(1000, 1);

    $("#box-home")
            .find("h1")
            .animate({ color: colors[index] }, 500);

    $("#box-home")
            .find("p")
            .animate({ color: colors[index] }, 500);

    var cFocus = $('#focus').children().size();
    var cWidth = $('#focus').children(0).outerWidth();
    var wFocus = cFocus * cWidth;

    $('#focus').css({ 'width': +wFocus + 'px' });

    if ($('#slides').children().size() > 0) {
        $("#slides").slides({
            preload: true,
            fadeEasing: "easeInOutExpo",
            slideEasing: "easeInOutExpo",
            preloadImage: '/assets/img/loading.gif',
            play: 5000,
            pause: 2000,
            fadeSpeed: 1000,
            slideSpeed: 1000,
            hoverPause: true,
            animationComplete: tweenClaim
        });
    }

    LoadImagesCollection();

    function tweenClaim() {

        if (++index == 3)
            index = 0

        //console.log( colors[index-1] );

        $("#box-home")
            .find("h1")
            .animate({ color: colors[index] }, 500);

        $("#box-home")
            .find("p")
            .animate({ color: colors[index] }, 500);

    }

});

//LOADING IMAGES COLLECTION
function LoadImagesCollection() {  

    $('.visual').each( function(i){ 
        var loader = $(this);
        LoadImage(loader);
    });

};  
  
function LoadImage(loader){
    var image_src = $(loader).attr("data-img");
    var image_alt = $(loader).attr("data-alt");
    if (image_src)
    {                
        var img = new Image();
        $(img).css({'opacity':0});
        $(img).load(function() {
            $(loader).prepend($(img));
            $(loader).removeClass("loadingcenter");
            $(loader).removeAttr("data-img");
            $(loader).removeAttr("data-alt");
            $(img).attr("alt",image_alt);
            ResizeImg(img, 70, 50);
            $(img).animate({'opacity':1},500);
            
        });
        img.src = image_src;
    }
    else
    {
      $(loader).removeClass("loadingcenter");
    }
};

//FUNC RESIZE
function ResizeImg(obj, w, h) {

    var ratio = 0;
    if (h && w) {
        ratio = w / h; 
    }
    
//    obj.removeAttribute( "height" );
//    obj.removeAttribute( "width" );
//    obj.style.height = obj.style.width = "";
    
     var imgHeight = obj.height
        ,	imgWidth = obj.width
        ,	imgAspectRatio = imgWidth / imgHeight
        ,	bxHeight = h
        ,	bxWidth = w
        ,	bxAspectRatio = ratio;
        
      
      // If one parameter is missing, we just force calculate it
      if (!bxAspectRatio) 
      {
        if ( bxHeight ) {
            bxAspectRatio = imgAspectRatio + 1;
        }
        else {
            bxAspectRatio = imgAspectRatio - 1;
        }
      }
      
      // Only resize the images that need resizing
      if ( (bxHeight && imgHeight > bxHeight) || (bxWidth && imgWidth > bxWidth) ) {

        if (imgAspectRatio > bxAspectRatio) 
        {
            bxHeight = ( imgHeight / imgWidth * bxWidth );
        }
        else 
        {
            bxWidth = ( imgWidth / imgHeight * bxHeight );
        }

        //IMPOSTO dimensioni
        obj.height = bxHeight;
        obj.width = bxWidth;
      }
}
