/* Serverdan Al?nacak De?i?kenler*/
/*var CulturString_add_link_text = "1";
var CulturString_add_img_tooltip="2";
var CulturString_rem_img_tooltip="3";
*/

/**------------------------------------------------------------------------
 * Set of function and classes which deal with quick links 
 *
 * Dependencies:
 *  dojo 
 *
 * Author: 
 *  Bartlomiej.Pawlowski@swx.com
 *
 * @version $Id: quickLinks.js,v 1.9 2008/06/02 16:21:49 ana Exp $
 *
 *-----------------------------------------------------------------------*/

dojo.require("dojo.cookie");

/**
 * 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

        REMOVE_IMG_MOVER    : "/App_Themes/imkbTheme/images/minus.gif",
        REMOVE_IMG_MOUT     : "/App_Themes/imkbTheme/images/minus.gif",
        REMOVE_IMG_NAME     : "ql_remove_img", 

        ADD_IMG_MOVER       : "/App_Themes/imkbTheme/images/plus.gif",
        ADD_IMG_MOUT        : "/App_Themes/imkbTheme/images/plus.gif",
        ADD_IMG_NAME        : "ql_add_img",

        ROW_CLASS           : "sb",
        CELL_MIDDLE_CLASS   : "sb-middle",
        CELL_BOTTOM_CLASS   : "sb-bottom",

        // order is important 
        ARROWS_IMG_SRC      : 
            [
                "/resources/images/layout/triangle_right_red.png",
                "/resources/images/layout/triangle_right.png"
            ]
    };
    
    // ------------------------ 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);
        }
    }
    
    /**
     * Hovers a 'remove' row on mouseover
     * @param A mouseover event
     */    
    function doHoverRowRemove(event) {
        doHover(_getRowByEvent(event));  // from marketpulse.xsl       
    }
    
    /**
     * Unhovers a 'remove' row on mouseout
     * @param A mouseout event
     */ 
    function undoHoverRowRemove(event) {
        undoHover(_getRowByEvent(event)); // from marketpulse.xsl
    }
    
    /**
     * Hovers the 'add' row on mouseover
     * @param A mouseover event
     */    
    function doHoverRowAdd(event) {
        document.getElementsByName(statics.ADD_IMG_NAME)[0].src = statics.ADD_IMG_MOVER;
    }
    
    /**
     * Unhovers the 'add' row on mouseout
     * @param A mouseout event
     */ 
    function undoHoverRowAdd(event) {
        document.getElementsByName(statics.ADD_IMG_NAME)[0].src = statics.ADD_IMG_MOUT;   
    }

    // ------------------------ 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 remImgName = statics.REMOVE_IMG_NAME + i;
            var link = newLinks[i];
            var row = table.insertRow(table.rows.length);
            row.className = statics.ROW_CLASS;
            
            //dojo.connect(row, "onmouseover", doHoverRowRemove);
            //dojo.connect(row, "onmouseout",  undoHoverRowRemove);
               
            var cell01 = row.insertCell(0);
            var cell02 = row.insertCell(1);
            cell02.width = "25";            

            var sameUrl = _currentLink[1] == link[1];
            if (addPlusButton && sameUrl) {
                addPlusButton = false;            
            }

            if (i == newLinks.length-1 && !addPlusButton) {
                cell01.className = cell02.className = statics.CELL_BOTTOM_CLASS;
            } else {
                cell01.className = cell02.className = statics.CELL_MIDDLE_CLASS;
            }           
            
            if (sameUrl) {
                cell01.innerHTML = '<a class="sb-link-selected" href="' + link[1] + '">' + link[0] + '</a>';
            } else {
                cell01.innerHTML = '<a class="sb-link" href="' + link[1] + '">' + link[0] + '</a>';
            }
                        
            cell02.innerHTML = '<a href="#" ' + 
            'onmouseover="document.' + remImgName + '.src=' + "'" + statics.REMOVE_IMG_MOVER + "'" + '"' +
            'onmouseout="document.' + remImgName + '.src=' + "'" + statics.REMOVE_IMG_MOUT + "'" + '"' +
            'onclick="quickLinks.removeQuickLink(' + i + ')"' +
            '>'+ 
            '<img border="0" src="' + statics.REMOVE_IMG_MOUT + '" ' +
            'class="rem-ql" ' +
            'title="' + _opts.rem_img_tooltip + '" ' + 
            'name="' + remImgName + '" id=""' + '/>' + '</a>';
        }

        if (addPlusButton) {
            var row = table.insertRow(table.rows.length);
            row.className = statics.ROW_CLASS;
            
            dojo.connect(row, "onmouseover", doHoverRowAdd);
            dojo.connect(row, "onmouseout",  undoHoverRowAdd);
                        
            var cell01 = row.insertCell(0);
            var cell02 = row.insertCell(1);
            cell01.className = cell02.className = statics.CELL_BOTTOM_CLASS;
            cell02.width = "25";
            
            var onclick = 'onclick="quickLinks.addQuickLink(' + "'" + _currentLink[0] + "', " + "'" + _currentLink[1] + "'" + ')"';
            
            cell01.innerHTML = '<a href="#" class="sb-link-small" ' + onclick +
                                  'style="margin-left:0p ; display:block; width:100%">' +
                                  opts.add_link_text + 
                               '</a>';
                                               
            cell02.innerHTML = '<a href="#" ' + onclick + ' >' +
                                 '<img src="' + statics.ADD_IMG_MOUT + '"' +
                                      'class="add-ql" ' +
                                    'title="' + _opts.add_img_tooltip + '"' +
                                      'name="' + statics.ADD_IMG_NAME + '"/>' + 
                               '</a>';
        }
    }

    /**
     * @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: "/"});
    }
}


var quicklinkTitle = document.title;

var quickLinks = new QuickLinks("quicklinksTable",
          "quicklinks-ipo",
          [ quicklinkTitle, window.location.href ],
          {
          add_link_text:
          
              CulturString_add_link_text
              //"H?zl? Eri?ime Ekle"
            ,

          add_img_tooltip:
          
              CulturString_add_img_tooltip
              //"Sayfay? Ekle"
            ,

          rem_img_tooltip:
          
              CulturString_rem_img_tooltip
              //"Sayfay? Kald?r"
            
          },
          []);
        
          
dojo.addOnLoad(function() { quickLinks.displayQuickLinks() } );
dojo.addOnLoad(function() {
      dojo.style("sbQuickLinksPane", "visibility", "visible");
      });


