/*global $, BrowserDetect, Image, window, document*/

var clearBackgroundUrl = function(dirtyURL)
{
     if (dirtyURL !== null && dirtyURL !== "")
     {
        return dirtyURL.replace("url(\"","").replace("\")","");
     }
};


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 ($(sender).is(":visible") === true)
    {
        $(sender).hide();
        $(".PopupArrow").hide();
        $(".PopupBackground").hide();
        $(".Nodes").children("li").each(function(){$(this).css("z-index","2");});
        $(".Layers").css("z-index","1");
    }
};


var popupShow = function(sender)
{
    if ($(sender).is(":visible") === false)
    {
        $(sender).show();
        $(".PopupArrow").show();
        $(".PopupBackground").show();
        $(".Nodes").children("li").each(function(){$(this).css("z-index","0");});   
        if (BrowserDetect.browser === "Explorer")
        {
            $(".Layers").css("z-index","-1");
        }else
        {
            $(".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 < 300 && mapComponentMode == 'wide');
        
        $(".Popup").hide();
        $(".PopupArrow").remove();
        $(".PopupBackground").remove();        
        
        var _popup = $("#POIPopUpContent_" + articlePageID);
        var _popupheight = 151;
        var _popupwidth = 336;

        var _popupBackgroundClass = "PopupBackground";
        var _popupBackgroundImageClass = (_showRight) ? "PopupRightBackgroundImage" : "PopupBackgroundImage";

        if (_popup.hasClass("NarrowPopup") === true)
        {
            _popupwidth = 179;
            _popupBackgroundClass = "PopupBackground PopupBackgroundNarrow";
            _popupBackgroundImageClass = (_showRight) ? "PopupRightBackgroundImageNarrow" : "PopupBackgroundImageNarrow";
        }
        
        var offsetX = 74;
        var offsetY = 24;
        var arrowWidth = 0;
        
        var _senderHeight = Math.round(_node.clientHeight/2);
        var _senderWidth = _node.clientWidth;
        var _left = _nodeLeft - _popupwidth + 290 - offsetX;
        
        if(_showRight)
        {
            _left = _nodeLeft + _senderWidth - offsetX;
            arrowWidth = 40;
        }
        
        var _top = parseInt(_node.style.top.replace("px",""), 10) - 120 - Math.round(_popupheight/2) + Math.round(_senderHeight) - offsetY;
  
  
        $(_popup).css('position','absolute').css('top',_top + offsetY).css('left',_left + offsetX + arrowWidth);
        
        var popupBackground = $.create('div',{'class':_popupBackgroundClass},[]);
        var popupArrow1 = $.create('div',{'class':'PopupArrow Arrow1'},[]);
        var popupArrow2 = $.create('div',{'class':'PopupArrow Arrow2'},[]);
        
        if(arrowWidth == 40)
        {
            //$(popupArrow1).css('position','absolute').css('top',_top+37+ offsetY).css('left',_left+20+ offsetX);
            //$(popupArrow2).css('position','absolute').css('top',_top+54+ offsetY).css('left',_left+ offsetX);
            $(popupArrow1).css('position','absolute').css('top',_top+141+ offsetY).css('left',_left+20+ offsetX);
            $(popupArrow2).css('position','absolute').css('top',_top+161+ offsetY).css('left',_left+ offsetX);
        }
        else
        {
            //$(popupArrow1).css('position','absolute').css('top',_top+37+ offsetY).css('left',_left+_popupwidth+ offsetX);
            //$(popupArrow2).css('position','absolute').css('top',_top+54+ offsetY).css('left',_left+_popupwidth+20+ offsetX);        
            $(popupArrow1).css('position','absolute').css('top',_top+141+ offsetY).css('left',_left+_popupwidth-335+ offsetX);
            $(popupArrow2).css('position','absolute').css('top',_top+161+ offsetY).css('left',_left+_popupwidth-320+ offsetX);        
        }

        
        $(popupBackground).css('position','absolute').css("top",_top-10).css("left",_left-10).hide();
        
        if(mode == 'node')
        {
            $(popupBackground).mouseover(function(){popupHide(_popup);});
        }
        else
        {
            $(sender).mouseout(function(){popupHide(_popup);});
        }
        
        $(_popup).parent().append(popupArrow1);
        $(_popup).parent().append(popupArrow2);
        $("#nodes").append(popupBackground);

        var popupBackgroundImage = $.create('div',{'class':_popupBackgroundImageClass},[]);

        $(popupBackground).append(popupBackgroundImage);

        if (BrowserDetect.browser === "Explorer" && BrowserDetect.version === 6)
        {
            $(popupBackgroundImage).css("behavior","url('/styles/iepngfix.htc');");
        }

        popupShow(_popup);

        $(_node).css("z-index",1);
    }
};


var showByType = function(type)
{
    $(".Popup").hide();
    $(".PopupArrow").hide();
    $(".PopupBackground").hide();

    if (type === null || type === "")
    {
        $('#nodes').children().css('display','block');
    } 
    else
    {
        $('#nodes').children().css('display','none');
        $('#nodes').children(type).css('display','block');
    }
};

var showByTypeLeftNavigation = function(sender, type)
{
    var checkElement = $(sender).parent().parent().next("div.menu_body");
    
    if((checkElement.is('div')) && (!checkElement.is(':visible'))) {
        $('.POIList ul li div.menu_body:visible').slideUp('fast');
        checkElement.slideDown('fast');
    }
    
    $('.POIList li').removeClass('Selected');
    $('.POIList li').removeClass('PreSelected');
    $(sender).parent().parent().parent().addClass('Selected');
    $(sender).parent().parent().parent().prev('li').addClass('PreSelected');
    
    showByType(type);
};

$(document).ready(function(){
    
    $('#nodes li a .Bullet').each(function(){
        var size = {};
        var url = $(this).css("background-image");

        size = getImgSize(url);
        
        if (size.width !== 0)
        {
            $(this).css("width",size.width+"px");
        }
        if (size.height !== 0)
        { 
            $(this).css("height",size.height+"px");
        }
    });
    
    $('.Layers li label').click(function() 
    {
        $('.Layers li').removeClass('Selected');
        $(this).parent().parent().addClass('Selected');
    });

    $('.MapPOILabel').each(function()
    {
        $(this).hover(function(){$(this).css('text-decoration','underline');},function(){$(this).css('text-decoration','none');});
    });
});
