





/**************************************************************************************************************
 objects.js 
***************************************************************************************************************/
var OBJECTS_IMAGE_PATH = '/system/cool-water/range';

function Objects(){
	var infos = { };
	this.alreadyMoved = false;

	this.init = function(options){
		Object.extend(infos, options);
		this.validateOptions();
		this.createHTMLNode();
	}

	this.validateOptions = function(){
		if (typeof infos.image == 'undefined'){  infos.image = OBJECTS_IMAGE_PATH + '/' + infos.name + '.png'; }
	}


	this.getPositions = function(){
		return {
			'back' : infos.back_percent + 'px',
			'middle' : infos.middle_percent + 'px',
			'front' : infos.front_percent + 'px'
		}
	}

	this.getMoveToRightBackground = function(){
		return infos.moveToRightBackground;
	}

	this.moveToBackground = function(collectionObjects){
		//dont do the action if not needed
		if (this.image.getStyle('left') != (infos.back_percent + 'px') ){
			var ani = { 'width' : infos.back_percent + 'px'};
			if (this.alreadyMoved == true){
				ani.left = this.oldLeftPosition + 'px';
				this.alreadyMoved = false;
			}
			jQuery( this.html.down('div') ).hide();
			jQuery(this.html).animate(ani, 450 );
			this.html.setStyle({ 'zIndex' : 1 });
		}
	}


	this.moveToFront = function(collectionObjects){
		//we move a collection to the front
		if (collectionObjects && collectionObjects instanceof Array){
			var x = 0;
			var newLeft = 0;
			collectionObjects.each(function(object){
				if (object.alreadyMoved == false){
					object.oldLeftPosition = parseInt(object.html.getStyle('left'));
					jQuery(object.html).animate({ 
						'width' : infos.front_percent + 'px', 
						'left' :  object.oldLeftPosition + newLeft + 'px' 
					});
					newLeft = newLeft + 50;
					object.alreadyMoved = true;
				}
				if ((collectionObjects.length == 3) && (x == 1)){
					object.html.setStyle({ 'zIndex' : 4 });
				}
				x++;
			}.bind(this));
		}else{
			if (this.html.getStyle('left') != (infos.front_percent + 'px') ){
				if (this.alreadyMoved == false){
					this.alreadyMoved = true;
					this.oldLeftPosition = parseInt(this.html.getStyle('left'));
					var xoffset = (infos.front_percent - infos.back_percent)/2 ;
					var leftPosition = parseInt(this.html.getStyle('left'));
					var newLeftPosition = leftPosition-xoffset;
					// console.log( xoffset );
					jQuery(this.html).animate({ 
						'width'	: infos.front_percent + 'px',
						'left'	: newLeftPosition
						}, 
						450,
						function() {
							jQuery( this ).css({ 'zIndex' : 3 });
							jQuery( '.rangetitle' ).hide();
							jQuery( '> div', this ).delay( 128 ).fadeIn();
						}
					);
					this.html
					//this.html.down('div').setStyle({ 'display' : 'block' });
					// jQuery( this.html.down('div') ).delay( 450 ).fadeIn();
				}
			} else {
				this.moveToBackground();
			}
		}
	}

	this.moveToRight = function(addLeft){
		if (this.alreadyMoved == false){
			this.oldLeftPosition = parseInt(this.html.getStyle('left'));
			jQuery(this.html).animate({ 'left' : (parseInt(this.html.getStyle('left')) +  parseInt(addLeft)) + 'px'});
			this.alreadyMoved = true;
		}
	}

	this.createHTMLNode = function(){
		this.html = OBJECTS_HTML_TEMPLATE.cloneNode(true);
		this.image = this.html.down('img');
		this.image.src = infos.image;
		this.html.down('.rangetitle').update(infos.display_name);
		this.html.show();
	}
}

