/**************************************
  (c)2007-2009 http://www.antwerps.be
**************************************/

/***********
  VARIOUS
***********/

// getel(): shorthand for document.getElementById()
function getel(rid) {
  return document.getElementById(rid);
}

// flip(): open/close a div element (rid)
function flip(rid) {
  var element = getel(rid);
  element.style.display = (element.style.display == 'none') ? 'block' : 'none';
}

// identifyBrowser(): "ie" or "moz"
function identifyBrowser() {
  if(document.all) { return "ie"; }
  return "moz";
}

// changeClass(id, new_class)
function changeClass(id, new_class) {
  var element = document.getElementById(id);
  element.className = new_class;
}



/***********
   News
***********/

/**
 * Ajax call to replace a news item with its own translation
 */

function transl_newsitem(post_id, lang) {
  $('#replace'+post_id).animate({ opacity: 0.01 }, 200 );
  $('#replace'+post_id).load(
    '/posts/translate/'+post_id+'/0/'+lang,
    function() { $('#replace'+post_id).animate({ opacity: 1 },600); }
  );
  return false;
}


/***********
  Comments
***********/

function delete_comment(cid, parent_type, parent_id, user_id) {
  $.get(
    '/comments/delete/'+cid+'/'+parent_type+'/'+parent_id+'/'+user_id,
    function() { $('#comment'+cid).fadeOut(1000); }
  );
  return false;
}


/***********
   Lyrics
***********/
function toggle_lyrics(lyr_class, lyr_show) {
  var area = document.getElementById("lyrics");
  var divs = area.getElementsByTagName("div");
  var ndivs = divs.length;
  for (i=0; i<ndivs; i++) {
    if (divs[i].className == lyr_class) {
      divs[i].style.display = (lyr_show) ? "block" : "none";
    }
  }
}



/**************
  Woordenboek
**************/

/**
 * id = woord.id (."_2" voor 2e flexie)
 */
function toggle_flexie(id) {
  var flexie = document.getElementById("flexie"+id);
  if (flexie) {
    var plusminus = document.getElementById("pm"+id);
    if (flexie.style.display == "block") {
      flexie.style.display = "none";
      plusminus.className = "wbw_switch";
    } else {
      flexie.style.display = "block";
      plusminus.className = "wbw_switch_m";
    }
  }
}


/**
 * woord opzoeken in wb
 */
function wb_lookup() {
  var zk = '';
  if (navigator.appName != 'Microsoft Internet Explorer') {
    zk = document.getSelection();
  } else {
    var t = document.selection.createRange();
    if(document.selection.type == 'Text' && t.text > '') {
      zk = t.text;
    }
  }
  if (zk.length > 30) return false;
  if (zk > '') {
    // trim: remove all non-letter signs from beginning and end
    var letters = "'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZëËéÉêÊùÙöÖ";
    while (letters.indexOf(zk.substr(zk.length-1, 1)) == -1) {
      zk = zk.substr(0, zk.length-1);
    }
    while (letters.indexOf(zk.substr(0, 1)) == -1) {
      zk = zk.substr(1);
    }
    if (zk > '') {
      // lookup
      window.open('/woordenboek/klikzoek/'+escape(zk), 'dict', 'width=660,height=400,resizable=1,menubar=0,scrollbars=1,status=0,titlebar=0,toolbar=0,location=0,personalbar=0');
    }
  }
}


/**************
  Forums
**************/

/**
 * markup textarea selection
 */
function ta_markup(ta_id, tag_open, tag_close){
  var cursor_pos = 0;
  var $ta = document.getElementById(ta_id);
  // IE
  if (document.selection) {
    var str = document.selection.createRange().text;
    var sel = document.selection.createRange();
    // add tags if something selected
    if (str.length) {
      // if last char = space, remove from selection
      if (str.substring(str.length - 1, str.length) == ' ') {
        sel.text = tag_open + str.substring(0, str.length - 1) + tag_close + ' ';
      } else {
        sel.text = tag_open + str + tag_close;
      }
      //set_cursor($ta, cursor_pos, cursor_pos);
      // $ta.focus();
      return false;
  }
  // FF
  } else if (typeof $ta.selectionStart != 'undefined') {
    var $before, $after, $selection;
    var sel_start = $ta.selectionStart;
    var sel_end = $ta.selectionEnd;
    // if last char = space, remove from selection
    if ($ta.value.substring(sel_end-1, sel_end) == ' ')  sel_end = sel_end - 1;
    //
    $before = $ta.value.substring(0, sel_start);
    $selection = $ta.value.substring(sel_start, sel_end);
    $after = $ta.value.substring(sel_end, $ta.value.length);
    // add tags if something selected
    if ($selection.length) {
      $ta.value= String.concat($before, tag_open, $selection, tag_close, $after);
      cursor_pos = $before.length + tag_open.length + $selection.length + tag_close.length;
      set_cursor($ta, cursor_pos, cursor_pos);
//      $ta.focus();
      return false;
    }
  }
  return true;
}

//
function set_cursor(el, st, end) {
  if(el.setSelectionRange) {
    el.focus();
    el.setSelectionRange(st,end);
  } else {
    if(el.createTextRange) {
      range=el.createTextRange();
      range.collapse(true);
      range.moveEnd('character',end);
      range.moveStart('character',st);
      range.select();
    }
  }
}



/*********************************

             TOOLTIP

**********************************/

/*
  based on the Tooltip-with-shadow script on www.dhtmlgoodies.com
    http://www.dhtmlgoodies.com/index.html?whichScript=tooltip_shadow
  requires the styles #gstooltip and #gstooltipShadow (cfr. css)
*/

var gstooltip = false;
var gstooltipShadow = false;
var gstooltip_shadowSize = 3;
var gstooltipMaxWidth = 400;
var gstooltipMinWidth = 70;
var gstooltip_iframe = false;


/**
 * setTooltip(): sets tooltip on an image
 *
 * <img ... onload="return setTooltip(this,'text of the tooltip')">   to set a tooltip
 */
function setTooltip(t_img, tip) {
  t_img.img_caption = tip;
  t_img.onmouseover = function () {showTooltip(this.img_caption); return false;}
  t_img.onmouseout = hideTooltip;
  t_img.onmousemove = moveTooltip;
  return false;
}


/**
 * setTooltipID(): sets tooltip on an element with id
 *                 -> for elements without onload (div, a, li, ...)
 *
 * <div id='xxx'> -> setTooltipID('xxx', 'text of the tooltip');
 */
function setTooltipID(t_id, tip) {
  var ob = $("#"+t_id);
  if (ob) {
    ob.mouseover(function() { showTooltip(tip); return false; } );
    ob.mouseout(hideTooltip);
    ob.mousemove(moveTooltip);
  }
}


/**
 * mouse events: showTooltip(), moveTooltip(), hideTooltip()
 */

function showTooltip(tooltipTxt) {
  // create tooltip if it doesn't exist yet
  if(!gstooltip){
    gstooltip = document.createElement('DIV');
    gstooltip.id = 'gstooltip';
    gstooltipShadow = document.createElement('DIV');
    gstooltipShadow.id = 'gstooltipShadow';
    document.body.appendChild(gstooltip);
    document.body.appendChild(gstooltipShadow);
    gstooltip.style.left = '10px';
//    gstooltip.style.top = posY + 'px';
  }

  // show tooltip
  gstooltip.style.display = 'block';
  gstooltipShadow.style.display = 'block';

  // fill tooltip
  gstooltip.style.width = null; // Reset style width if it's set
  gstooltip.innerHTML = tooltipTxt;

  // width between min and max
  if (gstooltip.offsetWidth < gstooltipMinWidth)
    gstooltip.style.width = gstooltipMinWidth + 'px';
  else if (gstooltip.offsetWidth < gstooltipMaxWidth)
//    gstooltip.style.width = gstooltipMinWidth + 'px';
    gstooltip.style.width = gstooltip.offsetWidth + 'px';
  else if (gstooltip.offsetWidth > gstooltipMaxWidth)
    gstooltip.style.width = gstooltipMaxWidth + 'px';

  // position it
  moveTooltip;

  return false;
}


function moveTooltip(e) {
  var browserWidth = Math.max(document.body.clientWidth, document.documentElement.clientWidth) - 20;
  var posX = 0, posY = 0;   // tooltip position

  // get scrolltop
  var st = Math.max(document.body.scrollTop,document.documentElement.scrollTop);
  if (navigator.userAgent.toLowerCase().indexOf('safari')>=0) st=0;

  if (!e) e = window.event;

  // position
  posX = e.clientX + 10;
  posY = e.clientY + 10 + st;

  // keep tooltip away from right edge
  var spill = posX + gstooltip.offsetWidth - browserWidth;
  if (spill > 0) {
    posX -= spill;
    posY += Math.min(spill, 20); // move tooltip lower when it can't go right anymore
  }
  gstooltip.style.left = posX + 'px';
  gstooltip.style.top = posY + 'px';

  // set shadow
  gstooltipShadow.style.width = gstooltip.offsetWidth + 'px';
  gstooltipShadow.style.height = gstooltip.offsetHeight + 'px';
  gstooltipShadow.style.left =  posX + gstooltip_shadowSize + 'px';
  gstooltipShadow.style.top = posY + gstooltip_shadowSize + 'px';
}

function hideTooltip() {
  gstooltip.style.display='none';
  gstooltipShadow.style.display='none';
}


