if (!window.functionExists)
{
	functionExists=function(funcString)
	{
		if (typeof window[funcString]=='function') return true;
		else return false;
	};
}
if (!window.isFunction)
{
	isFunction=function(func)
	{
		if (typeof func=='function') return true;
		else return false;
	};
}

var easyOverlay=new Object();
easyOverlay={
	count:0,
	overflows:new Array(),
	close:function($overlay){
		if (this.count>1) $('#overlay'+(this.count-1)).css({overflow:this.overflows[this.count]});
		else $('body').css(this.overflows[this.count]);
		if ($overlay) $overlay.remove();
		else $('#overlay'+this.count).remove();
		this.count--;
	},
	closeButton:function(){
		$('<a href="#">Close this</a>').css({
			position:'absolute',
			top:'20px',
			right:'20px'
		}).click(function(){
			$(this).closest('div.overlay').remove();
		}).appendTo($(this));
	},
	create:function(load,data,overlayCall,submitCall,css,bg){
		if (!css) css=new Object();
		if (!css.position) css.position='relative';
		// if (!css.top) css.top='5%';
		if (!css.float) css.float='left';
		if (!css.left) css.left='25%';
		if (!css.width) css.width='50%';
		if (!css.margin) css.margin=(15+(this.count*10))+'px 0 0 0';
		if (!css.cursor) css.cursor='default';
		if (!css.padding) css.padding='10px';
		//if (!css.overflow) css.overflow='auto';
		// if (!css['max-height']) css['max-height']='90%';
		this.count++;
		css['z-index']=(this.count*5)+1;
		var overlay=this;
		var $overlay=$('<div>').css({
			position:'fixed',
			width:'100%',
			height:'100%',
			top:0,
			left:0,
			'z-index':(this.count*50),
			background:(!bg?'rgba(0,0,0,0.5)':bg),
			cursor:'pointer',
			overflow:'auto'
		}).addClass('overlay').attr({title:'Click to close',id:'overlay'+this.count}).click(function(){
			easyOverlay.close($(this));
		});
		if (this.count>1)
		{
			this.overflows[this.count]=$('#overlay'+(this.count-1)).css('overflow');
			$('#overlay'+(this.count-1)).css({overflow:'hidden'});
		}
		else
		{
			this.overflows[this.count]={overflow:$('body').css('overflow'),'overflow-x':$('body').css('overflow-x'),'overflow-y':$('body').css('overflow-y')};
			$('body').css({overflow:'hidden'});
		}
		$content=$('<div>').css(css).click(function(e){
			e.stopPropagation();
		}).appendTo($overlay);
		if (data)
		{
			if (typeof data!='object') $content.append(data);
			else $content.html(data);
			if (isFunction(overlayCall)) overlayCall($content);
			if (isFunction(submitCall)) $('form',$content).easyOverlaySubmit(submitCall);
		}
		else $content.load(load,function(){
			if (isFunction(overlayCall)) overlayCall($(this));
			if (isFunction(submitCall)) $('form',$(this)).easyOverlaySubmit(submitCall);
		});
		$overlay.appendTo($('body'));
	},
	level:function(){
		this.close();
		$context=$('#overlay'+this.count);
		return $context;
	},
	submit:function(ajax,url,$submit,callback){
		$.ajax({
			type:'POST',
			url: url,
			data: ajax,
			success: function(msg){
				console.log('easy submit returned: '+msg);
				msg=msg.split('|');
				if (msg[0]=='success')
				{
					ajax=ajax.split('&');
					temp={};
					for (i in ajax)
					{
						if (isNumeric(i))
						{
							query=ajax[i].split('=');
							temp[query[0]]=query[1];
						}
					}
					// temp is passed through as active object "this"
					callback.apply(temp,[msg,$submit]);
				}
				else alert('Error: '+msg[1]);
			}
		});
	}
};

$.fn.easyOverlay=function(overlayCall,submitCall,css)
{
	var $content;
	this.click(function(e){
		e.preventDefault();
		//this.count=$(document).data('easythis.count');
		var query=appendQuery($(this).attr('href'),'ajax=1');
		easyOverlay.create(query,false,overlayCall,submitCall,css);
		//$(document).data('easythis.count',this.count);
	});
	return this;
};

$.fn.easyOverlaySubmit=function(callback)
{
	return this.submit(function(e){
		e.preventDefault();
		var $submit=$(this).find('input[type="submit"]');
		var $form=$(this);
		var ajax=new Array();
		$form.find('input:not(.submit)').each(function(){console.log($(this));
			// we can't && the two IF statements below, as it adds the val of checkboxes which aren't ticked
			if ($(this).attr('type')=='checkbox')
			{
				if ($(this).prop('checked')==true) ajax.push($(this).attr('name')+'='+$(this).val());
			}
			else ajax.push($(this).attr('name')+'='+$(this).val());
		});
		$form.find('select').each(function(){
			if ($(this).find('option:selected').length>0) ajax.push($(this).attr('name')+'='+$(this).val());
		});
		$form.find('textarea').each(function(){
			ajax.push($(this).attr('name')+'='+$(this).val());
		});
		var url=$form.attr('action').replace('db_','ajax_').appendQuery('ajax=1');
		ajax=ajax.join('&');
		easyOverlay.submit(ajax,url,$submit,callback);
	});
};

$(document).data('easyOverlayCount',0);
