﻿
/// <summary>
///   This JavaScript provides common functions for the menu 
///   and other dynamic navigation functionality.
/// </summary>
/// <history>
///  <change author="Konrad Kyc" copyright="2010 © Vantagesoft.ca" date="2010-11-19">Creation</change>
/// </history>

var imageArray = [];
var isIphone = navigator.userAgent.toLowerCase().indexOf('iphone') != -1;
var currentHomePOV = 0;
var homePOVSlideWidth = 950;
var numberOfHomePOVSlides = 0;
var homePOVTimer;

// When the Document Loads, call the initialize on load function
$(document).ready(function() {
    InitializeOnLoad();
});

/// <summary>
///  Initialize function that loads on PageLoad.
/// </summary>
function InitializeOnLoad() {
    PageInitOnLoad();
}

function PageInitOnLoad(){} // Virtual Function
function PageInit(){}       // Virtual Function
function PreparePrint() { } // Virtual Function

/// <summary>
///  Initializes the Home POV navigation (i.e. next/previous buttons)
/// </summary>
function InitializeHomePOVNavigation() {
    var slides = $('.POVImageSlide');
    numberOfHomePOVSlides = slides.length;

    // Remove scrollbar in JS
    $('.POVImageContainer').css('overflow', 'hidden');

    // Wrap all .slides with #povImageSlideInner div
    slides
        .wrapAll('<div id="povImageSlideInner"></div>')

        // Float left to display horizontally, readjust .slides width
	    .css({
	        'float': 'left',
	        'width': homePOVSlideWidth
	    });

    // Set #slideInner width equal to total width of all slides
    $('#povImageSlideInner').css('width', homePOVSlideWidth * numberOfHomePOVSlides);

    // Hide left arrow control on first load
    ManageHomePOVControls(currentHomePOV);

    // Create click events for Next/Previous buttons
    $('.POVNavigation').click(function () {
        // Determine new position
        currentHomePOV = ($(this).attr('id') == 'nextButton') ? currentHomePOV + 1 : currentHomePOV - 1;

        // Hide / show controls
        ManageHomePOVControls(currentHomePOV);

        // Move the image using margin-left
        $('#povImageSlideInner').animate({
            'marginLeft': homePOVSlideWidth * (-currentHomePOV)
        }, "normal");

        // On the button click, clear the interval timer which changes the POV.
        clearInterval(homePOVTimer);
    });

    // Attach mouse-over functionality to the Previous Button
    $(".POVPreviousButton").mouseover(function () {
        $(this).fadeTo(1000, 0.5);
    }).mouseout(function () { $(this).fadeTo(1000, 0.35); });

    // Attach mouse-over functionality to the Next Button
    $(".POVNextButton").mouseover(function () {
        $(this).fadeTo(1000, 0.5);
    }).mouseout(function () { $(this).fadeTo(1000, 0.35); });
 }

 /// <summary>
 ///  Method which either hides and shows the home POV
 ///  controls depending on current position.
 /// </summary>
 function ManageHomePOVControls(position) {
     // Hide left arrow if position is first slide
     if (position == 0 || position == numberOfHomePOVSlides - 1) {
         $('.POVPreviousButtonFrame').hide();
         $('.POVNextButtonFrame').show();
     }
     else if (position == numberOfHomePOVSlides - 2) {
         $('.POVPreviousButtonFrame').show();
         $('.POVNextButtonFrame').hide();
     }
 }

 /// <summary>
 ///  Method which changes the POV on a timer
 ///  until a button is clicked. 
 /// </summary>
 function AutoChangePOV() {
     // Reset the index if it's at the end
     if (currentHomePOV >= (numberOfHomePOVSlides - 1))
         currentHomePOV = 0;
     else
         currentHomePOV++;
    
    // Move the image using margin-left
     $('#povImageSlideInner').animate({
         'marginLeft': homePOVSlideWidth * (-currentHomePOV)
     }, "normal", function () {
         ManageHomePOVControls(currentHomePOV);

         if (currentHomePOV >= (numberOfHomePOVSlides - 1)) {
             currentHomePOV = 0;
             $('#povImageSlideInner').css("marginLeft", homePOVSlideWidth * (-currentHomePOV));
         }
     });
 }

/// <summary>
///  Pre-loads the Portfolio POV images.
/// </summary>
function PreloadPortfolioPOVImages() {
     if (povImages != null) {
         // Pre-load the defined images for the current page
         for (var i = 0; i < povImages.length; i++) {
             var povImage = new Image(100, 100);
             povImage.src = povImages[i].imagePath;
             imageArray.push(povImage);
         } 
     }
}

/// <summary>
///  Initializes the Portfolio POV navigation (to be able
///  to switch the view between images.
/// </summary>
function InitializePortfolioPOVNavigation() {
    $(".PortfolioPOVNavigatorBox").click(function () {
        var selectedIndex = ($(this).attr("id").replace("selector", "")) - 0;

        // Make sure that the item 
        if (selectedIndex != currentSelectedPOVIndex) {
            $(".PortfolioPOV img").fadeOut("slow", function () {
                $(this).attr("src", povImages[selectedIndex].imagePath);
                $(this).fadeIn("slow");
            });

            $(".PortfolioPOVNavigatorBoxSelected").removeClass("PortfolioPOVNavigatorBoxSelected");
            $(this).addClass("PortfolioPOVNavigatorBoxSelected");
            currentSelectedPOVIndex = selectedIndex;
        }
    });
}

/// <summary>
///  Initializes the available properties
///  thumbnail selectors, which displays a
///  thumbnail for each property upon a click.
/// </summary>
function InitializeAvailablePropertiesThumbs() {
    // Show the box on mouse-over
    $(".AvailabilityTableTitle").mouseover(function () {
        $(".AvailabilityTableItemThumb", $(this)).show();
    });
    
    // Hide the box on mouse-out
    $(".AvailabilityTableTitle").mouseout(function () {
        $(".AvailabilityTableItemThumb", $(this)).hide();
    });  
}
