$(document).ready(function() {

	/* 
		$.run project
		- one command instead tget and fn.tget
		- calls skeleton/run.php which includes script

		execute (eval) <script>
		load rest into id (.html() and prepend() ) 

		<xml>
			<page/>
			<message/>
			<script/>
		</xml>

	*/  

	jQuery.run = function(script, serialized, callback) {	
		
		// redefine variables if callback function but no serialised data is passed
		
		if(serialized != undefined && callback == undefined) {
			var s = serialized.toString();
			s = s.replace(/ /g, "");
			if(s.substring(0,9) == 'function(') {
				
				callback = serialized;
				serialized = undefined;				
			}
		}

		// prepend script name to passed variables

		var serialized = serialized == undefined ? 'script=../' + script : 'script=../' + script + '&' + serialized;

		/* $('#message').prepend( '<div>Loading...</div>' ); */
		msg('Working...','silver');
		
		$.get('skeleton/run.php', serialized, function(data) {
			
			// "pages"
			
			$(data).find('page').each( function() {
				if( $(this).attr('method') == 'prepend' )
					$( $(this).attr('id') ).prepend( $(this).attr('content') );
				else if( $(this).attr('method') == 'html' )
					$( $(this).attr('id') ).html( $(this).attr('content') );
				else if( $(this).attr('method') == 'append' )
					$( $(this).attr('id') ).append( $(this).attr('content') );
			});	
			
			// messages
			
			$(data).find('message').each( function() {
				$('#message').prepend( '<div style="color: ' + $(this).attr('color') + '">' + $(this).attr('content') + '</div>' );
			});		
			$('#message > div').slice(24).remove();
			
			// execute javascript
			
			$(data).find('script').each( function() {
				eval( $(this).attr('content') );
			});
			
			// execute callback function
			
			if(callback != undefined)
				callback();
		});
	}; 

});