// make sure shit's loaded.
document.observe('dom:loaded', function() {

	// extend inplaceeditor so i can run Texpand() at the right time.
	Ajax.InPlaceEditor.prototype.__enterEditMode = Ajax.InPlaceEditor.prototype.enterEditMode;
	Object.extend(Ajax.InPlaceEditor.prototype, {
		enterEditMode:function(e) {
			this.__enterEditMode(e);
			this.triggerCallback('onFormReady',this._form);
		}
	});
//	Ajax.InPlaceEditor.prototype.__handleFormCancellation = Ajax.InPlaceEditor.prototype.handleFormCancellation;
//	Object.extend(Ajax.InPlaceEditor.prototype, {
//		handleFormCancellation:function(e) {
//			this.wrapUp();
//			if (e) Event.stop(e);
//			return false;
//			this.__handleFormCancellation(e);
//			this.triggerCallback('onCancel',this._form);
//		}
//	});

	// hint-text
	var hint = new HintText();

	// set up diddles.
	var rawkcount = [];
	var lastrawk = [];
	var diddlechecked = new Hash();
	var rawkshowlist = [];
	diddlecheck();

	// the dude menu.
	if ($('menu-go'))
	{
		$('menu-go').immediateDescendants().each(function(child) {
			Event.observe(child, 'mouseover', function(event){
				child.addClassName('over');
			});
			Event.observe(child, 'mouseout', function(event){
				child.removeClassName('over');
			});
		});
	}

	// togglin' shit.
	$$('.toggler').each(function(e){
		e.observe('click', function(e){
			var match;
			e.stop();
			if (match = this.className.match(/toggle-(\S+)/))
			{
				that = this;
				Effect.toggle($(match[1]),'blind', { duration: 0.3, afterFinish:function(e){
					if (match2 = that.className.match(/refresh-(\S+)/))
					{
						split = match2[1].split('-');
						new Ajax.Request('/x?grab=blobs&what=allofit&really='+split[1]+'&'+dude.toQueryString(), {
							method: 'get',
							onSuccess: function(x)
							{
								$(match2[1]).update(x.responseText);
								Effect.toggle($(match2[1]),'blind', { duration: 0.3 });
								Effect.toggle($(match[1]+'-link'),'blind', { duration: 0.3 });
							}
						});
					}
					else if (match3 = that.className.match(/toggle2-(\S+)/))
					{
						Effect.toggle($(match3[1]),'blind', { duration: 0.3 });
						$$('.highlightme.'+match3[0]).invoke('toggleClassName','highlight');
//						$$('.hideme.'+match3[0]).invoke('toggle');
					}
				} });
				$$('.highlightme.'+match[0]).invoke('toggleClassName','highlight');
//				$$('.hideme.'+match[0]).invoke('toggle');
				if (this.hasClassName('hideme'))
					$(this.identify()).toggle();
			}
		})
	});

	// one-click updatin' shit.
	$$('.clicker').each(function(e){
		e.observe('click', function(e){
			if (split = this.identify().split('_'))
			{
				var the_id = this.identify();
				var half1 = split[0];
				var half2 = split[1];
				var id;
				var joint = half1;
				var what = half2;
				var split1;
				var split2;
				if (split1 = half1.split('-'))
				{
					joint = split1[0];
					id = split1[1];
				}
				if (split2 = half2.split('-'))
				{
					what = split2[0];
					value = split2[1];
				}

				if (what == 'anchorer')
				{
					var newval = $(joint+'-'+id+'_'+what+'-diddlebox').down().value;
					if (newval.length != 0)
						newval = newval+', ';
					newval = newval+this.readAttribute('anchortitle');
					$(joint+'-'+id+'_'+what+'-diddlebox').down().update(newval);
				}
				else
				{
//					Effect.toggle($(joint+'-'+id),'appear', { duration: 0.3 });

					var duder = dude.toQueryString()+'&';
					var sendurl = new Hash();
					sendurl.set('shove',joint);
					sendurl.set('what',what);
					sendurl.set('really',id);
					sendurl.set('value',value);

					var eyjacks = new Hash();
					eyjacks.set('method','get');
//					eyjacks.set('onLoading',function(r) {Effect.toggle($(joint+'-'+id),'appear', { duration: 0.3 });});
//					eyjacks.set('onComplete',function(r) {Effect.toggle($(joint+'-'+id),'appear', { duration: 0.3 });});

					var updateid = null;
					if (what == 'bomber')
						updateid = joint+'-'+id;
					else
					{
						updateid = joint+'-'+id+'_'+what;
						if (what != 'anchorer')
							eyjacks.set('onComplete',function(r) {$(updateid).toggle(); $(updateid+'-list').toggle();});
					}
					var update = new Ajax.Updater(updateid, '/x?'+duder+sendurl.toQueryString(), eyjacks.toObject());
				}
			}
		});
	});

	// auto-updating rawk lists! holy shit!
	$$('.therawks').each(function(e){
		if (split = e.identify().split('_'))
		{
			var the_id = e.identify();
			var id;
			var joint = split[0];
			var split;
			if (split = joint.split('-'))
			{
				joint = split[0];
				id = split[1];
			}
			rawkcount[id] = 0;
			lastrawk[id] = 0;
			$$('.rawkfor_blobs-'+id).each(function(r){
				rawkcount[id]++;
				var rawk_the_id = r.identify().split('-');
				lastrawk[id] = rawk_the_id[1];
			});

			new PeriodicalExecuter(function(p){
				new Ajax.Request('/x?grab=rawks&what=count&really='+id+'&'+dude.toQueryString(), {
					method: 'get',
					onSuccess: function(x)
					{
						if (x.responseText != rawkcount[id])
						{
							rawkspray(id);
						}
					}
				});
			},10);
		}
	});	

	function rawkspray(blob_id)
	{
		var options = {
			method: "get",
			parameters: "intervalPeriod="+10,
			insertion: 'bottom',
			onComplete: function (oXHR, Json) {
//				$('blobs-'+blob_id+'_rawks').innerHTML = oXHR.responseText;
				diddlecheck();
				new Ajax.Request('/x?grab=rawks&what=lastrawk&really='+blob_id+'&last='+lastrawk[blob_id]+'&'+dude.toQueryString(), {
					method: 'get',
					onSuccess: function(x)
					{
						if (x.responseText)
						{
							var rawklist = x.responseText.split(',');
							for (var i = 0; i < rawklist.length; i++)
							{
								rawkshowlist.push('rawks-'+rawklist[i]);
								lastrawk[blob_id] = rawklist[i];
								rawkcount[blob_id]++;
							}
							rawkshow();
						}
					}
				});
			}
		};
		var update = new Ajax.Updater('blobs-'+blob_id+'_rawks', '/x?grab=rawks&what=rawkspray&really='+blob_id+'&last='+lastrawk[blob_id]+'&'+dude.toQueryString(), options);
	}

	function diddlecheck(grabbin)
	{
		if (typeof(grabbin) == 'undefined')
			grabbin = '.diddle-me';
		$$(grabbin).each(function(e){
			var the_id;
			if ((split = e.identify().split('_')) && (the_id = e.identify()) && typeof(diddlechecked.get(the_id)) == 'undefined')
			{
				var id;
				var main = split[0];
				var what = split[1];
				var split2;
				if (split2 = main.split('-'))
				{
					joint = split2[0];
					id = split2[1];
				}

				the_element = e.identify();
				var eyjacks = new Hash();
				eyjacks.set('method','get');
//				eyjacks.set('cancelControl','button');
				eyjacks.set('clickToEditText','click dis to diddle it.');
				eyjacks.set('savingText','savin\'...');
				eyjacks.set('loadingText','loadin\'...');
				eyjacks.set('highlightcolor','');
				eyjacks.set('highlightendcolor','');
				eyjacks.set('formId',the_id+'-diddlebox');
				var sendurl = new Hash();
				var loadurl = new Hash();
				var duder = dude.toQueryString()+'&';

				sendurl.set('shove',joint);
				loadurl.set('grab',joint);
				if (id)
				{
					sendurl.set('really',id);
					loadurl.set('really',id);
				}
				sendurl.set('what',what);
				loadurl.set('what',what);

				if (e.hasClassName('collect'))
				{
					eyjacks.set('loadingCollectionText','loadin\'...');
					eyjacks.set('loadCollectionURL','/x?'+duder+loadurl.toQueryString());
				}
				else
				{
					eyjacks.set('loadTextURL','/x?'+duder+loadurl.toQueryString());
				}

				if (what == 'mayor')
				{
					eyjacks.set('onComplete',function(r) { window.location=r.responseText; });
				}
//				if ((what == 'content' && joint == 'blobs') || what == 'mayor')
//				{
//					eyjacks.set('onEnterEditMode',function(r) { var time = Date(); sendurl.set('time',Date.parse(time.toString())/1000); });
///				}

				if (e.hasClassName('multi'))
				{
					eyjacks.set('rows','2');
					eyjacks.set('onFormReady',function(obj,form) {new Texpand(form.getElements().first(),{autoShrink: true, expandOnLoad: true, shrinkOnBlur: false, increment: 23});});
				}
				else
				{
					eyjacks.set('rows','1');
				}
				if (e.hasClassName('stretch'))
					eyjacks.set('formClassName','widest');
				if (e.hasClassName('remote') && (what == 'rawker' || what == 'dude' || what == 'anchorer'))
				{
					eyjacks.set('externalControl',the_id+'-link');
					eyjacks.set('externalControlOnly',true);
					if (what == 'rawker')
					{
						eyjacks.set('onEnterEditMode',function(f) {Effect.toggle($(the_id+'-wrapper'),'blind', { duration: 0.3 });});
						eyjacks.set('onLeaveEditMode',function(f) {Effect.toggle($(the_id+'-wrapper'),'blind', { duration: 0.3 });});
						eyjacks.set('onComplete',function(r) {rawkspray(id);});
					}
					else if (what == 'anchorer')
					{
						eyjacks.set('onEnterEditMode',function(f) {Effect.toggle($(the_id+'-wrapper'),'blind', { duration: 0.3 });});
						eyjacks.set('onLeaveEditMode',function(f) {Effect.toggle($(the_id+'-wrapper'),'blind', { duration: 0.3 });});
						eyjacks.set('onComplete',function(f) {
//							Effect.toggle($(the_id+'-wrapper'),'blind', { duration: 0.3 });
							$(the_id+'-baby').update(f.responseText);
						});
					}
					else if (what == 'dude' && joint == 'rawks')
					{
//						eyjacks.set('onEnterEditMode',function(f) {Effect.toggle($(the_id+'-icon'),'blind', { duration: 0.3 });});
//						eyjacks.set('onLeaveEditMode',function(f) {Effect.toggle($(the_id+'-icon'),'blind', { duration: 0.3 });});
//						eyjacks.set('onComplete',function(r) {
//							new Ajax.Request('/x?grab=rawks&what=header&really='+id+'&'+dude.toQueryString(), {
//								method: 'get',
//								onSuccess: function(x)
//								{
//									if (x.responseText)
//									{
//										$(the_id+'-wrapper').update(x.responseText);
//										diddlechecked.set(the_id,'0');
//										diddlecheck('#'+the_id);
//									}
//								}
//							});
//						});
					}
				}

				if (e.hasClassName('collect'))
					new Ajax.InPlaceCollectionEditor(the_element, '/x?'+duder+sendurl.toQueryString(), eyjacks.toObject());
				else
					new Ajax.InPlaceEditor(the_element, '/x?'+duder+sendurl.toQueryString(), eyjacks.toObject());
				diddlechecked.set(the_id,'1');
			}
		});
	}

	function rawkshow()
	{
		if (rawkshowlist.length)
		{
			var this_id = rawkshowlist.shift();
			Effect.toggle($(this_id),'blind', { 
				duration: 1.0, 
				afterFinish: function(r){ 
					Effect.Shake($(this_id), { distance: 2 });
					rawkshow();
				} 
			});
		}
	}

/*	var shaked;
	if (typeof(dude) != 'undefined')
	{
		shake(dude);
	}

	function shake(dude)
	{
		new Ajax.Request('/x?shake=true&'+dude.toQueryString(), {
			method: 'get',
			onSuccess: function(x)
			{
				response = response(x.responseText);
				if (response)
				{
					shaked = response;
				}
				else
					dude = undefined;
			}
		});

		return shaked;
	}

	function response(response)
	{
		if (response == 'null')
			response = null;
		else if (response == 'false')
			response = false;

		return response;
	}*/

//$('master').observe('click', respondToClick);
//$('test').observe('click', respondToClick);

//function respondToClick(event) {
//	alert('click'+this.identify());
//}

});
