$(function () {
        
/* Clicking the home boxes */
    $("div.firstHomeBox, div.homeBox, div#event").hover(
        function () { $(this).addClass('hovered'); }, 
        function () { $(this).removeClass('hovered'); }
    ).click(
        function (e) {
            e.preventDefault(); e.stopPropagation(); 
            $(this).removeClass('hovered'); 
            location.href = $(this).find('h4 a').get(0).href;   
        }
    ); 
    
/* Galleries */
    (function () {
        var lightBoxOptions = {
            imageLoading: '/system/modules/ro.grapefruit.ispe.website/resources/images/lightbox/loading.gif',
            imageBtnClose: '/system/modules/ro.grapefruit.ispe.website/resources/images/lightbox/close.gif',
            imageBtnPrev: '/system/modules/ro.grapefruit.ispe.website/resources/images/lightbox/prev.gif',
            imageBtnNext: '/system/modules/ro.grapefruit.ispe.website/resources/images/lightbox/next.gif',
            fixedNavigation:true
        }; 
        $('ul.gallery a').lightBox(lightBoxOptions); 
        $('ul.locations li a.map').each(function () {
            $(this).lightBox(lightBoxOptions).append($('<span class="plus">+</span>')); 
        });
    })(); 
    
/* Open links in new window */
    $('a.open-new-window').click(function () {
        window.open($(this).attr('href')); 
        return false; 
    })
    
/* Homepage headers */
if ($('#center.homepage').size() > 0) {
    // $('#center.homepage > img').remove();
    var homepageSlideshowTimeoutId; 
    function homepageSlideshowTimeoutCallback() {
        var activeLI = $('#center ul.domains li a.active').parent('li');
        var nextLI; 
        if (activeLI.is(':last-child')) {
            nextLI = $('#center ul.domains li:first'); 
        } else {
            nextLI = activeLI.next('li');  
        }
        nextLI.find('a').click(); 
    }
    function resetTimeout() {
        if (homepageSlideshowTimeoutId) clearTimeout(homepageSlideshowTimeoutId); 
        homepageSlideshowTimeoutId = setTimeout(homepageSlideshowTimeoutCallback, 5000); 
    }
    $('#center.homepage ul.domains li a').each(function () {
        var $this = $(this); 
        var $image = $(this).find('img'); 
        var currentImage = -1; 
        function imageClick() {
            location.href = $this.get(0).href; 
        }
        $image.css({ cursor: 'pointer' }).click(imageClick); 
        $this.click(function (e) {
            if ($this.hasClass('active')) return true; 
            e.preventDefault(); e.stopPropagation(); 
            $('#center ul.domains li a').removeClass('active');
            var $fadeOutImage = 
                $('#center > img').not('.fadeOut')
                    .addClass('fadeOut')
                    .animate(
                        { opacity: 0 }, 
                        { 
                            duration: 333, 
                            complete: function () { 
                                $fadeOutImage.remove(); 
                                $fadeOutImage.removeClass('fadeOut'); 
                                $fadeOutImage.css({ opacity: 1 }); 
                            }
                        }
                    )
                ; 
            currentImage++; 
            if (currentImage >= $image.size()) currentImage -= $image.size(); 
            $('#center').prepend($image.get(currentImage)); 
            $this.addClass('active'); 
            $this.get(0).blur(); 
            resetTimeout(); 
            return false; 
        }); 
    }); 
    $('#center ul.domains li:first a').click();
}

/* Homepage H4 heights */
   var maxHeight = 0; 
   $('div.firstHomeBox h4, div.homeBox h4, #event h4').each(function () {
       maxHeight = Math.max($(this).height(), maxHeight); 
   }); 
   $('div.firstHomeBox h4, div.homeBox h4, #event h4').height(maxHeight); 
    
/* Sidebar project teasers */
    var $ul, i = 0, selected; 
    $('#sidebarRight .projects').append($ul = $('<ul class="prjNumber"></ul>')); 
    $('#sidebarRight .projects .project').each(function () {
        i++; 
        var $this = $(this); 
        $ul.append(
            $('<li></li>').append(
                $('<a href="#">'+i+'</a>').click(function (e) {
                    e.stopPropagation(); 
                    if (selected) selected.hide();  
                    selected = this; 
                    selected.show();  
                    return false; 
                }).each(function () {
                    this.show = function () {
                        $(this).addClass('selected'); 
                        $this.show(); 
                    }; 
                    this.hide = function () {
                        $(this).removeClass('selected'); 
                        $this.hide(); 
                    }
                    if (i == 1) { this.show(); selected = this; } 
                    else this.hide(); 
                })
            )
        ); 
    });
    
/* Search label in the field */
    var $searchLabel = $("#search label").hide(); 
    var $searchField = $("#search input.searchText"); 
    var searchTeaserText = $searchLabel.text(); 
    $searchField
        .focus(function () {
            if ($searchField.attr("value") && $searchField.attr("value").replace(/^\s*|\s*$/, "") == searchTeaserText) $searchField.attr("value", ""); 
        })
        .blur(function () {
            if ($searchField.attr("value") == null || $searchField.attr("value").replace(/^\s*|\s*$/, "") == "") $searchField.attr("value", searchTeaserText); 
        })
        .attr("value", searchTeaserText)
    ;
    
/* Adding the pring button */
    if (!$('body').hasClass('no-print')) {
        $("#footer").prepend(
            $('<p class="print">'+(footer_print_label)+'</p>')
                .click(function () {
                    window.print(); 
                })
                .css({ cursor: 'pointer' })
        );
    }

/* Back link */
    if (location.href.match(new RegExp("\\bnoutati/evenimente/.+"))) {
        $('#tertiaryMenu li.active ul')
            .append(
                $('<li></li>')
                    .append(
                        $('<a href="#">Înapoi la listă</a>')
                            .click(function () { window.history.back(-1); return false; })
                    )
            )
        ;
    }

});  
