﻿//*************************************************************************************
// File     : deco_jq_bg_functions.js
// Requires : jquery.js (version 1.3.2+), braingnat.js (version 0.2.0+)
// Author   : Kyle Weems (ksw)
// Origin   : mindfly.com
// Created  : April 09, 2009
// Modified : April 14, 2009
//*************************************************************************************

setGoogleMapKey();

$(document).ready(function() {
    var t = setTimeout("loadLocationPrintMap();", 500);
});

function loadLocationPrintMap() {
    if ($('#map.locationPrint').length > 0) {
        BrainGnat.Google.map.loadFromHCard('.vcard', '#map');
        BrainGnat.Google.map.addDirections('#directions');
    }
    $('#directionWrapper button').bind('click', function() {
        BrainGnat.Google.map.getDirections( addFromHCard('.vcard'), $('#directionSearch').attr('value'));
    });
    $('#directionSearch').bind('focus', function() {
        if ($('#directionSearch').attr('value') == "Enter Your Address For Directions") {
            $('#directionSearch').attr('value', "");
        }
    });
    $('#directionSearch').bind('blur', function() {
        if ($('#directionSearch').attr('value') == "") {
            $('#directionSearch').attr('value', "Enter Your Address For Directions");
        }
    });
    $('#directionSearch').bind('keydown', function(e) {
        if (e.keyCode == 13) {
            BrainGnat.Google.map.getDirections(addFromHCard('.vcard'), $('#directionSearch').attr('value'));
            return false;
        }
    });
}

function setGoogleMapKey() {
    var key = "";
    if (window.location.host == "www.coastaltraining-wa.org") {
        key = "ABQIAAAAz_CZ0Th4Fq6neVS4niAZrBROM7HE4WOKiLs51Zm5BovBXdU0OxQ8HNwgIMw0FaonVZ-DfVKIKBhMTw";
    } else {
        key = "ABQIAAAAz_CZ0Th4Fq6neVS4niAZrBRtQFIWzlXTgj5VMcxASPCFOVr9ihSl_ndxDBYlQ8wDufV3OC3Y_pyD0Q";
    }
    keytag = "<script src='http://maps.google.com/maps?file=api&amp;v=2&amp;sensor=false&amp;key=" + key + "' type='text/javascript'></script>";
    document.write(keytag);
}

function addFromHCard(card) {
    var address = "";
    address += $(card + ' .street-address').text();
    address += ' ' + $(card + ' .locality').text();
    address += ', ' + $(card + ' .region').text();
    address += ' ' + $(card + '. postal-code').text();
    return address;
}
