﻿/*

File:		eqtr_functions.js
Project:	Ayr Race Course/Hotel
Created:	30/08/2010
Author:		Jamie Boyd @ Equator	

Notes:		Requires jQuery v1.4.2

*/

var googleApi = "http://www.google.com/jsapi?key=" + "ABQIAAAAtahBlgfTy6FurKAiarEdThRnI1Y8c6VnFPk0nftXZL_QpKH2iBTEHouG-WrIIjzKU-jH3qcN1CGt3Q";

$(function () {


    //// Match Heights of Main and Sub Columns

    var mainHeight = $(".mainContent").eq(0).height();
    var mainContentHeight = $(".mainContent .content .sectionPageContent").eq(0).height();
    var subHeight = $(".subContent").eq(0).height();

    if (subHeight != null && mainContentHeight != null) {
        if (mainHeight < subHeight) {
            var heightDif = subHeight - mainHeight;
            mainContentHeight = mainContentHeight + heightDif
                $(".mainContent .content .sectionPageContent").css("min-height", mainContentHeight);
        }

    }


    //// Features Carousel ////

    if ($(".featureCarousel li").length > 1) {

        var numberOfFeatures = $(".featureCarousel li").length;

        // Hide all features but then...
        $(".featureCarousel li").hide();
        // ...show first feature
        $(".featureCarousel li").eq(0).show();
        // Create feature buttons
        var controls = document.createElement("div");
        $(controls).addClass("controls");
        // Add buttons
        for (x = 0; x < numberOfFeatures; x++) {
            var button = document.createElement("a");
            $(button).attr("href", "#").text(x + 1);
            $(controls).eq(0).append(button);
        }
        $(".featureCarousel").append(controls);
        // Add class to first button
        $(".featureCarousel .controls a").eq(0).addClass("current");

        // Add click event functions to buttons
        $(".featureCarousel .controls a").live('click', function () {

            clearTimeout(timerID);
            // Do nothing if currently active button is clicked
            if ($(this).hasClass("current") == true) {
                return false;
            }
            // otherwise...
            // Remove class from control for current feature
            $(".featureCarousel .controls a").removeClass("current");
            // Get position of clicked button in array of buttons
            var controlsArray = $(".featureCarousel .controls a");
            var thisPos = jQuery.inArray(this, controlsArray);
            // Fade out current feature and fade in feature corresponding to clicked button
            $(".featureCarousel li.current").fadeOut(750).removeClass("current");
            $(".featureCarousel li").eq(thisPos).fadeIn(750).addClass("current");
            // Add class to just clicked button
            $(this).addClass("current");
            // Restart automatic advance
            //timerID = setTimeout(advanceFeature, 5000);
            // Return false to block anchor click
            return false;
        });

        // Automatic stepping through features

        // Loop through images and animate transition
        var features = $(this).find(".featureCarousel li");
        var currentFeature = 0;
        var nextFeature = 1;
        var length = features.length;
        var timerID = null;

        function advanceFeature() {

            if (nextFeature == (length)) {
                nextFeature = 0;
            }

            $(".featureCarousel .controls a").eq(nextFeature).click();

            timerID = setTimeout(advanceFeature, 5000);
            nextFeature++;
        }

        timerID = setTimeout(advanceFeature, 5000);
    }


    $("fieldset.newsletter input[type=text]").each(function () {
        var labelVal = $(this).attr("title");

        $(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));
        });
    });


    // Load Google Map if container is present on page
    if ($(".googleMap").length) {
        $.getScript(googleApi, loadGoogleMapApi);
    }

    //    $(".subContent .gallery a").each(function () {

    //        var path = $(this).attr("href");
    //        alert(path);
    //    });

    // Lightbox for Select Gallery
    $(".mainContent .gallery .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 .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
    });
});


// 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 Whitletts Rd, Ayr, <br />KA8 0JE <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(51.468218, -0.025942), 16);
    var firstPoint = new GLatLng(55.466892, -4.616575);
    //var secPoint = new GLatLng(51.4753135, -0.0234582);
    var mapCentre = new GLatLng(55.466892, -4.616575);
    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);
    });

}

/***************
Bookmark Button
****************/
function addBookmark() {
    var title = document.title;
    var url = self.location;
    if (window.sidebar && window.sidebar.addPanel) {
        window.sidebar.addPanel(title, location, "");
    }
    else if (window.opera && window.print) {
        var mbm = document.createElement('a');
        mbm.setAttribute('rel', 'sidebar');
        mbm.setAttribute('href', url);
        mbm.setAttribute('title', title);
        mbm.click();
    }
    else if (window.external && typeof window.external.addFavorite != 'undefined') {
        window.external.addFavorite(url, title);
    }
    else {
        alert("Press CTRL-D (Netscape) or CTRL-T (Opera) to bookmark");
    }
}



