/**
 *
 * @author Ardeleanu Ionut
 * @langversion JAVASCRIPT
 *
 * http://www.neokinetics.ro
 * iardeleanu@neokinetics.ro
 *
 */
 
 
/*****************************************************************************************************/
/*                                                                                                   */
/*                                         PRELOADER CLASS                                           */          
/*                                                                                                   */
/*****************************************************************************************************/ 
function Preloader(){
	
	var JSObject = this;
	
	this.defaultParams = {width: 450,
						height: 120,
						message: 'Sending data'};
						
	
	/*****************************************************************************************/
	/*                                      START PRELOADER                                  */
	/*****************************************************************************************/
	/**
	 * start the loader
	 * method type: LOCAL
	 * params: @params : a JSON with new params like defaultParams
	 */
	this.start = function(params){
		
		this.defaultParams = $.extend({}, this.defaultParams, params);
		
		$('#preloader_container').remove();
		$($('body').get(0)).append('<div id="preloader_container" style="position:fixed; z-index:999998; display:none;"><div class="preloader"></div><div style="position:fixed"><table id="preloader_table" cellpading="0" cellspacing="0"><tr><td align="center" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFF; padding:25px">'+this.defaultParams.message+'<br><br><img src="'+JSInterface.localpath+'includes/images/loading_animation.gif" /></td></tr></table></div></div>');
		
		var preloader_container = $('#preloader_container');
		var table = $('#preloader_table',preloader_container);
		var preloading_bg = $('.preloader',preloader_container);
		
		var w = this.defaultParams.width;
		var h = this.defaultParams.height;
		
		table.width(w);
		table.height(h);
		table.parent().width(w);
		table.parent().height(h);
		preloading_bg.width(w);
		preloading_bg.height(h);
		preloading_bg.parent().width(w);
		preloading_bg.parent().height(h);
				
		var newW = (-w/2)+'px';
		var newH = (-h/2)+'px';
		
		//for IE 6 do not display the loader
		if($.browser.msie && $.browser.version.substring(0,1) == 6){
			//
		}
		else{
			preloader_container.css({'top':'50%' , 'left':'50%' , 'margin-left': newW, 'margin-top':newH}).fadeIn(500);	
		}	
	}
	
	
	/*****************************************************************************************/
	/*                                      UPDATE PRELOADER                                 */
	/*****************************************************************************************/
	/**
	 * update the preloader with a new message
	 * method type: LOCAL
	 * params: @msg : the new message to display
	 */
	this.update = function(msg){
		
		var preloader_container = $('#preloader_container');
		var table = $('#preloader_table',preloader_container);
		table.get(0).rows[0].cells[0].innerHTML = msg;
		
	}
	
	/*****************************************************************************************/
	/*                                      REMOVE PRELOADER                                 */
	/*****************************************************************************************/
	/**
	 * remove the loader from the stage
	 * method type: LOCAL
	 * params: @time : the time for the loader to disappear
	 */
	this.remove = function(time){
		
		if ($('#loading_container') != null){
			$('#loading_container').remove();	
		}
		
		if (time == null){
			time = 100;	
		}
		
		var preloader_container = $('#preloader_container');
		preloader_container.stop();
		
		if($.browser.msie && $.browser.version.substring(0,1) == 6){
			//
		}
		else{
			preloader_container.fadeOut({duration: time},function(){ preloader_container.remove(); });
		}
	}
	
}



/*****************************************************************************************************/
/*                                                                                                   */
/*                                            MESSAGE CLASS                                          */          
/*                                                                                                   */
/*****************************************************************************************************/
function Message(){
	var JSObject = this;
	
	this.defaultParams = {
							width: 				450,
							time: 				5000,
							speed:				500,
							delay:				500,
							message:			""};
	
	this.parentContainer;				// the message container (div element)
	this.container;						
	
	
	
	/*****************************************************************************************/
	/*                                      VIEW MESSAGE                                     */
	/*****************************************************************************************/
	/**
	 * view message
	 * method type: LOCAL
	 * params: @params : a JSON with new params like defaultParams
	 */
	this.view = function(params){
		
		this.defaultParams = $.extend({}, this.defaultParams, params);
		
		// number of messages current displayed
		var no_messages = $('div[id="messageBox"]', this.parentContainer).get().length;
		
		// add message box to the parent container
		var html = '<div id="messageBox" class="loader" style="display:none;">';
			html += 	'<table id="boxTable" cellpading="0" cellspacing="0">';
			html += 		'<tr>';
			html += 			'<td align="right" valign="middle" height="15" style="padding-right:5px; padding-top:5px">';
			html += 				'<img id="closeMsg" src="'+JSInterface.localpath+'includes/images/buttons/btn_close_msg.png" border="0" style="cursor:pointer" />';
			html += 			'</td>';
			html += 		'</tr>';
			html += 		'<tr>';
			html += 			'<td align="center" valign="middle" style="font-family:Arial, Helvetica, sans-serif; font-size:12px; color:#FFF; padding:25px; padding-top:0px">' + this.defaultParams.message +'</td>';
			html += 		'</tr>';
			html += 	'</table>';
			html += '</div>';
			html += '<div class="spacer_xs"></div>';
		
		$(this.parentContainer).append(html);
						
		this.container = $('div[id="messageBox"]:eq('+no_messages+')', this.parentContainer);
		var table = $('#boxTable',this.container);
		var close_btn = $('#closeMsg',table);
		
		// set message table width
		var w = this.defaultParams.width;
		table.width(w);
		table.parent().width(w);
		
		// open message box
		this.container.delay(this.defaultParams.delay).slideDown(this.defaultParams.speed);
		
		
		// start message timer
		this.container.unbind("countTimer");
		this.container.bind("countTimer",function(){
			
			var seconds = JSObject.defaultParams.time;
																		
			//wait until elapsed time reaches 0 seconds
			if (seconds/1000 - 1 >= 1){
				JSObject.defaultParams.time -= 1000;
			}
			else{
				$(this).unbind("countTimer");
				clearInterval($(this).data("timerInterval"));
				$(this).removeData("timerInterval");
				
				//remove the message from the stage
				JSObject.remove();
			}
			
		})
		
		this.container.data("timerInterval", setInterval(function(){ JSObject.container.trigger("countTimer")},1000));
		
		
		// close button 'click' action
		close_btn.unbind("click");
		close_btn.bind("click",function(){
			
			$(this).unbind("click");
			
			//remove the message from the stage
			JSObject.remove();
		})
	}
	
	
	/*****************************************************************************************/
	/*                                      REMOVE MESSAGE                                   */
	/*****************************************************************************************/
	/**
	 * remove message
	 * method type: LOCAL
	 * params: no params
	 */
	this.remove = function(){
		
		// close message box
		JSObject.container.slideUp(JSObject.defaultParams.speed, function(){  
																		  var spacerDiv = $(this).next();
																		  
																		  $(this).remove();
																		  
																		  // close the bottom spacer
																		  spacerDiv.slideUp(200, function(){ $(this).remove(); })
																		  });
	}
	
}


/*****************************************************************************************************/
/*                                                                                                   */
/*                                            LOADER CLASS                                           */          
/*                                                                                                   */
/*****************************************************************************************************/ 
function Loader(){
	
 	var JSObject = this;
	
	this.arr_messages = [];		// an array with messages objects
	
	
	/*****************************************************************************************/
	/*                                      INIT LOADER                                      */
	/*****************************************************************************************/
	/**
	 * initialize the Loader, and create the messages box
	 * method type: LOCAL
	 * params: no params
	 */
	this.init = function(){
		
		$('#messages_container').remove();
		$($('body').get(0)).append('<div align="center" id="messages_container" style="position:fixed; z-index:999999; width: 100%; display:block; top: 5px; margin: 0 auto;"></div>');
		
		var messages_container = $('#messages_container');
				
	}
	
	/*****************************************************************************************/
	/*                                      DISPLAY LOADER                                   */
	/*****************************************************************************************/
	/**
	 * display a new message
	 * method type: LOCAL
	 * params: @params : a JSON with params {time, width, message, speed}
	 */
	this.display = function(params){
		
		var messages_container = $('#messages_container');
		
		var newMessage = new Message();
		newMessage.parentContainer = messages_container;
		this.arr_messages.push(newMessage);
		
		newMessage.view(params);
	}
	
}
