// ==UserScript==
// @name          SoftPedia Fix Video
// @author        Ciuly  
// @namespace     http://www.ciuly.com
// @description   Adds a button on the first post in a topic on my video forum for replacing [url] tags with [video] ones
// @include       http://forum.softpedia.com/index.php?showtopic=*
// @require       http://www.ciuly.com/greasemonkey/aa_all_common.user.js
// @require       http://www.ciuly.com/greasemonkey/aa_sp_common.user.js
// @require       http://www.ciuly.com/greasemonkey/aa_sp_mod_common.user.js
// ==/UserScript==

(function(wnd){

  var nav = document.getElementById('navstrip');
  var a = nav.getElementsByTagName("a");
  var url = "";
  if (a.length>3){
    url = a.item(2).getAttribute("href");
    url = url.substr(url.length-4);
  } else
    url = "";

  if (url != "=505")// only show button in my video forum
    return;

  if ( wnd.ajax_loaded && wnd.use_enhanced_js ){

  wnd.ciuly_DelayFixVideo = function(postid){
    if (document.getElementById(postid + '_textarea') == null){
      wnd.setTimeout("ciuly_DelayFixVideo("+postid+")", 250);
      return;
    }
    var str = document.getElementById(postid + '_textarea').value;
    str = str.replace(/\[url=\"(.*?)\"\](.*?)\[\/url\]/g, "[video]$1[/video]");
    document.getElementById(postid + '_textarea').value = str;
    wnd.ajax_save_for_edit(postid);
  }

  wnd.ciuly_FixVideo = function (postid, e){
    var evtobj=window.event? event : e;
    wnd.ajax_prep_for_edit(postid, evtobj);
    wnd.setTimeout("ciuly_DelayFixVideo("+postid+")", 500);
  };

  var firstOnly = false;

  function addButton(postid, uname, uid){
    if (firstOnly)
      return "";
    firstOnly = true;
    return "&nbsp;<button class=\"button\" name=\"Fix Video\" title=\"Fix Video\" onclick=\"return ciuly_FixVideo('"+postid+"', event)\">Fix Video</button>&nbsp;";
  } 

  if (wnd.ipb_md5_check){
    wnd.ciuly_on_every_post(addButton);
  }

} else {
  alert('Ajax not loaded or not using enhanced JS. Video Fix will not work. This is kind of bad.');
}

})(window.wrappedJSObject || window);