window.addEvent('load', function(){

	//SLIDE SHOW
		var data = {
		  'storage/photos/photo1.jpg': { caption: '' }, 
		  'storage/photos/photo2.jpg': { caption: '' }, 
		  'storage/photos/photo3.jpg': { caption: '' }

		};
		var myShow = new Slideshow($('SLIDESHOW1'), data, {controller: false, preload:true, paused:false, width:269, height: 239, loader:'storage://ext/slideshow2/css/loader_black.gif'});
		
	//TOOLTIPS
		//store titles and text
		$$('a.tip').each(function(element,index) {
			var content = element.get('title').split('::');
			element.store('tip:title', content[0]);
			element.store('tip:text', content[1]);
			element.set('title','');
		});
	
		var toolTips = new Tips('.tip',{
			className: 'tipz',
			hideDelay: 50,
			showDelay: 200
			});
			
		//ADD FADE IN/OUT EFFECT
		toolTips.addEvents({
			'show': function(tip) {
				tip.fade('in');
			},
			'hide': function(tip) {
				tip.fade('out');
			}
		});
		
	//NEWS VIEWER
	var NewsViewer = new Class({
		'initialize': function(bookList){
			this.books = bookList;
			this.index = 0;
			
			this.title_el = $('book-title');
			this.author_el = $('book-author');
			this.ed_el = $('book-ed');
			this.info_el = $('book-info');
			this.text_el = $('book-text');
			this.img_el = $('book-img');
			this.date_el = $('book-date');
			
			this.nav = $('book-nav');
			this.prev_button=null;
			this.next_button=null;
			
			this.fx = new Fx.Morph($('book-content'), {duration: 'short', link:'cancel', transition: Fx.Transitions.Sine.easeIn});
			this.fx2 = new Fx.Morph($('book-thumb'), {duration: 'short', link:'cancel', transition: Fx.Transitions.Sine.easeIn});
			
		},
		'buildNavigation': function(){
			
			this.prev_button = new Element('a',{
				'href':'javascript:;',
				'class':'prev',
				'styles':{'display':((this.books.length>1)? 'block' : 'none')},
				'text':'veure suggeriment anterior',
				'events':{
					'click':function(){
						if (this.index<this.books.length-1) this.change(this.index+1);
					}.bind(this)
				}
			});
			
			this.next_button = new Element('a',{
				'href':'javascript:;',
				'class':'next',
				'styles':{'display':'none'},
				'text':'veure suggeriment posterior',
				'events':{
					'click':function(){
						if (this.index>0) this.change(this.index-1);
					}.bind(this)
				}
			});
			
			this.prev_button.inject(this.nav);
			this.next_button.inject(this.nav);
		
		},
		'change': function(i){
			if (this.index!=i){
				this.index=i;
				this.hide();
			}
		
		},
		'hide': function(){
			
			if (this.index<this.books.length-1) this.prev_button.setStyle('display','block');
			else this.prev_button.setStyle('display','none');
			
			if (this.index>0) this.next_button.setStyle('display','block');
			else this.next_button.setStyle('display','none');

			this.fx.onComplete= function(){
				this.show();
			}.bind(this);
			this.fx.start({'opacity':0});
			this.fx2.start({'opacity':0});

			
		},
		'show': function(){
			this.date_el.set('html',this.books[this.index].date);
			this.title_el.set('html',this.books[this.index].title);
			this.author_el.set('html',this.books[this.index].author);
			this.ed_el.set('html',this.books[this.index].ed);
			this.info_el.set('html',this.books[this.index].info);
			
			var max_len = 250;
			var txt = this.books[this.index].text;
			//if (txt.length>max_len) txt = txt.substr(0,max_len-3) + ' ...';
			this.text_el.set('html',txt);
			
			var img = new Element('img',{
					'src':this.books[this.index].img,
					'alt':this.books[this.index].title
			})
			this.img_el.empty();
			img.inject(this.img_el);
			this.fx.start({'opacity':1});
			this.fx2.start({'opacity':1});
		
		}
		
	});
	
	
	var newsViewer1 = new NewsViewer(books);
		newsViewer1.buildNavigation();
		
});
