// Misc JavaScript code and functions for the Pleasanton site.
var DHTML = (document.getElementById);
// Put all url values into a url object.
var url = new Object();
if (document.URL.indexOf('?') != -1) {
    var qs = location.search.substring(1);
    var nv = qs.split('&');
    for(i = 0; i < nv.length; i++) {
        eq = nv[i].indexOf('=');
        url[nv[i].substring(0,eq).toLowerCase()] = unescape(nv[i].substring(eq + 1).replace(/\+/g," "));
    }
}
// Highlight search box.
function selectSearch() {
    if (!DHTML) return;
    var searchbox = "Search";
    if (url['searchbox']) { searchbox = url['searchbox']; }
    document.forms['searchform'].elements['searchbox'].value = searchbox;
    if (document.forms['searchpageform']) { // See if we are on search page.
        document.forms['searchpageform'].elements['searchbox'].value = searchbox;
        //document.forms['searchpageform'].elements['searchbox'].focus();
    } //else { document.forms['searchform'].elements['searchbox'].focus(); }
}
// Set the parent page in the menu.
function printParentLink() {
    var dirs = location.pathname.split('/');
    if (dirs[dirs.length-1] == "" || dirs[dirs.length-1] == "index.html")
        { len = dirs.length-3; }// skip first two.
    else { len = dirs.length-2 }
    for(i = len; i >= 0; i--) {
        switch(dirs[i])
        {
            // NOTE: Change menu_data.js if any of these links change!!!
            case "community": u="/community/"; t="Community"; break;
            case "pleasanton": u="/community/pleasanton/"; t="Pleasanton Community"; break;
            case "character": u="/community/character/"; t="Building a Community of Character"; break;
            case "new": u="/community/new/"; t="New to Pleasanton"; break;
            case "involved": u="/community/involved/"; t="Getting Involved"; break;
            case "visiting": u="/community/visiting/"; t="Visiting Pleasanton"; break;
            case "arts": u="/community/arts/"; t="Arts and Entertainment"; break;
            case "calendar": u="/community/calendar/"; t="Community Calendar"; break;
            case "links": u="/community/links/"; t="Community Links Page"; break;
            case "housing": u="/community/housing/"; t="Affordable Housing"; break;

            case "business": u="/business/"; t="Business"; break;
            case "development": u="/business/development/"; t="Economic Development"; break;
            case "planning": u="/business/planning/"; t="Planning Services"; break;
            case "building": u="/business/building/"; t="Building Inspection Services"; break;
            case "license": u="/business/license/"; t="Business License"; break;
            case "bids": u="/business/bids/"; t="City Bids"; break;

            case "government": u="/government/"; t="Government"; break;
            case "city": u="/government/city/"; t="Your City Government"; break;
            case "council": u="/government/council/"; t="Mayor and City Council"; break;
            case "representatives": u="/government/representatives/"; t="Commissions/Agencies"; break;
            case "meetings": u="/government/meetings/"; t="City Meetings"; break;
            case "search": u="/government/search/"; t="Search City Records"; break;
            case "departments": u="/government/departments/"; t="City Departments"; break;

            case "services": u="/services/"; t="Services"; break;
            case "library": u="/services/library/"; t="Library"; break;
            case "employment": u="/services/employment/"; t="Employment"; break;
            case "residential": u="/services/residential/"; t="Residential Services"; break;
            case "recreation": u="/services/recreation/"; t="Community Services"; break;
            case "senior": u="/services/senior/"; t="Senior Services"; break;
            case "youth": u="/services/youth/"; t="Youth Services"; break;
            case "police": u="/services/police/"; t="Police Services"; break;
            case "fire": u="/services/fire/"; t="Fire Services"; break;
            case "finance": u="/services/finance/"; t="Finance and Budget"; break;
            case "traffic": u="/services/traffic/"; t="Traffic Information"; break;
            case "transportation": u="/services/transportation/"; t="Transportation"; break;
            case "utility": u="/services/utility/"; t="Utility Billing"; break;
            case "e": u="/services/e/"; t="E-Services"; break;

            default: continue; // not found continue.
        }
        // found.
        document.writeln("<a href=\""+u+"\" class=\"sectiontext\"><b>"+t+"</b></a>");
        return;
    }
}
// Set content table to fill window.
function setPageHeight(delta) { // set_page_height
    if (!DHTML) return;
    // Set height of the contenttable to fill the screen.
    var y, test1 = document.body.scrollHeight, test2 = document.body.offsetHeight;
    if (test1 > test2) // All but Explorer Mac.
    { y = document.body.scrollHeight; }
    else // Explorer Mac. Would also work in Explorer 6 Strict, Mozilla and Safari.
    { y = document.body.offsetHeight; }
    y = y - delta; // Subtract the menu and header table. delta = 145 for subpage, 233 for homepage.
    document.writeln("<style>#contenttable{height: "+y+"px;}</style>");
}
// Cookies. Script Source URI: http://techpatterns.com/downloads/javascript_cookies.php
var COOKIES = false; // See test below.
// this function gets the cookie, if it exists
function getPlsCookie( name ) {
    var start = document.cookie.indexOf( name + "=" );
    var len = start + name.length + 1;
    if ( ( !start ) && ( name != document.cookie.substring( 0, name.length ) ) )
        { return null; }
    if ( start == -1 ) return null;
    var end = document.cookie.indexOf( ";", len );
    if ( end == -1 ) end = document.cookie.length;
    return unescape( document.cookie.substring( len, end ) );
}
// setPlsCookie. Only first two params are required.
function setPlsCookie( name, value, expires, path, domain, secure ) {
    var today = new Date();
    today.setTime( today.getTime() );
    if ( expires )
        { expires = expires * 1000 * 60 * 60 * 24; }
    var expires_date = new Date( today.getTime() + (expires) );
    document.cookie = name + "=" +escape( value ) +
        ( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) + //expires.toGMTString()
        ( ( path ) ? ";path=" + path : "" ) +
        ( ( domain ) ? ";domain=" + domain : "" ) +
        ( ( secure ) ? ";secure" : "" );
}
// this deletes the cookie when called
function deletePlsCookie( name, path, domain ) {
    if ( getPlsCookie( name ) ) document.cookie = name + "=" +
            ( ( path ) ? ";path=" + path : "") +
            ( ( domain ) ? ";domain=" + domain : "" ) +
            ";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}
// Check if browser supports cookies.
setPlsCookie( 'test', 'none', '', '/', '', '' );
if ( getPlsCookie( 'test' ) ) {
 COOKIES = true;
 deletePlsCookie('test', '/', '');
}
// BreadCrumbs.
var newLink;
if (DHTML) {
    newLink = document.createElement('span');
    var newA = document.createElement('a');
    newLink.appendChild(document.createTextNode(' > '));
    newLink.appendChild(newA);
}
function addBreadCrumb(title,link) {
    if (!DHTML) return;
    if (!title) title = "GIVE PAGE A TITLE!";
    title = title.replace(/City of Pleasanton - /i,"");
    var crumbs = document.getElementById('breadcrumb');
    if (crumbs) {
        var newCrumb = newLink.cloneNode(true);
        var theLink = newCrumb.lastChild;
        theLink.href = link;
        if (title.length > 27) {
            theLink.title = title;
            title = title.substring(0, 24) + "..."
            }
        theLink.appendChild(document.createTextNode(title));
        crumbs.appendChild(newCrumb);
    }
}
// doBreadCrumbs
function doBreadCrumbs(bc6title, bc6link) {
    if (!DHTML) return;
    if (!COOKIES) return;
    if ((end = bc6link.indexOf('#')) != -1) { bc6link = bc6link.substring(0, end); } // Remove hash from link.
    // Get cookies.
    bc1title = getPlsCookie('bc1title');
    bc1link = getPlsCookie('bc1link');
    bc2title = getPlsCookie('bc2title');
    bc2link = getPlsCookie('bc2link');
    bc3title = getPlsCookie('bc3title');
    bc3link = getPlsCookie('bc3link');
    bc4title = getPlsCookie('bc4title');
    bc4link = getPlsCookie('bc4link');
    bc5title = getPlsCookie('bc5title');
    bc5link = getPlsCookie('bc5link');
    if (bc6link == bc5link) {
        bc5title = bc6title;
        bc5link = bc6link;
    } else if (bc6link == bc4link) {
        bc4title = bc5title;
        bc4link = bc5link;
        bc5title = bc6title;
        bc5link = bc6link;
    } else if (bc6link == bc3link) {
        bc3title = bc4title;
        bc3link = bc4link;
        bc4title = bc5title;
        bc4link = bc5link;
        bc5title = bc6title;
        bc5link = bc6link;
    } else if (bc6link == bc2link) {
        bc2title = bc3title;
        bc2link = bc3link;
        bc3title = bc4title;
        bc3link = bc4link;
        bc4title = bc5title;
        bc4link = bc5link;
        bc5title = bc6title;
        bc5link = bc6link;
    } else {
        bc1title = bc2title;
        bc1link = bc2link;
        bc2title = bc3title;
        bc2link = bc3link;
        bc3title = bc4title;
        bc3link = bc4link;
        bc4title = bc5title;
        bc4link = bc5link;
        bc5title = bc6title;
        bc5link = bc6link;
    }
    if (bc1title && bc1link) {
        addBreadCrumb(bc1title, bc1link);
        setPlsCookie( 'bc1link', bc1link, '365', '/', '', '' );
        setPlsCookie( 'bc1title', bc1title, '365', '/', '', '' );
    }
    if (bc2title && bc2link) {
        addBreadCrumb(bc2title, bc2link);
        setPlsCookie( 'bc2link', bc2link, '365', '/', '', '' );
        setPlsCookie( 'bc2title', bc2title, '365', '/', '', '' );
    }
    if (bc3title && bc3link) {
        addBreadCrumb(bc3title, bc3link);
        setPlsCookie( 'bc3link', bc3link, '365', '/', '', '' );
        setPlsCookie( 'bc3title', bc3title, '365', '/', '', '' );
    }
    if (bc4title && bc4link) {
        addBreadCrumb(bc4title, bc4link);
        setPlsCookie( 'bc4link', bc4link, '365', '/', '', '' );
        setPlsCookie( 'bc4title', bc4title, '365', '/', '', '' );
    }
    if (bc5title && bc5link) {
        addBreadCrumb(bc5title, bc5link);
        setPlsCookie( 'bc5link', bc5link, '365', '/', '', '' );
        setPlsCookie( 'bc5title', bc5title, '365', '/', '', '' );
    }
}
// menu functions. also see menu_data.js.
var timerId = 0;
function timePopUp(menu) {
    if (popup){
        if (timerId != 0) { clearTimeout(timerId); }
        timerId = setTimeout("popup('"+menu+"')", 300);
    }
}
function timePopUpImage(menu, image) {
    if (popup){
        if (timerId != 0) { clearTimeout(timerId); }
        timerId = setTimeout("popup('"+menu+"', '"+image+"')", 100);
    }
}
function timePopDown() {
    if (timerId != 0)
        { clearTimeout(timerId); }
    if (popdown)
        { popdown(); }
}
// random subbanner.
function switchSubbanner() {
    if (!DHTML) return;
    var ban = document.getElementById('subbanner');
    if (ban) {
        rn = (Math.round((Math.random()*7)+1))
        ban.src = "/resources/images/subbanner"+rn+".jpg";
    }
}
