// ********************************************************************************************************************
// These are the constants used on this page - error messages, defaults, etc.
var waiting = [];

// ********************************************************************************************************************
// These functions deal with the UI itself - getting values to send, putting values in, showing user feedback, etc.
$(document).ready(function() {
	$("#scroller").simplyScroll({
		autoMode: 'loop'
	});
	
  	$('#content').delegate('form textarea','keypress', function() {
		var that = this;
		setTimeout(function() {
			while(that.scrollHeight > (that.offsetHeight - (parseInt($(that).css('paddingTop')) + parseInt($(that).css('paddingBottom'))))) that.rows++;
		}, 10);
	});
	
	$('#content').delegate('form ul.checkboxes li,form ul.checkboxes-short li','click', function() {
		var c = $(this).children('input.checkbox');
		c.attr('checked',!c.attr('checked'));
	});	
	
	$('#content').delegate('.close-box,.float-over','click',function(e){
		if(!e.isPropagationStopped()){
			close_modal();
		}
	});
	
	$('#content').delegate('.float-over .table-container','click',function(e){
		e.stopImmediatePropagation();
		e.stopPropagation();
	});
	
	setTimeout(check_n_click,500);
	
});

var check_n_click = function(){
	var loc = window.location.href.split('#');
	if(loc.length>1 && loc[1]!='') {
		var parts = loc[1].split('&');
		$('a[href=#'+parts[0]+']').click();
		if(parts.length > 1) {
			waiting.push(function(){
				var item = parts[1].split('/');
				$('#'+parts[0]+' li.'+item[0]+'-item').each(function(i,el){
					var e = $(el);
					var data = e.data('event');
					if(data && data.event_id==item[1]) {
						e.find('.'+parts[2]).click();
					}
				});
			});
		}
	}
};

var display_modal = function(el) {
	$('#content').append(template('modal-cover',{})).append($(el));
};

var close_modal = function(el) {
	$('#content').find('.cover').remove();
	$('#content').find('.float-over').remove();
};

var confirm_delete = function(info, exec){
	delete_el = $(template('confirm-delete',info));
	delete_el.find('.confirm-delete-cancel-click').click(close_modal);
	delete_el.find('.confirm-delete-submit-action').click(function(){exec();close_modal();});
	display_modal(delete_el);
};

var add_to_portal = function(prefix, name) {
	return function(data) {
		var list = $(prefix+' .'+name+'-list').empty();
		for(i=0;i<data.length;i++){
			var item_data = data[i];
			var item = $(template(name+'-item',item_data));
			item.data(name,item_data);
			list.append(item);
		}
	};
};

// ********************************************************************************************************************
// These functions make calls to the APIs


// ********************************************************************************************************************
// These functions are called by the APIs, and manage the data on the client side


// ********************************************************************************************************************
// These functions kick everything off
