// Objects
var nav = {
    init: function () {
        $('#nav ul').hide();
    },
    onmouseover: function (selector) {
        $(selector + ' .items').show();
        $(selector + ' h1').addClass('hover');
    },
    onmouseout: function (selector) {
        $(selector + ' .items').hide();
        $(selector + ' h1').removeClass('hover');
    }
};

var banner = {
    intervalDelay: 12000,
    fadeDelay: 1500,
    shuffle: function () {
        this.setActive($(getRandomItem($('#banner img'))));
        this.setActive($(getRandomItem($('#banner h1'))));
    },
    fade: function () {
        var this_ = this;
        var activeImg = $('#banner img.active');
        var activeTitle = $('#banner h1.active');
        var nextImg = activeImg.next('img').length ? activeImg.next('img') : $('#banner img:first');
        var nextTitle = activeTitle.next('h1').length ? activeTitle.next('h1') : $('#banner h1:first');
        this_.setActive(nextTitle);
        this_.setInactive(activeImg);
        this_.setActive(nextImg);
        activeTitle
            .animate({ top: $('#banner .info').height(), opacity: 0.0 }, this_.fadeDelay, function () {
                this_.setInactive(activeTitle);
                activeTitle.css('opacity', '1.0');
            });
        nextTitle
            .css('top', '-' + nextTitle.height() + 'px')
            .animate({ top: 0 }, this_.fadeDelay, function () {
                this_.setActive(nextTitle);
            });
    },
    setActive: function (el) {
        el.removeClass('inactive').addClass('active');
    },
    setInactive: function (el) {
        el.removeClass('active').addClass('inactive');
    }
};

var dialogCV = {
    init: function() {
        $('#dialog-CV').dialog({ autoOpen: false});
        },
    open: function () {
        $('#dialog-CV').dialog({ modal: true,
                                 width: '480px',
                                 resizable: false,
                                 beforeClose: function(event, ui) 
                                 {
                                    $('.error, .success').empty();
                                 }                                 
        });
        $('#dialog-CV').dialog('open');
    }
};

var news = {
    init: function () {
        var arr = $('.info h1, .news li, .description li');
        $.each(arr, function() {
            $('p:last', this).append($('a.more', this));
        });
    }
}

var fileUpload = {

    SetFileUpload: function () {
        $('#trigger').button();
        $('.file').fadeTo(0, 0.0);
        $(".TheTrigger").mousemove(function (e) {
            var offset = $('#dialog-CV').offset();
            $('.file').css('top', e.pageY - offset.top - 20);
            $('.file').css('left', e.pageX - offset.left);
        });
        $(".file, .TheTrigger").mouseover(function (e) {
            $('.TheTrigger').addClass('ui-state-hover');
        });
        $(".file, .TheTrigger").mouseout(function (e) {
            $('.TheTrigger').removeClass('ui-state-hover');
        });
        $(".file").change(function (e) {
            var msg = $(".file").val();
            msg = msg.substr((msg.lastIndexOf('\\') + 1));
            $('#fileName').text(msg);
            if (msg.length > 0)
                $('.fileNameMsg').show();
            else
                $('.fileNameMsg').hide();
        });
    }
}


// Common initializations
$(document).ready(function () {
    nav.init();
    news.init();
});

// Utils
function getRandomItem(el) {
    return $(el)[Math.floor(Math.random() * $(el).length)];
};
