
/* Javascript support file for TLRanch.com

<< insert some disclaimer, all rights reserved, etc. >>

*/


// ----------------------------------     VARIABLE DECLARATIONS
var DocURL = "";
var PageGenre = "";
var DocDepth = "";
var RelPath = "";
var newSrc = "";
// var Debug = '';

// first index is substring to match for "current"
// second index is to generate the anchor href
// third is the content for anchor tag (between the <a> and </a>
var nav_array = new Array(5);

// ----------------------------------     FUNCTION DECLARATIONS

// This is the parent function called by the html page to load all of the dynamic content
function loadDynamicContent(){    
    initializeVariables();
    
    get_docURL();
    set_depth();
    // moved the rel_path and proper_links up before genre to help debugging inactive pages
    set_rel_path(); //needs to be generated before any other "set" functions
    set_proper_links();

    get_genre();
    set_leftnav();
    set_leftnav_sub();
    set_header();
    set_footer();
    set_adspace();
    initializePortraitImage();    
}

function initializeVariables(){
    nav_array[0]= ['index.htm'  ,'index.htm'             ,'<b>Home</b>' ]; 
    nav_array[1]= ['/horses/'   ,'horses/horses.htm'     ,'<b>Horses</b>'];                              
    nav_array[2]= ['/cattle/'   ,'cattle/cattle.htm'     ,'<b>Cattle</b>'];                              
    nav_array[3]= ['/sold/'     ,'sold/sold.htm'         ,'<b>Sold</b>'];                              
    nav_array[4]= ['/hotdeals/' ,'hotdeals/hotdeals.htm' ,'<b>Hot Deals</b>']; 
    //nav_array[4]= ['/herdred/'  ,'herdred/herdred.htm'   ,'<b>Herd Reduction Sale</b>']; 
    // nav_array[5]= ['/facilities/' ,'facilities/facilities.htm' ,'<b>Facilities</b>']; 
}

function initializePortraitImage(){    
    if (document.getElementById("portrait_href") != null)
        loadImage(document.getElementById("portrait_href").href);
}

function build_nav_text(){
    var nav_text = '';
    nav_text = nav_text + '<ul>' + '\n';

    // iterate through the array, identify the current page, return the results
    nav_text = nav_text + build_listItems('L1', nav_array);

    nav_text = nav_text + '</ul>' + '\n';

    return nav_text;
}

function build_listItems(layer, nav_array){
    var listItem_text = '';

    var nav_search = -1;
    for (var i=0; i <nav_array.length; i++) {
        // check if this index is the "current page"
        if (DocURL.toLowerCase().indexOf(nav_array[i][0].toLowerCase()) > -1) {
            nav_search = i;
        } else {
            continue;
        }
    }

    if (nav_search < 0 && DocDepth < 1) {
        nav_search = 0;
    }

    for (var i=0; i <nav_array.length; i++) {
        // check if this index is the "current page"
        if (i == nav_search) {
            listItem_text = listItem_text + '  <li class="'+ layer +' current">' + '\n';
            listItem_text = listItem_text + '    <a class="current" href="' + RelPath + nav_array[i][1] + '">' + '<div class="raquo">&#187;&nbsp;</div>'+ 
                            nav_array[i][2] + '</a>';            
            if (layer.indexOf('L1') > -1) {
                listItem_text = listItem_text + '&nbsp;&nbsp;&nbsp;' + '\n';
                listItem_text = listItem_text + '    <div id="leftnav_sub"></div>';
            } else {
                listItem_text = listItem_text + '\n';
            }
            listItem_text = listItem_text + '  </li>' + '\n';
        } else {
            listItem_text = listItem_text + '  <li class="'+ layer +'">' + '\n';
            listItem_text = listItem_text + '    <a href="' + RelPath + nav_array[i][1] + '">' + nav_array[i][2] + '</a>' + '\n';
            listItem_text = listItem_text + '  </li>' + '\n';
        }
    }

    return listItem_text;
}

function build_home_nav_text(){
    return build_nav_text();
}
function build_home_nav_sub_text(){
    return '';
}

function build_horses_nav_text() {
    return build_nav_text();
}

function build_horses_nav_sub_text() {
    // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    var nav_sub_array = new Array(4);

    nav_sub_array[0]= ['horses/stallions' ,'horses/stallions/stallions.htm' ,'<b>Stallions</b>' ]; 
    nav_sub_array[1]= ['horses/mares'     ,'horses/mares/mares.htm'         ,'<b>Mares</b>'     ];                              
    nav_sub_array[2]= ['horses/foals'     ,'horses/foals/foals.htm'         ,'<b>Foals</b>'     ];                              
    nav_sub_array[3]= ['sold/horses'      ,'sold/sd_horses/sd_horses.htm'   ,'<b>Sold</b>' ];                              

    var nav_sub_text = '';
    nav_sub_text = nav_sub_text + '<ul>' + '\n';

    // iterate through the array, identify the current page, return the results
    nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);

    nav_sub_text = nav_sub_text + '</ul>' + '\n';

    return nav_sub_text;
}

function build_cattle_nav_text(){
    return build_nav_text();
}
function build_cattle_nav_sub_text() {
    // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    var nav_sub_array = new Array(3);

    nav_sub_array[0]= ['cattle/sires' ,'cattle/sires/sires.htm'       ,'<b>Herd Sires</b>' ]; 
    nav_sub_array[1]= ['cattle/cows'  ,'cattle/cows/cows.htm'         ,'<b>Cows</b>'];                                                          
    nav_sub_array[2]= ['sold/cattle'  ,'sold/sd_cattle/sd_cattle.htm' ,'<b>Sold</b>'];                              

    var nav_sub_text = '';
    nav_sub_text = nav_sub_text + '<ul>' + '\n';

    // iterate through the array, identify the current page, return the results
    nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);

    nav_sub_text = nav_sub_text + '</ul>' + '\n';

    return nav_sub_text;
}


function build_sold_nav_text(){
    return build_nav_text();
}
function build_sold_nav_sub_text() {
    // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    var nav_sub_array = new Array(2);
    
    
    nav_sub_array[0]= ['sold/sd_horses/sd_horses.htm' ,'sold/sd_horses/sd_horses.htm' , '<b>Horses</b>'  ];                                
    nav_sub_array[1]= ['sold/sd_cattle/sd_cattle.htm' ,'sold/sd_cattle/sd_cattle.htm' , '<b>Cattle</b>'  ]; 
    
    var nav_sub_text = '';
    nav_sub_text = nav_sub_text + '<ul>' + '\n';
    
    // iterate through the array, identify the current page, return the results
    nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);
    
    nav_sub_text = nav_sub_text + '</ul>' + '\n';
    
    return nav_sub_text;
}

function build_hotdeals_nav_text(){
    return build_nav_text();
}
function build_hotdeals_nav_sub_text() {
    // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    var nav_sub_array = new Array(2);
    
    nav_sub_array[0]= ['hotdeals/hd_horses/hd_horses.htm' ,'hotdeals/hd_horses/hd_horses.htm' , '<b>Horses</b>' ];                              
    nav_sub_array[1]= ['hotdeals/hd_cattle/hd_cattle.htm' ,'hotdeals/hd_cattle/hd_cattle.htm' , '<b>Cattle</b>' ]; 
    
    var nav_sub_text = '';
    nav_sub_text = nav_sub_text + '<ul>' + '\n';
    
    // iterate through the array, identify the current page, return the results
    nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);
    
    nav_sub_text = nav_sub_text + '</ul>' + '\n';
    
    return nav_sub_text;
}
function build_herdred_nav_text(){
    return build_nav_text();
}
function build_herdred_nav_sub_text() {
    // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    var nav_sub_array = new Array(2);
    
    nav_sub_array[0]= ['hotdeals/hd_broodmares/hd_broodmares.htm'     ,'hotdeals/hd_broodmares/hd_broodmares.htm'     , '<b>Mares & Foals</b>' ];                              
        nav_sub_array[1]= ['hotdeals/hd_ridinghorses/hd_ridinghorses.htm' ,'hotdeals/hd_ridinghorses/hd_ridinghorses.htm' , '<b>Riding Horses</b>' ]; 
    nav_sub_array[2]= ['hotdeals/hd_yearlings/hd_yearlings.htm'       ,'hotdeals/hd_yearlings/hd_yearlings.htm'       , '<b>Yearlings</b>' ]; 
    // nav_sub_array[3]= ['herdred/hd_cattle/hd_cattle.htm' ,'herdred/hd_cattle/hd_cattle.htm' , '<b>Cattle</b>' ]; 
    
    var nav_sub_text = '';
    nav_sub_text = nav_sub_text + '<ul>' + '\n';
    
    // iterate through the array, identify the current page, return the results
    nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);
    
    nav_sub_text = nav_sub_text + '</ul>' + '\n';
    
    return nav_sub_text;
}

function build_facilities_nav_text(){
    return build_nav_text();
}
function build_facilities_nav_sub_text() {
    //  // first index is the anchor href, second is the content for anchor tag (between the <a> and </a>
    //  var nav_sub_array = new Array(2);
    // 
    //  nav_sub_array[0]= ['facilities/horses' ,'facilities/horses/facilities.htm' ,'<b>Horses</b>' ]; 
    //  nav_sub_array[1]= ['facilities/cattle' ,'facilities/cattle/facilities.htm' ,'<b>Cattle</b>' ];                             
    // 
    //  var nav_sub_text = '';
    //  nav_sub_text = nav_sub_text + '<ul>' + '\n';
    // 
    //  // iterate through the array, identify the current page, return the results
    //  nav_sub_text = nav_sub_text + build_listItems('L2', nav_sub_array);
    // 
    //  nav_sub_text = nav_sub_text + '</ul>' + '\n';
    // 
    //  return nav_sub_text;
    
    return '';
}

function set_portrait_image(newSrc){      
    document.images['portrait_img'].src = newSrc;          
}
function set_portrait_image_wait(newSrc){
    document.images['portrait_img'].width = 500;
    document.images['portrait_img'].height = 100;
    document.images['portrait_img'].src = newSrc;
}
function set_portrait_href(newHref){          
    document.getElementById("portrait_href").href = newHref;
}

/*
  function set_pedigree(){
      document.images['pedigree'].src = RelPath + 'images/blank_pedigree.gif';
  }
*/

function set_leftnav(){

    var leftnav_header = '<img id="logo" src="'+ RelPath +'images/TL_Gate.gif" border="0">' + '\n';
    var e = document.getElementById('leftnav');

    if (PageGenre.toLowerCase().indexOf("Home".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_home_nav_text();
    } else if (PageGenre.toLowerCase().indexOf("Horses".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_horses_nav_text();
    } else if (PageGenre.toLowerCase().indexOf("Cattle".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_cattle_nav_text();    
    } else if (PageGenre.toLowerCase().indexOf("Sold".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_sold_nav_text();    
    } else if (PageGenre.toLowerCase().indexOf("HotDeals".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_hotdeals_nav_text();  
    } else if (PageGenre.toLowerCase().indexOf("HerdRed".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_herdred_nav_text();   
    } else if (PageGenre.toLowerCase().indexOf("Facilities".toLowerCase()) > -1) {
        e.innerHTML = leftnav_header + build_facilities_nav_text();
    }
}

function set_leftnav_sub(){

    var e = document.getElementById('leftnav_sub');

    if (PageGenre.toLowerCase().indexOf("Home".toLowerCase()) > -1) {
        e.innerHTML =  build_home_nav_sub_text();
    } else if (PageGenre.toLowerCase().indexOf("Horses".toLowerCase()) > -1) {
        e.innerHTML =  build_horses_nav_sub_text();
    } else if (PageGenre.toLowerCase().indexOf("Cattle".toLowerCase()) > -1) {
        e.innerHTML =  build_cattle_nav_sub_text();    
    } else if (PageGenre.toLowerCase().indexOf("Sold".toLowerCase()) > -1) {
        e.innerHTML =  build_sold_nav_sub_text();
    } else if (PageGenre.toLowerCase().indexOf("HotDeals".toLowerCase()) > -1) {
        e.innerHTML = build_hotdeals_nav_sub_text();   
    } else if (PageGenre.toLowerCase().indexOf("HerdRed".toLowerCase()) > -1) {
        e.innerHTML = build_herdred_nav_sub_text();   
    } else if (PageGenre.toLowerCase().indexOf("Facilities".toLowerCase()) > -1) {
        e.innerHTML =  build_facilities_nav_sub_text();
    } else {
        e.parentNode.removeChild(e);    
    }

    if (e.innerHTML == '') {
        e.parentNode.removeChild(e);
    }

}

function set_header(){
    // var header_text = '<img id="top_logo" src=\"'+ RelPath +'images/twisted_l_logo.gif\" alt="Twisted L Ranch Logo">\n';
    var header_text = '<h1><div class="the">The&nbsp;</div>Twisted L Ranch</h1>\n';

    document.getElementById('header').innerHTML = header_text;
}

function set_footer(){
    var curFooterText = document.getElementById('footer').innerHTML;

    var footer_text = ''+
                      '<div id=\"footerContent\">\n'+
                      '<div id=\"logo\">\n'+
                      '<img alt=\"TLRanch Logo\" src=\"'+ RelPath + 'images/Logo_med2.gif\"/>\n'+
                      '</div>\n'+
                      '<div id=\"ranchInfo\">\n'+
                      '<div id=\"name\">\n'+
                      '<div class=\"the\">The</div>Twisted L Ranch\n'+
                      '</div>\n'+
                      '1080 Grand Oaks Rd\n'+
                      '<br>\n'+
                      'Greenville, TX&nbsp;&nbsp;&nbsp;75401\n'+
                      '</div>\n\n'+
                      '<div id=\"contact\">\n'+
                      'email: info@tlranch.com\n'+
                      '<br>\n'+
                      'phone: (903) 455-6633\n'+
                      '<br>\n'+
                      'owners: Mark &amp; Sandy Lanning\n'+
                      '</div>\n'+
                      '<div id=\"footer\">\n'+
                      '</div>\n'+
                      '</div>\n';
    document.getElementById('footer').innerHTML = curFooterText + footer_text;
}

function set_adspace(){
    var curAdspaceText = document.getElementById('adspace').innerHTML;

    var adspace_text = ''+
                       '<br><br><br><br><br>\n'+
                       '<a href=\"http://www.tlranch.com\hotdeals\hotdeals.htm"></a>\n'+
                       '<br><br>\n'+
                       '<h2>See Horse<br></h2><br><a href =\"http://www.tlranch.com/hotdeals/hotdeals.htm"><h2>Hot Deals<br></h2><br></a> <h2>Updated 15 Jan.</h2>\n';


    document.getElementById('adspace').innerHTML = curAdspaceText + adspace_text;

}

/*
   function get_google_ad_slot(){
   
       if (PageGenre.toLowerCase().indexOf("Horses".toLowerCase()) > -1 || 
           PageGenre.toLowerCase().indexOf("Home".toLowerCase()) > -1) {
           return 3355263731;
       } else if (PageGenre.toLowerCase().indexOf("Cattle".toLowerCase()) > -1) {
           return 8877016461;
       } else if (PageGenre.toLowerCase().indexOf("Hotdeals".toLowerCase() ) > -1 || 
                  PageGenre.toLowerCase().indexOf("sold".toLowerCase() ) > -1) {
           return 0346027257;
       } else {
           return 3355263731; //use horses/home as default
       }
   }
*/

function set_rel_path(){

    var build_path = '';
    // build our relative links based upon this documents depth
    for (var i=0; i<DocDepth; i++) {
        build_path = build_path + '../';
    }
    RelPath = build_path;
    return;
}

function set_proper_links(){
    for (var i=0; i < document.links.length; i++) {
        if (document.links[i].href) {
            document.links[i].href = document.links[i].href.replace("%7BRelPath%7D/", RelPath);
        }
    }
    return;
}
/*
    function listLinks(){
        var newwin = window.open("", "linklist", "menubar, scrollbars, resizeable");
    
        for (var i=0; i< document.links.length; i++) {
            newwin.document.write('<A HREF="' + document.links[i].href + '">');
            newwin.document.write(document.links[i].href);
            newwin.document.writeln("</a><BR>");
        }
    }
*/
function get_docURL(){
    // Lets get the URI of this document so we can make some intelligent decisions
    // we replace backslashes for "correct" forward slashes on file-driven IE
    DocURL = document.URL.replace(/\\/g, '/');    
}

function set_depth(){
    
    var LocalDocRoot = "/Publish/";
    var WebDocRoot = "tlranch.com/";
    var index = -1;
    var lastSlash = -1;
    var reverseStr = '';
    var stop = -1;
    var curDepth = -1;

    
    // find the right-most slash we care about based on where we're serving the file from
    if (DocURL.toLowerCase().indexOf(LocalDocRoot.toLowerCase()) > -1) {
        index = DocURL.toLowerCase().indexOf(LocalDocRoot.toLowerCase()); //should be the first char in pattern, find end "/"
        // Debug = 'local index ['+ index + '] ';
    } else if (DocURL.toLowerCase().indexOf(WebDocRoot.toLowerCase()) > -1) {
        index = DocURL.toLowerCase().indexOf(WebDocRoot.toLowerCase());
        // Debug = 'web index ['+ index + '] ';
    } else {
        return -1;
    }

    /* "file:///M:/Websites/TLRanch.com/v3/Publish/horses/stallions/ty/ty.htm" */

    lastSlash = DocURL.toLowerCase().indexOf('/', index+1);
    // Debug += 'lastSlash['+ lastSlash +'] '

    reverseStr = DocURL.toLowerCase().reverse();
    // Debug += 'reverseStr['+ reverseStr +'] '

    index = 0;
    stop = (DocURL.length - lastSlash)-1;
    while (index < stop && index > -1) {
        // Debug += 'sindex['+ index +'] ';
        curDepth++;
        index = reverseStr.indexOf('/', index+1);        
    }

    // check for the home page and set depth accordingly...
    if (curDepth < 0) curDepth = 0;

    DocDepth = curDepth;   
    return;
}

function get_genre(){
    // We should have a [meta name="Genre"] in the head of each of our docs;
    // Expect values such as: Horses, Cattle, NA
    // coding this foresee error or wanting to allow more than one genre; currently last declaration wins.

    if (document.getElementsByName) {
        var metaArray = document.getElementsByName('Genre');
        for (var i=0; i<metaArray.length; i++) {
            PageGenre = metaArray[i].content;
        }
    }
}


//  These variables are nothing more than modified html to be javascript happy.
//  Readable, happy javascript will have:
//      - to escape all quotes with [\]
//      - to start each line with a quote
//      - to end each line with an escaped newline [\n] and a quote followed by the concat operator [+]   
//      - to terminate the last line with a quote and semicolon instead of newline, quote and concat.


// can only reverse an array, so create an array out of string...then reverse and return.
String.prototype.reverse = function(){
    var strArray = this.split("");
    var reversedArray = strArray.reverse();
    var retVal = reversedArray.join("");

    return retVal;
}


// IMAGE LOADING... help with slow internet connections

function setImage(image){
    var h = document.getElementById('portrait_href');    
    h.removeChild(document.getElementById('portrait_img'));    
    h.appendChild(image);
}

function loadImage(newSrc){  
    var newURL = newSrc.replace("images/tn_", "images/");
    var newId = "portrait_img";

    var loader = new ImageLoader(newURL, newId);

    set_portrait_image_wait(RelPath + 'images/loading.gif');

    loader.loadEvent = function(url, image){
        setImage(image);
        set_portrait_href(newURL);
    }
    loader.load();
}











