// ==UserScript==
// @name          SoftPedia Common Stuff
// @author        Ciuly
// @namespace     http://www.ciuly.com
// @description   Set common functions and variable for the softpedia scripts
// @include       http://forum.softpedia.com/*
// @require       http://www.ciuly.com/greasemonkey/aa_all_common.user.js
// ==/UserScript==

(function(wnd){

  if ( !wnd.ipb_md5_check){
    added = new Date();
    added.setTime( Date.parse(GM_getValue("ciuly_ipb_md5_check_added", "Jul 1, 2007")) );
    if (added<(new Date())-1){
      if (wnd.ciuly_is_ff)
        GM_setValue("ciuly_ipb_md5_check", "delete");
      else
        GM_setValue("ciuly_ipb_md5_check", "", "delete");
    }
    wnd.ipb_md5_check = GM_getValue("ciuly_ipb_md5_check", "");
    if ( !wnd.ipb_md5_check || wnd.ipb_md5_check=="delete"){ 
      s = wnd.ciuly_get_res("http://forum.softpedia.com");
      if (s.indexOf("ipb_md5_check")>-1){      
        s = s.substr(s.indexOf("ipb_md5_check")+14);
        i = 0;
        while (s.charAt(i)!="\"")
          i++;
        i++;
	wnd.ipb_md5_check = "";
        while (s.charAt(i)!="\""){
          wnd.ipb_md5_check = wnd.ipb_md5_check + s.charAt(i);
          i++;
        }
        GM_setValue("ciuly_ipb_md5_check", wnd.ipb_md5_check);
        GM_setValue("ciuly_ipb_md5_check_added", (new Date()).toString());
      }
    }
  }

  wnd.ciuly_sp_post_res = function(url, data, ignoreFailure) {
	var r = wnd.ciuly_post_res(url, data, "Sorry, an error occurred.", ignoreFailure);
	if (!ignoreFailure)
		return r;
	else
		return true;
  };

  wnd.ciuly_sp_post = function(url, data, ignoreFailure) {
	var r = wnd.ciuly_sp_post_res(url, data, ignoreFailure) != "";
	if (!ignoreFailure)
		return r;
	else
		return true;
  };

  wnd.ciuly_diacritice = function(post){// ugly fix for romanian special character support
	post = encodeURIComponent(post);
	post = post.replace(/%C4%83/gi,"%E3");// a tilda
	post = post.replace(/%C3%AE/gi,"%EE");// i caciula
	post = post.replace(/%C5%9F/gi,"%BA");// sh
	post = post.replace(/%C5%A3/gi,"%FE");// tz
	post = post.replace(/%C3%A2/gi,"%E2");// a caciula
	return post;
  }

  wnd.ciuly_send_pm = function(username, pmtitle, postContent){
	return wnd.ciuly_sp_post( "http://forum.softpedia.com/index.php?act=msg", 
		"removeattachid=0"+
		"&OID=0"+
		"&act=Msg"+
		"&CODE=04"+
		"&MODE=01"+
		"&attach_post_key="+
		"&auth_key="+wnd.ipb_md5_check+
		"&MAX_FILE_SIZE=0"+
		"&entered_name="+username+
		"&form_contact=-"+
		"&carbon_copy="+
		"&msg_title="+wnd.ciuly_diacritice(pmtitle)+
		"&ed-0_wysiwyg_used=0"+
		"&editor_ids[]=ed-0"+
		"&Post="+wnd.ciuly_diacritice(postContent),
	    false
	);
  };

  wnd.ciuly_reply = function(forum, topic, post){
	return wnd.ciuly_sp_post( "http://forum.softpedia.com/index.php?", 
		"act=Post"+
		"&CODE=03"+
		"&f="+forum+
		"&t="+topic+
		"&st=0"+
		"&auth_key="+wnd.ipb_md5_check+
		"&fast_reply_used=1"+
		"&Post="+wnd.ciuly_diacritice(post)+
		"&enabletrack=0"+
		"&enableemo=yes"+
		"&enablesig=yes"+
		"&submit=Add%20Reply"+
		"&qrc=Close%20Fast%20Reply",
	    false
	);
  }

  wnd.ciuly_newTopic = function(forum, title, description, postContent){
	return wnd.ciuly_sp_post( "http://forum.softpedia.com/index.php?", 
		"st=0"+
		"&act=Post"+
		"&s="+
		"&f="+forum+
		"&auth_key="+wnd.ipb_md5_check+
		"&removeattachid=0"+
		"&MAX_FILE_SIZE=0"+
		"&CODE=01"+
		"&attach_post_key="+
		"&TopicTitle="+wnd.ciuly_diacritice(title)+
		"&TopicDesc="+wnd.ciuly_diacritice(description)+
		"&poll_question="+
		"&ed-0_wysiwyg_used=0"+
		"&editor_ids[]=ed-0"+
		"&Post="+wnd.ciuly_diacritice(postContent)+
		"&enableemo=yes"+
		"&enablesig=yes"+
		"&enabletrack=0"+
		"&iconid=0"+
		"&dosubmit=Post%20New%20Topic",
	    false
	);
  }

  wnd.ciuly_on_every_post = function(callback){
    var uid, uname, div;

    var tables = document.body.getElementsByTagName("table");
    for (var i = 0; i<tables.length; i++){
      table = tables.item(i);
      if (table.className == "ipbtable" && table.getAttribute("cellspacing") == "1"){
        var aaa = table.getElementsByTagName("a");
        if (aaa.length == 0){
          continue;
        }
      
        var postid = aaa.item(0).getAttribute("name");
        if (!postid)
          continue;
        postid = postid.substr(5);

        text = aaa.item(1).getAttribute("href");
        var j = text.indexOf("showuser=");
        if (j>-1){
          uid = text.substr(j+9, text.length);
          uname = aaa.item(1).innerHTML;
        
          var t;
          if (wnd.is_ie){
	    var ttt=table.getElementsByTagName("tr");
	    for (var k=0; k<ttt.length; k++)
	      if (ttt.item(k).innerHTML.indexOf("<!-- REPORT / UP -->")>-1){
                t = ttt.item(k);
                break;
              }
	  }
	  else {
            if (wnd.is_opera)
              t = table.getElementsByTagName("tr").item(0);
            else
              t = table.getElementsByTagName("tr").item(1);// FF

            t = t.nextSibling.nextSibling;
  	}
          div = t.getElementsByTagName("td").item(1).getElementsByTagName("div").item(0);

	  btn = callback(postid, uname, uid);

          div.innerHTML = div.innerHTML + btn;
        }
      }
    }
  }

})(window.wrappedJSObject || window);