//////////////////////////
//  NEWJAX APPLICATION  //
//  BY DIMITRI BOYD     //
//////////////////////////


var url = location.pathname.split('/');

var newjax = {

	type: 'Application',
	name: 'Newjax',
	author: 'Dimitri Boyd',
	date: '12.18.2008',

	open_search: false,
	clicker: new Array(),
	clicked: '',
	mc: 0,
	mt: 0,
	inc: 0,
	lm: 'false',
	
	selectedList: '',
	selected: '',
	selected2: '',
	shiftKey: false,
	selected3: '',
	receiveComplete: false,
	stopReceive: false,
	attachmentCount: 0,
	current_page: 'orders',
	actions_page: 'index.php',
	account_id: '',
	loading_div: '<div class="loading_div"></div>',
	script_path: location.protocol+'//www.freshlunches.com/'+url[1]+'/lib/',
	script_path2:'',



	update: function(div, page, params, callback, loading) {
		loading = 0;
		if (loading) $('loading_div_container').innerHTML = newjax.loading_div;
		if ( typeof(callback) == "undefined" ) { callback=function(){}; }
				  
		new Ajax.Request(
			page,
			{
				method: 'get',
				parameters: params,
				onComplete: function(t) {
					
					if ($(div)) $(div).innerHTML = t.responseText; 
					if ($('loading_div_container')) $('loading_div_container').innerHTML = '';
					callback(t);

				}
			}
		);

	},

	update2: function(div, page, params, callback, loading) {
		loading = 0;
		if (loading) $(div).innerHTML = newjax.loading_div;
		if ( typeof(callback) == "undefined" ) { callback=function(){}; }
		  
		new Ajax.Update(
			div,
			page,
			{
				method: 'get',
				parameters: params,
				onSuccess: function(t) {
					if ($('loading_div_container')) $('loading_div_container').innerHTML = '';
					callback(t);

				}
			}
		);

	},

	expand_log: function() {
		$('account_status_div').innerHTML = '';
		$j('#account_log_div').slideToggle('slow');
	},

	page: function(qs) {

		qs += '&action=page'; 
		newjax.selectedList = '';
		newjax.update('list_messages_div', newjax.actions_page, qs);

	},

	sort: function(sorter, order) {
	
		var data = '';
	
		if (newjax.open_search == true) data += $('search_form').serialize();
		if (data) data += '&';
		  
		if (newjax.open_search == true) data += 'action=search';
		if (data) data += '&';
		  
		if (newjax.open_search == false) data += 'action=view&div='+newjax.current_page+'';
		if (data) data += '&';
		  
		data += 'sorter='+sorter+'&order='+order+'';
		  
		newjax.update('list_'+newjax.current_page+'_div', newjax.actions_page, data, 1);
	
	},

	post_form: function(name) {
		
		tinyMCE.triggerSave(false,true);
		tinyMCE.execCommand("mceRemoveControl", true, 'body_');
		$(name).submit();
		
	},

	set_form_value: function(name, value, id) {

		$('searchTerm'+id).value = value;
		$('searchLabel'+id).innerHTML = name;

		var menu = 'search_menu'+id;

		$(menu).style.top = '-1000px';

		
	},
	
	debug: function(params) {
		
		if ( typeof(params) == 'undefined' ) params = {}
		
		for (var key in params) {
			if ( typeof(params[key]) == 'string' || typeof(params[key]) == 'number' ) {
				//params[key] = params[key];
				alert(key + ' ' + params[key]);
			}
		}
		
	
		
	},

	/* SCRIPT LOADER */
	appendScript: function() {
	
		//if (!document.getElementById) return;
	
		var activeScripts = '';
		var script = '';
		var head = document.getElementsByTagName("head")[0];
	
		for (i=0; i < arguments.length; i++) {
		
			var file = arguments[i];
	
			//Check to see if added
			if (activeScripts.indexOf(file) == -1) { 
				
				//JS
				if (file.indexOf(".js") != -1) {
	
					script = document.createElement('script');
					script.setAttribute("type","text/javascript");
					script.setAttribute("src", file);
				
				//CSS
				} else if (file.indexOf(".css") != -1) { 
	
					script = document.createElement("link")
					script.setAttribute("rel", "stylesheet");
					script.setAttribute("type", "text/css");
					script.setAttribute("href", file);
					
				}
			}
		
			if (typeof(script) == 'object') {
			
				//Add
				head.appendChild(script);
	
				//Remember as added
				activeScripts += file + '\n';
				
			}
		}
	},

	/* EVENTS */
	loadEvents: function() {
		
		onkeypress = function(e) {

			if (newjax.current_page != 'login') return;


			if (e.keyCode == 13) $('login_form').submit();
		
		}

	}

}


/* LOAD APP ( DOESNT WORK IN SAFARI ON RELOAD ) 
newjax.appendScript(
	newjax.script_path+'lib/jsapi/calendar.js',
	newjax.script_path+'lib/jsapi/orders.js'
);
*/


/* LOAD EVENTS */
newjax.loadEvents();
//alert(location.protocol);
//alert(newjax.script_path);


/* AFTER LOADING */
Event.observe(window, 'load', function() {
	//
});





