var em_div;
var em;
var second_header;
var cycling_image;
var cycling_image_temp;
var image_path;
var images = new Array();
var thin_header = false;

function common_init()
{
    common_external_links();

    em_div = document.getElementById("common_em");
    em = em_div.offsetHeight;
    common_get_second_header();
    cycling_image = document.getElementById("common_photos");
    cycling_image_temp = document.getElementById("common_cycling_image_temp");

    common_cycle_images();

    common_resize();
    window.onresize = common_resize;
    common_detect_font_change();
}

function common_cycle_images()
{
    var initial_image = cycling_image.getAttribute("src");

    var path_elements = new Array();
    path_elements = initial_image.split("/");
    images.push(path_elements.pop());
    image_path = path_elements.join("/");

    var div = document.getElementById("common_more_photos");
    var image_spans = div.getElementsByTagName("span");
    for(var i=0; i<image_spans.length; ++i)
    {
        var filename = image_spans[i].firstChild.nodeValue;
        images.push(filename);
    }

    common_change_image(true);
}

function common_change_image(first_run)
{
    var randValue = Math.floor(Math.random() * images.length);
    var image = images[randValue];
    if(!first_run)
    { cycling_image.src = cycling_image_temp.getAttribute("src"); }
    cycling_image_temp.src = image_path+"/"+image;

    setTimeout("common_change_image();", 6000);
}

function common_get_second_header()
{
    if(document.getElementById("main_news"))
    { second_header = document.getElementById("main_news").getElementsByTagName("table")[0]; }
    else if(document.getElementById("secondary_header"))
    {
        thin_header = true; 
        second_header = document.getElementById("secondary_header").getElementsByTagName("table")[0];
    }
}

function common_external_links()
{
    var test_string = "testing";
    if(!test_string.match)
    { return; }

    var links = document.getElementsByTagName("a");
    for(var i=0; i<links.length; ++i)
    {
        var external = false;
        if(links[i].getAttribute("rel") == "external" || links[i].getAttribute("href").match(/^http/) && !links[i].getAttribute("href").match(document.domain))
        { links[i].target = "_blank"; }
    }
}

function common_detect_font_change()
{
    var new_em = em_div.offsetHeight;
    if(new_em != em)
    {
        em = new_em;
        window.onresize();
    }
    setTimeout("common_detect_font_change()", 1000);
}

function common_resize()
{
    second_header.style.position = "static";
    second_header.style.top = null;
    second_header.style.height = null;
    second_header.style.width = null;
    cycling_image.style.top = null;
    cycling_image.style.position = "static";

    if(thin_header)
    {
        var crumb_element = document.getElementById("secondary_crumbs");
        var left_margin = common_findPosX(document.getElementById("common_linkbar"));
        var header_top = common_findPosY(second_header);
        second_header.style.position = "absolute";
        second_header.style.left = left_margin-2+"px";
        second_header.style.top =
                common_findPosY(crumb_element) -
                second_header.clientHeight +
                2 +
                "px";
        second_header.style.width =
                common_findPosX(cycling_image) -
                common_findPosX(second_header) +
                2 +
                "px";
    }
    else
    {
        var image_bottom = common_findPosY(cycling_image)+cycling_image.clientHeight;
        var header_bottom = common_findPosY(second_header)+second_header.clientHeight;
        if(header_bottom <= image_bottom)
        { second_header.style.height = second_header.clientHeight+(image_bottom-header_bottom)+2+"px"; }
        else if(header_bottom - image_bottom <= 100)
        {
            var image_top = common_findPosY(cycling_image);
            cycling_image.style.position = "relative";
            cycling_image.style.top = header_bottom-image_bottom+"px";
        }
        var image_left = common_findPosX(cycling_image);
        var header_left = common_findPosX(second_header);
        second_header.style.width = (image_left-header_left)+"px";
    }
}

// http://blog.firetree.net/2005/07/04/javascript-find-position/
function common_findPosY(obj)
{
    var curtop = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
            curtop += obj.offsetTop;
            if(!obj.offsetParent)
            { break; }
            obj = obj.offsetParent;
        }
    }
    else if(obj.y)
    { curtop += obj.y; }
    return curtop;
}
function common_findPosX(obj)
{
    var curleft = 0;
    if(obj.offsetParent)
    {
        while(1)
        {
            curleft += obj.offsetLeft;
            if(!obj.offsetParent)
            { break; }
            obj = obj.offsetParent;
        }
    }
    else if(obj.x)
    { curleft += obj.x; }
    return curleft;
}

function common_window_size()
{
    var size = new Array();

    var browseWidth, browseHeight;

    if(document.layers||(document.getElementById&&!document.all))
    {
        browseWidth=window.innerWidth;
        browseHeight=window.innerHeight;
    }
    else if(document.all)
    {
        browseWidth=document.body.clientWidth;
        browseHeight=document.body.clientHeight;
    }

    size.push(browseWidth);
    size.push(browseHeight);

    return size;
}

function get_width(object)
{
    if(document.layers||(document.getElementById&&!document.all))
    { return object.innerWidth; }
    else
    { return object.clientWidth; }
}
