
jQuery.preloadImages = function()
{
    for(var i = 0; i<arguments.length; i++)
    {
        jQuery("<img>").attr("src", arguments[i]);
    }
}

var margin_right    = 10;
var container_width = 541;

// initial: if no votes, "No votes yet", disable all faces
//          if votes, from face to 0, make them big
//
// face hover: disabled if voted, hover from face to 0 if hasn't voted
// face not hovered: 

var advertise = false;

function advertise_toggle()
{
    if (advertise) advertise_hide();
    else advertise_show();
}
function advertise_hide()
{
    $("#advertise_slider").hide();
    $("#advertise_shade").hide();
    advertise = false;
    //window.location = "#";
}
function advertise_show()
{
    $("#advertise_slider").show();
    $("#advertise_shade").show();
    advertise = true;
    
    $("#advertise_shade").css("height", ($("body").height()-45)+"px");
    $("#advertise_slider").css("top", ($("body").height()-178-45)+"px");

    //window.location = "#advertise";
}
function face_defaults()
{
    for (var i = 1; i <= 5; i++)
    {
        $("#grandma_face_"+i).attr({src: "/images/grandma_face_off.png"});
    }
}
function has_voted_hover()
{
    $("#super_awesome").html("You have already voted");

    $("#super_awesome_original").hide();
    $("#super_awesome").show();
}
function has_voted_dehover()
{
    $("#super_awesome").hide();
    $("#super_awesome_original").show();
}
function face_hover(idx)
{
    if (!disable_faces)
    {
        face_defaults();

        for (var i = idx; i >= 0; i--)
        {
            $("#grandma_face_"+i).attr({src: "/images/grandma_face_hover.png"});
        }
        
        $("#super_awesome_original").hide();
        $("#super_awesome").show();

        switch(idx)
        {
            case 1:
                $("#super_awesome").html("1 Old Dying Grandma");
                break;
            case 2:
                $("#super_awesome").html("2 Crappy Grandmas");
                break;
            case 3:
                $("#super_awesome").html("3 Semi-Interesting Grandmas");
                break;
            case 4:
                $("#super_awesome").html("4 Legit Grandmas");
                break;
            case 5:
                $("#super_awesome").html("5 Super Awesome Grandmas");
                break;
        }
    }
}
function face_dehover(idx)
{
    if (!disable_faces)
    {
        face_defaults();

        // handle halfs here
        for (var i = idx; i >= 0; i--)
        {
            $("#grandma_face_"+i).attr({src: "/images/grandma_face_off.png"});
        }

        $("#super_awesome").hide();
        $("#super_awesome_original").show();
    }
}

function faces_dehover()
{
    if (!disable_faces)
    {
        if (stars != "none" && !has_voted)
        {
            // handle halfs here
            /*
            for (var i = 1; i <= stars; i++)
            {
                if (i == (stars-0.5))
                {
                    $("#grandma_face_"+i).attr({src: "/images/grandma_face_half.png"});
                }
                else $("#grandma_face_"+i).attr({src: "/images/grandma_face_hover.png"});
            }*/

            for (var i = 1; i <= 5; i++)
            {
                if (stars == (i-0.5))
                {
                    $("#grandma_face_"+i).attr({src: "/images/grandma_face_half.png"});
                }
                else if (stars >= i)
                {
                    $("#grandma_face_"+i).attr({src: "/images/grandma_face_hover.png"});
                }
            }
        }
    }
}

function update_calendar()
{
    $("#calendar_blacked").html("<div id='loading'>Loading...</div>");

    $.ajax({
      type: "POST",
      url: "/calendar.php",
      data: {page: page},
      success: function(msg)
      {
        $("#calendar_blacked").html(msg);
      }
    });
}
function page_go(new_page)
{
    $("#page_"+page).removeClass("page_current");
    page = new_page;
    $("#page_"+page).addClass("page_current");
    update_calendar();
}
function page_prev()
{
    if (page > 1)
    {
        $("#page_"+page).removeClass("page_current");
        page--;
        $("#page_"+page).addClass("page_current");
        update_calendar();
    }
}
function page_next()
{
    if (page < total_pages)
    {
        $("#page_"+page).removeClass("page_current");
        page++;
        $("#page_"+page).addClass("page_current");
        update_calendar();
    }
}
var disable_faces = false;

function super_awesome_to_new()
{
    var contents = $("#super_awesome_original").html();

    if (contents == "No votes yet")
    {
        $("#super_awesome_original").html("1 Vote");
    }
    else
    {
        var previous = $("#num_vote").html();
        previous++;
        $("#super_awesome_original").html(previous+" Votes");
    }
}

function face_vote(stars, id_video)
{
    if (!disable_faces)
    {
        $.ajax({
          type: "POST",
          url: "/vote.php",
          data: {stars: stars, id_video: id_video},
          success: function(msg)
          {
            if (msg == "okay")
            {
                $("#super_awesome").hide();
                $("#super_awesome_original").show();

                super_awesome_to_new();

                face_defaults();
                
                for (var i = 1; i <= stars; i++)
                {
                    $("#grandma_face_"+i).attr({src: "/images/grandma_face_hover.png"});
                    disable_faces = true;
                }
            }
          }
        });
    }
}

var current_thumb = -1;

function update_thumb()
{
    var offset = 14;

    if (current_thumb != -1)
    {
        $("#thumb_hover_"+current_thumb).css("top", (mouse_y+offset)+"px");
        $("#thumb_hover_"+current_thumb).css("left", (mouse_x+offset)+"px");
    }
}

function month_width(idx)
{
    return $("#month_0").width();
}

function thumb_hover(idx)
{
    current_thumb = idx;
    $("#thumb_hover_"+idx).show();
}

function thumb_hover_stop(idx)
{
    $("#thumb_hover_"+idx).hide();
    current_thumb = -1;
}

var search_cleared = false;

function clear_search_once()
{
    if (!search_cleared)
    {
        search_cleared = true;
        $("#search_text").val("");
        $("#search_text").removeClass("search_ghosted");
    }
}

function clear_search_back()
{
    /*
    search_cleared = false;
    $("#search_text").addClass("search_ghosted");
    $("#search_text").val("Find an old entry...");
    */
}

function select_link()
{
    $("#text_link").select();
    $("#text_link").copy();
}
function select_embed()
{
    $("#embed_link").select();
    $("#embed_link").copy();
}

var month_nav_offset = 0;
var month_nav_rate   = 8;
var month_nav_time   = 25;
var month_nav_slide  = true;

function month_nav_go()
{
    month_nav_slide = true;
}
function month_nav_left()
{
    if (month_nav_slide)
    {
        month_nav_offset = month_nav_offset - month_nav_rate;
        $("#month_nav_slider").css("left", month_nav_offset);
        setTimeout("month_nav_left()", month_nav_time);
    }
}
function month_nav_right()
{
    if (month_nav_slide)
    {
        month_nav_offset = month_nav_offset + month_nav_rate;
        $("#month_nav_slider").css("left", month_nav_offset);
        setTimeout("month_nav_right()", month_nav_time);
    }
}
function month_nav_stop()
{
    month_nav_slide = false;
}

$.preloadImages("/images/powered_lit.png", "/images/blue_90.png", "/images/grandma_face_hover.png", "/images/close_yellow.png");

