﻿/*

File:		eqtr_ht_functions.js
Project:	Ayr Race Course/Hotel
Created:	30/09/2010
Author:		Jamie Boyd @ Equator	

Notes:		Requires jQuery v1.4.2

*/

//var googleApi = "http://www.google.com/jsapi?key=" + googleKey;

var googleApi = "http://www.google.com/jsapi?key=" + "ABQIAAAAB4BSLw4ivSANEIA4kNR5PBT6MYFgXqEiAcaGHDxDjlpmRu6AqBQPWAk2w4ZZbdYoA19oW9k_NA-mTQ";
$(function () {

    // Open/Close booking component
    $(".bookNow .bookOpen a").click(function () {


        if ($(".bookNow .booking:visible").length == 0) {
            $(".bookNow .booking").slideDown(200);
            return false;
        }
        else {
            $(".bookNow .booking").slideUp(200);
            return false;
        }

    });

    // Add "last" classes to elements 
    $(".infoImageItem .details p:last-child").addClass("last");

    // Create Datepicker for booking component
    $(".fromDate input, .toDate input").datepicker({
        dateFormat: 'dd/mm/yy',
        dayNamesMin: ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
    });


    // Newsletter Signup field placeholder text
    $("fieldset.newsletter input[type=text], .booking fieldset .fromDate input, .booking fieldset .toDate input").each(function () {
        var labelVal = $(this).attr("placeholder");
        $(this).each(function () {
            createText(labelVal, $(this));
        });
        // Removal of text on user-focus
        $(this).focus(function () {
            removeText(labelVal, $(this));
        });
        // Restoration of default text on input blur, if no user input.
        $(this).blur(function () {
            restoreText(labelVal, $(this));
        });
    });

    // Bookmark This Page functionality

    // Create Bookmark control
    var bookmarkLink = document.createElement("div");
    $(bookmarkLink).addClass("bookmarkThisPage");
    var bookmarkHeader = document.createElement("h4");
    $(bookmarkHeader).text("Add this page to your favourites");
    $(bookmarkLink).append(bookmarkHeader);
    var bookmarkThis = document.createElement("p");
    $(bookmarkThis).addClass("action");
    var bookmarkAnchor = document.createElement("a");
    $(bookmarkAnchor).text("Bookmark this page").attr("href", "#");
    $(bookmarkThis).append(bookmarkAnchor);
    $(bookmarkLink).append(bookmarkThis);


    // Add to page is supported by Browser 
    // add a "rel" attrib if Opera 7+
    if (window.sidebar || window.external) {
        $(".forwardToFriend.component").append(bookmarkLink);
    }

    if (window.opera) {
        $(".forwardToFriend.component").append(bookmarkLink);
        if ($(".bookmarkThisPage a").attr("rel") != "") { // don't overwrite the rel attrib if already set
            $(".bookmarkThisPage a").attr("rel", "sidebar");
        }
    }

    $(".bookmarkThisPage a").click(function (event) {
        event.preventDefault(); // prevent the anchor tag from sending the user off to the link
        var url = this.href;
        var title = this.title;

        if (window.sidebar) { // Mozilla Firefox Bookmark
            window.sidebar.addPanel(title, url, "");
        } else if (window.external) { // IE Favorite
            window.external.AddFavorite(url, title);
        } else if (window.opera) { // Opera 7+
            return false; // do nothing - the rel="sidebar" should do the trick
        }
    });

    if ($(".googleMap").length) {
        $.getScript(googleApi, loadGoogleMapApi);
    }

    // Lightbox 

    $(".mainContent .gallery a").lightBox({

        imageLoading: '/assets/cssimages/hotel/lightbox-ico-loading.gif',
        imageBtnClose: '/assets/cssimages/hotel/lightbox-btn-close.gif',
        imageBtnPrev: '/assets/cssimages/hotel/lightbox-btn-prev.gif',
        imageBtnNext: '/assets/cssimages/hotel/lightbox-btn-next.gif',
        containerResizeSpeed: 750
    });

    $(".subContent .gallery a").lightBox({

        imageLoading: '/assets/cssimages/hotel/lightbox-ico-loading.gif',
        imageBtnClose: '/assets/cssimages/hotel/lightbox-btn-close.gif',
        imageBtnPrev: '/assets/cssimages/hotel/lightbox-btn-prev.gif',
        imageBtnNext: '/assets/cssimages/hotel/lightbox-btn-next.gif',
        containerResizeSpeed: 750
    });



});


// PLACEHOLDER TEXT FUNCTIONS

// Create default text for text field on page load
function createText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    if (inpType != "submit") {

        if (thisObj.attr("value") == defVal || thisObj.attr("value").length == 0) {

            thisObj.attr("value", defVal);
            thisObj.addClass("empty");
        }
    }
}

// Remove default text on focus. Ignore user-inserted text
function removeText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    if (inpType != "submit") {
        var currVal = thisObj.attr("value");
        if (currVal == defVal) {
            thisObj.attr("value", "");
            thisObj.removeClass("empty")
        }
    }
}

// Restore default text on focus. Ignore user-inserted text
function restoreText(defVal, thisObj) {
    var inpType = thisObj.attr("type");
    if (inpType != "submit") {
        var currVal = thisObj.attr("value");
        if (currVal != undefined && currVal != '') {
            thisObj.attr("value", currVal);
        }
        else if (currVal == undefined || currVal == '') {
            thisObj.attr("value", defVal);
            thisObj.addClass("empty");
        }
    }
}


function googlePageLoad() {
    // turned off for now. Gives the final dom load time as a google event. Easier to deploy if in this file.
    //	$(function() {
    //		try {
    //			pageTracker._trackEvent("Page load", "Load time", location.pathname, (new Date()).valueOf() - pageLoadTime.valueOf());
    //		}
    //		catch (err) {}
    //	});
}


// Create Google Map for Directions page

function loadGoogleMapApi() {
    google.load("maps", "2", { "callback": initMap });
}

function initMap() {

    var firstHtml = "<h3>Western House Hotel</h3><p style=\"line-height:1.2em;\">2 Craigie Road, Ayr<br /> KA8 0HA <br />United Kingdom</p>";
    var map = new GMap2($(".googleMap").get(0));
    map.addControl(new GLargeMapControl());
    map.addControl(new GMapTypeControl());
    map.setMapType(G_NORMAL_MAP);
    map.enableContinuousZoom();
    map.setCenter(new google.maps.LatLng(55.46511, -4.617112), 16);
    var firstPoint = new GLatLng(55.46511, -4.617112);
    var mapCentre = new GLatLng(55.46511, -4.617112);
    var secIcon = new GIcon(G_DEFAULT_ICON);
    secIcon.image = "http://www.google.com/intl/en_us/mapfiles/ms/micons/blue-dot.png";
    secIcon.iconSize = new GSize(32, 32);

    var firstMarker = new GMarker(firstPoint, new GIcon(G_DEFAULT_ICON));
    //var secMarker = new GMarker(secPoint, secIcon);
    map.setCenter(mapCentre, 15);
    map.addOverlay(firstMarker);
    //map.addOverlay(secMarker);



    GEvent.addListener(firstMarker, "click", function () {
        firstMarker.openInfoWindowHtml(firstHtml);
    });

//    GEvent.addListener(secMarker, "click", function () {
//        secMarker.openInfoWindowHtml(secHtml);
//    });

}
