// JavaScript Document

$(window).load(function() {
    $('#slider').nivoSlider();
});

$(document).ready(function() {
    if ($('ul.sc_menu').length > 0) {
        CreateGallery();
        AddClickThumbs();
    }

    $('#download-form input:radio').change(function() {
        var url = $('#download-form input:radio:checked').val();
        $('#download-btn').attr('href', url);
    });

    $('a#download-jump').click(function() {
        $(window).scrollTo($('a[name="download"]'), 1000);
    });

    /* Lightbox */
    if ($('.privacyForm').length > 0) {
        $('.privacyForm').colorbox({ iframe: true, innerWidth: 550, innerHeight: 450 });
    }
});

function AddClickThumbs() {
    var anchors = $('a.img-scheda');
    for (var i = 0; i < anchors.length; i++) {
        var anchor = anchors[i];
        anchor.onclick = function() {
            $('img.img-principale').attr('src', this.getAttribute('href'));
            return false;
        }
    }
}

function CreateGallery() {
    //Get our elements for faster access and set overlay width
    var div = $('div.sc_menu'), ul = $('ul.sc_menu'), ulPadding = 0;
    //Get menu width
    var divWidth = div.width();
    //Remove scrollbars	
    div.css({ overflow: 'hidden' });
    //Find last image container
    var lastLi = ul.find('li:last-child');
    //When user move mouse over menu
    div.mousemove(function(e) {
        //As images are loaded ul width increases,
        //so we recalculate it each time
        var ulWidth = lastLi[0].offsetLeft + lastLi.outerWidth() + ulPadding;
        var left = (e.pageX - div.offset().left) * (ulWidth - divWidth) / divWidth;
        div.scrollLeft(left);
    });
}
