﻿// JScript File

loadHpModule = true;

var hpModuleTimeout;

function rearrangePhotos() {
    $('#hp-module .photo').addClass('move-me');
    var totalWidth = 452; //$('#hp-module').width();
    var totalHeight = 278;
    var totalPhotos = $('#hp-module .photo').length;
    var focusedWidth = $('#hp-module .photo.focused img').attr('width');

    var i = 0;
    var newRight = 0;
    for (i=0; i<totalPhotos; i++) {
        var eqi = Math.floor(Math.random()*(totalPhotos-i));
        var $curItem = $('#hp-module .photo.move-me').eq(eqi);
        $curItem.removeClass('move-me');
        var newWidth = 0;
        var newOpacity = '0.45';
        if ($curItem.hasClass('focused')) {
            newWidth = focusedWidth;
            newOpacity = '1';
        }
        else if (i == totalPhotos-1) {
            newWidth = totalWidth - newRight;
        }
        else {
            newWidth = Math.round((totalWidth - focusedWidth) / (totalPhotos - 1));
        }
        $curItem.
            animate({width: newWidth+'px', right: newRight+'px'}, 452+100*i)
            .find('img')
                .animate({opacity: newOpacity});       /*, top: Math.random()*100+'px'*/
        newRight += newWidth;
    }
    
    if ($('#hp-module .caption.active').length == 1) {
        $('#hp-module .photo.focused').next()
            .addClass('next').hide().fadeIn(500,function() {
                $('#hp-module .caption.active').removeClass('active');
                $(this).addClass('active').removeClass('next');
            });
    }
    else {          //first round - show immediatly
        $('#hp-module .photo.focused').next().addClass('active');
    }
    
}


function initHpModule() {

    var totalPhotos = $('#hp-module .photo').length;

    var i = 0;
    for (i=0; i<totalPhotos; i++) {
        $('#hp-module .photo').eq(i).click(function(){
            if (!$(this).hasClass('focused')) {
                $('#hp-module .photo').removeClass('focused');
                $(this).addClass('focused');
                rearrangePhotos();
                window.clearTimeout(hpModuleTimeout);
                hpModuleTimeout = setTimeout( "pickRandomHpModule()", 9000 );
            }
        })
    }
    
    pickRandomHpModule();
}    

function pickRandomHpModule() {
    var totalPhotos = $('#hp-module .photo').not('.focused').length;
    var eqi = Math.floor(Math.random()*(totalPhotos));
    $('#hp-module .photo').not('.focused').eq(eqi).addClass('next-focused');
    $('#hp-module .photo.focused').removeClass('focused');
    $('#hp-module .photo.next-focused').removeClass('next-focused').addClass('focused');
    rearrangePhotos();
    window.clearTimeout(hpModuleTimeout);
    hpModuleTimeout = setTimeout( "pickRandomHpModule()", 3000 );
}
