﻿
/// <summary>
///   This JavaScript provides common functions for the menu 
///   and other dynamic navigation functionality.
/// </summary>
/// <history>
///  <change author="Konrad Kyc" copyright="2011 © Vantagesoft.ca" date="2011-01-19">Creation</change>
/// </history>

// Define the Name Spaces
var Vantagesoft = {};
Vantagesoft.Home = {};
Vantagesoft.Constants = {};
Vantagesoft.Google = {};
Vantagesoft.CurrentProperties = {};
Vantagesoft.PropertyInfo = {};
Vantagesoft.Experience = {};
Vantagesoft.Contact = {};

// Constants
Vantagesoft.Constants.IconPath = "Images/MapIcon.png";
Vantagesoft.Constants.GlobalMapElementId = "mapCanvas";

// Global Variables
Vantagesoft.Google.GlobalMapZoom = 5;
Vantagesoft.Google.GlobalMapDefaultLatitude = 40.85750715625203;
Vantagesoft.Google.GlobalMapDefaultLongitude = -117.759765625;
Vantagesoft.Google.InfoWindowDisableAutoPan = false;

Vantagesoft.Google.ExperienceMapZoom = 6;
Vantagesoft.Google.ExperienceMapDefaultLatitude = 36.85750715625203;
Vantagesoft.Google.ExperienceMapDefaultLongitude = -119.459765625;

Vantagesoft.Google.PropertyMapZoom = 11;

Vantagesoft.Google.InfoWindows = [];
Vantagesoft.Google.Map = null;
Vantagesoft.Google.Geocoder = null;


// Virtual Functions
Vantagesoft.PageOnLoad = function () { };

// When the Document Loads, call the initialize on load function
$(document).ready(function () {
    Vantagesoft.InitializeOnLoad();
});

/// <summary>
///  Initialize function that loads on PageLoad.
/// </summary>
Vantagesoft.InitializeOnLoad = function () {
    // Call the Page-On Load function (which may be specific to each page).
    Vantagesoft.PageOnLoad();
};

/// <summary>
///  Method which Changes the Anchor Store POVs on the home page.
/// </summary>
Vantagesoft.Home.ChangeAnchorStore = function () {
    $(".HomeContentAnchorStoreContainer img").fadeOut("slow", function () {
        $(this).attr("src", anchorStoreImages[anchorStoreImageIndex].ImagePath);
        $(this).attr("alt", anchorStoreImages[anchorStoreImageIndex].StoreName);
        $(this).fadeIn("slow");

        if ((anchorStoreImageIndex + 1) < anchorStoreImages.length)
            anchorStoreImageIndex++;
        else
            anchorStoreImageIndex = 0;
    });
};

/// <summary>
///  Method which Changes the POV item.
/// </summary>
Vantagesoft.Home.ChangePOVItem = function () {
    var selectionIndex = currentPOVIndex + 1;
    var homePOVAreas = $(".HomePOVArea");

    if (selectionIndex >= homePOVAreas.length) {
        selectionIndex = 0;
        clearInterval(Vantagesoft.Home.POVTimer);
        Vantagesoft.Home.POVTimer = null;
    }

    if (selectionIndex != 0 || Vantagesoft.Home.POVTimer != null)
        Vantagesoft.Home.SelectPOVItem(selectionIndex);
    else
        Vantagesoft.Home.SelectPOVItem(-1);
}

/// <summary>
///  Method which selects a home POV item based on the given index.
/// </summary>
Vantagesoft.Home.SelectPOVItem = function (index) {
    var homePOVAreas = $(".HomePOVArea");

    // Make sure the current item isn't already selected
    if (!$(".HomePOVExpandIcon-0" + (index + 1)).hasClass("HomePOVExpandIcon-Seleceted")) {
        // If a POV is already selected, then fade out the current item
        // and fade in the selected one.
        if (currentPOVIndex >= 0) {
            $(homePOVAreas[currentPOVIndex]).fadeOut("slow", function () {
                if (index >= 0) {
                    $(".HomePOVExpandIcon-Seleceted").removeClass("HomePOVExpandIcon-Seleceted");
                    $(".HomePOVExpandIcon-0" + (index + 1)).addClass("HomePOVExpandIcon-Seleceted");
                    $(homePOVAreas[index]).fadeIn("slow");
                    currentPOVIndex = index;
                }
                else {
                    $(".HomePOVExpandIcon-Seleceted").removeClass("HomePOVExpandIcon-Seleceted");
                    currentPOVIndex = 0;
                }
            });
        }
        else {
            $(".HomePOVExpandIcon-Seleceted").removeClass("HomePOVExpandIcon-Seleceted");
            $(".HomePOVExpandIcon-0" + (index + 1)).addClass("HomePOVExpandIcon-Seleceted");
            // Fade-in the specified item
            $(homePOVAreas[index]).fadeIn("slow");
            currentPOVIndex = index;
        }
    }
    else {
        Vantagesoft.Home.SelectPOVItem(-1);
        clearInterval(Vantagesoft.Home.POVTimer);
    }
};

/// <summary>
///  Method which initializes the map on the page.
/// </summary>
Vantagesoft.Google.InitializeMap = function () {
    Vantagesoft.Google.Geocoder = new google.maps.Geocoder();

    // Set the map options
    var mapOptions = {
        zoom: Vantagesoft.Google.GlobalMapZoom,
        center: new google.maps.LatLng(Vantagesoft.Google.GlobalMapDefaultLatitude, Vantagesoft.Google.GlobalMapDefaultLongitude),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // Create the instance of the Map
    Vantagesoft.Google.Map = new google.maps.Map(document.getElementById(Vantagesoft.Constants.GlobalMapElementId), mapOptions);
};

/// <summary>
///  Method which initializes the Experience map on the page.
/// </summary>
Vantagesoft.Google.InitializeExperienceMap = function () {
    Vantagesoft.Google.Geocoder = new google.maps.Geocoder();

    // Set the map options
    var mapOptions = {
        zoom: Vantagesoft.Google.ExperienceMapZoom,
        center: new google.maps.LatLng(Vantagesoft.Google.ExperienceMapDefaultLatitude, Vantagesoft.Google.ExperienceMapDefaultLongitude),
        mapTypeId: google.maps.MapTypeId.ROADMAP
    };

    // Create the instance of the Map
    Vantagesoft.Google.Map = new google.maps.Map(document.getElementById(Vantagesoft.Constants.GlobalMapElementId), mapOptions);
};

/// <summary>
///  Method which adds property markers (overlays) to the created map.
/// </summary>
Vantagesoft.Google.AddPropertyMarkers = function () {
    // Create property markers on the created map
    for (var i = 0; i < Vantagesoft.Properties.length; i++) {
        Vantagesoft.AddPropertyMarker(Vantagesoft.Google.Map, Vantagesoft.Properties[i]);
    }
};

/// <summary>
///  Method which creates a property marker on the given map, based
///  on the provided details in the Property object.
/// </summary>
/// <param name="map">The instance of the map where the marker is to be created.</param>
/// <param name="property">Property object containing information about a property
///  for which to create a marker on the map.</param>
Vantagesoft.AddPropertyMarker = function (map, property) {
    // Make sure that the Latitude and Longitude have been defined.
    if (property.Latitude != '' && property.Longitude != '') {
        var position = new google.maps.LatLng(property.Latitude, property.Longitude);
        var marker = new google.maps.Marker({ map: map, position: position, title: property.PropertyTitle, icon: Vantagesoft.Constants.IconPath });

        // Create the Info Window (close it if another one is shown)                
        var infoWindow = new google.maps.InfoWindow({ disableAutoPan: Vantagesoft.Google.InfoWindowDisableAutoPan, content: unescape(property.PropertyDetails).replace(/::::/gi, " ") });
        Vantagesoft.Google.InfoWindows.push(infoWindow);

        // Add the created Info Window to the created marker (when a click event occurrs)
        google.maps.event.addListener(marker, 'click', function () { Vantagesoft.CloseAllInfoWindows(); infoWindow.open(map, marker); });
    }
};

/// <summary>
///  Method which closes all the Map Info windows.
/// </summary>
Vantagesoft.CloseAllInfoWindows = function () {
    for (var i = 0; i < Vantagesoft.Google.InfoWindows.length; i++) {
        Vantagesoft.Google.InfoWindows[i].close();
    }
};

/// <summary>
///  Method which initializes and displays the map on the contact
///  page for the given Map Id and Address.
/// </summary>
/// <param name="mapId">The Id of the map to be initialized.</param>
/// <param name="address">The address that is to be shown on the map.</param>
Vantagesoft.Contact.DisplayMap = function (mapId, address) {
    // Initialize the map           
    Vantagesoft.Contact.Geocoder = new google.maps.Geocoder();

    // Set the map center with the created GeoCoder
    Vantagesoft.Contact.Geocoder.geocode({ 'address': address },
        function (results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                var position = results[0].geometry.location;

                // Set the map options
                var mapOptions = {
                    zoom: Vantagesoft.Google.PropertyMapZoom,
                    center: position,
                    mapTypeId: google.maps.MapTypeId.ROADMAP
                };

                var map = new google.maps.Map(document.getElementById(mapId), mapOptions);
                var marker = new google.maps.Marker({ map: map, position: position, title: "", icon: Vantagesoft.Constants.IconPath });
            }
        }
    );
}


