jQuery.noConflict();
jQuery(document).ready(function($) {
    var selectedProvince = '';
    //Functions when clicking on province on map
    provinceSelected = function(regionID, province) {
        document.getElementById('clickLayer').src = templatePath + 'images/landing_map/' + province + '.png';
        selectedProvince = province;
        $('#map_region_dropdown').html('');
        $('#searchingIn').hide();
        $('#searchingIn').html('<h3>SEARCHING IN ' + province + ':</h3>');
        $('#searchingIn').show('slow');
        resultsHTML = "";


        var clearCache = "";
        if (location.href.indexOf('ccs_clear_cache=1') != -1 || location.href.indexOf('.preview') != -1) clearCache = '&ccs_clear_cache=1';


        $.getJSON(ccs.contextPath + "/rest/looklocal/en/region/getRegions?cat_id=" + regionID + clearCache, function(data) {
            var provArr = new Array();
            $.each(data.data, function(id, item) {
                provArr[id] = item.regionName;

                var sectionName = item.categories[0];
                sectionName = sectionName.replace(/Website\//, '');


                resultsHTML += '<a onclick="openSite(\'' + sectionName + '\')" value="' + provArr[id] + '">' + provArr[id] + '</a><br>';
            });
            $("#map_region_dropdown").html(resultsHTML);
            var newProvArr = new Array();
            $("#autosearch").keyup(function() {
                var searchVal = $(this).val();
                newProvArr = jQuery.grep(provArr, function(n, i) {
                    n = n.toLowerCase();
                    return (n.indexOf(searchVal) != -1);
                });
                resultsHTML = "";
                $.each(newProvArr, function(n) {
                    resultsHTML += '<a onclick="openSite(\'' + newProvArr[n] + '\')" value="' + newProvArr[n] + '">' + newProvArr[n] + '</a><br>';
                });
                $("#map_region_dropdown").html(resultsHTML);
            });
        });
    };
    //Get json from rest service convert to text to simplify and extract sectionNames then convert back to json and pass to autoSuggest plugin
    var regionData;
    var dataObject;
    var regionArray = new Array();
    $("#autosearchProv").hide();
    $('#searchingIn').html('<h3>SEARCH:</h3>');
    $.getJSON(ccs.contextPath + "/rest/looklocal/en/region/getAllRegions", function(data) {
        resultsHTML = "";
        $.each(data.data, function(id, item) {
            regionArray[id] = item.regionName;

            var sectionNameProv = item.categories[0];
            sectionNameProv = sectionNameProv.replace(/Website\//, '');

            resultsHTML += '<a onclick="openSite(\'' + sectionNameProv + '\')" value="' + item.regionName + '">' + item.regionName + '</a><br>';
        });
        $("#map_region_dropdown").html(resultsHTML);
        var newArray = new Array();
        $("#autosearch").keyup(function() {
            var searchVal = $(this).val();
            newArray = jQuery.grep(regionArray, function(n, i) {
                n = n.toLowerCase();
                return (n.indexOf(searchVal) != -1);
            });
            $("#map_region_dropdown").html('');
            resultsHTML = "";
            $.each(newArray, function(n) {
                resultsHTML += '<a onclick="openSite(\'' + newArray[n] + '\')" value="' + newArray[n] + '">' + newArray[n] + '</a><br>';
            });
            $("#map_region_dropdown").html(resultsHTML);
        });
    });


    openSite = function(sectionDir) {
        sectionDir = sectionDir.replace(/ \/ /g, "-").replace(/ /g, "-").toLowerCase();
        url = ccs.contextPath + "/content/en/" + sectionDir + "/" + sectionDir;
        if (isMotoringLanding) {
            url += "-register?motoring=true";
        } else {
            url += "-home";
        }
        var expireDate = new Date();
        expireDate.setTime(new Date().getTime() + (5000 * 24 * 60 * 60 * 1000));
        var cookiePath = ccs.contextPath;
        setCookie("looklocal_region", sectionDir, expireDate, cookiePath);
        deleteGoogleCookie();
        window.location = url.toLowerCase();
    }

    highlightProvince = function(country) {
        document.getElementById('countryLayer').src = templatePath + 'images/landing_map/' + country + '.png';
    }

    clearCountry = function() {
        document.getElementById('countryLayer').src = templatePath + 'images/landing_map/map_transparent_new.gif';
    }

    deleteGoogleCookie = function() {
        deleteCookie("from_google", "<%=request.getContextPath()%>");
    }

});

