//
// 		Actions applied to the article
//
//
//
 
 
var posx, posy;				// mouse positions. invoke checkMousePosition(ev) to update

var oldonload = window.onload;
var lang="pt";
window.onload =function () {
		eval( "oldonload()" );
		divartigo = window.document.getElementById('artigo');
		
		bodys = window.document.getElementsByTagName("body");
		body_obj = bodys[0];
		
		lang = body_obj.className;
		
		if( !divartigo ) return false;
		//----------------------------
		
		divs = divartigo.getElementsByTagName("div");
			
		for(i=0;i<divs.length;i++) {
			if( divs[i].className.match( /actions/ ) ) {
				
					links = divs[i].getElementsByTagName("a");
					for(j=0;j<links.length;j++) {
						
						if( links[j].className.match( /printlink/ ) ) {
								links[j].onclick = function() {
										window.print(); 
										return false;
								}
						}
						
						if( links[j].className.match( /sendlink/ ) ) {
							links[j].target = "_self";
							links[j].onclick = function(e) {




									checkMousePosition(e);
									
									
									// if present, remove
									sendLink = window.document.getElementById('sendlink');
									if( sendLink ) {
										sendLink.parentNode.removeChild(sendLink);
										return false;
									}
										
										
									newObj = window.document.createElement("div");

									newObj.id = "sendlink";


									if ( lang == "pt" )
										newObj.innerHTML = "(A carregar...) <a href=\"javascript:coreRemoveId('sendlink')\">Fechar esta caixa.</a>";
									else if( lang == "en" );
										newObj.innerHTML = "(Now loading...) <a href=\"javascript:coreRemoveId('sendlink')\">Close this box.</a>";



									if( window.document.height ) {
										// advanced browser, ok then
										newObj.style.height = window.document.height + "px !important";
										newObj.style.width = "100%";
										

//										newObj.style.left = "" + (posx+5) + "px";
//										newObj.style.top = "" + (posy+5) + "px";
									} else {				
										newObj.className = "crippled";
										newObj.style.left = "" + (posx+5) + "px";
										newObj.style.top = "" + (posy+5) + "px";
									}


						
									window.document.body.appendChild(newObj);


getText("actions/sendLink.php", function() {
	obj = window.document.getElementById("sendlink");

	// dump the servers response. [AHAH]
	if( obj )
		obj.innerHTML = req.responseText;

	// focus on the email (compulsory field)
	objEmail = window.document.getElementById('email');
	if( objEmail )
		objEmail.focus();
		
	// if modern browser, reposition the container
	if( !obj.className.match( /crippled/ ) ) {
		divs = newObj.getElementsByTagName("div");
		divs[0].style.left 		= "" + (posx-250) + "px";
		divs[0].style.top 		= "" + (posy-150) + "px";
		divs[0].style.display 	= "block";
	}

/**	h3s = window.document.getElementsByTagName("h3");
	if( h3s[0].innerText )
		thistitle = h3s[0].innerText;
	else
		thistitle = h3s[0].innerHTML;*/
		
	thistitle = window.document.getElementById('titlemain').value;


	msg = window.document.getElementById('mensagem');
	if( msg ) {
		if( lang == "pt" )
			msg.value ="Um leitor da Sísifo - Revista de Ciências da Educação - achou que este artigo lhe poderia interessar.";
		else if ( lang == "en" )
			msg.value = "A reader of Sísifo - Educational Sciences Journal - thought you'd be interested in this article."; 
	}
	
	msg2 = window.document.getElementById('mensagem2');
	if( msg2 ) {
		if( lang == "pt" )
			msg2.value = "Para visualizar o artigo, clique no link ou copie o endereço e cole no seu browser:\n\n" + thistitle + "\n" + location.href + "\n\nObrigado.\n\n--\nSísifo. Revista de Ciências da Educação\nhttp://sisifo.fpce.ul.pt\n(mensagem automática enviada a partir do site a pedido de um leitor)";
		else if ( lang == "en" ) 
			msg2.value = "To read it, please, click on the link below or copy/paste it into your browser:\n\n" + thistitle + "\n" + location.href + "\n\nThank you.\n\n--\nSísifo. Educational Sciences Journal\nhttp://sisifo.fpce.ul.pt\n(automatic message sent from within the site, on request of a reader)";

	}
	// switch form action (to ajax response) [hijaxing]
	frm = window.document.getElementById('frmSendLink');
	frm.action = "javascript:sendArticle()";
	frm.onsubmit = function(e) {
			checkMousePosition(e);
	};


});


									return false;
							}
							links[j].href = "javascript:void(0)";

						}

					}

			}
		}
 		
		


window.document.onclick = function updateXY(e) {
	checkMousePosition(e);
};
		
		
}


function checkMousePosition (e) {
	posx = 0;
	posy = 0;
	if (!e) var e = window.event;
	if (e.pageX || e.pageY) 	{
		posx = e.pageX;
		posy = e.pageY;
	}
	else if (e.clientX || e.clientY) 	{
		posx = e.clientX + document.body.scrollLeft
			+ document.documentElement.scrollLeft;
		posy = e.clientY + document.body.scrollTop
			+ document.documentElement.scrollTop;
	}
	// posx and posy contain the mouse position relative to the document
	// Do something with this information
}


function sendArticle() {
		
		nome 		= window.document.getElementById('nome');
		myemail 	= window.document.getElementById('myemail');
		email 		= window.document.getElementById('email');
		//type		= window.document.getElementById('txt');
		title		= window.document.getElementById('titlemain').value;
		mensagem	= window.document.getElementById('mensagem');
		mensagem2	= window.document.getElementById('mensagem2');	
		
		if( !mensagem ) 
			mensagem = "";
		else
			mensagem = escape( mensagem.value );
			
		if( !mensagem2 ) 
			mensagem2 = "";
		else
			mensagem2 = escape( mensagem2.value );

		
		if( !email ) {
				if( lang == "pt" )
					alert("Não preencheu o campo obrigatório.");
				else if( lang == "en" )
					alert("You didn't fill all the compulsory fields.");
				return false;
		}
		
		nome = nome.value;
		myemail = myemail.value;
		email = email.value;
		
		//type = ((type.checked=='checked')?"txt":"html");
		
		
		/**getText( "actions/sendLink.php", function () {
												   
					sl = window.document.getElementById('sendlink');
					if( sl ) {
						sl.innerHTML = "-------------------------";
						sl.innerHTML += req.responseText;
						alert( req.responseText );												   	
					} else
						alert("ops!");
												   
		   });
//window.document.body.innerHTML = "<p>"+nome+"<br />"+myemail+"<br />"+email+"<br />"+type+"</p>" + window.document.body.innerHTML;
*/

		startupReq();
		req.onreadystatechange = function () {
				if( req.readyState == 4) {
					sl = window.document.getElementById('sendlink');
					divs = sl.getElementsByTagName( "div" );
					divs[0].innerHTML = req.responseText;
					divs[0].style.display = "none";
					flashObj( divs[0] );
						
					// switch form action (to ajax response) [hijaxing]
					frm = window.document.getElementById('frmSendLink');
					if( frm )
						frm.action = "javascript:sendArticle()";
	
					if( window.document.getElementById('okenvio') )
						goToTop();
				}
				


					

		};
		
		
	  params = "email=" + encodeURI( email ) + 
	  			//"&type=" + encodeURI( type ) +
				"&title=" + encodeURI( escape( title ) ) +
				"&mensagem=" + encodeURI( mensagem ) +
				"&mensagem2=" + encodeURI( mensagem2 ) +				
				"&nome=" + encodeURI( nome ) +
				"&myemail=" + encodeURI( myemail );

		
      req.open('POST', "actions/sendLink.php", true);
      req.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
      req.setRequestHeader("Content-length", params.length);
      req.setRequestHeader("Connection", "close");
	  req.send(params);
}

flashedObj = null;
function flashObj( obj ) {
		flashedObj = obj;
		obj.style.display="none";
		setTimeout( "unflash()", 50 );
}
function unflash() {
	flashedObj.style.display = "block";
}
	
	

function goToTop() {

    if( window.pageYOffset ) {
	   while(window.pageYOffset != 0) {
            offset = -30;
            
	   	window.scrollBy(0,offset);
        }
    } else {
        window.scrollTo(0,0);
    }
    
}

