/* Pirl! Javascript 
*    http://pirl.altervista.org
*    by w00fz
*/

function reg_focus(arg) {
	document.getElementById(arg).focus();
}

function reg_select(arg) {
	document.getElementById(arg).select();
}

function quote(id,author,sty) {
	var msg = $(id).innerHTML;
	// style=\'margin: 1em;padding-left: .3em;padding-right: .3em;padding-top: .3em;padding-bottom: .1em;background: #e6edf0;border-right: 1px solid #bacfd8;border-bottom: 1px solid #bacfd8;border-top: 1px solid #f2f4f5;border-left: 1px solid #f2f4f5;\'
	
	// style=\'margin: 1em;padding: .3em;background: #f5e0df;border-right: 1px solid #f6e8e8;border-bottom: 1px solid #f6e8e8;border-top: 1px solid #f4d1d1;border-left: 1px solid #f4d1d1;\'
	if (sty==1)
		var classNow = "style=\'margin: 1em;padding-left: .3em;padding-right: .3em;padding-top: .3em;padding-bottom: .1em;background: #e6edf0;border-right: 1px solid #bacfd8;border-bottom: 1px solid #bacfd8;border-top: 1px solid #f2f4f5;border-left: 1px solid #f2f4f5;\'";
	else
		var classNow = "style=\'margin: 1em;padding: .3em;background: #f5dede;border-right: 1px solid #f0c0bf;border-bottom: 1px solid #f0c0bf;border-top: 1px solid #feeaea;border-left: 1px solid #feeaea;\'";
		
	tinyMCE.execCommand('mceInsertContent',true,'<div class="quote" '+classNow+'><strong>'+author+'</strong> ha scritto...<p>'+msg+'</p></div><p></p>');
}

function pirlAjax(id){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			tinyMCE.execCommand('mceRemoveControl', false, 'edit_'+id);
			document.getElementById('comment_'+id).innerHTML = '';
			document.getElementById('comment_'+id).innerHTML = ajaxRequest.responseText;
			document.getElementById('loader_'+id).innerHTML = '';
			
			goEdit(id);
		}
		else {
			document.getElementById('loader_'+id).innerHTML = '<img src="img/loading.gif" />';
		}
	}
	ajaxRequest.open("GET", "comment_edit.php?id="+id, true);
	ajaxRequest.send(null); 
}

function pirlEditUndo(id){
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			tinyMCE.execCommand('mceRemoveControl', false, 'edit_'+id);
			document.getElementById('comment_'+id).innerHTML = '';
			document.getElementById('comment_'+id).innerHTML = ajaxRequest.responseText;
			document.getElementById('loader_'+id).innerHTML = '';
			$('q_'+id).setStyle('display', '');
		}
		else {
			document.getElementById('loader_'+id).innerHTML = '<img src="../img/loading.gif" />';
		}
	}
	ajaxRequest.open("GET", "comment_undo.php?id="+id, true);
	ajaxRequest.send(null); 
}

function pirlEditSave(id,base,ln){
	if (ln<2) { alert("Attenzione! E' necessario inserire almeno 2 caratteri."); return; }
	var ajaxRequest;  // The variable that makes Ajax possible!
	
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	// Create a function that will receive data sent from the server
	ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
			var today = new Date();
			var month = today.getMonth()+1;
			var year = today.getYear();
			var day = today.getDate();
			if(day<10) day = "0" + day;
			if(month<10) month= "0" + month;
			if(year<1000) year+=1900;
			p_date = month + "/" + day + "/" + year;
			
			var curDateTime = new Date();
			var curHour = curDateTime.getHours();
			var curMin = curDateTime.getMinutes();
			var curSec = curDateTime.getSeconds();
			var curTime = ((curHour < 10) ? "0" : "") + curHour + ":" 
						+ ((curMin < 10) ? "0" : "") + curMin + ":" 
						+ ((curSec < 10) ? "0" : "") + curSec;
			p_time = curTime;
			
			tinyMCE.execCommand('mceRemoveControl', false, 'edit_'+id);
			document.getElementById('comment_'+id).innerHTML = '';
			
			var time_done = false;
			$$('#time_'+id+' span em.time').each(function(t) {
				t.setHTML('Modificato il '+p_date+' - '+p_time+'');
				time_done = true;
			});
			if (!time_done)
				$$('#time_'+id+' span').each(function(t) {
					t.setHTML(t.innerHTML+'<br /><em class="time">Modificato il '+p_date+' - '+p_time+'</em>');
				});
			document.getElementById('comment_'+id).innerHTML = ajaxRequest.responseText;
			document.getElementById('loader_'+id).innerHTML = '';
			$('q_'+id).setStyle('display', '');
		}
		else {
			document.getElementById('loader_'+id).innerHTML = '<img src="../img/loading.gif" />';
		}
	}
	ajaxRequest.open("GET", "comment_save.php?id="+id+"&base="+escape(base), true);
	ajaxRequest.send(null); 
}

function goEdit(id) {
//	tinyMCE.execCommand('mceRemoveControl', false, 'edit_'+id);
	$('q_'+id).setStyle('display', 'none');
	document.getElementById("Salva").disabled = false;
	tinyMCE.execCommand('mceAddControl', false, 'edit_'+id);
}

var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";

function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}

function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

function chgAvatar(id,url) {
	document.getElementById(id).value = url;
}

function count(ms) {
	var tmp = ms;
	
	var nd = Math.floor(tmp / 60 / 60 / 24);
	var tmp = tmp - (nd * 60 * 60 * 24);
	var nh = Math.floor(tmp / 60 / 60);
	tmp = tmp - (nh * 60 * 60);
	var nm = Math.floor(tmp / 60);
	tmp = tmp - (nm * 60);
	var ns = Math.floor(tmp);
	if (nd<=0 && nh<=0 && nm<=0 && ns<=0) {
		$clear(SD);
		$('open_contest_vote').remove();
		$('timeleft').setHTML("Il contest mensile si è concluso.");
		return false;
	}
	else {
		if (nd==1) giorni = "giorno ";	else giorni = "giorni ";
		if (nh==1) ore = "ora "; else ore = "ore ";
		if (nm==1) minuti = "minuto "; else minuti = "minuti ";
		if (ns==1) secondi = "secondo "; else secondi = "secondi ";
		
		if (nd>0) $('timeleft').setHTML(nd+giorni+nh+ore+nm+minuti+ns+secondi);
		else if (nh>0) $('timeleft').setHTML(nh+ore+nm+minuti+ns+secondi);
		else if (nm>0) $('timeleft').setHTML(nm+minuti+ns+secondi);
		else $('timeleft').setHTML(ns+secondi);
	}
	SD=setTimeout("count("+(ms-1)+")", 1000);
}

function pwGen(id) {

    if (parseInt(navigator.appVersion) <= 3) {
        alert("Sorry this only works in 4.0+ browsers");
        return true;
    }

    var length=8;
    var sPassword = "";

    for (i=0; i < length; i++) {

        numI = getRandomNum();
		while (checkPunc(numI)) { numI = getRandomNum(); }
        sPassword = sPassword + String.fromCharCode(numI);
    }

    document.getElementById(id).innerHTML = "<strong>"+sPassword+"</strong>";
    return true;
}

function getRandomNum() {

    // between 0 - 1
    var rndNum = Math.random()

    // rndNum from 0 - 1000
    rndNum = parseInt(rndNum * 1000);

    // rndNum from 33 - 127
    rndNum = (rndNum % 94) + 33;

    return rndNum;
}

function checkPunc(num) {

    if ((num >=33) && (num <=47)) { return true; }
    if ((num >=58) && (num <=64)) { return true; }
    if ((num >=91) && (num <=96)) { return true; }
    if ((num >=123) && (num <=126)) { return true; }

    return false;
}


var PorkyPirl = {
	start: function() {
		var status = true;

		$('logo').addEvent('click', function(){
			if (!status) {
				var prk_remove = new Fx.Style('porky','opacity',{
					onComplete: function() {
						$('porky').remove();
					}}).custom(1,0);
				var opacizy = new Fx.Style('logo','opacity','{duration: 600, wait: false}').custom(.2,1);
				status=true;
			} else {
				status = false;
				var opacizy = new Fx.Style('logo', 'opacity','{duration: 1000}').custom(1,.2);
				var pig = new Element('div');
				pig.addClass('pig').setProperty('id','porky').injectBefore('logo');
				var where = "top";
				var prk_show = new Fx.Style('porky','opacity',{
					onComplete: function() {
						var where = "bottom";
						var mySlider = new Fx.Style('porky', 'margin-top', {duration: 3000, transition: Fx.Transitions.backInOut,
						onComplete: function() {
							if (where=="top") { where = "bottom"; mySlider.custom(25, (Window.getHeight()-145)); }
							else { where = "top"; mySlider.custom((Window.getHeight()-145), 25); }
						}});
						mySlider.custom(25, (Window.getHeight()-145));
					}
				}).custom(0,1);

			}
		});
	}
}


window.addEvent('load', function() {
	PorkyPirl.start();
});
window.addEvent('domready', function() { 
	new Tips($$('.toolTip'), {}); 
	new Tips($$('.toolTip-Cont'), {maxTitleChars: 90, className: 'toolcont'});
});