
/*

  sintax:
  
  alert( algoFPCookie.getCookie("cookieVar") );

the algoFPCookie is a static object, so we don't need to instantiate it
usage:
alert( algoFPCookie.getCookie("cookieVar") );
*/

var algoFPCookie = {
  /*

    Get the 'c_name' cookie's value, if it exists
  */
  getCookie: function(c_name){
    var thisCookie = document.cookie;
    if ( thisCookie.length > 0 ){
      c_start = thisCookie.indexOf( c_name + "=" );
      if ( c_start != -1 ){ 
        c_start = c_start + c_name.length + 1 ;
        c_end = thisCookie.indexOf( ";", c_start );
        if ( c_end == -1 ) 
          c_end = thisCookie.length
        return unescape( thisCookie.substring(c_start, c_end) );
        } 
      }
    return ""
  },
  /*
   Sets the value and expiry date for the 'c_name' cookie
  */
  setCookie: function(c_name, value, expiredays){
    var exdate = new Date();
    
    exdate.setDate(exdate.getDate() + expiredays);
    document.cookie = c_name+"="+escape(value)+((expiredays == null) ? "" : "; expires="+exdate.toGMTString());
  }
  
}

/*
  Saves the url of the current page to our list of favorite pages
*/

function algoFavoritePagesLink(opt){
  
  var options = {}; // its value is set in the _init() method
  var oTag = false; //the created link is added to this HTML tag
  var link =false;  // the link itself
  var listBox = false;
  /* 

    listBox is an algoFavoritePagesBox object.
    If we click the link and the link is not in the list, it gets added to listBox.
    Warning: the options object (initialized in the init() method) should contain the listBox variable. This is not mandatory, however. 
  */
  
  /*

    This method sets our link object's value, based on parameters given in the options variable.
    Is called if and only if the page was loaded.
    Warning: this method only adds itself to window object's onload event chain, it does not redefine the onload event.
  */
  function _init(){
    if ( options.displybox && options.displybox.algoObject )
      listBox = options.displybox;
    
    if ( typeof options.linkboxId == "string" )
      oTag = document.getElementById(options.linkboxId);
      
    link = new sLink();
    link.setSrc("javascript: void(0)");
    link.setPosition("static");
    
    var ac = escape(algoFPCookie.getCookie("algo_pages_container"));
    
    if (ac.search(escape("|sp|"+options.documentId+"|se|"+options.pageName)) > -1)
      link.setContent(options.inlisttext);
    else
      link.setContent(options.addforlisttext);
    
    link.getElement().onclick = function(){
      /*
        This event is fired when a link is clicked and the link wasn't saved before
      */
      if ( link.getContent() == options.addforlisttext ){
        var _ac = algoFPCookie.getCookie("algo_pages_container");
        algoFPCookie.setCookie("algo_pages_container", _ac+"|sp|"+options.documentId+"|se|"+options.pageName, 360);
        link.setContent(options.inlisttext);
        if (listBox){
          listBox.createNewItem( options.documentId, options.pageName );
          listBox.display();
        }
      }
    };
    
    oTag.appendChild( link.getElement() );
  }

  //set options' values (if it wasn't set in the constructor)
  //add init() to window.onload
  this.init = function(opt){ 
    if (opt)
      options = opt;
    
    if (window.addEventListener)
      window.addEventListener("load", _init, false);
    else if (window.attachEvent)
      window.attachEvent( "onload", _init );
  }
  /*

    A simple method to check whether the list contains the link. We set the link to its standard state, this way the object know whether the link is in the list.  The object follows this rule, so is no need to check listBox objects value, and no need to query the value of the cookie.
  */
  this.resertLink = function(){
    link.setContent(options.addforlisttext);
  }
  
  // return the ID of the included document
  this.getDocId = function(){
    return options.documentId;
  }
  
  //set the text of the link
  this.setLink = function(href, text){
    link.setSrc(href);
    link.getElement().innerHTML = text;
  }
  
  //set link style
  this.setLinkCSS = function(v){
    link.setCssClassName(v);
  }
  
  /*
    PL:
     if (valtozo.algoObject)
      alert("ez algo objektum");
    else alert("rosz valtozot adtal meg");

    Check whether a variable contains a given object. Eg.
     if (variable.algoObject)
      alert("this is an algo object");
    else alert("bad variable");
    
    Remark: we also could redefine the toString() method and return a customized string
  */
  
  this.algoObject = true;
  
}
/*
algoFavoritePagesBox([HTML tag ID], [object algoFavoritePagesLink]);

Azt a keretet kezeli, amelyben a megjegyzett oldalak kerulnek

Manages the frame which contains the saved urls

*/
function algoFavoritePagesBox(boxId, plink, aplink){
  var listBox = false;  // a document.getElementById(boxId) HTML teg objektumat fogja tarolni
  var bId = boxId;      // a HTML szulo tak ID-ja // the ID of parent HTML tag
  var list = "";        // a tarolt oldalk string formaju listaja (ahogy az a cookieba van) // the list of saved urls (string)
  var favoriteLink = plink; /* az aktualis oldal algoFavoritePagesLink objektuma 
    Erre azert van szukseg, hogy tudjuk melyik objektum erteket kell vissza allitanunk, ha epp azt toroltuk a listabol
    FIGYELEM: 
      Kotelezo megadni a konstruktorban
    
    MEGJEGYZES:
      Termeszetesen a felreertesek es az objektumot helytelenul hasznalo felhasznalok miatt ez az objektum is mukodik, 
      ha ez az objektum nincs megadva!

      algoFavoritePagesLink object of the current page
  */
  var _this = this; /* a korbehivas megvalositasa erdeket szollgalja
   Akkor hasznaljuk, ha egy kulon allo belso fuggveny belsejeben az ot korulvevo objektumra szeretnenk hivatkozni
   peldakent lasd a function _init() eljarast
  */
  var linkV = new Array();
  var linkInd = 0;
  
  var listPageID = aplink;
  
  /*
   az oldal betoltese utan fut le biztositva ezzel azt, hogy mar ertelmezte a bongeszo azt a teget, amelyre mutat a 
   konstruktorban meghatarozott boxId ID;
   
   A mukodeserol bovebb leiras az elozo objektum azonos metodus kommentjeben talalhato
  */
  function _init(){
    listBox = document.getElementById(bId);
    _this.display();
  }
  
  this.init = function(opt){ 
    
    if (window.addEventListener)
      window.addEventListener("load", _init, false);
    else if (window.attachEvent)
      window.attachEvent( "onload", _init );
  }
  
  // felepiti kirajzolja a linkek listajat
  // set up the list of links
  this.display = function(){
    linkInd = 0;
    if ( listBox)
      while ( listBox.firstChild )
        listBox.removeChild( listBox.firstChild );
    
    list = algoFPCookie.getCookie("algo_pages_container").split("|sp|");
    
    var allPageQuery = "&aw2p=";
    //alert(list.length);
    for (var i=0; i<list.length; i++){
      var item = list[i].split("|se|");
      if (item[0]){
        allPageQuery += item[0]+",";
        if (i < 3)
          this.createNewItem(item[0], item[1]);
        
      }
    }
    if ( list.length > 3 )
      createAllPageLink(encodeURI(allPageQuery), list.length-1);
  }
  
  function createAllPageLink(v, n){
    var link = new sLink("index.php?id="+aplink+v);
    link.setPosition("relative");
    link.setContent("&nbsp;Zur gesamten Merkliste ("+n+")");
    if (listBox)
      listBox.appendChild( link.getElement() );
    
    
  }
  
  //egy link torleset vegzi el
  //remove a link
  
  function deleteLink(id){
    list = algoFPCookie.getCookie("algo_pages_container").split("|sp|");
    var newCookie = "";
    
    for (var i=0; i<list.length; i++){
      var item = list[i].split("|se|");
      if ( item[0] && (item[0] != id) )
        newCookie += "|sp|"+item[0]+"|se|"+item[1];
    }
    algoFPCookie.setCookie("algo_pages_container", newCookie, 360);
  }
  
  //letrehoz egy uj bejegyzest a listBox-ban
  //create a new entry in the listBox
  
  this.createNewItem = function( docId, pageName ){
    
    if (listBox){
    
      var container = new sComponent("relative");
      container.setCssClassName( (linkInd % 2) ? "algo-w2-plinks2" : "algo-w2-plinks");
      container.setHeight(18);
      linkInd++;
      var link = new sLink("index.php?id="+docId);
      link.setPosition("static");
      link.setCssFloat("left");
      link.setContent(pageName);
      
      var deleteButton = new sImage("algomoduls/algofavoritepages/style/img/delete.gif");
      deleteButton.setVerticalAlign("middle");
      deleteButton.setMarginLeft("10px");
      deleteButton.setBorder("none");
      deleteButton.setPosition("static");
      deleteButton.setCssFloat("right");
      
      deleteButton.addEvent("click", function(){
        if ( favoriteLink && favoriteLink.algoObject )
          if (favoriteLink.getDocId() == docId )
          favoriteLink.resertLink();
        listBox.removeChild( container.getElement() );
        deleteLink(docId);
        _this.display();
      } );
      
      container.add(link);
      container.add(deleteButton);
      
      listBox.appendChild(container.getElement() );
    }
    
  }
  
  this.algoObject = true;
}

