//==================================================
// system

if (!system){var system={};};
(function($,$$){

//--------------------------------------------------
// translate a phrase

// TODO complete this
$$.translate = function(phrase){return phrase;};

//--------------------------------------------------
// call an action

$$.action = function(action,data,callback){
	data.action = action;
	$.ajax({
		cache: false,
		data: data,
		type: 'POST',
		url: '/service',
		dataType: 'json',
		success: callback,
		error: function(){
			if (typeof callback=='function'){
				callback({"action":action,"result":false,"errors":["AJAX Error"],"confirm":null,"success":null});
				}
			}
		});
	};
	
//--------------------------------------------------
// action shortcuts

// email a user
$$.emailUser = function(userid,subject,message,callback){
	system.action('email_user',{id:userid,subject:subject,message:message},callback);
	};

// set user meta (for current user)
$$.setUserMeta = function(name,value,callback){
	system.action('set_usermeta',{name:name,value:value},callback);
	};

//--------------------------------------------------
// system dialog

// create the modal object
$$.modal = null;

// create the modal
$.modalCreate = $$.modalCreate = function(fade){
	
	// make sure the modal doesn't exist
	if ($$.modal){return;}
	
	// create the modal
	var $modal = $(document.createElement('div'))
		.addClass('modal');
	
	// add the modal to the DOM
	$('body')
		.css('overflow','hidden')
		.append($modal);
	$modal.css('opacity',0).fadeTo(fade,0.7);
	
	// add the modal to the system
	$$.modal = $modal;
	
	}

// delete the modal
$.modalDelete = $$.modalDelete = function(fade){
	
	// make sure the modal exists
	if (!$$.modal){return;}
	
	// remove the modal from the DOM and system
	$$.modal.fadeTo(fade,0,function(){
		
		$('body').css('overflow','auto')
		
		$$.modal.remove();
		$$.modal = null;
		
		});
	
	}

// create the dialogs object
$$.dialogs = {};

// manipulate dialogs
$.dialog = $$.dialog = function(name,method,options){
	
	// sort the arguments
	if (typeof name!='string'){return;}
	if (typeof method=='object'){options = method; method = '';}
	
	// default options
	var options = $.extend({},options);
	
	// create the dialog (if it doesn't exist)
	if (!$$.dialogs[name]){
		$$.dialogCreate(name,options);
		}
	
	// perform methods on the dialog
	switch (method){
		case 'close':
			$$.dialogClose(name,options);
		break;
		}
	
	return $$.dialogs[name];
	
	}

// create a dialog
$.dialogCreate = $$.dialogCreate = function(name,options){
	
	// sort the arguments
	if (typeof name!='string'){return;}
	
	// make sure the dialog doesn't exist
	if ($$.dialogs[name]){return;}
	
	// default options
	var options = $.extend({
		close: true,
		delay: 100,
		fade: 300,
		header: true,
		height: 450,
		title: 'New Dialog',
		type: 'selector', // type can be iframe, ajax, html, selector
		content: '', // url for use in iframe and ajax, html content, or a selector
		width: 640
		},options);
	
	// create the dialog
	var $dialog = $(document.createElement('div'))
		.attr('id','dialog-'+name)
		.addClass('dialog')
		.data('name',name);
	
	var dialogHTML = '';
	
	// add headerbar
	if (options.header){
		dialogHTML +=
		'<div class="dialog-header">'+
		'<p class="dialog-title">'+options.title+'</p>';
		if (options.close){
			dialogHTML += '<a href="" class="button button-close">'+$$.translate('close')+'</a>';
			}
		dialogHTML += '</div>';
		}
	
	// add content area
	dialogHTML += '<div class="dialog-content dialog-content-'+options.type+'" style="width:'+options.width+'px;height:'+options.height+'px;">';
	switch (options.type){
		case 'selector':
			var selection = $(options.content);
			if (options.content && selection.size()>0){
				dialogHTML += selection.html();
				};
		break; case 'ajax':
			// TODO finish this
		break; case 'iframe':
			dialogHTML += '<iframe src="'+options.content+'" width="100%" height="100%" frameborder="0" name="frame-'+name+'"></iframe>'
		break; default:
			dialogHTML += options.content;
		break;
		}
	dialogHTML += '</div>';
	
	// create the modal
	$$.modalCreate(options.fade);
	
	// add the dialog to the DOM and center
	$dialog.html(dialogHTML);
	$('body').append($dialog);
	$dialog.css({
		opacity: 0,
		marginLeft: -($dialog.width()/2),
		marginTop: -($dialog.height()/2)
		}).fadeTo(options.delay,0).fadeTo(options.fade,1);
		
	// handle the close button
	$dialog
		.find('div a.button-close')
		.click(function(){
			var $dialog = $(this).closest('div.dialog');
			$$.dialogClose($dialog.data('name'));
			return false;
			})
	
	// add the dialog to the system array
	$$.dialogs[name] = $dialog;
	
	}

// close a dialog
$.dialogClose = $$.dialogClose = function(name,options){
	
	// sort the arguments
	if (typeof name!='string'){return;}
	
	// make sure the dialog exists
	if (!$$.dialogs[name]){return;}
	
	// default options
	var options = $.extend({
		fade: 200
		},options);
	
	// remove the dialog
	var $dialog = $$.dialogs[name]
		.fadeTo(options.fade,0,function(){
			$$.dialogs[name].remove();
			$$.dialogs[name] = null;
			});
	
	// remove the modal
	$$.modalDelete(options.fade);
	
	}

// listen for the escape key
$(document).keydown(function(e){
	if (e.keyCode==27){
		$('.dialog').each(function(){
			$$.dialogClose($(this).data('name'));
			});
		}
	});



//--------------------------------------------------

}(jQuery,system));;

//==================================================

//system.setUserMeta('testing','',function(result){alert(result);});
//system.emailUser('1','Hello From Ajax','This message was sent from AJAX!',function(){});
