/**------------------------------------------------------------------------
 * Set of function and classes which deal with quick links 
 *
 * Dependencies:
 *  dojo 
 *
 * Author: 
 *  Bartlomiej.Pawlowski@swx.com
 *
 * @version $Id: quickLinks.js,v 1.3 2010/05/07 12:36:31 obo Exp $
 *
 *-----------------------------------------------------------------------*/

/**
 * QuickLinks class
 *
 * @param qlTableId - string, id of the html table which displays quicklinks
 * @param qlCookieName - string, cooki name which keeps the quicklinks
 * @param currentLink - array (0-title 1-url) with the current quick link title and link
 * @param opts hash which should have the following keys
 *  add_img_tooltip
 *  rem_img_tooltip
 * @param defaultQuickLinks - array of arrays (0-title 1-url) with the default quicklinks
 *
 */
function QuickLinks(qlTableId, qlCookieName, currentLink, opts, defaultQuickLinks) {
    
    // ------------------------ constants 
    var statics = {
    		
        LINK_SEPARATOR      : "#;#",    // separates title from url
        LINKS_SEPARATOR     : "^^^"    // separates pairs (title,url) from each other
    };
    
    // ------------------------ private variables 

    var _tableId        = qlTableId;
    var _cookieName     = qlCookieName;
    var _currentLink    = currentLink;
    var _defaultLinks   = [];       // if there is no quicklinks is the cookie
                                    // use this array   
                                    // should it containg some links??
    var _opts           = opts;

    // ------------------------ public methods
    this.displayQuickLinks = function() {
        var links = dojo.cookie(_cookieName);

        if ( links == null || links.length == 0 ) 
            _refreshTable(_defaultLinks);
        else 
            _refreshTable(_parseQuickLinks(unescape(links)));
    }

    /**
     * Add url to the quick links
     *
     * @param title string
     * @param url string
     */
    this.addQuickLink = function (title, url) {
        var links = dojo.cookie(_cookieName);
        var arrLinks;

        if ( links != null && links.length > 0 ) {
            arrLinks = _parseQuickLinks(unescape(links));
            arrLinks.push([title, url]);
        }
        else
            arrLinks = [[title, url]];
        
        _store(arrLinks);
        _refreshTable(arrLinks);

    }

    /**
     * Remove quick link from quick links
     * @param number, linkIdx index of the quick link starting from 0
     */
    this.removeQuickLink = function(linkIdx) {
        var links = dojo.cookie(_cookieName);
        var arrLinks;
        if ( links != null ) {
            arrLinks = _parseQuickLinks(unescape(links));
            arrLinks.splice(linkIdx, 1);
            _store(arrLinks);
            _refreshTable(arrLinks);
        }
    }
    
    // ------------------------ private methods
    
    /**
     * @param A mouseover or mouseout event
     * @return The row object that triggered the event
     */
    function _getRowByEvent(event) {
        var obj;
        if (event["target"]) {
          obj = event["target"];     // Firefox & others
        } else {
          obj = event["srcElement"]; // IE 
        }
            
        while (obj!=null && obj.nodeName.toUpperCase() != "TR") {
           obj = obj.parentNode;
        }
        return obj;
    }
    
    /**
     * @param newLinks array of array's 0-title 1-url
     */
    function _refreshTable(newLinks) {           
        var addPlusButton = true;     // used to check if add quick link button should be displayed       
  
        var table = document.getElementById(_tableId); 
        _clearTable(table);
        
        for (var i = 0; i < newLinks.length; i++) {      
            var link = newLinks[i];
            var row = table.insertRow(table.rows.length);
               
            var cell01 = row.insertCell(0);
            var cell02 = row.insertCell(1);
            cell02.className = "sidebar-remove-ql";            

            var sameUrl = _currentLink[1] == link[1];
            if (addPlusButton && sameUrl) {
                addPlusButton = false;            
            }

            cell01.className = "sidebar-item";
            cell01.innerHTML = '<a class="sidebar-item" href="' + link[1] + '">' + link[0] + '</a>';            
            cell02.innerHTML = '<a class="sidebar-remove-ql" href="#" ' + 
            				     'onclick="quickLinks.removeQuickLink(' + i + ')"' + 
            				     'title="' + _opts.rem_img_tooltip + '">' +
            				     '<img src="/resources/images/trans.gif" width="13" height="10" border="0"/>' +
            				   '</a>';
        }

        if (addPlusButton) {        	
            var row = table.insertRow(table.rows.length);
            	
            var cell01 = row.insertCell(0);
            var cell02 = row.insertCell(1);
            cell01.className = "sidebar-item";
            if (table.rows.length!=1) {
            	cell01.height = "30";
            }
            var onclick = 'onclick="quickLinks.addQuickLink(' + "'" + _currentLink[0] + "', " + "'" + _currentLink[1] + "'" + ')"';
            
            cell01.innerHTML = '<a href="#" class="sidebar-add-ql" ' + onclick + '>' +
            				     opts.add_link_text
                               '</a>';                
            cell02.innerHTML = '';
        }
    }

    /**
     * @param qLinks string which contains titles and urls
     * @return array 0-title 1-url
     */
    function _parseQuickLinks(qLinks) {
        var allLinks, link;
        var i;
        var result = [];

        allLinks = qLinks.split(statics.LINKS_SEPARATOR);
        for (i = 0; i < allLinks.length; i++) {
            link = allLinks[i].split(statics.LINK_SEPARATOR);
            result.push(link);
        }
        return result;
    }

    /**
     * Remove all rows from table
     *
     * @param table - dom object
     */
    function _clearTable(table) {
        while (table.rows.length > 0) 
            table.deleteRow(0);
    }

    /**
     * @param newLinks array or array's 0-title 1-url
     */
    function _store(newLinks) {
        var i, u, s = '';

        for (i = 0; i < newLinks.length; i++) {
            u = newLinks[i];
            s = s.concat(u[0], statics.LINK_SEPARATOR, u[1]);
            if ( i < newLinks.length - 1 )
                s = s.concat(statics.LINKS_SEPARATOR);
        }

        dojo.cookie(_cookieName, s, { expires: 365, path: "/"});
    }
}


