/**
 * main_offer.js
 * 
 */

var myLibriMainOffer;

/**
 * Libri main offer options
 * 
 */
LibriMainOfferOptions = Object.extend({
	imagePath:'/img/main_offer/',
	currentHighlight : 1,
	autoPlay: true,
	autoPlayCurrent: 0,
	autoPlaySwitch: 5,
	autoPlayDelay: 1
	}, window.LibriMainOfferOptions || {});

/**
 * Libri main offer class
 *
 */
var LibriMainOffer = Class.create();

LibriMainOffer.prototype = Object.extend(new LibriCommon(), {
	items: [],
	imagePath: '',
	currentHighlight: 0,
	autoPlay: true,
	autoPlayCurrent: 0,
	autoPlaySwitch: 0,
	autoPlayDelay: 0,

	initialize: function() {
		this.items = LibriMainOfferOptions.items;
		this.imagePath = LibriMainOfferOptions.imagePath;
		this.currentHighlight = LibriMainOfferOptions.currentHighlight;
		this.autoPlay = LibriMainOfferOptions.autoPlay;
		this.autoPlayCurrent = LibriMainOfferOptions.autoPlayCurrent;
		this.autoPlaySwitch = LibriMainOfferOptions.autoPlaySwitch;
		this.autoPlayDelay = LibriMainOfferOptions.autoPlayDelay;

		if (typeof(this.items) != 'object' || this.items.length != 6) {
			return;
		}
		
		for (var i=0; i<=5; i++) {
			
			$("lmo"+(i+1)).innerHTML= this.createTitle(this.items[i], (i+1));
			$("lmo"+(i+1)).objid= i+1;
			//$("lmo"+(i+1)).onmouseover = function(){myLibriMainOffer.doHighlight(this.objid);};
			//$("lmo"+(i+1)).onmouseout = function(){myLibriMainOffer.doUnHighlight(this.objid);};
			//$("lmo"+(i+1)).onclick = function(){window.location = myLibriMainOffer.items[this.objid-1].url;}
			$("lmo"+(i+1)).onclick = function(){myLibriMainOffer.doHighlight(this.objid);};
			if (i!=0) {
				new Effect.Opacity('mainOfferImg'+(i+1), { duration: 0, from: 1, to: 0});
			}
		}
		
		$('offerPicture').src = this.items[0].picture;
		$('libriMainOffer').show();
		//Effect.BlindDown('libriMainOffer', { duration: 1.0 });

		new PeriodicalExecuter(function(){myLibriMainOffer.doAutoPlay();}, this.autoPlayDelay);
	},

	createTitle: function(obj, nr) {
		var image="itembg_selected.png";
		if (nr==1) { image = "itembg_selected_first.png"; }
		if (nr==6) { image = "itembg_selected_last.png"; }
		var html='<div class="mainOfferOver"><img id="mainOfferImg'+nr+'" src="'+this.imagePath+''+image+'">'+obj.displayTitle+'</div>';
		return html;
	},
	
	doHighlight: function(objid) {
		//this.autoPlay = false;

		if (this.currentHighlight == objid) {
			return;
		}
		this.autoPlay = false;
		new Effect.Opacity('mainOfferImg' + this.currentHighlight, {duration: 0.1, to: 0});
		new Effect.Opacity('mainOfferImg' + objid, {duration: 0.1, to: 1, afterFinish: this.onFinish});
		this.currentHighlight = objid;
		
		this.fadeBigPicture();
	},

	doUnHighlight: function() {
		myLibriMainOffer.autoPlay = true;
	},

	onFinish: function(obj) {
		/*console.log(obj.element.id.substr(12,1));*/
	},

	fadeBigPicture:function() {
		new Effect.Opacity('offerPicture', {duration:0.5, to:0,afterFinish: this.onFadeFinish});
	},

	onFadeFinish:function() {

		$('offerPicture').src = myLibriMainOffer.items[myLibriMainOffer.currentHighlight-1].picture;
		$('offerPicture').setStyle({'cursor':'pointer'});
		$('offerPicture').onclick = function() { window.location = myLibriMainOffer.items[myLibriMainOffer.currentHighlight-1].url; }

		new Effect.Opacity('offerPicture', { duration:0.5, from:0, to:1 });
		myLibriMainOffer.autoPlay = true;
	},

	doAutoPlay: function() {
		if (this.autoPlay == true) {
			this.autoPlayCurrent++;

			if (this.autoPlayCurrent > this.autoPlaySwitch) {
				this.autoPlayCurrent = 0;
				new Effect.Opacity('mainOfferImg'+this.currentHighlight, {duration: 0.1, to: 0});
				this.currentHighlight++;

				if (this.currentHighlight > 6) {
					this.currentHighlight=1;
				}
				new Effect.Opacity('mainOfferImg'+this.currentHighlight, {duration: 0.1, to: 1, afterFinish: this.onFinish});
				this.fadeBigPicture();
			}
		}
	}
});

function createLibriMainOffer() {
	myLibriMainOffer = new LibriMainOffer;
}

addOnLoadListener(createLibriMainOffer, {});