function clickNav(url)
{
    window.location=url;
}

function check_login_taken_remote(target)
{
   var login = $("login").value;
   url = "/user/check_available?login="+escape(login);
   new Ajax.Updater(target, url, {asynchronous:true, evalScripts:true});
}

function showLogin()
{
   Element.hide('register');
   $('l_btn').className = 'active';
   $('r_btn').className = '';
   $('new_user').value = '';
}

function showRegister()
{
   Element.show('register');
   $('l_btn').className = '';
   $('r_btn').className = 'active';
   $('new_user').value = 'true';
   clickNewUser();
}

function clickNewUser() {
   if (document.getElementById('login').value != '')
      check_login_taken_remote("availability");
   if(document.getElementById("login").value == '')
      document.getElementById("login").focus();
   else if(document.getElementById("password").value == '') document.getElementById("password").focus();
   else document.getElementById("confirm_password").focus();
}


function disable_click() {
  return false;
}

function select_image(new_img_link)
{
  if (new_img_link != '') {
    if (current_img_link != '') {
      old_img = document.getElementById(current_img_link);
      if (old_img != null)
        old_img.className = 'thumb_box';
    }
    new_img = document.getElementById(new_img_link);
    if (new_img != null)
      new_img.className = 'thumb_box_selected';
    // separated var from field for directly-input-img-link scenario
    current_img_link = new_img_link;
    img_link_field = document.getElementById('img_link');
    img_link_field.value = new_img_link;
  }
}


/***************************************
 * Article Animations
 ***************************************/

/* legacy up vote */
function animate_vote(article_id, vote_url)
{
   score = "scorebox"+article_id;
   new Ajax.Updater(score, vote_url, {asynchronous:true, evalScripts:true, onComplete:function(request){animate_voted_link(article_id)}});
}

function animate_voted(article_id)
{
   floater_id = 'floater'+article_id;
   $(floater_id).style.visibility='';
   new Effect.Fade(floater_id, {duration: 2.5});
   new Effect.Move(floater_id, { x:  0, y: -45, duration: 2.5, mode: 'relative'});
   $('approve_link_id'+article_id).className='approved_link';
   $('approve_btn_id'+article_id).className='approved_btn';
   $('approve_link_id'+article_id).onclick=disable_click;
   $('approve_btn_id'+article_id).onclick=disable_click;
}

function animate_voted_link(article_id)
{
   approve_l = "approve_link_id"+article_id;
   $(approve_l).onmouseover = disable_click;
   $(approve_l).onmouseout = disable_click;
   animate_voted(article_id);
   return false;

}

/* up-down vote */
var voteClicks = {};
function animate_updown_vote(article_id, vote_url, vote_type, vote_value)
{
  newstate = state = ($("up_btn"+article_id).className == "vote up_tic") - ($("down_btn"+article_id).className == "vote down_tic");
  if( vote_type == "up" && state <= 0 ) newstate++;
  else if( vote_type == "down" && state >= 0 ) newstate--;

  if( state != newstate )
  {
    score = "scorebox"+article_id;
    new Ajax.Request(vote_url, {asynchronous:true, evalScripts:true, onComplete: function(request){ eval(request.responseText); }});
    animate_updown_voted(article_id, vote_type, vote_value, newstate);
  } else {
    // doubleclick, don't send Ajax request.
  }
  //return false;
}

function animate_updown_voted(article_id, vote_type, vote_value)
{
  floater_id = 'floater'+article_id;
  $(floater_id).style.visibility='';
  $(floater_id).innerHTML=vote_value;
  y_move = (vote_type == 'up') ? -45 : 45;
  new Effect.Fade(floater_id, {duration: 2.5});
  new Effect.Move(floater_id, { x:  0, y: y_move, duration: 2.5, mode: 'relative'});
  target = vote_type+"_btn";
  $(target+article_id).className = "vote "+vote_type+"_tic";
  $('up_btn'+article_id).onclick=disable_click;
  $('down_btn'+article_id).onclick=disable_click;
}

/* Saving */
function toggleSave(article_id)
{
   var save_url = "/user/toggle_favorite/"+article_id;

   new Ajax.Updater("save"+article_id, save_url,  {asynchronous:true, evalScripts:true});
        return false; // ??
}

function clickReportAbuse(link, url) {
  if (link.disabled) {
  } else {
    link.disabled = true;
    new Ajax.Request(url, {asynchronous:true, evalScripts:true, onComplete:function(request){eval(request.responseText)}});
  }
}

function makeElementNonClickable(elementId) {
  var element = $(elementId);
  var dimensions = Element.getDimensions(element);
  var maskElement = document.createElement("div");
  maskElement.style.height = dimensions.height + "px";
  maskElement.style.width = dimensions.width + "px";
  maskElement.style.left = "0px";
  maskElement.style.top = "0px";
  maskElement.style.position = "absolute";
  maskElement.style.backgroundColor = "white";
  Element.setOpacity(maskElement, 0);
  maskElement.style.zIndex = 2000;
  element.appendChild(maskElement);
  element.style.position = "relative";
  element.style.height = dimensions.height + "px";
}

function check_category_selected() {
  var cat = $('tags_string');
  var catError = $('cat_error');

   if (cat != null && catError != null) {
    if (cat.value == '--null-selection--') {
     $('cat_error').style.visibility = '';
    }
    else {
     $('cat_error').style.visibility = 'hidden';
    }
   }
   return false;
}

function toggleTag(tag) {
  t = document.getElementById('t_'+tag);
  tags_string = document.getElementById('tags_string');
  /* marker near each category */
  mark_id = document.getElementById('mark'+tag);
  if (t.className == 'click_tag') {
    t.className='click_tag_selected';
    mark_id.style.display="block";
    tags_string.value+=' ';
    tags_string.value+=tag;
  }  
  else {
    t.className='click_tag';
    tagsr = tags_string.value.split(' ');
    for(i in tagsr) if (tagsr[i] == tag) tagsr[i] = '';
    mark_id.style.display="none";
    tags_string.value = tagsr.join(' ');    
    tags_string.value = tags_string.value.replace(/^\s+$/, '');
  }
}

function initTags() {
  tags_string = document.getElementById('tags_string');
  tags = tags_string.value.split(' ');
  for(i in tags) {
    x = document.getElementById('t_'+tags[i])
    if (x != null) x.className='click_tag_selected';
  }
}

// Email Validation.

function check_email(e) {
  ok = "1234567890qwertyuiop[]asdfghjklzxcvbnm.@-_QWERTYUIOPASDFGHJKLZXCVBNM";

  for(i=0; i < e.length ;i++){
    if(ok.indexOf(e.charAt(i))<0){
      return (false);
    }
  }

  if (document.images) {
    re = /(@.*@)|(\.\.)|(^\.)|(^@)|(@$)|(\.$)|(@\.)/;
    re_two = /^.+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
    if (!e.match(re) && e.match(re_two)) {
      return (-1);
    }
  }
}

function check_form(f) { // f is the form (passed using the this keyword)
  // check the first email address ( the exclamation means "not" )
  if(!check_email(f.profile_email.value)){
    alert("Adresse mail n'est pas valide !");
    f.profile_email.focus();
    // if the browser is Netscape 6 or IE
    if(document.all || document.getElementByID){
      // change the color of text field
      f.profile_email.style.background = "yellow";
    }
    // make sure the form is not submitted
    return false;
  }
}


function ClipBoard(c) 
{
  holdtext.innerText = c.innerText;
  Copied = holdtext.createTextRange();
  Copied.execCommand("RemoveFormat");
  Copied.execCommand("Copy");
}


var Pivotal = {};
Pivotal.InsidePlacement = Class.create();
Pivotal.InsidePlacement.prototype = {
	initialize: function(relatedElement, options) {
    this.relatedElement = $(relatedElement);
    this.options = {
      left: 10,
      top: 10
    };
    Object.extend(this.options, options || {});
  },

	place: function(element) {
    element = $(element);
    var relatedOffset = Position.cumulativeOffset(this.relatedElement);
    var leftOffset = (relatedOffset[0] + this.options.left);
    var topOffset = (relatedOffset[1] + this.options.top);
    element.style.position = "absolute";
    element.style.left = leftOffset + "px";
    element.style.top = topOffset + "px";
    element.style.display = "";
  }
};

