﻿
    var curIndex = 0;
    var curClicked = -1;
    var curClickedName = '';
    var searchType = '';

    $(function () {
        turnOnMouseover();

        $("#countrySelectLib").dialog({
            autoOpen: false,
            modal: true,
            position: 'center',
            overlay: {
                backgroundColor: '#000',
                opacity: 0.5
            },
            resizable: true,
            width: 300,
            height: 200
        });

    });

    function setSearch(type) {
        searchType = type;
    }

    function turnOnMouseover() {
        $('#mapAreas').children('area').each(function () {
          
            var idVal = $(this).attr('id');
            idVal = idVal.replace('ply', '');
            var continentName = $(this).attr('alt');
            $(this).click(function () {
                submitForm(continentName, idVal);
            });
            $(this).mouseover(function () {
               
                switchMap(idVal, continentName);
            });
        });
    }

    // Upon mousing over a region, show the corresponding image (with the region highlighted, etc)
    // And replace the "Region" text with the name of the region
    function switchMap(mapIndex, continentName) {
        var controlID = '#img' + continentName;

        var curClickedControl = '#img' + curClickedName;
        
        if ((curClicked != -1) && (curClicked.toString() != mapIndex)) {
            $(curClickedControl).hide();
            $("#regName").text(continentName);
            $("#imgWorld").hide();
            $(controlID).show();
        }
        else {
            
            $("#regName").text(continentName);
            $("#imgWorld").hide();
            $(controlID).show();
  
        }
    }

    // Upon mousing out of the image, reset to the original world map (no highlights)
    function showWorld(continentName) {
        var controlID = '#img' + continentName;

       
        if (curClickedName == '') {
          
            $("#imgWorld").show();
            $(controlID).hide();
//            switch (mapIndex) {

//                case -1:  //Reset all
//                    $("#imgAmericas").hide();
//                    $("#imgEurope").hide();
//                    $("#imgAfrica").hide();
//                    $("#imgAsia").hide();
//                    $("#imgOceania").hide();
//                    $("#imgWorld").show();
//                    break;
//            }
            // Clear the region name text
            $("#regName").text('');
        }
       // else if (mapIndex != -1) {
        else if (continentName != '') {
          
            $(controlID).hide();

            var curClickedControl = "#img" + curClickedName;
            $(curClickedControl).show();
         
            $("#regName").text(curClickedName);
//            switch (curClicked) {

//                case "-1":    //Reset
//                    $("#imgWorld").show();
//                    $("#regName").text('');
//                    break;
//            }

        }
        else {
            alert('here-else');
            //Reset all
            $('#continentImgs').children('img').each(function () {
                $(this).hide();
            });
            $("#imgWorld").show();
        }
        
    }

    function submitForm(country, mapIndex) {
        switchMap(mapIndex);
        $("#StatesRegions").hide();
        $("#CountriesRegions").show();
        if (searchType.toLowerCase() == 'lib') {
            $("#hidCountryValLib").val(mapIndex);
            $("#frmImgMapLib").submit();
        }
        else if (searchType.toLowerCase() == 'ret') {
            $("#hidCountryValRet").val(mapIndex);
            $("#frmImgMapRet").submit();
        }
        curIndex = mapIndex;
        curClicked = mapIndex;
        curClickedName = country;
    }

    
     

