﻿var lightBoxForwardingUrl = new Object();
lightBoxForwardingUrl["South"] = "";
lightBoxForwardingUrl["Hermitage"] = "";
lightBoxForwardingUrl["NorthernCounties"] = "";

$(document).ready(function() {
    var dlg = $('#LightBox').dialog({
        height: 440,
        width: 550,
        modal: true,
        draggable: false,
        resizable: false,
        autoOpen: false,
        closeOnEscape: true,
        open: function(event, ui) {
            $(this).parent().appendTo("form");
            $('#txtPostcode').val('Enter your postcode...');
            $('#failureMessage').text('');
            $('#progressBar').hide();
            $('.ui-dialog-titlebar').hide();
            $('#txtPostcode').keyup(function(event) {
                if (event.keyCode == 13) {
                    $('#btnSearch').click();
                }
            });
        }
    });

    $('a.SouthSubMenuItem').click(function() { OpenLightBox(); return false; });
    $('a.HermitageSubMenuItem').click(function() { OpenLightBox(); return false; });
    $('a.NorthernCountiesSubMenuItem').click(function() { OpenLightBox(); return false; });

    $('#progressBar').hide();

    $('#txtPostcode').keyup(function(event) {
        if (event.keyCode == 13) {
            $('#btnSearch').click();
        }
    });

    $("#MapContainer AREA").mouseover(function() {
        var regionMap = '#' + $(this).attr('id') + '-map';
        var regionList = '#' + $(this).attr('id') + '-div';
        $(regionMap).css('display', 'inline');
        $('#SearchPanel #RegionLogos div').hide();
        $(regionList).show();
    }).mouseout(function() {
        var regionMap = '#' + $(this).attr('id') + '-map';
        var regionList = '#' + $(this).attr('id') + '-div';
        if (!$(regionMap).hasClass('selected')) {
            $(regionMap).css('display', 'none');
        }
        $('#SearchPanel #RegionLogos div').hide();
        $('#SearchPanel #RegionLogos div.selected').show();
    });

    $("#MapContainer AREA").click(function() {
        $('#MapContainer img.region').removeClass('selected').css('display', 'none');
        $('#SearchPanel #RegionLogos div').removeClass('selected').hide();

        var regionMap = '#' + $(this).attr('id') + '-map';
        var regionDiv = '#' + $(this).attr('id') + '-div';
        $(regionMap).addClass('selected').css('display', 'inline');
        $(regionDiv).addClass('selected').show();
    });

    switch (region) { //this is declared and set inside LightBox.ascx.cs
        case 'South':
            $('#South-map').addClass('selected').css('display', 'inline');
            $('#South-div').addClass('selected').show();
            break;

        case 'Hermitage':
            $('#Hermitage-map').addClass('selected').css('display', 'inline');
            $('#Hermitage-div').addClass('selected').show();
            break;

        case 'NorthernCounties':
            $('#NorthernCounties-map').addClass('selected').css('display', 'inline');
            $('#NorthernCounties-div').addClass('selected').show();
            break;
    }
});

function OpenLightBox() {
    lightBoxForwardingUrl["South"] = "/resident-services/guinness-south.aspx";
    lightBoxForwardingUrl["Hermitage"] = "/resident-services/guinness-hermitage.aspx";
    lightBoxForwardingUrl["NorthernCounties"] = "/resident-services/guinness-northern-counties.aspx";
    
    $('#LightBox').dialog('open').addClass('show');
}

function OpenLightBoxForUrl(southUrl, hermitageUrl, northernCountiesUrl) {
    lightBoxForwardingUrl["South"] = southUrl;
    lightBoxForwardingUrl["Hermitage"] = hermitageUrl;
    lightBoxForwardingUrl["NorthernCounties"] = northernCountiesUrl;
    $('#LightBox').dialog('open');
}

function DoAjaxStuff(value) {
    $('#progressBar').show();
    $.ajax(
            {
                'url': '/RegionSelectionHandler.ashx?Postcode=' + value,
                'dataType': 'json',
                'type': 'POST',
                'success': function(data) {
                    if (data.Success == true) {
                        window.location.href = lightBoxForwardingUrl[data.Region] + '?SetRegion=' + data.Region; //data.RedirectUrl;
                    }
                    else {
                        $('#failureMessage').text(data.Status);
                    }
                    $('#progressBar').hide();
                },
                'error': function(XMLHttpRequest, textStatus, errorThrown) {
                    $('#progressBar').hide();
                    //<%--TODO think of something better to do than show a popup message.--%>
                }
            });
}
