var Image = new Image(); 
Image.src = "http://rnm.me/images/pencil_icon.png";

function StickyShow(id) {
  document.getElementById(id).style.display = '';
}

function StickyCancelShow(id) {
  document.getElementById(id).style.display = '';
}

function StickyHide(id) {
  document.getElementById(id).style.display = 'none';
}

function StickyCancelHide(id) {
  document.getElementById(id).style.display = 'none';
}

function Sticky(url, shorturl) {

  var oldcookie = StickyCookieRead();
  
  if (oldcookie) {
    oldcookie = oldcookie.split(",");
    var isin = false;
    
    for (var x = 0; x < oldcookie.length && x < 6; x++) {
      if (oldcookie[x] == url && oldcookie[x] != '') {
        isin = true;
        document.getElementById('url_'+shorturl).src = 'images/pencil_icon_50.png';
      }
    }
    
    if (!isin) {
      document.getElementById('url_'+shorturl).src = 'images/pencil_icon.png';
    }
      
    StickyCookieWrite(shorturl);
    LoadSticky(oldcookie.length);
  } else {
    if (url != "") {
      StickyCookieWrite(shorturl);
      document.getElementById('url_'+shorturl).src = 'images/pencil_icon.png';
    }
    LoadSticky(0);
  }
}

function StickyDel(url, shorturl) {

  var oldcookie = StickyCookieRead();
  
  if (oldcookie) {
    oldcookie = oldcookie.split(",");

    if (document.getElementById('sticky_'+shorturl) && document.getElementById('options_'+shorturl) && document.getElementById('url_'+shorturl)) {        
      document.getElementById('sticky_'+shorturl).style.opacity = 0;
      document.getElementById('sticky_'+shorturl).style.cursor = 'default';
      document.getElementById('options_'+shorturl).style.cursor = 'default';
      document.getElementById('url_'+shorturl).src = 'images/pencil_icon_50.png';
    }
         
    new Ajax.Request('http://rnm.me/ajax_sticky_funct.php',
    {
      method:'get',
      parameters: 'funct=delete&short='+shorturl,
      onSuccess: 
        function(transport){
          var response = transport.responseText || "unknown error";    
          LoadSticky(oldcookie.length);
        },
    });	
  }
}

function StickyCookieRead() {
	
	new Ajax.Request('http://rnm.me/ajax_sticky_funct.php',
    {
      method:'get',
      parameters: 'funct=read',
      onSuccess: 
        function(transport){
          var response = transport.responseText || "unknown error";
          
          return response;
        },
    });	
}

function StickyCookieWrite(value) {	
	new Ajax.Request('http://rnm.me/ajax_sticky_funct.php',
    {
      method:'get',
      parameters: 'funct=write&short='+value,
      onSuccess: 
        function(transport){
          var response = transport.responseText || "unknown error";    
        },
    });	
}

function LoadSticky(l) {

  var oldcookie = StickyCookieRead();
  if (oldcookie) oldcookie = oldcookie.split(",").length;
  else oldcookie = 0;

  new Ajax.Request('http://rnm.me/ajax_sticky.php',
    {
      method:'get',
      parameters: 'new='+oldcookie+'&old='+l,
      onSuccess: 
        function(transport){
          var response = transport.responseText || "unknown error";
          
          var oldcookie = StickyCookieRead();
          
          if (!oldcookie) {
              // Keine URL mehr vorhanden, Box wird komplett ausgeblendet.
              new Effect.Fade('sticky-fade', {duration:0.3, from:1.0, to:0.0});
              setTimeout('Effect.SlideUp(\'sticky-slide\', \{duration:0.3, scaleFrom: 100, scaleTo: 0\})', 302);
          } else {
            // Noch URLs vorhanden
            
            oldcookie = oldcookie.split(",");
            
            if (oldcookie.length == 1 && oldcookie.length > l) {
              // Genau eine URL im Cookie, vorher keine
              Effect.SlideDown('sticky-slide', {duration:0.3});
              setTimeout('inHTML(\'sticky-box\', \''+response+'\')', 301);
              setTimeout('new Effect.Appear(\'sticky-fade\', \{duration:0.3, from: 0.0, to: 1.0\})', 302);
              
            } else if (oldcookie.length == 1 && oldcookie.length < l) {
              // Genau eine URL im Cookie, vorher 2
              new Effect.Opacity('sticky-box',{duration:0.4, from:1.0, to:0.0});
              setTimeout('document.getElementById(\'sticky-slide\').style.height = \'135px\'', 400);
              setTimeout('inHTML(\'sticky-box\', \''+response+'\')', 401);
              setTimeout('new Effect.Opacity(\'sticky-box\', \{duration:0.4, from:0.0, to:1.0\})', 402);
              
            } else {
              if (oldcookie.length == 2)
                setTimeout('document.getElementById(\'sticky-slide\').style.height = \'180px\'', 402);
              if (oldcookie.length >= 3)
                setTimeout('document.getElementById(\'sticky-slide\').style.height = \'225px\'', 402);
                
              new Effect.Opacity('sticky-box',{duration:0.4, from:1.0, to:0.0});
              setTimeout('inHTML(\'sticky-box\', \''+response+'\')', 401);
              setTimeout('new Effect.Opacity(\'sticky-box\', \{duration:0.4, from:0.0, to:1.0\})', 703);
            }
          }
        },
      onFailure: 
        function(transport){ 
          var response = transport.responseText || "unknown error";
          document.getElementById('sticky-box').innerHTML = response;
        }
    });
}

/*
new Effect.Opacity('sticky-box',{duration:0.4, from:1.0, to:0.0});
setTimeout('inHTML(\'sticky-box\', \''+response+'\')', 401);
setTimeout('new Effect.Opacity(\'sticky-box\', \{duration:0.4, from:0.0, to:1.0\})', 402);
*/

