// Google Maps
var hasInitGoogleMaps = false;

function initGoogleMaps() {
    // Initialize Google Map
    if (!hasInitGoogleMaps) {
        if (GBrowserIsCompatible()) {
          hasInitGoogleMaps = true;
          var map = new GMap2(document.getElementById("map_canvas"));
          var center = new GLatLng(37.680415,-122.49257);
          var marker = new GMarker(center);
          var infoWindowString = "<h4>Address:</h4>50 Northridge Drive,<br/>Daly City, CA 94015<br/><br/><b>Get directions:</b> To here<br/><span class='small'>Start address</span><br/><form name='GoogleDirectionsForm' action='http://maps.google.com/maps' method='get' target='_blank'><input type='hidden' name='f' value='d' /><input type='hidden' name='hl' value='en' /><input type='hidden' name='daddr' value='50 Northridge Dr, Daly City, CA 94015' /><input value='' name='saddr' type='text'/> <input value='Go' type='submit'/></form>";

          map.setCenter(center, 13);
          map.addControl(new GSmallMapControl());
          map.addControl(new GMapTypeControl());
          map.addOverlay(marker);
          map.openInfoWindowHtml(map.getCenter(), infoWindowString);        
        }
    }
}


// YUI Pop Up
YAHOO.namespace("kcpc.popup");

function initPopups() {
    // Initialize Directions Layer
    YAHOO.kcpc.popup.directions = new YAHOO.widget.Panel("directions", { 
        close: true,
        effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25 }, 
        modal: true,
        shadow: true,
        visible: false,
        width: "620px",
        zIndex: 1000
    } );

    YAHOO.kcpc.popup.directions.beforeShowEvent.subscribe(positionDirectionsPopup);
    YAHOO.kcpc.popup.directions.showEvent.subscribe(scrollToTop);
    YAHOO.kcpc.popup.directions.showEvent.subscribe(initGoogleMaps);
    YAHOO.kcpc.popup.directions.render();
    
    

    // Initialize Contact Layer
    YAHOO.kcpc.popup.contactus = new YAHOO.widget.Panel("contact-us", { 
        close: true,
        effect: { effect: YAHOO.widget.ContainerEffect.FADE, duration: 0.25 }, 
        modal: true,
        shadow: true,
        visible: false,
        width: "561px",
        zIndex: 1000
    } );

    YAHOO.kcpc.popup.contactus.beforeShowEvent.subscribe(positionContactUsPopup);
    YAHOO.kcpc.popup.contactus.showEvent.subscribe(scrollToTop);
    YAHOO.kcpc.popup.contactus.render();
}

function positionDirectionsPopup() {
    var popX = (screen.width-630)/2;
    var popY = 20;
    
    YAHOO.kcpc.popup.directions.cfg.setProperty("xy", [popX, popY]);
}

function positionContactUsPopup() {
    var popX = (screen.width-571)/2;
    var popY = 20;
    
    YAHOO.kcpc.popup.contactus.cfg.setProperty("xy", [popX, popY]);
}

function scrollToTop() {
    window.scrollTo(0,0);
}


// Clock
function updateClock() {
    var currentHours = currentTime.getHours();
    var currentMinutes = currentTime.getMinutes();

    // Pad minutes with leading zeros
    currentMinutes = (currentMinutes < 10 ? "0" : "" ) + currentMinutes;

    // AM versus PM
    var timeOfDay = (currentHours < 12) ? "AM" : "PM";

    // Convert from military time
    currentHours = (currentHours > 12) ? currentHours - 12 : currentHours;
    currentHours = (currentHours == 0) ? 12 : currentHours;

    // Compile for display
    var currentTimeString = currentHours + ":" + currentMinutes + " " + timeOfDay;

    // Display
    if (document.getElementById("clock")) {
        document.getElementById("clock").innerHTML = currentTimeString;
    }

    // Add a minute for next update
    currentTime = new Date(currentTime.valueOf() + 60000);
}


// Bulletin
var currentBulletin = 1;
var BULLETIN_COUNT = 0;

function updateBulletin() {
    var nextBulletin = currentBulletin + 1;
    
    if (BULLETIN_COUNT > 1) {
        // Loop back
        if (nextBulletin > BULLETIN_COUNT) {
            nextBulletin = 1;
        }
        if (currentBulletin > BULLETIN_COUNT) {
            currentBulletin = 1;
            nextBulletin = 2;
        }
        
        // Change display
        if (document.getElementById("bulletin" + currentBulletin)) {
            document.getElementById("bulletin" + currentBulletin).style.display = 'none';
        }        
        if (document.getElementById("bulletin" + nextBulletin)) {
            document.getElementById("bulletin" + nextBulletin).style.display = 'block';
        }    
    }
    
    currentBulletin++;
}

// Onload and Unload Calls
YAHOO.util.Event.addListener(window, "load", initPopups);
YAHOO.util.Event.addListener(window, "load", updateClock);
YAHOO.util.Event.addListener(window, "load", setInterval('updateBulletin()', 5000));
YAHOO.util.Event.addListener(window, "unload", GUnload);