
/* conf */
var YOOLINK_DEBUG = false;
var YOOLINK_TIMER_DELAY = 1000;
var YOOLINK_OFFSET_TOP = 2;
var YOOLINK_OFFSET_LEFT = -10;

/* accessor to the user's API url */ 
function yoolink_api_base_url() {
    return('http://www.yoolink.fr/free_add_or_share/mini_landing');
    //return('http://alexis.dev.yoolink.fr/free_add_or_share/mini_landing');
}

function yoolink_static_base_url() {
    return("http://static.yoolink.fr");
}

function yoolink_share_base_url() {
    return("http://www.yoolink.fr/addorshare");
}

function debug(msg) {
    if (YOOLINK_DEBUG)
        console.log(msg);
}

function yoolink_dom_get(id) {
  return(window.document.getElementById(id));
}

function yoolink_dom_display(obj) {
  obj.style.display = 'block';
}

function yoolink_dom_hide(obj) {
  obj.style.display = 'none';
}

function yoolink_dom_is_visible(obj) {
    return(obj.style.display != 'none');
}

function yoolink_get_unique_id() {
  return(Math.floor(Math.random()*10000000));
}

function yoolink_container_height() { 
  return(190);
}
function yoolink_button_height(){
  return(17);
}

function yoolink_find_position(obj) {
    var curleft = curtop = 0;
    if (obj.offsetParent) {
        do {
            curleft += obj.offsetLeft;
            curtop += obj.offsetTop;
        } while (obj = obj.offsetParent);
    }
    return [curleft,curtop];
}

function yoolink_get_body() {
  var this_body = document.body;
  if (document.all && document.compatMode && document.compatMode != "BackCompat") 
    this_body = document.documentElement;
  return(this_body);    
}

function yoolink_get_scrollY(a_body) {
    return (document.all) ? a_body.scrollTop : window.scrollY;
}

function yoolink_get_container_position(button) {
  var yoolink_body     = yoolink_get_body();
  var yoolink_position = yoolink_find_position(button);
  var yoolink_scrollY  = yoolink_get_scrollY(yoolink_body);
  var remaining_height = yoolink_position[1] - yoolink_scrollY;

  var abs_top;
  var abs_left;

  if (remaining_height < yoolink_container_height()) {
    abs_top  = yoolink_position[1] + yoolink_button_height() + YOOLINK_OFFSET_TOP;
    abs_left = yoolink_position[0] + YOOLINK_OFFSET_LEFT;
  }
  else {
    abs_top  = yoolink_position[1] - yoolink_container_height() - YOOLINK_OFFSET_TOP;
    abs_left = yoolink_position[0] + YOOLINK_OFFSET_LEFT;
  }
  return {abs_top: abs_top, abs_left: abs_left};
}

function yoolink_get_button(id) {
  return(yoolink_dom_get('yoolink_button_'+id));
}

function yoolink_get_container(id) {
    return(window.document.getElementById('yoolink_container_'+id));
}

function yoolink_get_iframe(id) {
  return(yoolink_dom_get('yoolink_iframe_'+id));
}


function yoolink_iframe_url(id) {
  var meta = read_yoolink_meta(id);
  return(yoolink_api_base_url()+
    '?url_value='+encodeURIComponent(meta.permalink)+
    '&parent_iframe='+window.location.hostname+
    '&title='+encodeURIComponent(meta.title));
}

function yoolink_button_on_mouse_out(id) {
    try{
        var y_container = yoolink_get_container(id);
        yoolink_start_hiding(y_container);
    }catch(e){ debug (e.message); }
}

function yoolink_button_on_mouse_over(id) {
  try{
    yoolink_scheduler_register(id);
  
    yoolink_scheduler_hide_all_except(id);
    var button    = yoolink_get_button(id); 
    var frame     = yoolink_get_iframe(id);
    var y_container = window.document.getElementById('yoolink_container_'+id);
    var coords = yoolink_get_container_position(button);

    y_container.style.left = coords.abs_left+'px';
    y_container.style.top  = coords.abs_top+'px';

    yoolink_stop_hiding(y_container);

    // then, display the container
    if(y_container.innerHTML=='') y_container.innerHTML = '<iframe onmouseover="yoolink_container_on_mouse_over(\''+id+'\');" onmouseout="yoolink_container_on_mouse_out(\''+id+'\');" id="yoolink_iframe_'+id+'" height="170" frameBorder="0" width="215" scrolling="no" style="border:1px solid rgb(187, 187, 187) !important; background-color: #ffffff;" onLoad="yoolink_refresh_iframe('+id+');" src="'+yoolink_iframe_url(id)+'"></iframe>';
    else yoolink_dom_display(y_container);
    
  }
  catch(e){ debug(e.message)}
}

function yoolink_container_on_mouse_over(id) {
    yoolink_scheduler_hide_all_except(id);
    y_container = yoolink_get_container(id);
    yoolink_stop_hiding(y_container);
}

function yoolink_container_on_mouse_out(id) {
    var y_container = yoolink_get_container(id);
    yoolink_start_hiding(y_container);
}

function yoolink_refresh_iframe(id) {
    var frame = yoolink_get_iframe(id);
    var y_container = yoolink_get_container(id);

    if (frame.src != 'about:blank') {
        yoolink_dom_display(y_container);
    }
}

/* Timer stuff */

function yoolink_get_scheduler() {
    if (! window.yoolink_timer) {
        window.yoolink_timer = {};
    }
    return window.yoolink_timer;
}

function yoolink_clear_interval(id) {
    timers = yoolink_get_timers_for(id);
    for (i=0; i<timers.length; i++) {
        t = timers[i];
        if (t) {
            clearTimeout(t);
        }
    }
    // now purge the list
    yoolink_reset_timers_for(id);
}

function yoolink_scheduler_register(id) {
    var scheduler = yoolink_get_scheduler();
    if (! scheduler.register)
        scheduler.register = [];
    scheduler.register[scheduler.register.length] = id;
}

function yoolink_scheduler_hide_all_except(id) {
    var scheduler = yoolink_get_scheduler();
    var register = scheduler.register;
    if (! register) 
        return;

    for (i=0; i<register.length; i++) {
        if (id != register[i]) {
            var y_container = yoolink_get_container(register[i]);
            if (y_container != null) {
                if (yoolink_dom_is_visible(y_container)) {
                    yoolink_dom_hide(y_container);
                }
            }
        }
    }
}

function yoolink_push_to_scheduler(id, timer) {
    yoolink_timer_button = yoolink_get_timers_for(id);
    yoolink_timer_button[yoolink_timer_button.length] = timer;
}

function yoolink_add_timer_for(id, code) {
    yoolink_clear_interval(id);
    var scheduler = yoolink_get_scheduler(); 
    yoolink_push_to_scheduler(id, setInterval(code, YOOLINK_TIMER_DELAY));
}

function yoolink_get_timers_for(id) {
    var scheduler = yoolink_get_scheduler();
    if (! scheduler.id) 
        scheduler.id = [];
    return(scheduler.id);
}

function yoolink_reset_timers_for(id) {
    var scheduler = yoolink_get_scheduler();
    scheduler.id = [];
}

/* hide and show with timer */

function yoolink_start_hiding(e) {
    if (e == null) 
        return;
    hide_code = function() {
        if (e != null) {
            if (yoolink_dom_is_visible(e)) {
                yoolink_dom_hide(e);
                yoolink_clear_interval(e.id);
                var scheduler = yoolink_get_scheduler();
                scheduler.register = [];
            }
        }
    };
    yoolink_add_timer_for(e.id, hide_code);
}

function yoolink_stop_hiding(e) {
    yoolink_clear_interval(e.id);
}

function get_yoolink_permalink() {

  try {
      /* gros bloc de backward compat avec les anciens JS */
      if (window.yoo_permalink && window.yoo_permalink.match(/^http:\/\//))
        return window.yoo_permalink;

      if (window.yoo_site_url && window.yoo_site_url.match(/^http:\/\//)) 
        return window.yoo_site_url;

      if (window.POST_PERMALINK && window.POST_PERMALINK.match(/http:\/\//))
        return window.POST_PERMALINK;

      if (window.SITE_URL && window.SITE_URL.match(/http:\/\//)) 
        return window.SITE_URL;

      /* les variables de la nouvelle API */
      if (window.yoolink_permalink && window.yoolink_permalink.match(/^http:\/\//)) {
        return(window.yoolink_permalink);
      }
      else {
        return(window.location);
      }
  }
  catch(e) {
    return window.location;
  }
}

function get_yoolink_title() {
    try {
        if (window.yoo_title && (! window.yoo_title.match(/^_[A-Z_]+$/)))
            return window.yoo_title;

        if (window.yoo_site_url) {
            return document.title;
        }

        if (window.yoolink_title && 
            (! window.yoolink_title.match(/^_[A-Z_]+$/))) {
            return(window.yoolink_title);
        }

        if (window.POST_TITLE)
            return window.POST_TITLE;

        if (get_yoolink_permalink() == window.location) {
            return(document.title);
        }
        else {
            return("");
        }
    }
    catch(e) {
        return "";
    }
}

function get_yoolink_size() {
    if (window.yoo_size) {
        var s = window.yoo_size;
        if (s == 'S')
            return 'small';
        if (s == 'M')
            return 'medium';
        if (s == 'L')
            return 'large';
    }

    if (window.yoolink_size) {
        return window.yoolink_size;
    }
    else {
        return 'medium';
    }
}

function get_yoolink_layer() {
    if (window.yoolink_layer) {
        return window.yoolink_layer;
    }
    else {
        return true;
    }
}

function yoolink_save_meta(id) {
    var meta = {
        title: get_yoolink_title(), 
        permalink: get_yoolink_permalink(),
        size: get_yoolink_size(),
        layer: get_yoolink_layer()};
        
    document.write('<span id="yoolink_title_'+id+'" style="display:none;">'+meta.title+'</span>');
    document.write('<span id="yoolink_permalink_'+id+'" style="display:none;">'+meta.permalink+'</span>');
    document.write('<span id="yoolink_size_'+id+'" style="display:none;">'+meta.size+'</span>');
    document.write('<span id="yoolink_layer_'+id+'" style="display:none;">'+meta.layer+'</span>');

    debug("bouton pour permalink: "+meta.permalink);
    return meta;
}

function read_yoolink_meta(id) {
    var yoo_title = document.getElementById('yoolink_title_'+id).innerHTML;
    var yoo_permalink =document.getElementById('yoolink_permalink_'+id).innerHTML;
    var yoo_size = document.getElementById('yoolink_size_'+id).innerHTML;
    var yoo_layer = document.getElementById('yoolink_layer_'+id).innerHTML;  
    return {title: yoo_title, permalink: yoo_permalink, size: yoo_size, layer: yoo_layer};
}

function yoolink_write_button_image(id,meta) {
    var img_tag = '<img id="yoolink_button_'+id+'" '; 
    var style = "border: 0 !important; vertical-align: text-bottom !important; line-height: 18px !important;";
    if (meta.size == 'small') {
        return img_tag + ' style="' + style + '" src="'+yoolink_static_base_url()+'/images/yoolink_16x16.gif" alt="Add to Yoolink" border="0" width="16" height="16" />';
    }
    if (meta.size == 'large') {
        return img_tag + ' style="' + style + '" src="'+yoolink_static_base_url()+'/images/yoolink_large.gif" alt="Add to Yoolink" border="0" width="" height="" />';
    }
    /* le cas par défaut : medium */
    else {
        return img_tag + ' style="' + style + '" src="http://api.yoolink.fr/button?url='+encodeURIComponent(meta.permalink)+'" alt="Add to Yoolink" border="0" />';
    }
}

function yoolink_button_link_base(yoolink_id) {
    return '<a style="font-size: 18px !important;line-height: 18px !important"  href="#" onClick="window.open(\''+yoolink_share_base_url()+'?from=site&url='+encodeURIComponent(meta.permalink).replace(/\'/g, '\\\'')+'&title='+encodeURIComponent(meta.title).replace(/\'/g, '\\\'')+'&container=popup\', \'yoolink_tagging\', \'width=800,height=565,resizable=1\');return(false);"';
}

function yoolink_write_share_button() {
  yoolink_id = yoolink_get_unique_id();
  meta = yoolink_save_meta(yoolink_id);

    if (meta.layer) {
      document.write(yoolink_button_link_base(yoolink_id)+' onmouseout="yoolink_button_on_mouse_out(\''+yoolink_id+'\');" onmouseover="yoolink_button_on_mouse_over(\''+yoolink_id+'\');">'+yoolink_write_button_image(yoolink_id,meta)+'</a>');
      try{
        var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
        var yoo_container_div = document.createElement('div');
        yoo_container_div.id = 'yoolink_container_' + yoolink_id;
        yoo_container_div.style.zIndex=2147483647;
        yoo_container_div.style.padding='10px';
        var extension = (IE6)?'.gif':'.png';
        yoo_container_div.style.background='transparent url("'+yoolink_static_base_url()+'/images/bg_iframe_share_button' +extension+'") no-repeat center center';
        yoo_container_div.style.top = '0px';
        yoo_container_div.style.left = '0px';
        yoo_container_div.style.display='none';
        yoo_container_div.style.position = 'absolute';
        document.body.insertBefore(yoo_container_div,window.document.body.firstChild);
      }
      catch(e){ debug(e.message) }
    }
    else {
      document.write(yoolink_button_link_base(yoolink_id)+'>'+yoolink_write_button_image(meta)+'</a>');
    }
}

function yoolink_tools_is_loaded() {
  return(true);
}

/* start & stop & main */

function yoolink_tools_start() {
    return(true);
}

function yoolink_tools_stop() {
    window.yoolink_title = '';
    window.yoolink_permalink = '';
    window.yoolink_size='';
    window.yoolink_layer='';
    window.yoo_title = '';
    window.yoo_site_url = '';
    window.yoo_permalink = '';
    window.POST_TITLE = '';
    window.POST_PERMALINK = '';
    window.SITE_URL = '';
}

/* we don't want to mess the client side if an error occurs */
try { 
    yoolink_tools_start();
    yoolink_write_share_button(); 
    yoolink_tools_stop();
} catch(e) { debug(e.message) }

