function popupDynamicEvents(){

    $("img.infoButton,img.infoButtonReplace,img.infoButtonExisting").hover(
        function(){$(this).attr("src","/library/digitalswitchover/images/customers/more_info_over.png");},
        function(){$(this).attr("src","/library/digitalswitchover/images/customers/more_info.png");}
    )
        
    // Position the entertainment mix information box to its associated link
    
    $("a[rel='info_popup']").click(function() {                
    
        link = $(this);
        target = $(this).attr('href');
        target = target.slice(target.search('#'));
        renderPopup (-60, $(this).width() +10, ''+target+' div.content div.content', link);        
        return false;
    });

}


$(function() {
    popupDynamicEvents();
});

// Render an information box popup
function renderPopup (offsetY, offsetX, target, source){    
    
    var infoBox = $(link).attr('href').split('#')[1];
    
    // Only if the content hasn't already been loaded
    
    if ($(target).children().length == 0 && source != ''){
        // Load and inject content in DOM
        $(target).load(source, function(){
            
            // Position the injected content relative to the calling link
            var infoBoxX = $(source).offset().left;
            var infoBoxY = $(source).offset().top;
            
            if(infoBoxX + $(target).width() + offsetX > $(window).width()){ 
                negOffset = $(target).width()  + 40;
                infoBoxX = infoBoxX - negOffset; 
                $('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX});
            }
            else { 
                $('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX + offsetX});           
            }
        });        
    } else {
        // Position the injected content relative to the calling link
        var infoBoxX = $(source).offset().left;
        var infoBoxY = $(source).offset().top; 
        
       
        
        if(infoBoxX + $(target).width() + offsetX + 100 > $(window).width()){ 
            negOffset = $(target).width()  +40;
            infoBoxX = infoBoxX - negOffset;
            
            $('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX});
        }
        else { 
         
            $('div#' + infoBox).css({top:infoBoxY + offsetY, left:infoBoxX + offsetX});           
        }   

    }
    
    // For all popups
    $(target).after('<a href=\"#close\" class=\"close\"><strong>Close X</strong></a>');                
    $('div#' + infoBox + ' a.close').click(function(){
       $ ('div#' + infoBox).css({left:'-100em'});
       $(this).remove();
        return false;
    });
}

