// ***
// Author: Paolo Moretti
// Contacts: littlebrown@gmail.com | pmoretti@e-tree.com | pmoretti@etnoteam.it 
// Company: E-TREE - Etnoteam - Valueteam
// Year: 2007 / 2008
//
// This script is based on prototypejs, amazing framework that aims to ease development of dynamic web applications
// Engine is the head of a Javascript collection, built by Paolo Moretti. It mix and set-up every javascript togheter.
// ***

/* Framework required */
document.write('<script src="js/scriptaculous.js"></script>');
document.write('<script src="js/JavaScriptFlashGateway.js"></script>');
document.write('<script src="js/Utils.js"></script>');

var Engine = {
	jsLoaded: '',
	elements: [],
	delayTime: 500,		// msecs
	jsFiles: new Array(),
	references: 
	[
		['*', 'ExtraFunctions.js'],
		['*', 'Lightbox.js'],
		['LatestAlbumBox', 'coverSwitcher.js', 'CoverSwitcher.init();'],
		['boxAccordion', 'Accordions.js', 'Accordions.init("boxAccordion");']
	],
	load: function() 
	{
		//this.elementLooper($A($$('div')).concat($A($$('img'))).concat($A($$('a'))));
		this.elementLooper($A(document.getElementsByTagName('a')).concat($A(document.getElementsByTagName('img'))).concat($A(document.getElementsByTagName('div'))));
	},
	require: function(jsName) 
	{
		// Aggiungo i JS che mi servono in base alla pagina
		document.write('<script type="text/javascript" src="js/'+jsName+'"></script>');
		this.jsFiles.push(jsName);
	},
	debug: function(t) 
	{
		if($('debug'))
			$('debug').innerHTML += t;
	},
	asyncLoad: function(jsFile, options)
	{
		new Ajax.Request('js/' + jsFile, {
			onComplete: function(ajax) {
				eval (ajax.responseText);
				Engine.jsFiles.push(jsFile);
				if(options && options.runAfterLoad) {
					eval(options.runAfterLoad);
				}
			}
		})
	},
	elementLooper: function(e) 
	{
		e.each(function(elem, j){
			if(elem != null && elem != '')
				Engine.elementLoad(elem);
		});
		
		/* Common routines */
		encryptEmailAddresses();
		//CheckLayout();
	},
	elementLoad: function(elem)
	{
		for(var i=0; i<this.references.length; i++) {
			if(Element.identify(elem) == this.references[i][0] || Element.hasClassName(elem, this.references[i][0]) || this.references[i][0] == '*') {
				if (this.jsLoaded.indexOf(this.references[i][1]) == -1) {
					this.jsLoaded += this.references[i][1]+',';
					this.require(this.references[i][1]);			// BUG //
					this.elements[this.references[i][0]] = new Array();
					this.elements[this.references[i][0]].push(elem);
				}
			}
		}
	}
}

var Radio = {
	
	history: {},
	delay: 200,
	
	createProxy: function(options) 
	{
		this.uid = new Date().getTime();
		this.flashProxy = Utils.initFlashProxy(this.uid);
	},
	
	writeRadio: function() 
	{
		if(this.uid == null)
			this.createProxy();
			
		Utils.writeFlash('src/musicPlayer.swf', {
			w: 185, 
			h: 60, 
			flashVars: 'lcId='+Radio.uid , 
		//	updateElement: 'RadioDisplay', 
			updateType: 'top', 
			transparent: true
		});
	},
	
	checkRadio: function(req) 
	{
		if (window.name != 'MadcapPlayer') {
			window.open('player.php?' + Object.toQueryString(req), 'MadcapPlayer', 'width=230,height=200,resizable=no,menubar=no,location=no,status=no,scrollbars=no,toolbar=no');
			return false;
		} else {
			this.createProxy();
			return true;
		} 
	},
	
	randomPlay: function()
	{
		this.checkRadio();
		this.flashProxy.call('randomPlay', 'a');
		
	},
	
	Pause: function() 
	{
		this.flashProxy.call('doPause', 'a');
	},
	
	Resume: function() 
	{
		this.flashProxy.call('doResume', 'a');
	},
	
	playSong: function(a, SongID) 
	{
		if(this.checkRadio({f: 'playSong', SongID: SongID})) 
			this.flashProxy.call('loadAndPlay', SongID);
	},
	
	playAlbum: function(id) 
	{
		if(this.checkRadio({f: 'playAlbum', id: id})) 
			this.flashProxy.call('loadAndPlayAlbum', id);
	},
	
	updatePlaying: function(item) 
	{
		if($('MyDisplay'))
			Element.remove($('MyDisplay'));
		var n = document.createElement('p');
		n.id = 'MyDisplay';
		item.replace('"from', '" - from:')
		n.innerHTML = item;
		$('RadioDisplay').appendChild(n);
	},
	
	_log: function(msg)
	{
		//alert(msg);
	}
	
}

if(Locale == null || Locale == undefined)
	var Locale = 'it';
	
if (Locale == 'it') {
	var Dictionary = {
		
		'CLOSE'					: 'CHIUDI',
		'CloseNextGigs'			: 'CHIUDI PROSSIMI CONCERTI',
		'ConfirmRequested'		: 'Richiesta conferma',
		'FullDetails'			: 'SCHEDA COMPLETA',
		'mandatoryAliasField'	: 'Il campo NICKNAME e\' obbligatorio<br><br>Sei pregato di compilarlo.',
		'mandatoryTextField'	: 'Il campo TESTO e\' obbligatorio<br><br>Sei pregato di compilarlo.',
		'OpenNextGigs'			: 'PROSSIMI CONCERTI',
		'PaymentMethods'		: 'Metodi di pagamento',
		'requestComment'		: 'Sei sicuro di inviare questo commento?<br /><br />'
		
	}
} else {
	var Dictionary = {
		
		'CLOSE'					: 'CLOSE',
		'CloseNextGigs'			: 'CLOSE BOX',
		'ConfirmRequested'		: 'Confirmation required',
		'FullDetails'			: 'FULL DETAILS',
		'mandatoryAliasField'	: 'NICKNAME field is mandatory.<br><br>Please fill it.',
		'mandatoryTextField'	: 'TEXT field is mandatory.<br><br>Please fill it.',
		'OpenNextGigs'			: 'NEXT GIGS',
		'PaymentMethods'		: 'Payment methods',
		'requestComment'		: 'Are you sure to send the commento below?<br /><br />'
		
	}
}


function sendMail(a) 
{
	window.location.href = 'mailto:' + a;
}

function encryptEmailAddresses() {
	$$('a.MAIL').each(function(e) {
		encryptEmailAddress(e)
	});
}

function encryptEmailAddress(elem) {
	if(elem.hasClassName('MAIL')) {
		if(elem.innerHTML.indexOf('{aa}') > -1) {
			var e = elem;
			var mailAddress = e.innerHTML.replace('{aa}', '@');
			e.onclick = function() {
				sendMail(mailAddress);
			}
			if(Element.hasClassName(e, 'FOUNDER')) {
				e.innerHTML = e.innerHTML.replace('{aa}', '<img src="i/at_founder.gif" align="absmiddle" />');
			}
			else {
				e.innerHTML = e.innerHTML.replace('{aa}', '<img src="i/at.gif" align="absmiddle" />');
			}
			e.onmouseover = function() {
				this.style.cursor = 'pointer'
			}
		}
	}
}
