//load!
var init=new Array();
window.addEvent('domready',function()
{
	init.each(
		function(item, index)
		{
			item();
		});
});

init.push(function()
{
	
	//first things first: the async object.
	window.popup = new Popup;
	window.async = new Async;
	
	initSearchInput($('searchInput'));
	
	registerRequiredLogin($E("div.navigation")); 
	$$('div.usermenu a.register').addEvent('click', function(event)
	{
		window.async.send($$('div.usermenu a.register').getProperty('href'));
		ev = new Event(event);
		ev.preventDefault();
	});
	
});


function initSearchInput(searchy)
{
	searchy.addEvent('focus', function(event)
	{
		searchy.removeClass('idleSearch');
		if(searchy.value=="search")
		{
			searchy.value="";
		}
	});
	
	searchy.addEvent('blur', function(event)
	{
		searchy.addClass('idleSearch');
		searchy.value="search";

	});
	
	if(searchy.value=="")
		searchy.value="search";
		
	if(searchy.value=="" || $('searchInput').value=="search")
		searchy.addClass('idleSearch');
	else
		searchy.removeClass('idleSearch');
}

function registerRequiredLogin(where)
{
	if($('loggedIn').value!=1)
	{//register login interceptors
		$(where).getElements('a.mustLogin').each(function(el)
		{
			$(el).addEvent('click', function(event)
			{
				//for some reason, some don't like preventDefault. Like IE.
				ev = new Event(event);
				try{ev.preventDefault();}
				catch(err){}
				window.async.send($(ev.target).getProperty("href")); 
				return false;
			});
		});
	}
}

function toggle(elementId)
{
	$(elementId).toggleClass('hidden');
}


function userEntryToggle()
{	

	if($("registerActionBox").hasClass('hidden'))
	{ //what do we do with the links first?
		hideLink = $('registerLink');
		fadeInLink = $('loginLink');
	}
	else
	{
		hideLink = $('loginLink');
		fadeInLink = $('registerLink');
	}
	
	hideLink.toggleClass('hidden');
	$each([ $("registerActionBox"), $("loginActionBox") ], function(el)
	{
		if(el.hasClass('hidden'))
		{
			el.removeClass('hidden');
			el.effect('opacity', {duration:200, onComplete: function()
			{
				el.getElement("input[name=username]").focus();
			}}).start(0,1);
		}
		else
		{
			el.effect('opacity', {duration:200, onComplete: function()
			{
				el.addClass('hidden');
				fadeInLink.effect('opacity').set(0);
				fadeInLink.removeClass('hidden');
				fadeInLink.effect('opacity', {duration:2000}).start(0,1);
			}}).start(1,0);
		}
	});
}

var Popup = new Class({  //the popup handler class
	options:
	{
		container: null,
		curtain: null,
		popup: null
	},
	
	initialize: function(options)
	{
		this.options={
		container: $('popupContainer'),
		curtain: $('curtains'),
		popup: $('popup')};
	
		this.options.curtain.getComputedStyle=function(property)
		{  
			var result = false;  
			if (this.currentStyle) 
				result = this.currentStyle[property.camelCase()];  
			else 
				result = window.getComputedStyle(this, null).getPropertyValue([property.hyphenate()]);  
			return result;
		};
	
		this.setOptions(options);
	
		this.options.curtain.addEvent('click', this.unpop.bind(this)); //clicking on the curtain will hide the popup.
	},
	
	wait: function(wait)
	{  
		var self=this;
		if(wait!==false)
			this.clear();
		
		this.showCurtain(
		{
			onStart: function()
			{
				this.options.popup.addClass("loading");
				this.options.container.removeClass("hidden");
			}.bind(self),
			onComplete: function()
			{
				this.options.popup.removeClass("loading");
			}.bind(self),
			fast: function()
			{
				this.options.popup.addClass("loading");
			}.bind(self)
		});
	},
	
	pop: function(content)
	{

		this.options.popup.setHTML(content);
		this.show();
		this.asynchronize();
	},
	
	show: function()
	{
		var self=this;
		this.showCurtain(
		{
			onStart:function()
			{
				this.options.container.removeClass('hidden'); 
				this.options.popup.removeClass('loading');
				this.options.popup.removeClass('hidden');
			}.bind(self),
			fast:function()
			{
				this.options.container.removeClass('hidden'); 
				this.options.popup.removeClass('loading');
				this.options.popup.removeClass('hidden');
			}.bind(self)
		});
		
	},
	
	showCurtain: function(props)
	{
		//fade in if the curtain's new
		if(this.options.curtain.hasClass('hidden') || this.options.container.hasClass('hidden'))
		{
			try{this.options.curtain.fade.stop()}catch(err){};
			this.options.curtain.fade = new Fx.Style(this.options.curtain, 'opacity', 
			{
				duration: 250, 
				onStart:function()
				{
					this.options.curtain.setStyle('opacity',0);
					props.onStart();
				}.bind(this),
				onComplete:function()
				{	
					try{onComplete();}catch(e){};
					this.options.curtain.setStyle('opacity',0.8);
				}.bind(this)
			});
			this.options.curtain.fade.start(0, 0.8);
		}
		else
		{
			try{props.fast()}catch(e){};
		}
	},
	
	unpop: function()
	{
		if($type(this.options.unpopOverride)=='function')
			this.options.unpopOverride();
		else
		{
			try{this.options.curtain.fade.stop()}catch(err){};
			this.options.curtain.fade=new Fx.Style(this.options.curtain, 'opacity',
			{
				duration:100,
				onStart:function()
				{
					this.options.popup.addClass("hidden");
					this.options.popup.setHTML("");
				}.bind(this),
				onComplete: function()
				{
					this.options.container.addClass("hidden");
					this.options.popup.removeClass("hidden");
				}.bind(this)
			}).start(0.8,0);
			
		}
	},
	
	unpopOverride: function(func)
	{
		this.options.unpopOverride=func;
	},
	
	clear: function()
	{
		this.options.popup.setHTML("");
	},
	
	cancelUnpopOverride: function()
	{
		this.options.unpopOverride=undefined;
	},
	
	asynchronize: function() //make forms and links submit asynchronously and such
	{
		$each(this.options.popup.getElementsByTagName('form'), function(el)
		{
			$(el).addEvent('submit', function(event)
			{
				ev = new Event(event);
				ev.preventDefault();
				window.async.send($(el).getProperty('action'), {data: $(el), clear:false});
			});
		
		});
	
	}
	
});
Popup.implement(new Options);

var Async = new Class({
	options:
	{
		
		
	},
	
	initialize: function(options)
	{
		this.popup = window.popup;
	},
	
	send: function(url, options)
	{	
		// figure out the options
		if(options==null)
			options={};
			
		//add "mode:async to postBody";
		if     (options.data==null)
			options.data={ 'mode' : 'async' };
		else if($type(options.data)=='element')
		{
			options.data=options.data.clone();  //make sure we're not writing to our original element.
			options.data.adopt(new Element('input', {'type':'hidden', 'name':'mode', 'value':'async'}));
		}
		else if($type(options.data)=='object')
			options.data.mode='async';
		else if($type(options.data)=='string')
			options.data=options.data + "&mode=async";			
		
		if(options.expectPopup!==false && options.wait !== false)
			this.popup.wait(options.clear); //wait on the popup
		
		var jay = null; //need this for the onComplete reference in the next few lines
		
		var defaults = //default events. can be overridden with the options.
		{
			onComplete: function(reply) 
			{	
				jason=this.processJason(reply);
				if(jason==false) //if json is invalid
					this.popup.pop(reply); //send the output to the popup with no fancy-pants processing
				else
				{
					jay.fireEvent("onJSON", [ jason ], 1);
				}
				return jason;
			}.bind(this),
			onFailure: function(reply)
			{
				this.popup.pop(reply.responseText);
			}.bind(this),
			method: 'post'
		};
		
		//extract onSuccess & onFailure

		var jay = new Ajax(url, $merge(defaults, options));
		
		if($type(options.onJSON)=="function")
		{
			//options.onJSON=options.onJSON.bind(this); //this makes onJSON execute twice!! (once without the bind(), the other with.
			jay.addEvent("onJSON", options.onJSON);
		}

		jay.request();

	},
	
	processJason: function(e)
	{
		try
		{
			var resp=Json.evaluate(e);
		}
		catch(error)
		{
			return false;
		}
		if($type(resp)!='object')
			return false;
			
		if(resp.actions!=null)
		{
			resp.actions.each(function(action)
			{
				this.processJasonAction(action);
			}, this);
		}
		return resp;
		
	},
	
	processJasonAction: function(act)
	{
		if(act.action=="popup")
		{
			this.popup.pop(act.content);
		}
		if(act.action=="refresh")
		{
			location.reload(true);
		}
		
		if(act.action=="go")
		{
			if(act.mode=="async")
				new Ajax(act.url, {method: act.method, postBody: act.data}).request();
			else
			{
				window.location=act.url;
			}
		}
	}
});
Async.implement(new Options, new Events);

//emale obfuscator
var emale=function(domain, tld, user)
{
	return ("mai" +  "l" +"to" + ":" + user + "%40" + domain + "." + tld);
}
