var MORE_INFO_URL = "/wikitap/mi/";
function show_hide_video_desc()
{
    var vdesc_id = document.getElementById("videodesc");
    if ($(vdesc_id).is(":hidden"))
    {
        $(document.getElementById("videodesc").getElementsByTagName("embed")).show()
        $(vdesc_id).slideDown("slow");
    }
    else
    {
        $(document.getElementById("videodesc").getElementsByTagName("embed")).hide()
        $(vdesc_id).slideUp("slow");
    }
}
function resize_iframe()
{
    var wikiframe = document.getElementById("wikiframe");
    if (wikiframe)
    {
        var window_size = get_window_size()
        var wiki_frm_top = wikiframe.offsetTop;
        var tot_height = window_size.height - wiki_frm_top;
        tot_height = tot_height - 20;
        $(wikiframe).height(tot_height);
    }
}

function realign_clip_detail(bottom_bar_height)
{
    var cd = document.getElementById("clip_detail");
    var win = get_window_size();
    if (!bottom_bar_height)
        bottom_bar_height = 80; 
    var top = win.height - 465 - bottom_bar_height;
    if ( top > 0)
    {
        top = top / 2;
    }
    else
    {
       /* var wikiframe = document.getElementById("wikiframe");
        top = wikiframe.offsetTop;*/
        top = 0;
    }
    var width = win.width - 465;
    if (width > 0)
    {
        width = width /2;
    }
    else
    {
        width = 0;
    }
    $(cd).css({"top":top, "left":width});
}
function get_window_size() {
    var myWidth = 0, myHeight = 0;
    if( typeof( window.innerWidth ) == 'number' ) {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    return {"width":myWidth, "height":myHeight}
}
function toggle_related_videos()
{
    var sr = document.getElementById("search_results");
    $(sr).toggle();

}
function initialize()
{
    resize_iframe();
    realign_clip_detail();
    // close the clip detail if escape key is pressed
    $(document).keyup(function(event){
        if (event.keyCode == 27) {
            hide_clip_detail()
        }
    });

    // for storing the previous results. 
    $.stack = [];

    var sr = document.getElementById("results_page");
    $(sr).show("slide", { direction: "right" }, 600);
    highlight_results();
    //let's try this
    get_next_results();
}
function highlight_results()
{
    $(".search_img").hover(function(){$(this).css({ border:"1px solid yellow" })},function(){$(this).css({ border:"1px solid #333333"})});
}
function show_more_info(gid)
{
    $.get(MORE_INFO_URL+gid, function(data){
        var vdesc_id = document.getElementById("clip_detail");
        if ($(vdesc_id).is(":hidden"))
        {
            //$(vdesc_id).show();
            $(vdesc_id).fadeIn();
        }
        else
        {
            vdesc_id.innerHTML = "";
        }
        vdesc_id.innerHTML = data;
    });
}
function hide_clip_detail()
{
    var vdesc_id = document.getElementById("clip_detail");
    //$(vdesc_id).hide();
    $(vdesc_id).fadeOut();
}
function get_next_results()
{
    var rid = document.getElementById("od_tag");
    if($(rid).attr("od"))
    {
        var url = $(rid).attr("od");
        $.get(url, function(data){
            if(data.indexOf("search_result") != -1)
            {
                var results_sctn = document.getElementById("search_results");
                $(results_sctn).show();
            }
            var results_id = document.getElementById("results_page");
            $.stack.push(results_id.innerHTML);
            $(results_id).hide("slide", { direction: "left" }, 600);
            results_id.innerHTML = data;
            $(results_id).show("slide", { direction: "right" }, 600);
            highlight_results();
        });
    }
}
function get_prev_results()
{
    var results_id = document.getElementById("results_page");
    var data = $.stack.pop();
    if (data)
    {
        $(results_id).hide("slide", { direction: "right" }, 600);
        results_id.innerHTML = data;
        $(results_id).show("slide", { direction: "left" }, 600);
        highlight_results();
    }
}
