/*global $, BrowserDetect, Image, window, document*/

var clearBackgroundUrl = function(dirtyURL)
{
     if (dirtyURL !== null && dirtyURL !== "")
     {
        return dirtyURL.replace("url(\"","").replace("\")","").replace("url(","").replace(")","");
     }
     else
     {
        return "/images/mapcomponent/city-bullet.png";
     }
};


function getImgSize(url)
{
    var size = {};

    if (url !== null && url !== "" && url !== "none")
    {
        var _url = clearBackgroundUrl(url);

        var newImg = new Image();
        newImg.src = _url;


        size.width = newImg.width;
        size.height = newImg.height;
    }
    else
    {
        size.width = 16;
        size.height = 16;
    }
     
    return  size;
}


var popupHide = function(sender)
{
    if (jQuery(sender).is(":visible") === true)
    {
        jQuery(sender).hide();
        jQuery(".PopupArrow").hide();
        jQuery(".PopupBackground").hide();
        jQuery(".Nodes").children("li").each(function(){jQuery(this).css("z-index","2");});
        jQuery(".Layers").css("z-index","1");
    }
};


var popupShow = function(sender)
{
    if (jQuery(sender).is(":visible") === false)
    {
        jQuery(sender).show();
        jQuery(".PopupArrow").show();
        jQuery(".PopupBackground").show();
        jQuery(".Nodes").children("li").each(function(){jQuery(this).css("z-index","0");});   
        if (BrowserDetect.browser === "Explorer")
        {
            jQuery(".Layers").css("z-index","-1");
        }else
        {
            jQuery(".Layers").css("z-index","0");
        }
    }
};


var showPOIPopUp = function(sender, articlePageID, mode)
{

    window.status = "";
    
    if (articlePageID !== null && articlePageID !== "Links" && document.getElementById("POIPopUpContent_" + articlePageID) !== null)
    {
        
        var _node = (mode == 'node') ? sender.parentNode : document.getElementById("node_" + articlePageID);
        var _nodeLeft = parseInt(_node.style.left.replace("px",""), 10);
        var _showRight = (_nodeLeft < 320 && mapComponentMode == 'wide');
        
        jQuery(".Popup").hide();
        jQuery(".PopupArrow").remove();
        jQuery(".PopupBackground").remove();
        
        var _popup = jQuery("#POIPopUpContent_" + articlePageID);
        var _popupheight = 151;
        var _popupwidth = 320;

        var _popupBackgroundClass = "PopupBackground";
        var _popupBackgroundImageClass = (_showRight) ? "PopupRightBackgroundImage" : "PopupBackgroundImage";

        if (_popup.hasClass("NarrowPopup") === true)
        {
            _popupwidth = 160;
            _popupBackgroundClass = "PopupBackground PopupBackgroundNarrow";
            _popupBackgroundImageClass = (_showRight) ? "PopupRightBackgroundImageNarrow" : "PopupBackgroundImageNarrow";
        }
        
        if(mode == 'node')
        {
            _popupBackgroundImageClass += " PopupBackgroundImagePosition";
        }
        
        var offsetX = 74;
        var offsetY = 20;
        var arrowWidth = 0;
        
        var _senderHeight = Math.round(_node.clientHeight/2);
        var _senderWidth = _node.clientWidth;
        var _left = _nodeLeft - _popupwidth - 36 - offsetX;
        
        if(_showRight)
        {
            _left = _nodeLeft + _senderWidth - offsetX - 2;
            arrowWidth = 30;
        }
        
        var _top = parseInt(_node.style.top.replace("px",""), 10) - Math.round(_popupheight/2) + Math.round(_senderHeight) - offsetY;
        
        jQuery(_popup).css('position','absolute').css('top',_top + offsetY).css('left',_left + offsetX + arrowWidth);
        
        var popupBackground = jQuery.create('div',{'class':_popupBackgroundClass},[]);
        var popupArrow1 = jQuery.create('div',{'class':'PopupArrow Arrow1'},[]);
        var popupArrow2 = jQuery.create('div',{'class':'PopupArrow Arrow2'},[]);
        var popupArrow3 = jQuery.create('div',{'class':'PopupArrow Arrow3'},[]);
        
        if(arrowWidth == 30)
        {
            jQuery(popupArrow1).css('position','absolute').css('top',_top+17+ offsetY).css('left',_left + offsetX + 20);
            jQuery(popupArrow2).css('position','absolute').css('top',_top+43+ offsetY).css('left',_left + offsetX + 8);
            jQuery(popupArrow3).css('position','absolute').css('top',_top+60+ offsetY).css('left',_left + offsetX - 4);
        }
        else
        {
            jQuery(popupArrow1).css('position','absolute').css('top',_top+17+ offsetY).css('left',_left + _popupwidth + offsetX + 2);
            jQuery(popupArrow2).css('position','absolute').css('top',_top+43+ offsetY).css('left',_left + _popupwidth + offsetX + 14);
            jQuery(popupArrow3).css('position','absolute').css('top',_top+60+ offsetY).css('left',_left + _popupwidth + offsetX + 26);            
        }
        
        if(mode == 'node')
        {
            jQuery(popupBackground).css('position','absolute').css("top",_top-10).css("left",_left-10).hide();
            jQuery(popupBackground).mouseover(function(){popupHide(_popup);});
        }
        else
        {
            jQuery(popupBackground).css('position','absolute').css("top",_top + offsetY).css("left",_left + offsetX - 4).hide();
            jQuery(sender).mouseout(function(){popupHide(_popup);});
        }
        
        jQuery(_popup).parent().append(popupArrow1);
        jQuery(_popup).parent().append(popupArrow2);
        jQuery(_popup).parent().append(popupArrow3);
        jQuery("#nodes").append(popupBackground);

        var popupBackgroundImage = jQuery.create('div',{'class':_popupBackgroundImageClass},[]);

        jQuery(popupBackground).append(popupBackgroundImage);

        if (BrowserDetect.browser === "Explorer" && BrowserDetect.version === 6)
        {
            jQuery(popupBackgroundImage).css("behavior","url('/styles/iepngfix.htc');");
        }

        popupShow(_popup);

        jQuery(_node).css("z-index",1);
        
        if(jQuery("#POIPopUpContent_" + articlePageID + " .PreviewImage img") != null && jQuery("#POIPopUpContent_" + articlePageID + " .PreviewImage img").is(":visible") === false)
            jQuery("#POIPopUpContent_" + articlePageID + " .PreviewImage img").attr("src", jQuery("#POIPopUpContent_" + articlePageID + " .PreviewImage").attr("rel")).fadeIn("normal");

    }
};


var showByType = function(type)
{
    jQuery(".Popup").hide();
    jQuery(".PopupArrow").hide();
    jQuery(".PopupBackground").hide();

    if (type === null || type === "")
    {
        jQuery('#nodes').children().css('display','block');
    } 
    else
    {
        jQuery('#nodes').children().css('display','none');
        jQuery('#nodes').children(type).css('display','block');
    }
};

var showByTypeLeftNavigation = function(sender, type)
{
    var element = jQuery(sender).parent().parent().next("div.menu_body");
    var visibleElement = jQuery('.POIList ul li div.menu_body:visible');
    
    jQuery('.POIList li').removeClass('PreSelected');
    jQuery(sender).parent().parent().parent().prev('li').addClass('PreSelected');
    
    if(element.is('div') && !element.is(':visible')) {
        if(visibleElement.length)
        {
            
            visibleElement.slideUp('fast', function(){
                jQuery('.POIList li').removeClass('Selected');
                jQuery(sender).parent().parent().parent().addClass('Selected');
                element.slideDown('fast');
            });
        }
        else
        {
            element.slideDown('fast');
        }
    }
    
    showByType(type);
};

jQuery(document).ready(function(){
    
    jQuery('#nodes li a .Bullet').each(function(){
        var size = {};
        var url = jQuery(this).css("background-image");

        size = getImgSize(url);
        
        if (size.width !== 0)
        {
            jQuery(this).css("width",size.width+"px");
        }
        if (size.height !== 0)
        { 
            jQuery(this).css("height",size.height+"px");
        }
    });
    
    jQuery('.Layers li label').click(function() 
    {
        jQuery('.Layers li').removeClass('Selected');
        jQuery(this).parent().parent().addClass('Selected');
    });

    jQuery('.MapPOILabel').each(function()
    {
        jQuery(this).hover(function(){jQuery(this).css('text-decoration','underline');},function(){jQuery(this).css('text-decoration','none');});
    });
});