//based on prototype's ajax class
//to be used with prototype.lite, moofx.mad4milk.net.

ajax = Class.create();
ajax.prototype = {
	initialize: function(url, options){
		this.transport = this.getTransport();
		this.postBody = options.postBody || '';
		this.method = options.method || 'post';
		this.onComplete = options.onComplete || null;
		this.update = $(options.update) || null;
		this.request(url);
	},

	request: function(url){
		this.transport.open(this.method, url, true);
		this.transport.onreadystatechange = this.onStateChange.bind(this);
		if (this.method == 'post') {
			this.transport.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
			if (this.transport.overrideMimeType) this.transport.setRequestHeader('Connection', 'close');
		}
		this.transport.send(this.postBody);
	},

	onStateChange: function(){
		if (this.transport.readyState == 4 && this.transport.status == 200) {
			if (this.onComplete) 
				setTimeout(function(){this.onComplete(this.transport);}.bind(this), 10);
			if (this.update)
				setTimeout(function(){this.update.innerHTML = this.transport.responseText;}.bind(this), 10);
			this.transport.onreadystatechange = function(){};
		}
	},

	getTransport: function() {
		if (window.ActiveXObject) return new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest) return new XMLHttpRequest();
		else return false;
	}
};

flash = function(t) {
	obj = new fx.Opacity(t, {
		duration: 200,
		onComplete: function(j) {
				if (this.now < 0.9) obj.custom(0.4,1);
			}
		});
	obj.custom(1,0.4);
}

var accordion = null;
var note_effect = null;
function init()
{
	// Its GO time
	var divs = $S('.dropbox');
	var buts = $S('.droplink').concat($S('.droplink_close'));
	
	//then we create the effect.
	accordion = new fx.Accordion(buts, divs, {opacity: true});
	
	// create a notelist effect
	note_effect = new fx.Opacity($('note_list'), {
					duration: 500
				});
				
	initLightbox();
}

var aceffect = null; // add
var eteffect = null; // email to
var cpeffect = null; // change password

var ajax_run = function(t) {
	if (do_debug) alert(t.responseText);
	//document.write(t.responseText);
    eval(t.responseText);
}

/* BASE 64 functions except + is replaced by ! */
var b64 = "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 + b64.charAt(enc1) + b64.charAt(enc2) + 
         b64.charAt(enc3) + b64.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 = b64.indexOf(input.charAt(i++));
      enc2 = b64.indexOf(input.charAt(i++));
      enc3 = b64.indexOf(input.charAt(i++));
      enc4 = b64.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 addbit(subject,data,tags)
{
	$('acdata').disabled = "disabled";
	$('actags').disabled = "disabled";
	$('acsubject').disabled = "disabled";
	$('acbutton').disabled = "disabled";
	$('acflags').disabled = "disabled";
	if (aceffect == null)
	{
		aceffect = new fx.Opacity($('acstatus'), {
						duration: 200
					});
	}
	aceffect.setOpacity(0);
	$('acstatus').innerHTML = 'Adding...';
	aceffect.custom(0.0,1.0);

	// do this thing
	new ajax(baseurl+'ajax/addbit', {postBody:'uid='+uid+'&flag='+encode64($('acflags').value)+'&tags='+encode64(tags)+'&subject='+encode64(subject)+'&content='+encode64(data), onComplete:ajax_run});

}

var sT;
			
function has_string(set,str) {
	for (var x=0; x<set.length; x++) 
		if (set[x] == str) return true;
	return false;
}
function sreq(path)
{
	note_effect.shrink();
	new ajax(baseurl+path, {postBody:'uid='+uid, onComplete:ajax_run});
}
function dotags(tags)
{
	note_effect.shrink();
	new ajax(baseurl+'ajax/tags', {postBody:'uid='+uid+
		'&tags='+encode64(tags), onComplete:ajax_run});
}
function dochangepasswd()
{
	if ($('cpass').value != $('vcpass').value)
	{
		$('cpass').value = "";
		$('vcpass').value = "";
		alert("Password was not verified correctly");
		return;
	}
	
	$('cpass').disabled = "disabled";
	$('vcpass').disabled = "disabled";
	$('cpbutton').disabled = "disabled";
	
	if (cpeffect == null)
	{
		cpeffect = new fx.Opacity($('cpstatus'), {
						duration: 200
					});
	}
	cpeffect.setOpacity(0);
	$('cpstatus').innerHTML = 'Updating...';
	cpeffect.custom(0.0,1.0);

	new ajax(baseurl+'ajax/cpass', {postBody:'uid='+uid+
		'&np='+encode64($('cpass').value), onComplete:ajax_run});
}
function dosearch()
{
	note_effect.shrink();
	new ajax(baseurl+'ajax/search', {postBody:'uid='+uid+
		'&search_with='+encode64($('search_with').value)+
		'&search_one_of='+encode64($('search_one_of').value)+
		'&search_exact='+encode64($('search_exact').value)+
		'&search_without='+encode64($('search_without').value), onComplete:ajax_run});
}
function hideclassified(id)
{
	$('note'+id+'classifiedinfo').style.display = "block";
	e = new fx.Opacity($('note'+id+'classifiedinfo'), {duration: 500});
	e.custom(0,1);
					
	
	$('note'+id+'classifiedcontent').style.display = "none";
	
	
	$('note'+id+'classifiedhide').style.display = "none";
	//e = new fx.Opacity($('note'+id+'classifiedhide'), {duration: 200});
	//e.custom(1.0,0.0);
	
	$('note'+id+'classifiedshow').style.display = "inline";
	e = new fx.Opacity($('note'+id+'classifiedshow'), {duration: 200});
	e.setOpacity(0.0);
	e.custom(0.0,1.0);
}
function showclassified(id)
{
	$('note'+id+'classifiedcontent').style.display = "block";
	e = new fx.Opacity($('note'+id+'classifiedcontent'), {duration: 500});
	e.custom(0,1);
					
	
	$('note'+id+'classifiedinfo').style.display = "none";
	
	$('note'+id+'classifiedshow').style.display = "none";
	//e = new fx.Opacity($('note'+id+'classifiedshow'), {duration: 200});
	//e.custom(1.0,0.0);
	
	$('note'+id+'classifiedhide').style.display = "inline";
	e = new fx.Opacity($('note'+id+'classifiedhide'), {duration: 200});
	e.setOpacity(0.0);
	e.custom(0.0,1.0);
}
function savenote(id)
{
	if (!($('note'+id+'editc')))
		return; // cant save
	
	var contentArea = $('note'+id+'content');
	
	var content = $('note'+id+'editc');
	content.disabled = "disabled";
	var tags = $('note'+id+'editt');
	tags.disabled = "disabled";
	var subject = $('note'+id+'edits');
	subject.disabled = "disabled";
	var flags = $('note'+id+'editf');
	flags.disabled = "disabled";
	
	var statusSpan = $('note'+id+'status');
	statusSpan.innerHTML = 'Updating...';
	
	e = new fx.Opacity(statusSpan, {
					duration: 200
				});
	e.setOpacity(0);
	statusSpan.innerHTML = 'Updating...';
	e.custom(0.0,1.0);
	
	
	// do this thing
	new ajax(baseurl+'ajax/editbit', {postBody:'uid='+uid+'&nid='+id+'&flag='+encode64(flags.value)+'&subject='+encode64(subject.value)+'&tags='+encode64(tags.value)+'&content='+encode64(content.value), onComplete:ajax_run});

}
function editnote(id)
{
	// switch the actual note with a text area
	if ($('note'+id+'editc'))
	{
		 // busy editting - do nothing
		return;
	}	
	var contentArea = $('note'+id+'content');
	
	var classified = false;
	if ($('note'+id+'classifiedcontent'))
	{
		classified = true;
		var contentData = $('note'+id+'classifiedcontent').innerHTML;
		if ($('note'+id+'classifiedshow'))
		{
			e = new fx.Opacity($('note'+id+'classifiedshow'), {duration: 200});
			e.custom(1.0,0.0);
		}
		if ($('note'+id+'classifiedhide'))
		{
			e = new fx.Opacity($('note'+id+'classifiedhide'), {duration: 200});
			e.custom(1.0,0.0);
		}
	}else
	{
		if (contentArea.classifiedContent)
		{
			classified = true;
		}
		var contentData = contentArea.innerHTML;
	}
	
	while (contentData.indexOf("<br>") >= 0)
		contentData = contentData.replace(/<br>/, '\n');
	while (contentData.indexOf("<BR>") >= 0)
		contentData = contentData.replace(/<BR>/, '\n');
	while (contentData.indexOf("<br/>") >= 0)
		contentData = contentData.replace(/<br\/>/, '\n');
	while (contentData.indexOf("<BR/>") >= 0)
		contentData = contentData.replace(/<BR\/>/, '\n');
	while (contentData.indexOf("<br />") >= 0)
		contentData = contentData.replace(/<br \/>/, '\n');
	while (contentData.indexOf("<BR />") >= 0)
		contentData = contentData.replace(/<BR \/>/, '\n');
	
	//savenote(\''+id+'\');
	contentArea.innerHTML = '<textarea onBlur="sT=setTimeout(\'savenote(\\\''+id+'\\\');\',10);" onFocus="clearTimeout(sT);" id="note'+id+'editc" rows="5" cols="60">'+contentData+'</textarea>';
	
	$('note'+id+'editc').focus();

	// now the tags
	var tagList = "";
	var tagArea = $('note'+id+'tags');
	
	tags = $S('#note'+id+'tags a');
	if (tags)
	{
		tags.each(function(a) {
		  if (tagList != "") tagList += ", ";
		  tagList += a.innerHTML;
		});
	}
	tagArea.innerHTML = '<input type="text" onBlur="sT=setTimeout(\'savenote(\\\''+id+'\\\');\',10);" onFocus="clearTimeout(sT);" id="note'+id+'editt" size="40" value="'+tagList+'"/>&nbsp;'+
						'<select onBlur="sT=setTimeout(\'savenote(\\\''+id+'\\\');\',10);" onFocus="clearTimeout(sT);" id="note'+id+'editf"><option'+(classified?'':' selected')+'>Normal</option><option'+(classified?' selected':'')+'>Classified</option></select>';

	var subjectArea = $('note'+id+'subject');
	var subjectValue = $('note'+id+'subjectv')?$('note'+id+'subjectv').innerHTML:'';
	subjectArea.innerHTML = ' - <input type="text" onBlur="sT=setTimeout(\'savenote(\\\''+id+'\\\');\',10);" onFocus="clearTimeout(sT);" id="note'+id+'edits" size="40" value="'+subjectValue+'"/>';

	var statusSpan = $('note'+id+'status');
	statusSpan.innerHTML = '<button>Update</button>';
	
}
function dodel(id)
{
	// do it
	new ajax(baseurl+'ajax/delbit', {postBody:'uid='+uid+'&nid='+id, onComplete:ajax_run});
}
function delnote(id)
{
	var exDiv = document.createElement('div');
	Element.addClassName(exDiv,"lightask");
	exDiv.innerHTML = "<div align=\"center\">Are you sure you want to delete this bit?<br/><br/><a class=\"jslink boxlink_red\" onClick=\"dodel('"+id+"'); hideLightbox();\">Yes</a>&nbsp;<a class=\"jslink boxlink_blue\" onClick=\"hideLightbox();\">No</a></div>";

	showLightbox(exDiv,250,50);
}
function do_eteffect()
{
	if (eteffect == null)
	{
		eteffect = new fx.Opacity($('emailto_status'), {
						duration: 200
					});
	}
	eteffect.setOpacity(0);
	$('emailto_status').innerHTML = 'Updating...';
	eteffect.custom(0.0,1.0);
}
function emailto_enable_change()
{
	do_eteffect();
	if ($('email2snapbits').checked)
	{
		// its on
		new ajax(baseurl+'ajax/emailto', {postBody:'uid='+uid+'&enable=1', onComplete:ajax_run});
	}else
	{
		// its off
		new ajax(baseurl+'ajax/emailto', {postBody:'uid='+uid+'&disable=1', onComplete:ajax_run});
	}
}
function emailto_from_change()
{
	$('email2snapbits_from').disabled = "disabled";
	$('emailto_from_change').disabled = "disabled";
	
	do_eteffect();
	
	// do this thing
	new ajax(baseurl+'ajax/emailto', {postBody:'uid='+uid+'&change='+encode64($('email2snapbits_from').value), onComplete:ajax_run});
}