$(function() {
    $("#search-input").click(function(e) {
            e.stopPropagation();
            $("#search-options").fadeIn("fast");
        });
    $("#search-options").click(function(e) {
            e.stopPropagation();
        });
    $(document).click(function() {
            $("#search-options").fadeOut("fast");
        });
    $(".tooltip").tooltip({showURL: false});
    if (!window.location.href.match(/events\//))
        $("img").lazyload();
    //setTimeout('$("img").lazyload();', 1000);
    
    // sliders
            $("div.slider")
                .css({
                        'margin' : '0', 
                        'padding' : '0', 
                        'overflow' : 'auto', 
                        'height' : '18px',
                        'border' : 'none'
                    })
                .each(function() {
                        var div = $("<div style='width: " + ($(this).width() + pageCount - 1) + "px; font-size: 1px; height: 1px;'><!----></div>");
                        $(this).append(div);
                        this.scrollLeft = page - 1;
                    })
                .scroll(function() {
                        $('.button-goto-page').attr('href', rootURL + '/page/' + (this.scrollLeft + 1));
                        var newValue = (this.scrollLeft + 1);
                        $('input.input-goto-page').each(function() {
                                if (this.value != newValue)
                                    this.value = newValue;
                            });
                    });
            $(".input-goto-page")
                .each(function() {
                        this.value = page;
                    })
                .change(function() {
                        $('.button-goto-page').attr('href', rootURL + '/page/' + this.value);
                        var newValue = this.value - 1;
                        $('div.slider').each(function() {
                                if (this.scrollLeft != newValue)
                                    this.scrollLeft = newValue;
                            });
                    });
});

function parseGetParams() {
    var result = {};
    if (window.location.href.indexOf("?") > -1) {
            var params = window.location.href.split("?")[1];
            params = params.split("&");
            for (var i = 0; i < params.length; i++) {
                    var param = params[i].split("=");
                    result[param[0]] = param[1];
                }
        }
    return result;
}

function getPrevSibling(el) {
    el = el.previousSibling;
    while(el.innerHTML == null)
        el = el.prevSibling;
    return el;
}

function selectLang(lang) {
    createCookie("lang", lang, 1000);
    window.location = window.location;
}


//////////// cookie management
function createCookie(name, value, days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    } else {
        var expires = "";
    }
    document.cookie = name + "=" + value + expires + "; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}