/*------------------------------------------------------------------------
# JOOFORGE.com - Il Web All'Italiana
# ------------------------------------------------------------------------
# Copyright © 2004-2009 JOOFORGE.com. Tutti i diritti riservati.
# Website:  http://www.jooforge.com/
# Project: jPod v.1.0 (& SoundManager v.2.95a.20090717)
-------------------------------------------------------------------------*/

var jPod = {
	durata: 6000,
	switchtime: 800,
	times: 1,
	start: function () {
		jPod.changeCover($('jf-jpod').getElement('li.track1'));
		jPod.movingBg.delay(1000);
		jPod.menu();
		$('jpodmenu').addEvent("click", jPod.backMenu);
		$('jpodplay').addEvent("click", jPod.playPause);
		$('jpodnext').addEvent("click", jPod.nextPrevSong);
		$('jpodprev').addEvent("click", jPod.nextPrevSong)
	},
	getTrackList: function () {
		var d = new Array();
		$$("#jf-jpod li").each(function (a, b) {
			var c = a.getElement("span.name").innerHTML;
			d[d.length] = c.slice(c.search(/\|/) + 1, c.length)
		});
		return d
	},
	getTrackNumber: function () {
		var a = $$("div#jf-jpod li");
		for (var i = 0; i < a.length; i++) {
			if (a[i].hasClass('selected')) return i
		}
	},
	backMenu: function () {
		var a = $('jpod-rail1');
		if (a.getStyle("left").toInt() == -119) new Fx.Style(a, 'left', {
			duration: 300,
			transition: Fx.Transitions.Quad.easeInOut
		}).start(-119, 0)
	},
	playPause: function () {
		($defined(jPod.mp3) && jPod.mp3.playState == 1) ? jPod.mp3.togglePause() : jPod.playscreen(this)
	},
	newSong: function (a) {
		if (!$defined(jPod.mp3) || jPod.mp3.playState == 1) {
			soundManager.destroySound('jPod')
		}
		jPod.mp3 = soundManager.createSound({
			id: 'jPod',
			url: 'media/jukebox/' + a,
			onfinish: function () {
				jPod.backMenu()
			}
		});
		jPod.mp3.play()
	},
	nextPrevSong: function () {
		var a = false;
		if (this.id == "jpodnext" && jPod.track < jPod.getTrackList().length - 1) {
			jPod.track++;
			a = true
		} else if (this.id == "jpodprev" && jPod.track > 0) {
			jPod.track--;
			a = true
		}
		if (a) {
			var b = $$("#jf-jpod li")[jPod.track];
			jPod.newSelected(b);
			jPod.changeCover(b);
			new Fx.Style($('jpod-rail2'), 'top', {
				duration: 300,
				transition: Fx.Transitions.Quad.easeInOut
			}).start($('jpod-rail2').getStyle('top').toInt(), +12 - (jPod.track * 77));
			jPod.newSong(jPod.getTrackList()[jPod.track])
		}
	},
	playscreen: function () {
		if ($('jpod-rail1').getStyle('left').toInt() == 0) new Fx.Style($('jpod-rail1'), 'left', {
			duration: 300,
			transition: Fx.Transitions.Quad.easeInOut
		}).start(0, -119);
		var a = $$("#jf-jpod li.selected")[0].getElement("span.name").innerHTML;
		var b = a.slice(a.search(/\|/) + 1, a.length);
		if (!$defined(jPod.mp3) || jPod.mp3.playState == 0 || jPod.track != jPod.getTrackNumber()) {
			jPod.newSong(b);
			jPod.track = jPod.getTrackNumber()
		}
	},
	changeCover: function (a) {
		var a = a.getElement("span.name").innerHTML;
		var b = 'http://demo.jooforge.com/extensions/media/jukebox/' + a.slice(0, a.search(/\|/));
		if (b.length == 16) {
			b = 'http://demo.jooforge.com/extensions/modules/mod_jf-jpod/images/unknown.png';
			jPod.coverLess()
		} else if (jPod.notfound) {
			jPod.unknown = new Fx.Style($("jf-notfound"), 'opacity', {
				duration: jPod.switchtime,
				transition: Fx.Transitions.Quad.easeInOut
			}).start(1, 0);
			jPod.notfound = false
		}
		if ($defined(jPod.coverBg1) && $defined(jPod.coverBg2)) {
			jPod.coverBg1.stop();
			jPod.coverBg2.stop()
		}
		jPod.coverBg1 = new Fx.Style($("jf-coverbg1"), 'opacity', {
			duration: jPod.switchtime,
			transition: Fx.Transitions.Quad.easeInOut
		});
		jPod.coverBg2 = new Fx.Style($("jf-coverbg2"), 'opacity', {
			duration: jPod.switchtime,
			transition: Fx.Transitions.Quad.easeInOut
		});
		if (jPod.times == 1) {
			$("jf-coverbg2").setStyle("background-image", "url(" + b + ")");
			jPod.coverBg1.start(1, 0);
			jPod.coverBg2.start(0, 1);
			jPod.times = 2
		} else {
			$("jf-coverbg1").setStyle("background-image", "url(" + b + ")");
			jPod.coverBg2.start(1, 0);
			jPod.coverBg1.start(0, 1);
			jPod.times = 1
		}
	},
	coverLess: function () {
		jPod.notfound = true;
		if ($defined(jPod.notFound)) {
			jPod.unknown.stop()
		}
		jPod.unknown = new Fx.Style($("jf-notfound"), 'opacity', {
			duration: jPod.switchtime,
			transition: Fx.Transitions.Quad.easeInOut
		}).start(0, 1)
	},
	movingBg: function () {
		var a = new Fx.Style($("jf-coverbg1"), "background-position", {
			duration: jPod.durata,
			transition: Fx.Transitions.Quad.easeOut
		});
		var b = new Fx.Style($("jf-coverbg2"), "background-position", {
			duration: jPod.durata,
			transition: Fx.Transitions.Quad.easeOut
		});
		a.start("-76px 0", "-38px -31px").chain(function () {
			this.start("0 0")
		}).chain(function () {
			this.start("0 -31px")
		}).chain(function () {
			this.start("-38px 0")
		}).chain(function () {
			this.start("-76px -31px")
		}).chain(function () {
			this.start("-76px 0")
		});
		b.start("-76px 0", "-38px -31px").chain(function () {
			this.start("0 0")
		}).chain(function () {
			this.start("0 -31px")
		}).chain(function () {
			this.start("-38px 0")
		}).chain(function () {
			this.start("-76px -31px")
		}).chain(function () {
			this.start("-76px 0")
		});
		jPod.movingBg.delay(jPod.durata * 6)
	},
	menu: function () {
		$$("#jf-jpod li").each(function (a, b) {
			if (a.hasClass("selected")) {
				a.getElement("span.b").setStyle("display", "none");
				a.getElement("span.override1").addClass("override2");
				a.getElement("span.override1").removeClass("override1");
				jPod.titleScroll()
			}
			a.addEvent("dblclick", function () {
				jPod.playscreen()
			});
			a.addEvent("click", function () {
				jPod.changeCover(a);
				$('jpod-rail2').setStyle("top", 12 - (b * 77));
				if (!a.hasClass("selected")) {
					jPod.newSelected(a)
				}
			})
		})
	},
	newSelected: function (a) {
		var b = $('jf-jpod').getElement('li.selected');
		if ($defined(jPod.title)) jPod.title.stop();
		if ($defined(jPod.timer)) clearInterval(jPod.timer);
		b.removeClass("selected");
		b.getElement("span.b").removeProperty("style");
		b.getElement("span.w").setStyle("display", "none");
		b.getElement("span.override2").addClass("override1");
		b.getElement("span.override2").removeClass("override2");
		a.addClass("selected");
		a.getElement("span.w").removeProperty("style");
		a.getElement("span.b").setStyle("display", "none");
		a.getElement("span.override1").addClass("override2");
		a.getElement("span.override1").removeClass("override1");
		jPod.titleScroll()
	},
	titleScroll: function () {
		var a = $('jf-jpod').getElement('li.selected');
		var b = 61;
		var a = a.getElement('span.w');
		var c = a.getElement('span').getCoordinates().width;
		if (c > b) {
			var d = (c - b);
			var e = 140 * d;
			jPod.title = new Fx.Style(a, 'margin-left', {
				duration: e,
				transition: Fx.Transitions.linear,
				onStart: function () {
					jPod.onScrolling = a.getParent('li')
				},
				onComplete: function () {
					jPod.onScrolling = null
				}
			});
			jPod.title.start(0, -d).chain(function () {
				this.start(0)
			});
			jPod.timer = setInterval(function () {
				jPod.title.start(0, -d).chain(function () {
					this.start(0)
				})
			},
			e * 2 + 2000)
		}
	}
};
window.addEvent("domready", jPod.start);
