/**
 * 
 * 
 * @category    Unic
 * @package     Unic_Powerwidget
 * @author      Monica Frisoni
 * @version     0.1
 */
var UnicPowerWidget = Class.create();

UnicPowerWidget.prototype = {

	activeContainers: [],
	// list of elements ids
	activeElements: [],
	closingTimeOut: null,
	closingDelay: 1,
	// the index of the current shown div element
	currentElement: 0,
	// the div elements size
	elementsSize: 0,
	
	currentProductsList: null,
	
	powerwidgetUrl: null,
//	productListId: 'powerwidget-products',
	
	initialize: function( hash ){
		this.hash = hash;
	},
	
	// containerList: an array with the container element ids
	// to be shown
	initContainers: function(containersList){
		this.activeContainers = containersList;
	},
	
	initElements: function(elementsList){
		this.activeElements = elementsList;
		this.elementsSize = elementsList.length;
		
		// random number between 0 and elementsSize
		this.currentElement = parseInt(Math.random() * (this.elementsSize));
		this.currentElement = this.currentElement % this.elementsSize;
		// animate element
		var element = $(this.activeElements[this.currentElement]);
		var bup = {
			duration: 1,
			scaleFrom: 0,
			scaleTo: 100,
			from: 0,
			to: 1,
			queue:{ position: 'end', scope: 'pwinit'+this.hash},
			transition: Effect.Transitions.sinoidal,
			beforeSetup: function(e){ e.element.show(); },
			afterFinish: function(e){ e.element.show(); }
		};
		var info, teaser;
		if( info = element.down('.pw-info-container')){ info.hide().fade(bup); }
		if( teaser = element.down('.pw-teaser')){ teaser.hide().blindUp(bup); }
		element.fade({
			duration: 2.0,
			from: 0,
			to: 1,
			delay: 1.5,
			transition: Effect.Transitions.sinoidal,
			beforeStart:function(){element.setStyle({opacity:0}).show();}, queue:{ position: 'front', scope: 'pwinit'+this.hash}
		});
	},
	
	displayElement: function(id){
		var obj;
		if(id != null && (obj = $(id)) != null){
			obj.show();
		}
	},
	
	hideElement: function(id){
		var obj;
		if(id != null && (obj = $(id)) != null){
			obj.hide();
		}
	},
	
	prevElement: function(){

		this.hideCurrent();
		
		this.currentElement -= 1;
		if(this.currentElement == -1){
			this.currentElement = this.elementsSize - 1;
		}
		this.showProductList();
		this.showCurrent();

	},
	
	nextElement: function(){
		this.hideCurrent();
		this.currentElement += 1;

		this.currentElement = (this.currentElement % this.elementsSize);
		this.showProductList();
		this.showCurrent();
	},
	
	
	loadProductsList: function(){
		if(this.powerwidgetUrl != null && 
				this.powerwidgetUrl != null && 
				$('powerwidget-products') != null){
			var id = this.activeElements[this.currentElement];
			var options = {
					method: 'get',
					evalJS: 'false',
					onSuccess: function(transport){
										
						var html = transport.responseText;
						$('powerwidget-products').update(html);
						
					}
				};
			var url = this.powerwidgetUrl + '?id=' + id;

			new Ajax.Request(url, options);
		}
	},

	showProductList: function(){
		
		if(this.currentProductsList != null && 
				$(this.currentProductsList) != null){
		
			$(this.currentProductsList).hide();
			this.currentProductsList = null;
		}
		
		var elementId = this.activeElements[this.currentElement];
		
		if($('product-list-' + elementId) != null){
			this.currentProductsList = 'product-list-' + elementId;
			$(this.currentProductsList).show();
		}
	},
	
	hideCurrent: function(){
		var hideId = this.activeElements[this.currentElement];
		var hideObj;
		if(hideId != null && (hideObj = $(hideId))){
			hideObj.hide();
		}
	},
	
	showCurrent: function(){
		var showId = this.activeElements[this.currentElement];
		var showObj ;
		if(showId != null && (showObj = $(showId))){
			showObj.show();
			
			if($('pw-item-switch') != null){
				var image = '';
				if (showObj.hasClassName('red')) {
					image = 'red';
				}

				if (showObj.hasClassName('white')) {
					image = 'white';
				}

				if (showObj.hasClassName('black')) {
					image = 'black';
				}

				$('pw-item-switch').className = image;
			}
		}
	},	
	
	setClosingDelay: function(delay){
		this.closingDelay = delay;
	},
	
	setPowerwidgetUrl: function(url){
		this.powerwidgetUrl = url;
	},
	
	validateMouseEvent: function( id, event){
		return ( typeof id != 'undefined' &&
			id != null && 
			( ( event.relatedTarget && event.relatedTarget != id && !$(event.relatedTarget).descendantOf(id) ) ||
			  ( event.toElement && event.toElement != id && !$(event.toElement).descendantOf(id) )
			)
		);
	},

	showContainer : function(id, event) {
		if( this.validateMouseEvent( $(id) , event))
			if( !$(id).hasClassName('expanded') && !$(id).hasClassName('expanding') && !$(id).hasClassName('contracting') ) {
				this.closeAll();
				$(id).addClassName('expanded').addClassName('expanding');
				new Effect.SlideDown($(id).down(),{
					duration: .3 ,
					scaleFrom: 0,
					scaleContent: false,
					queue: { position: 'end', scope: 'powerwidget'+this.hash },
					afterFinish: function(ef){ 
						$(ef.element).up().removeClassName('expanding');
					}
				});
			} else if($(id).hasClassName('contracting') && (queue = Effect.Queues.get('powerwidget'+this.hash)).effects.length > 0 ) {
				$(id).removeClassName('contracting');
				queue.each(function(effect) { effect.cancel(); });
			}
	},

	hideContainer : function(id, event) {
		if( this.validateMouseEvent( $(id), event))
			if( $(id).hasClassName('expanded') && !$(id).hasClassName('contracting') ){
				new Effect.SlideUp($(id).addClassName('contracting').down(),{
					duration: .3 ,
					scaleFrom: 100,
					delay: .6,
					queue: { position: 'end', scope: 'powerwidget'+this.hash },
					afterFinish: function(ef){ 
						$(ef.element).setStyle({'height':'auto'}).up().removeClassName('expanded').removeClassName('contracting');
					}
				});
			} else {
				$(id).removeClassName('expanding');
				Effect.Queues.get('powerwidget'+this.hash).each(function(effect) { effect.cancel(); });
				new Effect.SlideUp($(id).addClassName('contracting').down(),{
					duration: .3 ,
					scaleFrom: 100,
					queue: { position: 'end', scope: 'powerwidget'+this.hash },
					afterFinish: function(ef){ 
						$(ef.element).setStyle({'height':'auto'}).up().removeClassName('expanded').removeClassName('contracting');
					}
				});
			}
	},
	
	closeAll: function(){
		if(this.closingTimeOut != null){
			window.clearTimeout(this.closingTimeOut);
		}
		for(var i=0; i<this.activeContainers.length; i++){
			this.close(this.activeContainers[i]);
		}
	},
		
	close: function(id){
		if(id != null && $(id) != null && $(id).hasClassName('expanded')) {
			$(id).removeClassName('expanded').down().hide(); 
		}
	}
};


