/*
	CUSTOM FUNCTIONS
*/
$.fn.LOADING = function() { 
	var loading_gif='<img src="http://mydash.aiwebsystems.com/stylesheets/images/big_loader.gif" />';
    return $(this).append('<div class="loading_div">Loading</div>'); 
}

/*
	UTILITIES
*/

function now() { return (new Date).getTime(); }

function fit_ta(id, maxHeight){
	var text = id && id.style ? id : document.getElementById(id);
   	if(!text) return;

   	var adjustedHeight = text.clientHeight;
   	if (!maxHeight || maxHeight > adjustedHeight){
    	adjustedHeight = Math.max(text.scrollHeight, adjustedHeight);
      	
		if(maxHeight) adjustedHeight = Math.min(maxHeight, adjustedHeight);
      	if(adjustedHeight > text.clientHeight) text.style.height = adjustedHeight + "px";
	}
}

function _OPEN_ADDRESS_BOOK(returnto){
	if(returnto==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/address_book/open/'+escape(returnto) });
}

function _RELOAD_ADDRESS_BOOK(page, query, returnto){
	var datastring='&page='+escape(page)+'&query='+escape(query);
	
	$('#_ADDRESS_BOOK_WRAPPER').LOADING();
	
	$.ajax({
		type: "POST",
		url: 'http://mydash.aiwebsystems.com/address_book/open/'+escape(returnto),
		data: "bust="+Date()+datastring,
		dataType: "html",
		cache: false,
		success: function(html){ 			
			$('#_ADDRESS_BOOK_WRAPPER').html(html);
		}
	});
}


function _NEW_COMMENT(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/comment/new_/'+escape(location)+'/'+escape(id) });
}
function _VIEW_COMMENT(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/comment/load/'+escape(location)+'/'+escape(id) });
}

function _REPLY(put, location, location_id, id){
	$('#reply_form').remove();
	var datastring='&location='+escape(location)+'&location_id='+escape(location_id)+'&parent_id='+escape(id);
	$.ajax({
		type: "POST",
		url: 'http://mydash.aiwebsystems.com/comment/reply/',
		data: "bust="+Date()+datastring,
		dataType: "html",
		cache: false,
		success: function(html){ 			
			$('#'+put).after(html);
		}
	});
}

function _DELETE_REPLY(id, parent, location){
	var agree = confirm('Are you sure you want to delete this reply?  \n\This can only be undone by tech support.\n');
	if(!agree){ return false; }
	var datastring='&comment_id='+escape(id)+'&parent_id='+escape(parent)+'&Delete=yup';
	$.ajax({
		type: "POST",
		url: 'http://mydash.aiwebsystems.com/comment/delete_reply/',
		data: "bust="+Date()+datastring,
		dataType: "json",
		cache: false,
		success: function(json){ 			
			if(json.success=='true'){
				_COMMENT_SAVED(json.location_id, location);
			}
		}
	});
}



function _NEW_TASK(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/task/new_/'+escape(location)+'/'+escape(id) });
}
function _VIEW_TASK(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/task/load/'+escape(location)+'/'+escape(id) });
}



function _NEW_WORK_LOG(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/work/new_/'+escape(location)+'/'+escape(id) });
}
function _VIEW_WORK_LOG(location, id){
	if(id==''){ return false; }
	if(location==''){ return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/work/load/'+escape(location)+'/'+escape(id) });
}



function _NEW_FILE(location, location_id){
	if(location==''){ $.jGrowl('No location passed.'); return false; }
	if(location_id==''){ $.jGrowl('No location indentifier passed.'); return false; }
	jQuery.facebox({ ajax: 'http://mydash.aiwebsystems.com/file/uploader/'+escape(location)+'/'+escape(location_id) });
}

function _DELETE_FILE(location, location_id, filename){
	if(location==''){ $.jGrowl('No location passed.'); return false; }
	if(filename==''){ $.jGrowl('No file passed.'); return false; }
	
	var agree = confirm('Are you sure you want to delete '+filename+'?\n\nThis action can not be undone.\n\n');
	
	if(agree){
		var datastring='&location='+escape(location);
	
		$.ajax({
			type: "POST",
			url: 'http://mydash.aiwebsystems.com/file/delete/',
			data: "bust="+Date()+datastring,
			dataType: "html",
			cache: false,
			success: function(html){ 			
				_FILE_DELETED(location_id);
			}
		});
	}
}
