// RctProgress rct_progress.js v1.0.1

// Copyright (c) 2009 Ralf Kramer (http://www.progress-bar.de, http://www.progress-bar.de)
//
// RctProgress is freely distributable under the terms of an Creative Commons Attribution 3.0
// You are free to to change, copy, distribute and transmit the work
// Under the following conditions:
//  - This copyright notice is unchanged
//
// For documentation and further information visit: 
//  - http://www.progress-bar.de
//  - http://www.progress-bar.net
//
var RctProgress = Class.create({

	// width of the image on startup
	offset: 10,
	
	// max width of the progress
	length: 300,
	
	// Id of the progress bar image
	progressBar: null,
	
	// The Id of a surrounding div
	container: null,
	
	// horizontal | vertical
	direction: 'horizontal',
	
	// Id of the div that contains the progress image 
	// should be invisible, get visible when RctProgress.start() is called
	loadingMessage: 'loading-message',
	
	// Id of the container with a success message
	successMessage: 'success-message',
	
	// Id of the container with a success message
	failureMessage: 'failure-message',	
	
	// Seconds until the bar expands to full size
	duration: 2.0,
	
	// How long is the success message shown
	successMessageDuration: 3.0,
	
	// How long is the failure message shown
	failureMessageDuration: 5.0,
	
	// Allows to change the width of the progress bar
	// if left to null, the width will not be touched
	width: 10,
	
	// Allows to change the height of the progress bar
	// if left to null, the height will not be touched
	height: 10,	
	
	// internal flag, avoids that the Event.Tween Effect is invoked in an inifinte loop
	halted: false,
	
	// internal flag, indicates the status of an ajax request
	status: false,

  	initialize: function(progressBar, container, options) {
		this.progressBar = progressBar;
		this.container   = container;
		Object.extend(this, options);		
  	},

	 // increase the width of the bar
	updateHorizontal: function(val) {
		$(this.progressBar).style.width = val+'px';
		$(this.progressBar).style.height = this.height+'px';
		
		if(val == this.length && !this.halted){
			$(this.progressBar).style.width = 0;
			this.start();
		}	
	},

	 // increase the height of the bar
	updateVertical: function(val) {
		val = parseInt(val);
		$(this.progressBar).style.width = this.width+'px';
		$(this.progressBar).makePositioned();
		$(this.progressBar).style.height = val+'px';
		$(this.progressBar).style.top = (this.length - val) + 'px';
		
		if(val == this.length && !this.halted){
			$(this.progressBar).style.height = 0;
			this.start();
		}	
	},

	// increase the bar depending on the direction
	update: function(val) {
		
		if(this.direction == 'horizontal'){
			this.updateHorizontal(val);
			return;
		}
		
		if(this.direction == 'vertical'){
			this.updateVertical(val);
			return;			
		}
		
		alert('direction '+this.direction+' not supported, use horizontal or vertical');
	},
	
	// Call this method when your ajax request was successful
	success: function(){
		this.status = 'success';
	    this.stop();	
		this.hide();
	},
	
	// Call this method when your ajax request has failed
	failure: function(){
		this.status = 'failure';
		this.stop();
		this.hide();
	},	
	
	// internal function for hiding the progress bar
	// after an ajax request, and showing subsequently
	// a status message
	hide: function(message){
		if(this.container){
			$(this.container).hide();
		}else{
			$(this.progressBar).hide();
		}
	
		if(this.status == 'success'){
			message = this.successMessage;
		}else{
			message = this.failureMessage;
		}
		$(message).show();
		$(message).highlight();
		
		if(this.status == 'success'){
			$(message).highlight();
		}		
		
		$(message).fade({duration: 3.0});		
	},
	
	// Call this method to start the progress bar
	start: function() {
		
		if(this.container){
			$(this.container).show();
		}
		
		this.halted = false;
		
		if(this.height){
			$(this.progressBar).style.height = this.height;
		}
		
		if(this.width){
			$(this.progressBar).style.width = this.width;
		}		
		
		// this binding is required to transport 
		// the this.update into the inline.function in the Effect.Tween
		var transporter = this.update.bind(this);
		
		new Effect.Tween( 	null, 
							this.offset, 
							this.length, 
							{ duration: this.duration }, function(p){transporter(p)} );
	},
	
	// use this to let the progess bar run to it's
	// full length and then stop
	stop: function(){
		this.halted = true;	
	}
	

});
