var mapDivID = 'divMapObject';
var smallMap = 'smallMap';
var outerMapModalID = 'divOuterMapObject';

var strRedirect = "default.aspx";

// Use the document load to Initialize the functions to be used.
$(document).ready(function () {

    $('#' + smallMap).click(function (e) {
        e.preventDefault();
        $('#' + outerMapModalID).modal({ containerId: 'modalContainer', overlayId: 'modalOverlay', onClose: MapModalClose });
    });

    var state = $(document).getUrlParam("s");
    var country = $(document).getUrlParam("cc");
    var mapNum = $(document).getUrlParam("mn");
    var regionNum = $(document).getUrlParam("rn");

    var mapObjStr1 = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" width="500" height="400" id="world_usa_map" align="middle"> <param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="world_usa_map.swf" /> <param name="quality" value="high" /> <param name="wmode" value="transparent" /> <param name="bgcolor" value="#ffffff" />';

    var mapObjStr2 = '<param name=FlashVars value="xmlfile1=world_map.xml&show_map=0"> <embed src="world_usa_map.swf?xmlfile1=world_map.xml&show_map=0" quality="high" wmode="transparent" bgcolor="#ffffff" width="500" height="400" name="world_usa_map" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';

    if (regionNum && mapNum) {
        mapObjStr2 = '<param name=FlashVars value="xmlfile1=world_map.xml&show_map=' + mapNum +
            '&show_country=' + regionNum +
            '"> <embed src="world_usa_map.swf?xmlfile1=world_map.xml&show_map=' + mapNum +
            '&show_country=' + regionNum +
            '" quality="high" wmode="transparent" bgcolor="#ffffff" width="500" height="400" name="world_usa_map" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
    }

    if (!regionNum && country == 'US') {
        mapObjStr2 = '<param name=FlashVars value="xmlfile1=world_map.xml&show_map=1&show_country=0' +
            '"> <embed src="world_usa_map.swf?xmlfile1=world_map.xml&show_map=1&show_country=0' +
            '" quality="high" wmode="transparent" bgcolor="#ffffff" width="500" height="400" name="world_usa_map" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
    }

    if (!regionNum && mapNum) {
        mapObjStr2 = '<param name=FlashVars value="xmlfile1=world_map.xml&show_map=' + mapNum +
            '&show_country=0"> <embed src="world_usa_map.swf?xmlfile1=world_map.xml&show_map=' + mapNum +
            '&show_country=0" quality="high" wmode="transparent" bgcolor="#ffffff" width="500" height="400" name="world_usa_map" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>'
    }
    strRedirect = GetUrl(state, country, regionNum, mapNum);

    var mapobject = mapObjStr1 + mapObjStr2;

    $('#' + mapDivID).html(mapobject);

});

function GetUrl(state, country, regionNum, mapNum) {
    var keyword = $(document).getUrlParam("ss");
    var city = $(document).getUrlParam("cy");
    var category = $(document).getUrlParam("c");

    if (category) {
        strRedirect = "default.aspx?c=" + category;
    }
    else {
        strRedirect = "default.aspx?sa=true"; //sa = search all categories
    }

    if (country) { strRedirect += "&cc=" + country; }
    if (state) { strRedirect += "&s=" + state; }
    if (regionNum) { strRedirect += "&rn=" + regionNum; }
    if (mapNum) { strRedirect += "&mn=" + mapNum; }
    if (city) { strRedirect += "&cy=" + city; }
    if (keyword) { strRedirect += "&ss=" + keyword; }

    return strRedirect;
}

function MapModalClose(dialog) {
    dialog.data.fadeOut('slow', function () {
        dialog.container.slideUp('slow', function () {
            $.modal.close(); // must call this!
            window.location = strRedirect;
        });
    });
}

function CloseMapModal() {
    $('#' + outerMapModalID).fadeOut(1000, function () {
        $.modal.close();
        window.location = strRedirect;
    });
}

function set_region(mapNum, region, regionNum) {
    $("#divMapObject").html('Updating List...');

    strRedirect = "default.aspx?" + regionNum + '&' + mapNum + '&' + region;

    CloseMapModal();
}  

