﻿// Namespace the functions to remove possibility of conflict.
var Peach = {

    /// <summary>
    /// Instantializes the cufon font replacement engine.
    /// </summary>
    initializeCufonFonts: function () {

        Cufon.replace('h1', { fontFamily: 'DIN-Black', fontWeight: '900' });
        Cufon.replace('h2', { fontFamily: 'DIN-Black', fontWeight: '900' });
        Cufon.replace('h3', { fontFamily: 'DIN-Black', fontWeight: '900' });
        Cufon.replace('h4', { fontFamily: 'DIN-Black', fontWeight: '900' });
    },

    /// <summary>
    /// Fixes the main and catalogue menus to change the product page link to highlighted when a sub page.
    /// is open.
    /// </summary>
    fixMenuHighlight: function () {

        // Fix the main menu.
        jQuery("#bmsNavbar a").each(function () {

            if (jQuery(this).attr("href").indexOf("/Products") > 0 && location.href.indexOf("/Products") > 0) {

                jQuery(this).parent("li").addClass("sel");
            }
        });

        // Fix the catalogue menu.
        jQuery("ul.catalogue li a, a.breadcrumb").each(function () {
            var href = jQuery(this).attr("href");
            href = href.substring(0, href.length - 5);
            if (location.href.indexOf(href) > 0) {
                jQuery(this).addClass("sel");
            }
        });
    },

    /// <summary>
    /// Uses ajax get to display the contact form on the product details page.
    /// </summary>
    passThroughShopForm: function () {

        if (location.href.indexOf("ProdID") > 0) {
            // Make the machinery wanted action visible.
            jQuery(".whateverAction").css("display", "block");
            // Hide the noStock Action.
            jQuery(".noStockAction").css("display", "none");

            // Load data from the server and place the returned HTML into the matched element.
            var $iFrame = "<iframe id=\"iframe\" src =\"/Form.aspx\" width=\"100%\" scrolling=\"no\" frameBorder=\"0\"><p>Your browser does not support iframes.</p></iframe>";

            jQuery("#bmsShopFormWrapper").append($iFrame);
            jQuery("#iframe").iframeAutoHeight();

            // Remove the title from the details.
            jQuery("#iframe").closest(".container").find("h1:first-child").remove("h1:first-child");
        }
    },

    /// <summary>
    /// Edits the hard-coded values of elements in the DNN form module
    /// </summary>
    tidyUpForms: function () {

        jQuery(".UDT_Default").attr("value", "Send your enquiry");
        jQuery(".UDT_Buttons span").first().remove();
    },

    /// <summary>
    /// Adjusts the height of the wrapper div to fix a css issue.
    /// </summary>
    adjustFormHeight: function () {

        var $bodyHeight = jQuery(window).height();
        jQuery("#bmsWrapper").css("min-height", $bodyHeight);

    },

    /// <summary>
    /// Executes the functions when the DOM is fully loaded.
    /// </summary>
    trimProductStrings: function () {
        jQuery("td.productSummery").each(function () {
            jQuery(this).html(Peach.simpleTruncate(jQuery(this).html(), 100));
        });
    },

    /// <summary>
    /// Truncates a given string by trimming the string to a set maximum length and 
    /// adds ... to the end.
    /// </summary>
    /// <param name="input" type ="String">The string to truncate.</param>
    /// <param name="maxLength" type ="Integer">The maximum length to set the given input.</param>
    /// <returns>A truncated string with ... at the end.</returns>
    simpleTruncate: function (input, maxLength) {
        if (input.length > maxLength) {
            return input.substring(0, maxLength) + "...";
        }
    },

    /// <summary>
    /// Initializes the jQuery lightbox functionality.
    /// <remarks>
    /// Depends on the lightbox plugin contained within the nb_store module.
    /// </remarks>
    /// </summary>
    initializeLightbox: function () {
        jQuery('.lightboxLink').lightBox();
    },

    /// <summary>
    /// Initializes the call to google to get the latest map script.
    /// </summary>
    initializeMapScript: function () {

        if (jQuery("#googleMapCanvas").length > 0) {

            jQuery.getScript("http://maps.google.com/maps/api/js?sensor=false&callback=Peach.createMap");
        }

    },

    /// <summary>
    /// Creates a map on the page.
    /// <remarks>
    /// Depends on the initializeMapScript function to load the google map script.
    /// </remarks>
    /// </summary>
    createMap: function () {
        // Create the map variables.
        var $latLong = new google.maps.LatLng(55.945845, -2.892338);
        var $mapCentre = new google.maps.LatLng(55.942596, -3.055086);

        var $options = {
            zoom: 10,
            center: $mapCentre,
            mapTypeId: google.maps.MapTypeId.ROADMAP
        }

        // Create the Map instance.
        var $map = new google.maps.Map(document.getElementById("googleMapCanvas"), $options);

        // Create a Marker for the map
        var $marker = new google.maps.Marker({
            clickable: true,
            position: $latLong,
            map: $map,
            title: "Bindery Machinery Services"
        });

        // Create an instance of the InfoWindow class and assign it some content.
        var $InfoWindow = new google.maps.InfoWindow({
            maxWidth: 150,
            content: "<div><strong>Bindery Machinery Services</strong><br/>Unit, 24A Macmerry Industrial Estate, TRANENT, East Lothian EH33 1RD.</div>"
        });

        // Add the click event listener to the marker which will trigger the InfoWindow's open method
        google.maps.event.addListener($marker, 'click', function () {
            //open the infowindow
            $InfoWindow.open($map, $marker);
        });

        // Hide the ajax loader
        jQuery(".mapLoader").hide();

    },

    /// <summary>
    /// Uses feature detection to return the internet explorer browser number.
    /// </summary>
    getIEVersion: function () {
        var browser = 0;
        // The browser is IE 6 - 8.
        if (!jQuery.support.leadingWhitespace) {

            // IE 6 & 7.
            if (!jQuery.support.boxModel) {
                if (!jQuery.support.opacity && !window.XMLHttpRequest) {
                    browser = 6;
                }
                else {
                    browser = 7;
                }
            }
            else {
                browser = 8;
            }
        }
        return browser;
    }
};

/// <summary>
/// Executes the functions when the DOM is fully loaded.
/// </summary>
jQuery(document).ready(function () {

    Peach.passThroughShopForm();
    Peach.fixMenuHighlight();
    Peach.initializeCufonFonts();
    Peach.tidyUpForms();
    Peach.trimProductStrings();
    Peach.adjustFormHeight();
    Peach.initializeLightbox();

});

/// <summary>
/// Executes the functions when the window is fully loaded.
/// </summary>
jQuery(window).load(function () {

    Peach.initializeMapScript();

});




