// para.site 
// version 0.1 BETA!
// 2006-02-06
// Copyright (c) 2006, Armando Sosa
// Released under the GPL license
// http://www.gnu.org/copyleft/gpl.html
//
// --------------------------------------------------------------------
//
// This is a Greasemonkey user script.
//
// To install, you need Greasemonkey: http://greasemonkey.mozdev.org/
// Then restart Firefox and revisit this script.
// Under Tools, there will be a new menu item to "Install User Script".
// Accept the default configuration and install.
//
// To uninstall, go to Tools/Manage User Scripts,
// select "Hello World", and click Uninstall.
//
// --------------------------------------------------------------------
//
// ==UserScript==
// @name          para.site
// @namespace     http://nolimit-studio.com/yosoysosa
// @description   Clientside script for myComments
// @include       *
// @exclude				*google*
// ==/UserScript==

var myParasite={
	author:'tunombre',
	email:'tunombre@tuemail',
	url:'http://www.tu-url.com/opcional',
  init:function(){
        if (!document.getElementById('parasite')){
          if (myParasite.commentForm=myParasite.findCommentForm()){
					  cf=myParasite.commentForm;
            parasite= "<label><input type='checkbox' checked='checked' name='parasite_do'/>";
            parasite+= "send to mycomments</label>"
        		insertPoint=cf.getElementsByTagName('textarea')[0];//.nextSibling;
            div=document.createElement('div');
            div.className='parasite';
            div.id='parasite';
            div.innerHTML=parasite;
            insertAfter(cf,div,insertPoint)
        		css=document.createElement('link');
        		css.setAttribute('href','http://nolimit-studio.com/parasite/parasite.css');
        		css.setAttribute('rel','stylesheet');
        		css.setAttribute('type','text/css');
        		document.body.appendChild(css);
						cf.addEventListener('submit',myParasite.hijack,false);						
        	}
        	else GM_log('no encontramos la forma de comentario'); 
        }
  },
	findCommentForm:function(){
      	forms=document.getElementsByTagName('form');
      	rexp=/comm/;
      	for (var i=0;i<forms.length;i++){
      	   	if ((rexp.exec(forms[i].name)) || (rexp.exec(forms[i].id)) || (rexp.exec(forms[i].className) || (rexp.exec(forms[i].getAttribute('action'))))) return forms[i];	
      	}				
	},
	hijack:function(e){	      
 	   	  e.preventDefault(); // Gerd Riesselmann thanks a lot	
				if (document.getElementsByName('parasite_do')[0].checked){
          GM_xmlhttpRequest({
            method:"POST",
            url:"http://mycomments.idslab.com.ar/app/ping.php",
    				data:myParasite.queryString(),
    				headers:{'Content-Type':'application/x-www-form-urlencoded'},
            onload:function() {myParasite.commentForm.submit();}
          });
				}	            					   		 	
	},
	queryString:function(){
	  var query,hiddens,blogName,el,h1;		
		var rexp=/id/;
		h1s=document.getElementsByTagName('h1');
		if (h1s.lenght){
		  imgsInH1=h1s[0].getElementsByTagName('img');
			blogName=(imgsInH1.length)?imgsInH1[0].getAttribute('alt'):h1s[0].innerHTML.replace(/(<([^>]+)>)/ig,"");
		}
		else{
		  blogName=encodeURIComponent(window.location.host);
		}
    hiddens = document.evaluate("//input[@type='hidden']",myParasite.commentForm,null,XPathResult.UNORDERED_NODE_SNAPSHOT_TYPE,null);
    for (var i = 0; i < hiddens.snapshotLength; i++) {
        el = hiddens.snapshotItem(i);
				if(( rexp.exec( el.getAttribute('name').toLowerCase() ) )) myParasite.postId=el.value;
    }		
		myParasite.commentContent=myParasite.commentForm.getElementsByTagName('textarea')[0].value;
		query="blogName="+blogName;
	  query+="&blogUrl="+encodeURIComponent(window.location.host);		
	  query+="&postUrl="+encodeURIComponent(location.href);
	  query+="&postTitle="+escape(document.title);
	  query+="&postId="+myParasite.postId;				
		query+="&commentAuthor="+myParasite.author;
		query+="&commentEmail="+myParasite.email;
		query+="&commentUrl="+myParasite.url;
		query+="&commentContent="+escape(myParasite.commentContent);									
		return query;
	}
}


function insertAfter(parent, node, referenceNode) {
    try {
    parent.insertBefore(node, referenceNode.nextSibling);
		}
		catch (e){    
    parent.appendChild(node);    
    }		
}

window.addEventListener('load',function(){
  var submits=document.getElementsByName('submit');
  for (var i=0;i<submits.length;i++){
    	submits[i].setAttribute('name','submitty');	
  }	
  myParasite.init();
		
},false)
