jQuery(document).ready(function($){
    var popup_stack = {};
    var id_now = false;
    var is_focus = false;
    
    function href2uri(href) {
        var _popup_url = "/no_cache/xhr/";
        var popup_url = false; 
        var tokes = href.split("/area/");
        if (tokes.length == 2 && tokes[1] != "") {
            popup_url = _popup_url + "area/" +  tokes[1];
        } else {
           tokes = href.split("/kategorie/");
           if (tokes.length == 2 && tokes[1] != "") {
               popup_url = _popup_url + "kategorie/" + tokes[1];
           }
        }
        return popup_url;
    }
    
    function uri2id(uri) {
      uri = uri.split("?");
      uri = uri[0];
      return uri.substr(1, uri.length-6)
         .replace(/\//g, "_")
         .replace(/ß/g, "sz")
         .replace(/ä/g, "ae")
         .replace(/ö/g, "oe")
         .replace(/ü/g, "ue")
         .replace(/Ä/g, "Ae")
         .replace(/Ö/g, "Oe")
         .replace(/Ü/g, "Ue");
    }
    
    function spawn(id, data) {
        var elem = $('#'+id);
        if (elem.length > 0) {
            var x = data.pageX+5;
            var y = data.pageY;
            $("#"+id).css("top", y+"px");
            $("#"+id).css("left", x+"px");        
            $("#"+id).show();
        }
        return false;
    }

    function removePopups(sender) {
       
       for (var i in popup_stack) {
          if (uri2id(i) == is_focus && sender != "document")
            continue;
          $("#"+uri2id(i)).hide();
       }
    }

    var prev = $(".tx_mwimagemap");
    $(prev).mousemove(function (data) {
        return spawn(id_now, data);
    });

    $(prev).mouseout(function () {
        removePopups("mwimagemap");
        //id_now = false;
    });

    $("area").mousemove(function (data) {
        var popup_url = href2uri($(this).attr("href"));
        if (!popup_url) {
           return false;
        }
        var id = uri2id(popup_url);
        id_now = id;
        spawn(id, data);
        return false;
    });

    $("area").click(function (data) {
        var popup_url = href2uri($(this).attr("href"));
        if (popup_url) {
           var id = uri2id(popup_url);
           is_focus = id;
           var elem = $('#'+id);
           if (elem.length > 0) {
               var x = data.pageX-175;
               var y = data.pageY-50;
               $("#"+id).css("top", y+"px");
               $("#"+id).css("left", x+"px");
               $("#"+id).show();
               return false;
           }
        }
        return false;
    });

    $("area").mouseover(function (data) {
        is_focus = false;
        removePopups("area");
        var target_url = $(this).attr("href")
        var popup_url = href2uri(target_url);
        if (popup_url != false && popup_stack[popup_url] == undefined) {
            popup_stack[popup_url] = 1;
            var id = uri2id(popup_url);
            id_now = id;
            var title = $(this).attr("alt");
            $("body").append('<div id="'+id+'" class="map_popup"><div class="header">'+title+'</div><div class="waitstate"></div></div>');
            spawn(id, data);
            $.get(popup_url, function(data) {
                var cont = data.split("<body>");
                cont = cont[1].split("</body>");
                cont = cont[0];
                $('#'+id+' .waitstate').remove();
                $('#'+id).append('<div class="content">'+cont+'</div>');
                $('.cal_prod_area').each(function () {
                   var _html = $(this).html().split("<br>");
                   var html = [];
                   for (var i=0; i<_html.length; i++) {
                     if (_html[i] != "")
                        html.push(_html[i]);
                   }
                   $(this).html(html.join(", "));
                });

                $('#'+id+' .content').prepend('<div class="more_link" style="font-weight: bold;"><a href="'+target_url+'">alle Länderinformationen anzeigen</a></div>');

                $('#'+id+' .news-list-popup-container').append('<div class="more_link"><a href="'+target_url+'">weitere anzeigen...</a></div>');
                $('#'+id+' .cal-list-popup-container').append('<div class="more_link"><a href="'+target_url+'">weitere anzeigen...</a></div>');
                $('#'+id+' .tx-ttproducts-pi1 > div').append('<div class="more_link"><a href="'+target_url+'">weitere anzeigen...</a></div>');
            });
        } 
        return false;
    });

    $(document).click(function (event) {
        var has = false;
        $(event.target).parents().map(function () {
            if ($(this).hasClass("map_popup")) {
                has = true;
            }
        });
        if ($(event.target).hasClass("map_popup")) {
            has = true;
        }
        if (!has) {
             id_now = false;
             is_focus = false;
             removePopups("document");
        }
    });   
});

