﻿function pageLoad(sender, args) {
    // Re-bind jQuery elements on AJAX postback.
    Sys.WebForms.PageRequestManager.getInstance().add_endRequest(function () { bindEvents() });
}

$(document).ready(function () {
    bindEvents();
});

function bindEvents() {
    // --== Date Pickers ==--
    $(".jdate-all").datepicker({
        showOn: "button",
        buttonImage: "/resources/images/calendar.png",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true
    });

    $(".jdate-enquiry-renewal").datepicker({
        showOn: "button",
        buttonImage: "/resources/images/calendar.png",
        buttonImageOnly: true,
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true
    });

    $(".jdate-quote-startdate").datepicker({
        showOn: "button",
        buttonImage: "/resources/images/calendar.png",
        buttonImageOnly: true,
        minDate: new Date(),
        maxDate: "+1M",
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true
    });

    $(".jdate-quote-previnsurance").datepicker({
        showOn: "button",
        buttonImage: "/resources/images/calendar.png",
        buttonImageOnly: true,
        maxDate: new Date(),
        changeMonth: true,
        changeYear: true,
        showOtherMonths: true,
        selectOtherMonths: true
    });

    // --== Open target in blank (XHTML Strict override) ==--
    $('a[rel*=external]').click(function () {
        window.open(this.href);
        return false;
    });

//    var $floatingDiv = $('div.jfloatpanel');
//    $(window).scroll(function () {
//        $floatingDiv.stop().animate({ 'marginTop': ($(window).scrollTop() + 30) + 'px' }, 'slow');
//    });

    // --== Tooltip Backgrounds for Textboxes ==--
    function tooltip_classchange(sender) { if ($(sender).val().length <= 0) { $(sender).addClass('empty') } else { $(sender).removeClass('empty') } }
    $('input.clientreference,input.policyreference').each(function () { tooltip_classchange($(this)); });
    $('input.clientreference,input.policyreference').focusin(function () { $(this).removeClass('empty'); });
    $('input.clientreference,input.policyreference').focusout(function () { tooltip_classchange($(this)); });

    // --== Primary Navigation ==--
    var config = { sensitivity: 2, interval: 200, over: megaHoverOver, timeout: 300, out: megaHoverOut };
    if (isIE6() == false) {
        $("#navigation > li div").parent().find("a:first").append("<span class=\"selector\"></span>");
        $("#navigation > li div").parent().hoverIntent(config);
        $("div#body").append("<div id='fader'></div>");
    }

    function megaHoverOver() { $("div#fader", document).stop().fadeTo('fast', 0.7).show(0); $(this).find("div").stop().show(0); $(this).find("a").addClass("selected") }
    function megaHoverOut() { $("div#fader", document).stop().fadeTo('fast', 0).hide(0); $(this).find("div").stop().hide(0); $(this).find("a").removeClass("selected") }

    // --== Search Box ==--
    var bSearchEmpty = true;
    var sEmpty = 'What are you looking for? ...';
    $('input.search-box').val(sEmpty);
    $('input.search-box').focusin(function () { if (bSearchEmpty == true) { $(this).val('') } });
    $('input.search-box').focusout(function () { if ($(this).val().length <= 0) { $(this).val(sEmpty) } });
    $('input.search-box').keydown(function (e) { if ($(this).val().length > 0) { bSearchEmpty = false } else { bSearchEmpty = true } });

    // --== Slide Show ==--
    // --== Uses http://malsup.com/jquery/cycle/ plugin
    // $('div.slideshow').find('a.link').hide(0);
    $('div.slideshow')
        .cycle({
            fx: 'fade',
            speed: 1000,
            timeout: 12000
        });

    $('div.slideshow').find('a.pause').click(function () {
        if ($(this).hasClass('play') == true) {
            $('div.slideshow').cycle('resume');
            $(this).removeClass('play');
        } else {
            $('div.slideshow').cycle('pause');
            $(this).addClass('play');
        }
    });

    $('div.slideshow').find('a.slide-1').click(function () { $('div.slideshow').cycle(0); return false; });
    $('div.slideshow').find('a.slide-2').click(function () { $('div.slideshow').cycle(1); return false; });
    $('div.slideshow').find('a.slide-3').click(function () { $('div.slideshow').cycle(2); return false; });
}

// --== Check for IE6 ==--
function isIE6() {
    var bReturn = false;
    if ($.browser.msie) {
        if ($.browser.version == '6.0') {
            bReturn = true;
        }
    }
    return bReturn;
}
