
var globalCurrentHighlight = "divBannerItem1";
var debug = 0;

function InitJS()
    {
    var id;
//    alert( globalBaseURL);

    // determine page highlight
    // horizontal tabs
//    if ( globalPostName == 'home') // page name
//        id = 'divBannerItem1';


    // highlight
//    SetCurrentHighlight ( id);

    // fix columns
    SetColumnHeights( 'divMainContainer', 'divSidebarLeft', 'divSidebarRight', 37);

    SetColumnHeights( 'divMainContainer', 'divSepVertLeft', 'divSepVertRight', 0);
    SetColumnHeights2( 'divMainContainerWide', 'divSidebarLeft', 37);
    SetColumnHeights2( 'divMainContainerWide', 'divSepVertLeft', 0);


    return;

    SetSlide( '', 0);

    }

//
function BuildImageName( filename)
    {
    var name;

    name = "url(" + globaltemplateURL + "/images/" + filename + ")";

//    alert( name);
    return( name);
    }



function RandomIndex()
    {
    var ran_unrounded=Math.random()*15; // returns 0->(n-1)

    ran_unrounded += 15;

    var ran_number=Math.floor(ran_unrounded);

    // debug
    str = 'ur: ' + ran_unrounded + ' rn:' + ran_number;
//    alert( str);


    return( ran_number);
    }

var slideNumberList = new Array ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24 );
var slideIndex = -1;

function FisherYates ( myArray ) {
  var i = myArray.length;
  if ( i == 0 ) return false;
  while ( --i ) {
     var j = Math.floor( Math.random() * ( i + 1 ) );
     var tempi = myArray[i];
     var tempj = myArray[j];
     myArray[i] = tempj;
     myArray[j] = tempi;
   }
}


function GetIndex()
    {
    if ( slideIndex == -1)
        {
        FisherYates( slideNumberList);  // shuffle
        slideIndex = RandomIndex();     // start at random spot
        slideIndex = 0;
        }
    else
        {
        slideIndex++;
        if ( slideIndex >= slideNumberList.length)
            slideIndex = 0;
        }

    index = slideNumberList[ slideIndex]; // 0 - n, before shuffling

    index += 13; // offset into new slides

    // debug
    str = 'index: ' + index;
//    alert( str);

    return( index);
    }

// Set a slide
function SetSlide( id, index)
    {

    if ( debug) return;

    var index;
    index = GetIndex();

    id = 'divMainSlide';
    filename = 'slides/slideshow_raw';
    filename += index + '.jpg';


    if ( d = document.getElementById( id))
        d.style.backgroundImage = BuildImageName( filename);

//alert( filename);

    // start the js action,
    ds.Start( 'divMainSlide', 0, 600, 1);
    }



//
function SetCurrentHighlight ( id)
    {
    globalCurrentHighlight = id;

    return;

    if ( debug) return;

    // change color
//    document.getElementById( id).style.backgroundColor = '#E1D2E7';

    // only do this for divBannerItem elements
    var name = id.substring( 0, 9);
    if ( name == 'divBanner')
        {
        document.getElementById( id).style.backgroundImage = BuildImageName( 'tab_on.jpg');
        document.getElementById( id).style.borderBottom = '1px solid #E1D2E7';
        }
    else // divBrowse
        {
//        document.getElementById( id).style.backgroundImage = "url('http://babystayasleep.andregagnon.com/images/button_on.jpg')";
//        document.getElementById( id).style.backgroundImage = "url('" + globalBaseURL + "/images/button_on.jpg')";
        document.getElementById( id).style.backgroundImage = BuildImageName( 'button_on.jpg');
        }

//    alert( globalCurrentHighlight);  // test
    }

// highlight a horizontal tab
function HighlightTab ( id, on)
    {
    return;

    if ( debug) return;

    if ( on == true)
        {
        // change color
        document.getElementById( id).style.backgroundImage = BuildImageName( 'tab_on.jpg');
        document.getElementById( id).style.borderBottom = '1px solid #E1D2E7';
        }
    else
        {
        if ( id != globalCurrentHighlight)
            {
            // change color
            document.getElementById( id).style.backgroundImage = BuildImageName( 'tab_off.jpg');
            document.getElementById( id).style.borderBottom = '1px solid #FDEF97';
            }
        }
    }


// highlight a left column navigation button
function HighlightButton ( id, on)
    {
    return;

    if ( debug) return;

    if ( on == true)
        {
//        document.getElementById( id).style.backgroundColor = '#E1D2E7';
//        document.getElementById( id).style.backgroundImage = "url('http://babystayasleep.andregagnon.com/images/button_on.jpg')";
//        document.getElementById( id).style.backgroundImage = "url('" + globalBaseURL + "/images/button_on.jpg')";
        document.getElementById( id).style.backgroundImage = BuildImageName( 'button_on.jpg');
        }
    else
        {
        if ( id != globalCurrentHighlight)
//            document.getElementById( id).style.backgroundImage = "url('http://babystayasleep.andregagnon.com/images/button_off.jpg')";
//            document.getElementById( id).style.backgroundImage = "url('" + globalBaseURL + "/images/button_off.jpg')";
            document.getElementById( id).style.backgroundImage = BuildImageName( 'button_off.jpg');
//            document.getElementById( id).style.backgroundColor = '#FDEF97';
        }
    }


function SetColumnHeights( id1, id2, id3, offset)
    {
    // find the height of the tallest column, set the columns
    var h, h1, h2, h3;
    var d1 = document.getElementById( id1);
    var d2 = document.getElementById( id2);
    var d3 = document.getElementById( id3);

    if ( d1 && d2 && d3)
    {

    h1 = d1.offsetHeight;
    h2 = d2.offsetHeight;
    h3 = d3.offsetHeight;

//    alert( ' d1 oh ' + d1.offsetHeight);
//    alert( ' d2 oh ' + d2.offsetHeight);
//    alert( ' d3 oh ' + d3.offsetHeight);

    h = h1;
    if ( h2 > h)
        h = h2;
    if ( h3 > h)
        h = h3;

//    h = h + "px";

//    alert( '1 dml h ' + document.getElementById( 'div_menuleft').style.height);
//    alert( '1 dr h ' + document.getElementById( 'div_right').style.height);

    var hMod = h - offset; // ? unique to layout

    document.getElementById( id1).style.height = (hMod) + "px";
    document.getElementById( id2).style.height = h + "px";
    document.getElementById( id3).style.height = h + "px";

//    alert( '2 dml h ' + document.getElementById( 'div_menuleft').style.height);
//    alert( '2 dr h ' + document.getElementById( 'div_right').style.height);

    }
//    else
//        alert( 'column div ids?');

    }

function SetColumnHeights2( id1, id2, offset)
    {
    // find the height of the tallest column, set the columns
    var h, h1, h2, h3;
    var d1 = document.getElementById( id1);
    var d2 = document.getElementById( id2);

    if ( d1 && d2)
    {

        h1 = d1.offsetHeight;
        h2 = d2.offsetHeight;

        h = h1;
        if ( h2 > h)
            h = h2;

        var hMod = h - offset; // ? unique to layout

        document.getElementById( id1).style.height = (hMod) + "px";
        document.getElementById( id2).style.height = h + "px";

    }

    }

