
/**
* jQuery.ScrollTo - Easy element scrolling using jQuery.
*/
; (function($) { var m = $.scrollTo = function(b, h, f) { $(window).scrollTo(b, h, f) }; m.defaults = { axis: 'xy', duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1 }; m.window = function(b) { return $(window).scrollable() }; $.fn.scrollable = function() { return this.map(function() { var b = this, h = !b.nodeName || $.inArray(b.nodeName.toLowerCase(), ['iframe', '#document', 'html', 'body']) != -1; if (!h) return b; var f = (b.contentWindow || b).document || b.ownerDocument || b; return $.browser.safari || f.compatMode == 'BackCompat' ? f.body : f.documentElement }) }; $.fn.scrollTo = function(l, j, a) { if (typeof j == 'object') { a = j; j = 0 } if (typeof a == 'function') a = { onAfter: a }; if (l == 'max') l = 9e9; a = $.extend({}, m.defaults, a); j = j || a.speed || a.duration; a.queue = a.queue && a.axis.length > 1; if (a.queue) j /= 2; a.offset = n(a.offset); a.over = n(a.over); return this.scrollable().each(function() { var k = this, o = $(k), d = l, p, g = {}, q = o.is('html,body'); switch (typeof d) { case 'number': case 'string': if (/^([+-]=)?\d+(\.\d+)?(px)?$/.test(d)) { d = n(d); break } d = $(d, this); case 'object': if (d.is || d.style) p = (d = $(d)).offset() } $.each(a.axis.split(''), function(b, h) { var f = h == 'x' ? 'Left' : 'Top', i = f.toLowerCase(), c = 'scroll' + f, r = k[c], s = h == 'x' ? 'Width' : 'Height'; if (p) { g[c] = p[i] + (q ? 0 : r - o.offset()[i]); if (a.margin) { g[c] -= parseInt(d.css('margin' + f)) || 0; g[c] -= parseInt(d.css('border' + f + 'Width')) || 0 } g[c] += a.offset[i] || 0; if (a.over[i]) g[c] += d[s.toLowerCase()]() * a.over[i] } else g[c] = d[i]; if (/^\d+$/.test(g[c])) g[c] = g[c] <= 0 ? 0 : Math.min(g[c], u(s)); if (!b && a.queue) { if (r != g[c]) t(a.onAfterFirst); delete g[c] } }); t(a.onAfter); function t(b) { o.animate(g, j, a.easing, b && function() { b.call(this, l, a) }) }; function u(b) { var h = 'scroll' + b; if (!q) return k[h]; var f = 'client' + b, i = k.ownerDocument.documentElement, c = k.ownerDocument.body; return Math.max(i[h], c[h]) - Math.min(i[f], c[f]) } }).end() }; function n(b) { return typeof b == 'object' ? b : { top: b, left: b} } })(jQuery);

; (function($) { var l = location.href.replace(/#.*/, ''); var g = $.localScroll = function(a) { $('body').localScroll(a) }; g.defaults = { duration: 1e3, axis: 'y', event: 'click', stop: true, target: window, reset: true }; g.hash = function(a) { if (location.hash) { a = $.extend({}, g.defaults, a); a.hash = false; if (a.reset) { var e = a.duration; delete a.duration; $(a.target).scrollTo(0, a); a.duration = e } i(0, location, a) } }; $.fn.localScroll = function(b) { b = $.extend({}, g.defaults, b); return b.lazy ? this.bind(b.event, function(a) { var e = $([a.target, a.target.parentNode]).filter(d)[0]; if (e) i(a, e, b) }) : this.find('a,area').filter(d).bind(b.event, function(a) { i(a, this, b) }).end().end(); function d() { return !!this.href && !!this.hash && this.href.replace(this.hash, '') == l && (!b.filter || $(this).is(b.filter)) } }; function i(a, e, b) { var d = e.hash.slice(1), f = document.getElementById(d) || document.getElementsByName(d)[0]; if (!f) return; if (a) a.preventDefault(); var h = $(b.target); if (b.lock && h.is(':animated') || b.onBefore && b.onBefore.call(b, a, f, h) === false) return; if (b.stop) h.stop(true); if (b.hash) { var j = f.id == d ? 'id' : 'name', k = $('<a> </a>').attr(j, d).css({ position: 'absolute', top: $(window).scrollTop(), left: $(window).scrollLeft() }); f[j] = ''; $('body').prepend(k); location = e.hash; k.remove(); f[j] = d } h.scrollTo(f, b).trigger('notify.serialScroll', [f]) } })(jQuery);

var bPhotoOrig = true;
var viewingRef, viewingTel;

$(document).ready(function () {

    // photos

    $("#slider").easySlider({
        auto: false,
        controlsShow: true,
        thumbnailsId: 'thumbs'
    });
    $("#slider li img").css("visibility", "visible");

    $("#slider li img, #resize").click(
            function () {
                if (bPhotoOrig) {
                    growPic();
                } else {
                    shrinkPic();
                }
            });

    $("#nextBtn a").hover(
            function () { $("#nextBtn a").css("background-position", "0 -40px"); },
            function () { $("#nextBtn a").css("background-position", "0 0"); }
            );

    $("#prevBtn a").hover(
            function () { $("#prevBtn a").css("background-position", "0 -40px"); },
            function () { $("#prevBtn a").css("background-position", "0 0"); }
            );

    $("#thumbs li").hover(
            function () { $(this).addClass('hl'); },
            function () { $(this).removeClass('hl'); }
            );

    var isWide = ($(window).width() >= 1140);
    var isWideNow;

    $(window).resize(function () {

        isWideNow = ($(this).width() >= 1140);
        if (isWide != isWideNow) {

            //alert(globalGalleryIndex);

            $("#slider").easySlider({
                controlsShow: false,
                thumbnailsId: 'thumbs',
                startIndex: globalGalleryIndex
            });
        }
        isWide = isWideNow;
    })

});

function growPic() {
    bPhotoOrig = false;
    var isWide = $(window).width() >= 1140;
    $("#slider").animate({ height: isWide ? "533px" : "465px"}, 1000);
    $("#slider li").animate({ marginTop: 0 }, 1000);
    $("#nextBtn a").animate({ top: isWide ? "239px" : "205px" }, 1000);
    $("#prevBtn a").animate({ top: isWide ? "239px" : "205px" }, 1000);
    $(".oText").animate({ bottom: "-131px" }, 1000);
    $("#resize").css("background-position", "0 -22px");
}

function shrinkPic() {
    bPhotoOrig = true;
    var isWide = $(window).width() >= 1140;
    $("#slider").animate({ height: isWide ? "413px" : "334px" }, 1000);
    $("#slider li").animate({ marginTop: "-70px" }, 1000);
    $("#nextBtn a").animate({ top: isWide ? "186px" : "152px" }, 1000);
    $("#prevBtn a").animate({ top: isWide ? "186px" : "152px" }, 1000);
    $(".oText").animate({ bottom: "-70px"}, 1000);
    $("#resize").css("background-position", "0 0");
}


function showMort() {
    $("#divMort").dialog("open");
}

function clearAll(Except) {
    if (Except != 'Photo') { if (!bPhotoOrig) shrinkPic(); }
    if (Except != 'Mort') {
        $("#divMort").fadeOut("fast");
        $("#tbMort").removeClass("tbselected");
    }
    if (Except != 'Email') {
        $("#divEmail").fadeOut("fast");
        $("#tbEmail").removeClass("tbselected");
    }
    if (Except != 'SMS') {
        $("#divSMS").fadeOut("fast");
        $("#tbSMS").removeClass("tbselected");
    }
}

/* Functions for Mortage calculator module */

function initMortgageCalculator() {
    $("#divMort").css('display', 'block');
    $("[id$='ddlMortgageType']").change(recalc);
    $("[id$='tbDepositAmount']").keyup(recalc);
    $("[id$='tbDepositPercentage']").keyup(recalc);
    $("[id$='tbAER']").keyup(recalc);
    $("[id$='tbValue']").keyup(recalc);
    $("[id$='tbAER']").blur(function () {
        if ($(this).val() == '') { $(this).val(0) }
    }
    );

    $("input[name$='rbDepositType']").click(function () {
        if ($(this).val() == 'percent') { $("[id$='tbDepositPercentage']").show(); } else { $("[id$='tbDepositPercentage']").hide(); }
        if ($(this).val() == 'amount') { $("[id$='tbDepositAmount']").show(); } else { $("[id$='tbDepositAmount']").hide(); }
    });
}

function recalc() {
    $("[id$=tbAER]").val($("[id$=tbAER]").val().toString().replace(/[^0-9\.]/g, ''));
    $("[id$=tbValue]").val($("[id$=tbValue]").val().toString().replace(/[^0-9, ]/g, ''));
    var iPropertyValue = $("[id$=tbValue]").val().toString().replace(',', '');
    var iDeposit;
    if ($("input[name$='rbDepositType']:checked").val() == 'percent') {
        $("[id$=tbDepositPercentage]").val($("[id$=tbDepositPercentage]").val().toString().replace(/[^0-9\.]/g, ''));
        var fDepositPercent = ($("[id$='tbDepositPercentage']").val() == "") ? 0.0 : $("[id$='tbDepositPercentage']").val();
        iDeposit = Math.floor(iPropertyValue * fDepositPercent / 100);
    } else {
        $("[id$=tbDepositAmount]").val($("[id$=tbDepositAmount]").val().toString().replace(/[^0-9\.,]/g, ''));
        iDeposit = ($("[id$='tbDepositAmount']").val() == "") ? 0 : parseInt($("[id$='tbDepositAmount']").val());
    }

    $("#mortMonthly").html(calculateMonthly(iPropertyValue - iDeposit, $("[id$='ddlMortgageType']").val() * 1, $("[id$=tbAER]").val() * 1));
}

function formatCurrency(num) {
    num = num.toString().replace(/\jQuery|\,/g, '');
    if (isNaN(num)) num = "0";
    pounddigits = num.indexOf(".");
    for (var i = 0; i < Math.floor((pounddigits - (1 + i)) / 3); i++)
        num = num.substring(0, pounddigits - (4 * i + 3)) + ',' + num.substring(pounddigits - (4 * i + 3));
    return ("&pound;" + num);
}

function calculateMonthly(iTotalLoan, iYears, dInterest) {
    var iMonthAmount;
    if (iTotalLoan <= 0) { iMonthAmount = 0; }
    if (dInterest == 0) { iMonthAmount = iTotalLoan / (iYears * 12); }
    if (iTotalLoan > 0 && dInterest > 0) {
        if (iYears == 0) {
            iMonthAmount = (iTotalLoan * dInterest / 100) / 12;
        } else {
            var dblMonthlyRate = Math.pow(1 + dInterest / 100, 1 / 12) - 1;
            iMonthAmount = iTotalLoan * dblMonthlyRate * Math.pow(1 + dblMonthlyRate, iYears * 12) / (Math.pow(1 + dblMonthlyRate, iYears * 12) - 1);
        }
    }
    return formatCurrency(iMonthAmount.toFixed(2));
}
