﻿/* This script is used to find and select
*  all car parking booking buttons on the page
*  and attach additional event tracking actions 
*  for Google Analytics [uses jQuery].
*/

/* This method is used to add event tracking on
booking form submit buttons. */
function addCarParkingButtonEventTracking(bookingButtons) {
    bookingButtons.click(function() {
        trackEvent("Book_parking");
    });
}

/* This method is used to add event tracking on
commerical bumps under the parking form. */
function addCommercialBumpsEventTracking(bumpListItems) {
    bumpListItems.each(function() {
        var bumpListItem = $(this);
        var bumpLinks = bumpListItem.find('a');

        //console.log('Commercial bump links: ' + bumpLinks.length);

        if (bumpLinks.length > 0) {
            bumpLinks.click(function() {
                trackEvent("Commercial-" + bumpListItem.attr('id'));
            });
        }
    });
}

/* This method formats the event category, action
and label and then the track event */
function trackEvent(category) {
    var currentUrl = urlClean(sitecoreUrlClean(location.pathname));
    var splitPath = currentUrl.split('/');

    var action = splitPath[0];
    var label = "home-page";

    if (splitPath.length > 2) {
        label = splitPath[1] + "-" + splitPath[2];
    } else if (splitPath.length > 1) {
        label = splitPath[1];
    }

    if (action == '') {
        action = "site-home-page";
        label = "site-home-page";
    }

    // add click event to the link
    pageTracker._trackEvent(category, action, label);

    //console.log("pageTracker._trackEvent('" + category + "', '" + action + "', '" + label + "'); fired");
}

/* This function is used to strip out 'http://',
'https://', 'www.', querystrings, forward slashes
and .aspx extensions from URLs used in
Google Analytics external link tracking. */
function urlClean(inputUrl) {

    // remove http:// and https://
    var outputUrl = inputUrl.replace("http://", "").replace("https://", "");

    // remove www.
    if (outputUrl.indexOf("www.") == 0) {
        outputUrl = outputUrl.substring(4, outputUrl.length);
    }

    // remove querystring
    if (outputUrl.indexOf("?") >= 0) {
        outputUrl = outputUrl.substring(0, outputUrl.indexOf("?"));
    }

    // remove anchors
    if (outputUrl.indexOf("#") >= 0) {
        outputUrl = outputUrl.substring(0, outputUrl.indexOf("#"));
    }

    // remove last forward slash
    if (outputUrl.lastIndexOf("/") == outputUrl.length - 1) {
        outputUrl = outputUrl.substring(0, outputUrl.lastIndexOf("/"));
    }

    // remove .aspx extension
    outputUrl = outputUrl.replace(".aspx", "");

    return outputUrl.substring(1);
}

/* This function is used to remove certain pages from Sitecore
URLs, such as home.aspx and default.aspx to 
consolidate home page results in Google Analytics. */
function sitecoreUrlClean(inputUrl) {

    // remove index, home and default
    var outputUrl = inputUrl.toLowerCase().replace("home.aspx", "").replace("default.aspx", "");

    return outputUrl;
}

$(function() {
    var bookingButtons = $("#airportParking p.book input.button, #airportParking .getQuote .control input.button");

    //console.log('Booking buttons: ' + bookingButtons.length);

    if (bookingButtons.length > 0) {
        addCarParkingButtonEventTracking(bookingButtons);
    }

    bookingButtons = $("#airportParking p.book input:image.carParkBookButton");

    //console.log('Booking buttons (v3): ' + bookingButtons.length);

    if (bookingButtons.length > 0) {
        addCarParkingButtonEventTracking(bookingButtons);
    }

    var bumpListItems = $("ul.secondaryTools li, #airportParking ul.promoted li");

    //console.log('Bump list items: ' + bumpListItems.length);

    if (bumpListItems.length > 0) {
        addCommercialBumpsEventTracking(bumpListItems)
    }
	
	bumpListItems = $("#airportParking #promoted li");
	
	//console.log('Bump list items: ' + bumpListItems.length);
	
    if (bumpListItems.length > 0) {
        addCommercialBumpsEventTracking(bumpListItems)
    }
});
