(function($) {
	    $("#info").hide();

	    if($("#noRegionmap").val() == 1) {
	    	return null;
	    }

        $.fn.maphilight.defaults = {
                fill: true,
                fillColor: 'EE3224',
                fillOpacity: 0.2,
                stroke: true,
                strokeColor: 'EE3224',
                strokeOpacity: 1,
                strokeWidth: 1,
                fade: true,
                alwaysOn: false,
                neverOn: false,
                groupBy: false
        };
        $('img[usemap]').maphilight();

        // ajax call to get the areas localized name
        $('area').each(function() {
        	var jsonUrl = "index.php?id=" + $('#pageUid').val() + "&type=1519&L=" + $('#languageId').val() + "&tx_lwregionmaps_pi1[action]=showAreaTitle&tx_lwregionmaps_pi1[controller]=Area&tx_lwregionmaps_pi1[areaid]="+$(this).attr('id');
        	var curArea = $(this);
        	$.get(
        		jsonUrl,
        		{},
        		function(json) {
        			var result = json;
        			curArea.attr('title', result);
        		},
        		"text"
        	);
        });

        // click on an area loads the responsible data set
        $('area').click(function(e) {

        		$("#info").html('<img src="/typo3conf/ext/lw_regionmaps/Resources/Public/Images/ajax-loader.gif" alt="loading" />').fadeIn("fast");
        		resetMapHighlight();
                var data = $(this).data('maphilight') || {};
                data.alwaysOn = !data.alwaysOn;
                $(this).data('maphilight', data).trigger('alwaysOn.maphilight');
                $("#info").hide().load("index.php?id=" + $('#pageUid').val() + "&type=1519&L=" + $('#languageId').val() + "&tx_lwregionmaps_pi1[action]=show&tx_lwregionmaps_pi1[controller]=Address&tx_lwregionmaps_pi1[areaid]="+$(this).attr('id')).fadeIn("slow");

                return false;
        });

        function resetMapHighlight(){
                $('area').each(
                        function (e){
                                var data = $(this).data('maphilight') || {};
                                data.alwaysOn = false;
                                $(this).data('maphilight', data).trigger('alwaysOn.maphilight');
                        }
                );
        }
})(jQuery);

