function log(mssg){
    if(typeof(console) === 'object'){
        console.log(mssg);
    }
    return true;
}

String.prototype.trim = function() {
    return this.replace(/^\s+|\s+$/g,"");
}


function submitForm(form_id){
    var elem = document.getElementById(form_id);
    if (elem){
        elem.submit();
    }
    return true;
}


$(function(){
    //alert('Hi');
    $('#top_menu ul li').hover(
        function(){
            $(this).addClass('show');
        },
        function(){
            $(this).removeClass('show');
        }
        );

    /* indexinio paveiksliuko animacija */

    var allbublelis = $('ul.buble li');
    var all_items = $('#contFlash .item');
    var curent_buble = 1;
    var buble_interval = 3000;
    var inbublet = null;

    $('.buble li').mouseover(function(e){
        $(this).addClass('active');
    });

    $('.buble li').mouseout(function(e){
        $(this).removeClass('active');
    });

    $('.buble a').click(function(e){
        clearTimeout ( inbublet );
        inbublet = null;
        e.preventDefault();
        var id = $(this).attr('href').replace('#','');
        do_buble_step(id);
        setTimeout(function(){
            curent_buble = id;
            start_buble_interval();
        }, 3000);
    });

    do_buble_step(1);
    start_buble_interval();

    function start_buble_interval(){
        if(typeof inbublet == 'number'){
            clearTimeout ( inbublet );
        }

        inbublet = setInterval ( function(){
            var next_buble = curent_buble++;

            if(next_buble == 5){
                curent_buble = 1;
            }

            do_buble_step(next_buble);
        }, buble_interval );
    }

    //
    function do_buble_step(id){
        var active_li = $('ul.buble li.buble_li'+id);
        var active_item = $('#contFlash .item'+id);

        if(active_li.is('.current')){
        //li is active wtf do you want?
        }else{
            allbublelis.removeClass('current');
            active_li.addClass('current');
            all_items.css('display', 'none');
            active_item.css('display', 'block');
        }
    }


});


function ExtendActions(){
/*
     var elem = document.getElementById("footer_text_id");
     if(elem){
       elem.style.bottom = "88px";
     }

     elem = document.getElementById("copyright_id");
     if(elem){
       elem.style.bottom = "30px";
     }*/
}


function PageExtender(used_height){
    var elemet_id = "page_extender";
    var elem = null;
    if(elem = document.getElementById(elemet_id)){
        //alert('Your resolution is '+screen.width+'x'+screen.height);

        var w, h;
        if (self.innerHeight) // all except Explorer
        {
            w = self.innerWidth;
            h = self.innerHeight;
        }
        else if (document.documentElement && document.documentElement.clientHeight)
        // Explorer 6 Strict Mode
        {
            w = document.documentElement.clientWidth;
            h = document.documentElement.clientHeight;
        }
        else if (document.body) // other Explorers
        {
            w = document.body.clientWidth;
            h = document.body.clientHeight;
        }
        //alert("W: "+ w + " H: " + h);
        var diff = h - used_height;
        //alert(diff);
        if(diff < 0) diff = 0;
        elem.style.height = diff +"px";
        ExtendActions();
    }
}


function expandCollapse() {
    for (var i=0; i<expandCollapse.arguments.length; i++) {
        var element = document.getElementById (expandCollapse.arguments[i]);
        element.style.display = (element.style.display == "none") ? "block" : "none";
    }
}

function ExecuteOnLoadFunctions(){
    /*ReplaceSelect('kamSkirta');
	ReplaceSelect('laikoIntervalai');	*/
    setForm();//replace all select to <ul>
}

function ScrollToElement(elem_id){
    var elem = document.getElementById(elem_id);
    if(elem){
        elem.scrollIntoView();
    }
}

function showActiveContacts(){
    //alert('ok');
    var anchor = self.document.location.hash.substring(1);
    //alert(anchor);
    if(anchor && anchor !== ''){
        anchor = 'i'+anchor;
        /*
		var elem = document.getElementById(anchor);
		if(elem){
			alert('OK');
		}*/
        $('#'+anchor+' .on').css('display', 'none');
        $('#'+anchor+' .off').css('display', 'block');
    }
}

$(function(){

    var calendar = $('#calendarDiv');
    var bubble = $('#calendar_bubble');
    var top = $('.top', bubble);
    var bottom = $('.bottom', bubble);

    if(calendar.length){

        var calendar_bubble = {

            cal: calendar,
            days: null,
            data: {},
            obj: null,
            on_bubble: false,

            init: function(){
                var self = this;
                this.days = $('a.type1, a.type2, a.type3', calendar);

                this.days.live({
                    mouseenter: function(){
                        self.obj = $(this);
                        self.on_bubble = true;
                        self.mousein();
                    },
                    mouseleave: function(){
                        self.obj = $(this);
                        self.on_bubble = false;
                        self.mouseout();
                    }
                });

                bubble.live({
                    mouseenter: function(){
                        self.on_bubble = true;
                    },
                    mouseleave: function(){
                        self.on_bubble = false;
                        self.hide_bubble();
                    }
                });
            },

            mousein: function(){
                var self = this;
                var day = String(self.day(self.obj));
                if(typeof(this.data[day]) == 'undefined'){
                    $.ajax({
                        type: "POST",
                        dataType: 'json',
                        url: window.location.href,
                        data: {
                            'day': day,
                            'action': 'calendar-day-events'
                        },
                        success: function(response){
                            if(typeof(response) === 'object'){
                                var status = parseInt(response.status);
                                if(status === 1){
                                    response.show_bubble = ($.trim(response.html) == 'null') ? false : true;
                                    response.offset = self.obj.offset();
                                    self.data[day] = response;
                                    self.show_bubble(response);
                                }
                            }
                        }
                    });
                }else{
                    this.show_bubble(self.data[day]);
                }
            },
            mouseout: function(){
                this.hide_bubble();
            },

            day: function(j){
                var href = j.attr('href').split('/');
                var last_index = parseInt(href.length - 1);
                return href[last_index].replace('day', '');
            },

            show_bubble: function(data){
                if(!data.show_bubble){
                    return false;
                }

                bottom.html(data.html);
                top.html(data.today_title);
                bubble.show();
                
                var new_offset = {
                    top: data.offset.top - bubble.height() - 2,
                    left: data.offset.left - parseInt(bubble.width() / 2) + 16
                };
                bubble.offset(new_offset);
                return true;
            },

            hide_bubble: function(){
                var self = this;
                setTimeout(function(){
                    if(!self.on_bubble){
                        bubble.hide();
                    }
                }, 500);
            }



        };
        calendar_bubble.init();
    }
});
