/*
	TODO:
	- pre-load immagini di tutti i temi
*/
function LisarIntro(selector, cookieName) {
	this.selector = selector;
	this.cookieName = cookieName;
}
LisarIntro.prototype.init = function(force) {
	this.target = jQuery(this.selector).first();
	var _lisarIntro = this;
	
	var introNumber = 0;
	this.target.each(function(){
		this._lisarIntroParams = MyLib.getParams(this,
			[
				'autostart', 'start-fade-time', 'end-fade-time', 'step-time', 'fade-time', 'steps',
				'end-pause', 'slow-down-time', 'slow-down-start', 'slow-down-ratio',
				'force'
			],
			{'fade-time': 0, 'end-pause': 0, 'steps': -1, 'force': false}
		);
		var cookieName = _lisarIntro.cookieName + '-' + introNumber;
		this._lisarHasCookie = jQuery.cookie(cookieName);
		force = force || this._lisarIntroParams.force;
		if (!this._lisarHasCookie) {
			// jQuery.cookie(cookieName,"true", { expires: 30 });
			jQuery.cookie(cookieName,"true");
		}
		this._lisarIntroSkip = (this._lisarHasCookie || this._lisarIntroParams.autostart != 'true') && (!force) ;
		if (this._lisarIntroSkip) {
			jQuery(this).find('.loader').show();
			_lisarIntro.target.hide();
		} else {
			jQuery(this).find('.loader').show();
			jQuery(this).find('div.background').hide();
			jQuery(this).find('div.images img').hide();
		}
		introNumber++;
	});
}
LisarIntro.prototype.start = function(callback, force) {
	var _lisarIntro = this;

	this.target.each(function(){
		if (!this._lisarIntroSkip) {
			var animationElem = jQuery(this);
			animationElem.find('.loader').hide();
			animationElem.find('div.background').css({
				'z-index': -1
			});
			var images = animationElem.find('div.images img');
			if (images.length > 2) {
				var scene = Scene(this);
				if (animationElem.attr('data-order') == 'random') {
					MyLib.shuffleArray(images, 0, -1);
				}
				var params = this._lisarIntroParams;
				
				var skip = false;
				animationElem.find('.images').click(function(){
					skip = true;
				});
				animationElem.find('.js-skipMsg').click(function(){
					skip = true;
				});
				
				var curImage = images[0];
				scene.exec(function(scene) {
					jQuery('.page_ext').attr('style', 'opacity: 0');
					/*jQuery('.page_ext').animate({'opacity':0},params['end-fade-time']);*/
				}).fadeIn(params['start-fade-time'], jQuery(curImage)).after(params['step-time']);
				var i = 1;
				var slowDownStart = (new Date()).getTime() + params['start-fade-time'] + params['slow-down-start'];
				var sceneStep = function(scene) {
					if (skip) {
						scene.exec(function(scene){
							jQuery(curImage).fadeOut(params['end-fade-time']);
							jQuery(images[images.length - 1]).fadeIn(params['end-fade-time']);
							_lisarIntro.target.find('div.background').fadeIn(params['end-fade-time']);
						}).exec(function(scene) {
							jQuery('.page_ext').attr('style', 'opacity: 1');
							//pageExt.css({'opacity':1});
							/*
							var pageExt = jQuery('.page_ext');
							pageExt.animate({'opacity':1},params['end-fade-time'], function() {
								pageExt.show();
							});
							*/
						}).after(params['end-pause'] + params['end-fade-time']).fadeOut(params['end-fade-time']).exec(function() {
							jQuery(images[images.length - 1]).hide();
							if (callback) {
								callback();
								window.location = "it/home.html";
							}
						});
					} else {
						var prevImage = curImage;
						curImage = images[i];
						var nextImage = curImage;
						var fadeTime = params['fade-time'];
						var stepTime = params['step-time'];
						var slowDownRatio = params['slow-down-ratio'];
						var curFadeTime, curStepTime;
						var enlapsedTime = (new Date()).getTime() - slowDownStart;
						if (enlapsedTime <= 0) {
							curFadeTime = fadeTime;
							curStepTime = stepTime;
						} else if (enlapsedTime > params['slow-down-time']) {
							curFadeTime = fadeTime * slowDownRatio;
							curStepTime = stepTime * slowDownRatio;
						} else {
							curFadeTime = fadeTime + (fadeTime * (slowDownRatio - 1)) * enlapsedTime / params['slow-down-time'];
							curStepTime = stepTime + (stepTime * (slowDownRatio - 1)) * enlapsedTime / params['slow-down-time'];
						}
						scene.fadeTo(curFadeTime, jQuery(prevImage), jQuery(nextImage)).after(curStepTime);
						i++;
						if (i >= images.length - 1) {
							i = 0;
						}
						scene.exec(sceneStep);
					}
				}
				scene.exec(sceneStep);
				scene.start();
			}
		} else {
			if (callback) {
				callback();
				window.location = "it/home.html";
			}
		}
	});
}
LisarIntro.prototype.run = function(callback) {
	this.target = jQuery(this.selector);
	var _lisarIntro = this;
	this.target.each(function() {
		var params = MyLib.getParams(this,
			['start-fade-time'], {}
		);
		jQuery(this).fadeIn( params['start-fade-time'], function(){
			_lisarIntro.init(true);
			_lisarIntro.start(callback, true);
		});
	});
}

