// javascript

var Util = 
{
	// utility functions
	
	init : function()
	{
		$('input:password').bind('change', Util._passwordcheck);
		$('input.slug:text').bind('keyup', Util._slugupdate);
	},
	
	expand : function()
	{
		var id = $(this).attr('rel');
		if($('#'+id).is(':visible'))
		{
			$('#'+id).slideUp(250);
		}
		else
		{
			$('#'+id).slideDown(250);
		}
		return false;
	},
	
	switch_tab : function()
	{
		$(this).addClass('active').siblings().each(function(index) {
			$(this).removeClass('active');
		});
		$('.tabrel.active').hide().removeClass('active');
		$('#'+$(this).attr('rel')).show().addClass('active');
		return false;
	},
	
	
	_alert : function(message, options)
	{
		var close_on_click = (options && options.close_on_click) ? true : false;
		
		var overlay = $$('div', '', { className:'overlay'});
		overlay.width($(document).width());
		overlay.height($(document).height());
		
		var box = $$('div',
			$$('p', message, {}),
			{ className:'alertbox' });
		
		
		$('body').append(overlay).append(box);
		box.css(
			{
				top: ($(window).height()/2) - (box.height()/2),
				left: ($(window).width()/2) - (box.width()/2)
			});
		
		if(close_on_click)
		{	
			
			overlay.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				box.remove();
			});
			box.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				overlay.remove();
			});
		}
	},
	
	_popup : function(message, options)
	{
		var close_on_click = (options && options.close_on_click) ? true : false;
		var box_width = (options && options.box_width) ? options.box_width : 'auto';
		
		var overlay = $$('div', '', { className:'overlay'});
		overlay.width($(document).width());
		overlay.height($(document).height());
		
		var box = $$('div',
			$$('p', message, {}),
			{ className:'alertbox' });
		
		
		$('body').append(overlay).append(box);
		box.css(
			{
				top: ($(window).height()/2) - (box.height()/2),
				left: ($(window).width()/2) - (((box_width!='auto') ? box_width : box.width())/2),
				width: box_width+'px'
			});
		
		if(close_on_click)
		{	
			
			overlay.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				box.remove();
			});
			box.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				overlay.remove();
			});
		}
	},
	
	_passwordcheck : function()
	{
		var pw1 = $('#password1').val();
		var pw2 = $('#password2').val();
		if (pw1!='' && pw2!='' && pw1 != pw2)
		{
			$('input:password').addClass('ERROR');
			$('button:submit').attr('disabled', 'disabled');
			Util._alert('Your passwords do not match. Please review your entries for errors and resubmit.',{ close_on_click:true });
		}
		else
		{
			$('input:password').removeClass('ERROR');
			$('button:submit').removeAttr('disabled');
		}
	},
	
	_slugupdate : function()
	{
		var slug = $(this).val().replace(/[^a-zA-Z]+/g,'').toLowerCase();
		$('#slug').val(slug);
	},
	
	_window : function(message, options)
	{
		var close_on_click = (options && options.close_on_click) ? true : false;
		var box_width = (options && options.box_width) ? options.box_width : 'auto';
		var title = (options && options.title) ? options.title : 'Title Goes Here';
		
		var overlay = $$('div', '', { className:'overlay', id:'overlay'});
		overlay.width($(document).width());
		overlay.height($(document).height());
		
		var box = $$('div',
			$$('h2', title, { className:'title' }),
			$$('div', message, {}),
			{ className:'windowbox' });
			
		var close_button = $$('img', '', { className:'close', src:'/images/icons/close.png' });
		
		$('body').append(overlay).append(box);
		box.append(close_button);
		box.css(
			{
				top: ($(window).height()/2) - (box.height()/2),
				left: ($(window).width()/2) - (((box_width!='auto') ? box_width : box.width())/2)
			});
		
		if(close_on_click)
		{	
			overlay.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				box.remove();
			});
			box.css(
			{
				cursor:'pointer'
			}
			).click(function() {
				$(this).remove();
				overlay.remove();
			});
		}
		
		close_button.bind('click', function()
		{
			box.remove();
			overlay.remove();
		});
		
		return box;
	},
	
	_update : function(e)
	{
		alert(e.data.method);
	},
	
	_validate : function(w)
	{
		var submit = true;
		w.find('.REQUIRED').each(function(index) {
			if ($(this).val()=='')
			{
				$(this).addClass('ERROR');
				submit = false;
			}
			else
			{
				$(this).removeClass('ERROR');
			}
		});
		return submit;
	},
	
	_validateform : function(f)
	{
		var submit = true;
		$(f).find('.REQUIRED').each(function(index) {
			if ($(this).val()=='')
			{
				$(this).addClass('ERROR');
				submit = false;
			}
			else
			{
				$(this).removeClass('ERROR');
			}
		});
		if(!submit) Util._alert('Fields highlighted in red are required. Please review your entries for errors and resubmit.',{ close_on_click:true });
		return submit;
	},
	
	_submit : function(w,o)
	{
		var variables = new Array();
		w.find('input,select,textarea').each(function(index)
		{
			var o = new Object();
			o.name = $(this).attr('id');
			o.value = $(this).val();
			variables.push(o);
		});
		
		$.post(
			'/ajax/submit',
			{ id:o.row_id, type:o.type, tablename: o.tablename, variables: JSON.stringify(variables)},
			function(resp)
			{
				//stuff to do *after* page response
				w.trigger('SUBMITTED',[ resp ]).remove();
				$('#overlay').remove();
				
			}
		);
	}
}

$(document).ready(Util.init);

