﻿var selected_province = null;

$(document).ready(function() {
    if ($('#map')) {
        // Loop through each AREA in the imagemap
        $('#map area').each(function() {
            // Assign an action to the mouseover event
            $(this).mouseover(function(e) {
                var id = $(this).attr('id').replace('area_', '');

                switch (id) {
                    case "c1":
                    case "c2":
                    case "c3":
                        {
                            if (selected_category != id)
                                $('#' + id).show();
                            break;
                        }
                    default:
                        {                            
                            if (selected_province != id)
                                $('#' + id).show();

                            if (selected_province == null)
                                $('#divChooseProvince').hide(); 
                                
                            break;
                        }
                }
                //$('#'+ id).fadeIn('medium');         
            });

            // Assign an action to the mouseout event
            $(this).mouseout(function(e) {
                var id = $(this).attr('id').replace('area_', '');

                switch (id) {
                    case "c1":
                    case "c2":
                    case "c3":
                        {
                            if (selected_category != id)
                                $('#' + id).hide();
                            break;
                        }
                    default:
                        {
                            if (selected_province != id)
                                $('#' + id).hide();                            

                            if (selected_province == null)
                                $('#divChooseProvince').show();
                                
                            break;
                        }
                }
                //$('#'+ id).fadeOut('medium');              
            });

            // Assigning an action to the click event
            $(this).click(function(e) {
                    e.preventDefault();

                var id = $(this).attr('id').replace('area_', '');

                if (id == "popup") {
                    $('#divCityList').hide();
                    $('#divArrow').hide();
                    $('#' + selected_province).hide();
                    selected_province = null;
                    return;
                }

                if (id == "c1" || id == "c2" || id == "c3") {
                    if (selected_category == id)
                        return;
                    $('#' + selected_category).hide();
                    selected_category = id;

                    getCityList();

                    if (id == "c1") {
                        if (typeof chkResale_click == 'function')
                            chkResale_click();
                    }
                    else if (id == "c2") {
                        if (typeof chkRental_click == 'function')
                            chkRental_click();
                    }
                    else if (id == "c3") {
                        if (typeof chkNewHomes_click == 'function')
                            chkNewHomes_click();
                    }
                }
                else {
                    if (selected_province == id)
                        return;
                    if (selected_province != null)
                        $('#' + selected_province).hide();
                    selected_province = id;

                    getCityList();

                    if (selected_province == "QC"){
                                            
                        if (selected_category == "c1")
                            window.open('http://immo.lespac.com/immobilier-achat-vente');
                        else if (selected_category == "c2")
                            window.open('http://immo.lespac.com/immobilier-location');
                        else
                            window.open('http://immo.lespac.com/');
                            
                        $('#divCityList').hide();
                        $('#divArrow').hide();
                        $('#' + selected_province).hide();
                        selected_province = null;
                        return;
                    }
                }
            });
        });
    }
});

//    $("#map area_pupup").hover(function() {
//        $(this).css("cursor", "pointer");
//    });

function map_btnResale_click() {
    $('#c2').hide();
    $('#c3').hide();
    $('#c1').show();
    selected_category = "c1";
    getCityList();
}

function map_btnRental_click() {
    $('#c1').hide();
    $('#c3').hide();
    $('#c2').show();
    selected_category = "c2";
    getCityList();
}

function map_btnNewHomes_click() {
    $('#c1').hide();
    $('#c2').hide();
    $('#c3').show();
    selected_category = "c3";
    getCityList();
}

function msieversion() {
    var ua = window.navigator.userAgent
    var msie = ua.indexOf("MSIE ")

    if (msie > 0)      // If Internet Explorer, return version number
        return parseInt(ua.substring(msie + 5, ua.indexOf(".", msie)))
    else                 // If another browser, return 0
        return 0
}

function getCityList() {
    if (selected_province == null) return;

    var array;
    var province;
    var browser = msieversion();
    var ext = '.png';
    
    if (browser > 0 && browser < 7)
        ext = '.gif';
   
    $('#divMiddle_map').empty();
    $('#imgTop').attr('src', '../../Looks/default/images/map/cityPopupTop' + ext);
    $('#divMiddle_map').css({ 'background': 'url(../../Looks/default/images/map/cityPopupMiddle' + ext + ') repeat-y left top' });
    $('#imgBottom').attr('src', '../../Looks/default/images/map/cityPopupBottom' + ext);

    switch (selected_province) {
        case "ON":
            {
                province = "Ontario";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof ON_Ren != "undefined")
                                array = ON_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof ON_New != "undefined")
                                array = ON_New;
                            break;
                        }
                    default:
                        {
                            if (typeof ON_Res != "undefined")
                                array = ON_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '360px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_left' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '56px',
                    'left': '326px',
                    'top': '130px'
                });

                break;
            }
        case "QC":
            {
                province = "Quebec";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof QC_Ren != "undefined")
                                array = QC_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof QC_New != "undefined")
                                array = QC_New;
                            break;
                        }
                    default:
                        {
                            if (typeof QC_Res != "undefined")
                                array = QC_Res;
                            break;
                        }
                }
                break;
            }
        case "BC":
            {
                province = "British Columbia";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof BC_Ren != "undefined")
                                array = BC_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof BC_New != "undefined")
                                array = BC_New;
                            break;
                        }
                    default:
                        {
                            if (typeof BC_Res != "undefined")
                                array = BC_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '180px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_left' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '56px',
                    'left': '146px',
                    'top': '110px'
                });

                break;
            }
        case "AB":
            {
                province = "Alberta";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof AB_Ren != "undefined")
                                array = AB_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof AB_New != "undefined")
                                array = AB_New;
                            break;
                        }
                    default:
                        {
                            if (typeof AB_Res != "undefined")
                                array = AB_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '234px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_left' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '56px',
                    'left': '200px',
                    'top': '130px'
                });

                break;
            }
        case "MB":
            {
                province = "Manitoba";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof MB_Ren != "undefined")
                                array = MB_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof MB_New != "undefined")
                                array = MB_New;
                            break;
                        }
                    default:
                        {
                            if (typeof MB_Res != "undefined")
                                array = MB_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '320px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_left' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '56px',
                    'left': '286px',
                    'top': '110px'
                });

                break;
            }
        case "SK":
            {
                province = "Saskatchewan";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof SK_Ren != "undefined")
                                array = SK_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof SK_New != "undefined")
                                array = SK_New;
                            break;
                        }
                    default:
                        {
                            if (typeof SK_Res != "undefined")
                                array = SK_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '280px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_left' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '56px',
                    'left': '246px',
                    'top': '100px'
                });

                break;
            }
        case "NS":
            {
                province = "Nova Scotia";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof NS_Ren != "undefined")
                                array = NS_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof NS_New != "undefined")
                                array = NS_New;
                            break;
                        }
                    default:
                        {
                            if (typeof NS_Res != "undefined")
                                array = NS_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '337px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_ns' + ext + ') no-repeat left top',
                    'width': '36px',
                    'height': '121px',
                    'left': '463px',
                    'top': '102px'
                });

                break;
            }
        case "NB":
            {
                province = "New Brunswick";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof NB_Ren != "undefined")
                                array = NB_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof NB_New != "undefined")
                                array = NB_New;
                            break;
                        }
                    default:
                        {
                            if (typeof NB_Res != "undefined")
                                array = NB_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '320px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_nb' + ext + ') no-repeat left top',
                    'width': '36px',
                    'height': '109px',
                    'left': '446px',
                    'top': '100px'
                });

                break;
            }
        case "NL":
            {
                province = "Newfoundland";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof NL_Ren != "undefined")
                                array = NL_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof NL_New != "undefined")
                                array = NL_New;
                            break;
                        }
                    default:
                        {
                            if (typeof NL_Res != "undefined")
                                array = NL_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '297px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_nfl' + ext + ') no-repeat left top',
                    'width': '37px',
                    'height': '38px',
                    'left': '423px',
                    'top': '106px'
                });

                break;
            }
        case "PE":
            {
                province = "Prince Edward";

                switch (selected_category) {
                    case "c2":
                        {
                            if (typeof PE_Ren != "undefined")
                                array = PE_Ren;
                            break;
                        }
                    case "c3":
                        {
                            if (typeof PE_New != "undefined")
                                array = PE_New;
                            break;
                        }
                    default:
                        {
                            if (typeof PE_Res != "undefined")
                                array = PE_Res;
                            break;
                        }
                }

                $('#divCityList').css({ 'left': '340px', 'top': '75px' });

                $('#divArrow').css({
                    'background': 'url(../../Looks/default/images/map/m_arrow_pei' + ext + ') no-repeat left top',
                    'width': '36px',
                    'height': '99px',
                    'left': '466px',
                    'top': '100px'
                });
                break;
            }
    }

    if (selected_province == "QC") {
        hideCityListPopup();
        return;
    }

    if (array == null || array.length == 0)
        $('#divMiddle_map').append('<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class="map_heading">No listings match<br/>your current<br/>selection</td></tr>');
    else {           
        var city_list = '<table border=\"0\" cellspacing=\"0\" cellpadding=\"0\"><tr><td class="map_heading">' + province + '</td></tr>';
        $.each(array, function(index, value) {
            var city = value;

            if (city == "See all listings") {
                if (selected_category == "c1") {
                    switch (selected_province) {
                        case "AB":
                            city = "Calgary";
                            break;
                        case "BC":
                            city = "Vancouver";
                            break;
                        case "MB":
                            city = "Winnipeg";
                            break;
                        case "NB":
                            city = "Fredericton";
                            break;
                        case "NS":
                            city = "Halifax";
                            break;
                        case "ON":
                            city = "Toronto";
                            break;
                        case "PE":
                            city = "Charlottetown";
                            break;
                        case "SK":
                            city = "Regina"
                            break;
                    }
                }
                else if (selected_category == "c2") {
                    switch (selected_province) {
                        case "AB":
                            city = "Calgary";
                            break;
                        case "BC":
                            city = "Vancouver";
                            break;
                        case "MB":
                            city = "Winnipeg";
                            break;
                        case "NB":
                            city = "Fredericton";
                            break;
                        case "NL":
                            city = "St. John's";
                            break;
                        case "NS":
                            city = "Halifax";
                            break;
                        case "ON":
                            city = "Toronto";
                            break;
                    }
                }
                else if (selected_category == "c3") {
                    switch (selected_province) {
                        case "AB":
                            city = "Calgary";
                            break;
                        case "BC":
                            city = "Vancouver";
                            break;
                        case "MB":
                            city = "Winnipeg";
                            break;
                        case "ON":
                            city = "Toronto";
                            break;
                    }
                }
            }

            if (value == "See all listings")
                city_list += '<tr><td class=\"map_see_all_listings\"><a href="javascript:__doPostBack(\'lnkCity_Click\',\'' + city.replace("'", "&apos") + ', ' + selected_province + '|' + selected_category + '\')">' + value + '</a></td></tr>';
            else
                city_list += '<tr><td><a href="javascript:__doPostBack(\'lnkCity_Click\',\'' + city.replace("'", "&apos") + ', ' + selected_province + '|' + selected_category + '\')">' + value + '</a></td></tr>';  
            });
        city_list += '</table>'

        $('#divMiddle_map').append(city_list);           
    }

    showCityListPopup();     
}

function showCityListPopup() {
    $('#divCityList').show();       
    $('#divArrow').show();
}

function hideCityListPopup() {
    $('#divCityList').hide();       
    $('#divArrow').hide();
}
