﻿
$(function() {

    // setup sifr prior to hiding any elements
    setupSifr();

    // activate correct member panel
    if (hireClubActive)
        showPHPanel();
    else
        showTCPanel();

    // google map integration
    if ($('#man_map').length) {
        $("#man_map").gMap(
            options = {
                zoom: 16,
                markers: [{ latitude: 53.48710075932455, longitude: -2.1899056434631347, html: "<strong>Mayne Coaches Limited <br />(Manchester Depot)</strong>", popup: true}]
            }
        );
    }
    if ($('#warr_map').length) {
        $("#warr_map").gMap(
            options = {
                zoom: 16,
                markers: [{ latitude: 53.39790, longitude: -2.57960, html: "<strong>Mayne Coaches Limited <br />(Warrington Depot)</strong>", popup: true}]
            }
        );
    }

    // date pickers
    $('.date_picker').datepicker({
        dateFormat: 'dd/mm/yy',
        defaultDate: +7
    });

    // back to top link
    $('#back_top').click(function() {
        $('html, body').animate({ scrollTop: '0px' }, 300);
        return false;
    });

    // setup private hire club login panel
    $('#ph_login_tab').click(function() {
        showPHPanel();
        return false;
    });

    // setup travel club login panel
    $('#tc_login_tab').click(function() {
        showTCPanel();
        return false;
    });

    // excursion description
    $('.show_more').click(function() {
        $(this).closest('tr').hide().next('tr').show();
        return false;
    });
    $('.show_less').click(function() {
        $(this).closest('tr').hide().prev('tr').show();
        return false;
    });

    // setup lightbox
    if ($('a.lightbox').length)
        $('a.lightbox').lightBox();

    // setup notification dialogs
    $('.notification_dialog').dialog({
        modal: true,
        resizable: false,
        draggable: false,
        autoOpen: true,
        buttons: {
            "Ok": function() { $(this).dialog("close"); }
        }
    });

    bindAjaxHandlers();
});

function showPHPanel() {
    $('#tc_login').hide();
    $('#ph_login').show();
    $('#tc_login_tab').removeClass('active');
    $('#ph_login_tab').addClass("active").parent().parent().removeClass('tabs1');
    // replace sifr numbers here to ensure it occurs after the block has been displayed (http://discuss.joyent.com/viewtopic.php?id=9970)
    sIFR.replaceElement(named({ sSelector: "span.ph_number", sFlashSrc: "/Content/Flash/als-script.swf", sColor: "#c62e3f", nPaddingTop: "0", nPaddingBottom: "0", nPaddingLeft: "0", sWmode: "transparent" }));    
}

function showTCPanel() {
    $('#ph_login').hide();
    $('#tc_login').show();
    $('#ph_login_tab').removeClass('active');
    $('#tc_login_tab').addClass("active").parent().parent().addClass('tabs1');
    sIFR.replaceElement(named({ sSelector: "span.tc_number", sFlashSrc: "/Content/Flash/als-script.swf", sColor: "#c62e3f", nPaddingTop: "0", nPaddingBottom: "0", nPaddingLeft: "0", sWmode: "transparent" }));        
}

function formResponseCallback(responseText, statusText) {
    bindAjaxHandlers();
}

function bindAjaxHandlers() {
    $('.ajax_form').submit(function() {
        $(this).ajaxSubmit({ target: $(this).attr('lang'), success: formResponseCallback });
        return false;
    });

    $('.ajax_link').click(function() {
        $($(this).attr('lang')).load($(this).attr('href'), null, formResponseCallback);
        return false;
    });
}
