/*******************************************
* Menu functions (search, server messages) *
* UbiCast 2009 - 2012, all rights reserved *
* Author: Stephane Diemer                  *
*******************************************/

var search_message_words = "Search words must have 3 characters at least";
var search_message_filters = "You need to choose at least one filter";

function display_search_message(text) {
    $("#search_tooltip").remove();
    
    var top = $("#search_back #id_search").offset().top + 30;
    var left = $("#search_back #id_search").offset().left - 48;
    
    var tooltip = "<div id=\"search_tooltip\" class=\"chart-tooltip\" ";
    tooltip += " style=\"left: " + left + "px; top: " + top + "px;\"";
    //tooltip += " onmouseover=\"javascript: $(this).fadeOut('fast');\"";
    tooltip += "><div class=\"warning\">" + text + "</div>";
    tooltip += "</div>";
    $("body").append(tooltip);
    
    setTimeout(function () {
        $("#search_tooltip").fadeOut("fast");
    }, 4000);
}
function check_search() {
    var value = $("#search_back #id_search").attr("value");
    var regexp = new RegExp(" ", "g");
    value = value.replace(regexp, "");
    //alert(value);
    
    if (value.length < 3) {
        display_search_message(search_message_words);
        return false;
    }
    else if ($("#search_filters #search_videos_input").val() != "on" && $("#search_filters #search_chapters_input").val() != "on" && $("#search_filters #search_audio_input").val() != "on") {
        display_search_message(search_message_filters);
        return false;
    }
    else {
        return true;
    }
}
function toggle_search_filter(id) {
    if ($("#search_filters #search_" + id + "_input").attr("value") == "on") {
        $("#search_filters #search_" + id + "_input").attr("value", "off");
        $("#search_filters #search_" + id + "_button").removeClass("enabled");
    }
    else {
        $("#search_filters #search_" + id + "_input").attr("value", "on");
        $("#search_filters #search_" + id + "_button").addClass("enabled");
    }
}


function set_server_message(id, content) {
    var title = " ";
    var text = content;
    var splitted = content.split("\n");
    if (splitted.length > 0) {
        title = splitted[0];
        splitted.shift();
        text = splitted.join("");
    }
    
    var html = "<div id=\"maintenance_message\" style=\"display: none;\">";
    html += "<div class=\"title\">"+title+"</div>";
    html += "<div class=\"text\">"+text+"</div>";
    html += "</div>";
    $("body").append(html);
    
    if (id != get_last_server_message_id())
        overlay.show({
            mode: "html",
            title: title,
            html: "<div class=\"overlay-paragraph\">"+text+"</div>"
        });
    
    set_cookie("maintenance_message", id, 10);
}
function display_server_message() {
    var title = $("#maintenance_message .title").html();
    var text = $("#maintenance_message .text").html();
    if (text)
        overlay.show({
            mode: "html",
            title: title,
            html: "<div class=\"overlay-paragraph\">"+text+"</div>"
        });
}
function get_last_server_message_id() {
    var cookie = get_cookie("maintenance_message");
    if (cookie)
        return cookie;
    else
        return null;
}


