/**
 * @author Riccardo Canalicchio
 * Cantiere Creativo 2009
 */
var Carousel = Class.create({
	initialize: function(ul, current){
		this.ul = $(ul);
		this.currentIndex = current || 0;
		this.items = this.ul.select('li');
		this.length = this.items.length;
		this.selectItem(this.items[this.currentIndex]);
	},
	next: function(event){
		if(this.currentIndex<this.length-1){
			this.deselectItem(this.items[this.currentIndex]);
			this.currentIndex++;			
			this.selectItem(this.items[this.currentIndex]);
		}
		this.updateIndex();
		Event.stop(event);
	},
	previous: function(event){
		if (this.currentIndex > 0) {
			this.deselectItem(this.items[this.currentIndex]);
			this.currentIndex--;
			this.selectItem(this.items[this.currentIndex]);
		}
		this.updateIndex();
		Event.stop(event);
	},
	updateIndex:function(){},
	selectItem: function(itm){},
	deselectItem: function(itm){}
});

 
var PerleCarousel  = Class.create(Carousel,{
	selectItem: function(itm){
		$(itm).select('a img').first().morph('width:302px;height:364px;margin-top:-182px;margin-left:-151px;',
		{
			duration: 1	
		});
		var newleft = 320-305*this.currentIndex;
		this.ul.morph('left:'+newleft+'px',{
			duration: 1			
		});
		$('current').innerHTML = itm.select('a')[0].rel;
	},
	deselectItem: function(itm){
		$(itm).select('a img').first().morph('height:218px;width:181px;margin-top:-109px;margin-left:-90px;',
		{
			duration: 1	
		});
	}
});
/*
window.dhtmlHistory.create({
	toJSON: function(o) {
		return Object.toJSON(o);
	}
	, fromJSON: function(s) {
		return s.evalJSON();
	}
});

var yourListener = function(newLocation, historyData) {
	Main.load(newLocation);
}

window.onload = function() {
	dhtmlHistory.initialize();
	dhtmlHistory.addListener(yourListener);
};
*/
var Main={
	init: function(){
		$$('#top .menu li a, #bottom .menu li a, #top a').each(
			function(link){
				link.observe('click',function(event){
					Main.load(this.href);
					Event.stop(event);
				});
			}
		);
	},
	load: function(page){
		if (page != this.current) {
			this.current = page;
			var bottom=true;
			if(page=="http://www.perledibianca.com/ita/prodotti.html")
				bottom=false;
			if(page=="http://www.perledibianca.com/eng/prodotti.html")
				bottom=false;
			if(bottom)
				$('bottom').appear();
			else
				$('bottom').fade();
			new Ajax.Request(page, {
				method: "get",
				onCreate: this.loadInit,
				onComplete: this.loadComplete
			});
		}
	},
	loadInit:function(){
		$('wrap').morph('opacity:0',{queue:{scope:'loading', position:'end'}});
	},
	loadComplete:function(transport){
		$('wrap').morph('opacity:0',{
			queue:{scope:'loading', position:'end'},
			afterFinish:function(){
				$('wrap').innerHTML = transport.responseText;
				var script = $('wrap').select('script');
				if(script.size())
					eval(script.first().innerHTML);
			}
		});
		$('wrap').morph('opacity:1',{queue:{scope:'loading', position:'end'}});
	}
}

