﻿function ProductsList() {

    // Summary: Hide 'more product info' box and 'hide product details' button
	$(".moreProductInfo").hide();
	$(".noJSAltButton").hide();
    $(".btnShowAccountDetails").show();

    $url = window.location.hash;
    $($url).find(".moreProductInfo").show(0, function() {
        $($url).find(".showProductDetails").hide();
        $($url).find(".hideProductDetails").show();
        window.location = window.location;
    });

    // Summary: Show 'more product info'
    $(".btnShowAccountDetails").click(function() {
        $(this).hide();
        $(this).parent().parent().parent().find(".moreProductInfo").show();
        $(this).parent().parent().parent().find(".btnHideAccountDetails").show();
        $(this).parent().addClass('revealProductInfo');
    });

    // Summary: Hide 'more product info'
    $(".btnHideAccountDetails").click(function() {
        $(this).hide();
        $(this).parent().parent().parent().find(".moreProductInfo").hide();
        $(this).parent().parent().parent().find(".btnShowAccountDetails").show();
        $(this).parent().removeClass('revealProductInfo');
    });

    // Product feature additional text
    $(".additional").hide();
    $(".additional").parent().addClass("incAdditional");
    $(".toggleAdditional").css("cursor", "pointer");

    $(".toggleAdditional").click(function() {
        $(this).parent().find(".additional").toggle();
        $(this).toggleClass("selected");
    });


}

// Function: showModal
// Summary: Shows the modal window message
function showModal($width) {
    var widthOfPage = pageWidth() + "px";
    var heightOfPage = pageHeight() + "px";
    $('#modalBackGround').width(widthOfPage);
    $('#modalBackGround').height(heightOfPage);
    $('#modalWrapper').show();
    $('#modalMessage').show();
    $('#modalMessage').css('width', $width);
    $('#modalMessage').css('margin-left', ((pageWidth() / 2) - ($('#modalMessage').width() / 2)));
}


// Function: pageWidth
// Summary: Gets Page Width.
function pageWidth() {
    return window.innerWidth != null ? window.innerWidth : document.body != null ? document.body.clientWidth : null;
}


// Function: pageHeight
// Summary: Gets Page Height.
function pageHeight() {
    return window.innerHeight != null ? window.innerHeight : document.body != null ? document.body.clientHeight : null;
}

// Function: openModaliFrame
// Summary: Opens the modal window iframe to display hosted content.
function openModaliFrame($_href, $_title, $_width, $_height) {
	$('#contactFrame').attr('src', $_href);
	$('#contactFrame').attr('title', $_title);
	$('#contactFrame').show();
	$('#contactFrame').focus();
	$('#contactFrame').attr('width', $_width);
	$('#contactFrame').attr('height', $_height);
	showModal($_width);
    return false;
}

// Function: closeModalWindow
// Summary: Closes the modal window and removes iframe source.
function closeModalWindow() {
	$('#modalWrapper').hide();
	$('#modalMessage').hide();
	$('#savingsCalculator').hide();
	$('#applicationChecklist').hide();
	$('#contactFrame').hide();
	$('#contactFrame').attr('src', '');
	return false;
}


function ToggleSpecialConditions() {
	$('.scContent').hide();
	$('.scToggle').click(function() {
		$(this).next().toggle('fast');
		$(this).toggleClass('open');
	});
}


////////////////////////
// Navigation Functions
////////////////////////
var $delayMenuShow = true;
var $setDelay = 0;
var $maxDelay = 200;

// Summary: General function to detect and set the menu states.
function ToggleMenuOptions() {
	$('#menu .savings').mouseenter(function() {
		ShowMenu('#savingsMenu', $(this));
	});
	$('#menu .mortgages').mouseenter(function() {
		ShowMenu('#mortgagesMenu', $(this));
	});
	$('#menu .financial').mouseenter(function() {
		ShowMenu('#financialplanningMenu', $(this));
	});
	$('#menu .insurance').mouseenter(function() {
		ShowMenu('#insuranceMenu', $(this));
	});
	$('#navigation #navigationInner').mouseleave(function() {
		ResetMenu();
	});	
}

// Summary: Show menu option
function ShowMenu($menuToShow, $selected) {
	$setDelay = ($delayMenuShow) ?  $maxDelay : 0;
	HideMenus();
	$($selected).addClass("selectedmenu");
	if ($setDelay == $maxDelay)
		$($menuToShow).delay($setDelay).show(0);	
	else if ($setDelay == 0)
		$($menuToShow).show(0);			
	$delayMenuShow = false;
}

// Summary: Hide all menus.
function HideMenus() {
	$('.subMenu').stop();
	$('.subMenu').hide();
	$delayMenuShow = false;
    $("#menu li").removeClass("selectedmenu");
}

// Summary: Reset menu state
function ResetMenu() {
	$('.subMenu').stop();
	$('.subMenu').hide();
	$setDelay = 0;
	$delayMenuShow = true;
    $("#menu li").removeClass("selectedmenu");
}
////////////////////////////
// END Navigation Functions
////////////////////////////

$(document).ready(function() {
    $('#wrapper').addClass('js-enabled');
    ProductsList();
	ToggleSpecialConditions();

	ToggleMenuOptions();
	
	// Summary: Close modal window on 'ESC' keyup
	$('body').live('keyup', function(e){
		if(e.which == 27){
			closeModalWindow();
		}
	});



    // Make full advert clickable or widget by adding a class to wrapper and using the href of the link inside
    $(".clickable").click(function() {
        window.location = $(this).find('a').attr("href");
        return false;
    });

    // Form input placeholder text
    $('.incPlaceHolder').focus(function() {
        if ($(this).val() == $(this).attr('defaultValue')) {
            $(this).val('');
        }
    });
    $('.incPlaceHolder').blur(function() {
        if ($(this).val() == '') {
            $(this).val($(this).attr('defaultValue'));
        }
    });

    // Summary: Footer SiteMap Toggle
    $('.smCols').hide();
    $('.smActivate a').click(function() {
        $(this).parent().toggleClass('smSelected');
        $('.smCols').toggle('slow', 'linear');
    });


    // Summary: Savings Calc Button Hover Image Swap
    $('.savingsSection .btnToggleCalculator').hover(
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-calculate-over.gif');
        },
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-calculate.gif');
        }
    );	
	
    // Summary: Savings Calc Button Hover Image Swap
    $('.savingsSection .single .btnToggleCalculator').hover(
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-calculate-over-single.gif');
        },
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-calculate-single.gif');
        }
    );


    // Summary: Savings Calc Button Hover Image Swap
    $('.savingsSection .btnHideAccountDetails').hover(
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-hide-account-details-over.gif');
        },
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-hide-account-details.gif');
        }
    );


    // Summary: Savings Show Account Details Hover Image Swap
    $('.savingsSection .btnShowAccountDetails').hover(
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-show-account-details-over.gif');
        },
        function() {
            $(this).find('img').attr('src', '/App_Template/images/savings-show-account-details.gif');
        }
    );

});

