/* Javascript helper functions
*/

function fixEmailLink() {
    /*  try to modify link address to working one
    
        links needed syntax:
          <a coord="ACCOUNT,DOMAIN,CONTENT" ... />
          will result in this output
          <a href="ACCOUNT@DOMAIN" ...>CONTENT</a> 
    */
    var token = $(this).attr('rel').split(",");
    if (token.length == 3) {
      var mail = token[0] + '@' + token[1];
      $(this).attr("href", "mailto:" + mail);
      $(this).html(token[2]);
    }
}
//---------------------------

function secureEmailLinks(expr) {
    /*  modify all links inside the element specified by CSS expression
    */
    var links = $(expr + " a[href='mailto:#']");
    if (links) {
        links.each(fixEmailLink);
    }
}
//---------------------------


function setLinkTargets(expr) {
    /* set the link attribute for links inside the element specified by CSS expression
    */
    if (! expr)
        expr = '';
         
    var links = $(expr + " a[class*='_blank']");
    links.each( function () {
        $(this).attr('target', '_blank');
    });
}
//---------------------------


function hoverPersonalaktenDetailLink() {
    /* add hover script to the detail links in personalakten
    */
    var ref = $('#content.personalakten');
    if (ref) {
        // events for TR's
        var trs = $('#content.personalakten tr');
        trs.each( function() {
            // mouse over
            $(this).bind('mouseover', function() {
                $(this).children('td').each( function() {
                    $(this).css('background-color', '#DCDCDC');
                });
            });
            // mouse out
            $(this).bind('mouseout', function() {
                $(this).children('td').each( function() {
                    $(this).css('background-color', '#FFFFFF');
                });
            });
        
        });
        // events for links
        var links = $('a.details-link', ref);
        links.each( function() {
            // mouse over
            $(this).bind('mouseover', function() {
                var img = $(this).children('img')[0];
                $(img).attr('src', $(img).attr('src').replace('.gif', '_over.gif'));
            });
            // mouse out
            $(this).bind('mouseout', function() {
                var img = $(this).children('img')[0];
                $(img).attr('src', $(img).attr('src').replace('_over.gif', '.gif'));
            });
        }); 
    
    }
}
//---------------------------



$(document).ready( function () {

    secureEmailLinks('#content');
    setLinkTargets();
    hoverPersonalaktenDetailLink();
});
