jQuery.noConflict();
jQuery(document).ready(function($){
    $("table.vsup-news:not(table.vsup-news-frontpage)").parent("div.moduletable").addClass('module-background');
    $("div.events:not(div.events-frontpage, div.events-month)").parent("div.moduletable").addClass('module-background');

    // Editor
    $("a.mceButtonDisabled img").hide();

    // Nameday
    $("div.people img.nameday").each(function() {
       var left = $(this).position().left;
       var top = $(this).position().top;
       top = top + 35;
       left = left + 10;
       $(this).after('<div class="bubble-info" style="left: ' + left + 'px; top: ' + top + 'px;">Svátek</div>');
    });

    // Update
    $("div.people img.updated").each(function() {
       var top = $(this).position().top;
       var left = $(this).position().left;
       var h = $(this).height();
       var w = $(this).width();
       top += h - 15;
       left += 5;
//       $(this).after('<div class="bubble-info updated" title="updated" style="left: ' + left + 'px; top: ' + top + 'px;"></div>');
       $(this).css("border-bottom", "red dotted 1px");
       $(this).css("margin-bottom", "12px");
//       $('div.bubble-info').css("opacity", "0.75");
    });

    // Menu
    $("div#left").hover(
        function() {
            // hide items in active menu
            $("ul.menu li.active:not(li.item1)").siblings("li").animate({
                opacity:"1"
            }, {
                queue:false,
                duration:200
            } );

            // hide inactive menus
            $("ul.menu").each(function(){
                if ( $(this).children("li.active").size() == 0 && !$("li.item1").hasClass("active") ) $(this).animate({
                        opacity:"1"
                    }, {
                        queue:false,
                        duration:200
                    } );
            });
        },
        function() {
            $("ul.menu li.active:not(li.item1)").siblings("li").animate({
                opacity:"0"
            }, {
                queue:false,
                duration:200
            } );

            $("ul.menu").each(function(){
                if ( $(this).children("li.active").size() == 0 && !$("li.item1").hasClass("active") ) $(this).animate({
                        opacity:"0"
                    }, {
                        queue:false,
                        duration:200
                    } );
            });
        }
        );

    /* Hide "Home" item in Main menu */
    $("ul.main-menu li.item1").hide();
    $("ul.menu li span.separator").css("opacity", "0");

    /**********************************************************************/
    /******************************** Events *******************************/

    $("li.vsup-news").hover(
        function() {
             $("div.bubble").stop().animate({
                opacity: 1.0
                }, 150);
            var title = $(this).children('a:first').attr('title');
            $("div.bubble div.bubble-content").text(title);
            $("div.bubble").css("background-color", personsColors[ rand(0, personsColors.length-1)]);

        },
        function() {
             $("div.bubble").stop().animate({
                opacity: 0
                }, 150);
        }
        );




    $("a.vsup-news").hover(
        function() {
            var ref = $(this).attr("href");
            if ($("div.events a[href=" + ref + "] span.event-label").css("opacity", "1").size() > 0) {
//                $("div#events div.events-day").css("opacity", "1");
            }
        },
        function() {
            var ref = $(this).attr("href");
            $("div.events a[href=" + ref + "] span.event-label").css("opacity", "0");
//            $("div#events div.events-day").css("opacity", "0");
        }
        );

    $("div.events a").hover(
        function() {
            var ref = $(this).attr("href");
            $("a.vsup-news[href=" + ref + "]").parent("td").addClass("highlight");
        },
        function() {
            var ref = $(this).attr("href");
            $("a.vsup-news[href=" + ref + "]").parent("td").removeClass("highlight");
        }
    );

    // Events hide on start
    $("div.events:not(.events-month) span.event-label").css("opacity", "0");
    $("div.events:not(.events-month) div.events-day").css("opacity", "0");
    $("div.events span.vsup-news-all").css("opacity", "0");

    // Events fading
    $("div.events").hover(
        function() {
            $("div.events:not(.events-month) span.event-label, div.events:not(.events-month) div.events-day, div.events span.vsup-news-all").animate( {
                opacity:"1"
            }, {
                queue:false,
                duration:100
            } );
        },
        function() {
            $("div.events:not(.events-month) span.event-label, div.events:not(.events-month) div.events-day, div.events span.vsup-news-all").animate( {
                opacity:"0"
            }, {
                queue:false,
                duration:100
            } );
        }
        );


    
    $("ul.vsup-news li").hover( 
        function() { 
            $(this).addClass("highlight");
        }, 
        function() { 
            $(this).removeClass("highlight"); 
        } 
    );

    /**********************************************************************/
    /********************************* Persons - Bubble ******************************/
    // bubble invisible at begin
    $("div.bubble").animate({
        opacity: 0.0
    }, 0);

    var personsColors = new Array();
    personsColors["department1"] = "#E06156";
    personsColors["department2"] = "#00B5F1";
    personsColors["department3"] = "#00AE6A";
    personsColors["department4"] = "#EE4F53";
    personsColors["department5"] = "#B7A04A";
    personsColors["department6"] = "#9C539B";

    // hover over gallery image
    $("div.gallery img").hover(
        function () {
            $("div.bubble").css('display', 'block').stop().animate({
                opacity: 1.0
            }, 150);
            var title = $(this).attr("alt");
//            var title = '<span class="bubble-head">' + personsNames[ rand(0, personsNames.length-1) ];
            $("div.bubble div.bubble-content").html(title);
            $("div.bubble").css("background-color", 'gray');
        },
        function () {
             $("div.bubble").stop().animate( {opacity:0}, 150, function(){  $(this).css('display', 'none');  } );
        }
        );


    // bubble position
    $().mousemove(function(e){
        $("div.bubble").css("left", e.pageX+15);
        $("div.bubble").css("top", e.pageY+15);

        $("div.bubble-profile").css("left", e.pageX+15);
        $("div.bubble-profile").css("top", e.pageY+29);
    });


    /**********************************************************************/
    /********************************* Utils ******************************/
   
    //    random int
    function rand(from, to) {
        return Math.round( Math.random() * (to - from) + from );
    }

    // interval
    jQuery.timer = function (interval, callback)
    {

        var interval = interval || 100;

        if (!callback)
            return false;

        _timer = function (interval, callback) {
            this.stop = function () {
                clearInterval(self.id);
            };

            this.internalCallback = function () {
                callback(self);
            };

            this.reset = function (val) {
                if (self.id)
                    clearInterval(self.id);

                var val = val || 100;
                this.id = setInterval(this.internalCallback, val);
            };

            this.interval = interval;
            this.id = setInterval(this.internalCallback, this.interval);

            var self = this;
        };

        return new _timer(interval, callback);
    };

});

