/* Run apps */
//~ coding: utf-8
if( 'undefined' === typeof PopPub) var PopPub = new Object();

function PopPub() {
	this.name;
	this.type;
	this.hash = '';
	this.allPubs = [];
	var code = 'abcdefghijklmnopqrstuvwxyz0123457689'.split('');	
	for(var i=0; i<10; i++) {
		this.hash += code[Math.floor(Math.random() * code.length)];
	}
	this.layout = E('popPub');
	this.pubBox = E().create('div', {id: '__'+this.hash});
}

var ppproto = PopPub.prototype;


ppproto.getHash = function () {
	return this.hash;
}

ppproto.send = function() {
	if(!E().find(this.pubBox)) { this.layout.appendChild(this.pubBox); }
	var a = new Ajax('./--pubs--/pubMediator.xml', {});
	var ref = this;
	a.onComplete = function(xhr, xml) {
		ref.onShowInfos(xml);
	} 
}

ppproto.onShowInfos = function(xml) {
	allPubs = new Array();
	allPubs = xml.getElementsByTagName('pub');
	
	if(allPubs.length == 1) {
		var pub = allPubs.item(0);
		//~ var txt = pub.getElementsByTagName('text').item(0);
		var txt = pub.getAttribute('text')
		//~ if(window.location == 'http://www.elmediator.org/index.php?debug=ok' )
			//~ alert(pub);
		var a = E().create('a', {
			href	: pub.getAttribute('url'),
			title	: pub.getAttribute('title'),
			target	: '_blank'
		});
		
		with(a.style) {
			display = 'block';
			margin = '0 auto';
			padding = '0';
			textDecoration = 'none';
			width = pub.getAttribute('width')+'px';
			height = pub.getAttribute('height')+'px';
			background = 'transparent url(./--pubs--/'+pub.getAttribute('img')+') no-repeat 50% 50%';
		}
		this.pubBox.empty();
		this.pubBox.appendChild(a);
		
		var span = E().create('span', {innerHTML: txt}) // txt.text is 4 the motha Fucka IE
		//~ var span = E().create('span', {innerHTML: txt.text || txt.textContent}) // txt.text is 4 the motha Fucka IE
		//~ span.style.textAlign = 'left';
		span.style.display = 'block';
		this.pubBox.appendChild( span );
		
	} else {
		// do nothing now!
	}
	
	with( this.pubBox.style ) {
		position		= 'relative';		
		width 			= '490px';
		height			= '230px';
		textAlign		= 'center';
		fontSize		= '11px';
		fontFamily		= 'Arial, Helvetica, sans-serif';		
	}
}

// ---------------------------------------------------------------------- //

function launchTiltAnim() {
	var id = 'tiltfestival';
	//~ var swf = new SWFObject('./--swf--/tiltLight.swf', 'tiltLightFlash', '50', '400', '8', '#FFFFFF');
	//~ swf.addParam('wmode', 'transparent');
	//~ swf.write(id);
	
	var params = {
		wmode	: 'transparent',
		bgcolor	: '#FFF',
		menu	: "false",
		scale	: "noScale" ,
		align	: "TL"
	};

	var vars = {}

	swfobject.embedSWF('./--swf--/tiltLight.swf', 'tiltfestival', 50, 400, "9.0.0", "expressInstall.swf", vars, params);
	
	
}

// ---------------------------------------------------------------------- //

function getFlashAudios() {
	var audLinks = document.getElementsByTagName('a');
	
	var cpt=0, i=0; //, l=audLinks.length;
	for(i=0; i<audLinks.length; ++i) {
		var lnk = audLinks[i];
		
		if(lnk.getAttribute('rel') && lnk.getAttribute('rel') == 'audioLink') {
		
			var bruts = lnk.title.split('|');
			// lnk.id = 'audLNK'+(++cpt);
			var params	= {
				wmode: 'transparent',
				scale: 'noScale',
				align: 'TL',
				menu: 'false'
			};
			var vars	= {
				myMP3:		'./--media--/promo/'+bruts[0]+'.mp3',
				myTitle:	bruts[1]
			};
			var e = E().create('span', {id: 'audLNK'+(++cpt)});
			lnk.parentNode.appendChild(e);
			swfobject.embedSWF('./--swf--/audioPlayer.swf', e.id, 300, 30, "9.0.0", "expressInstall.swf", vars, params);
		
		}
	}
	
	I(audLinks).each(function(i, e) { if(e.getAttribute('rel') && e.getAttribute('rel') == 'audioLink') E(e).erase(); });
	I(audLinks).each(function(i, e) { if(e.getAttribute('rel') && e.getAttribute('rel') == 'audioLink') E(e).erase(); });
	
	/*for(i=0; i<audLinks.length; ++i) {
		var lnk = audLinks[i];
		
		if(lnk.getAttribute('rel') && lnk.getAttribute('rel') == 'audioLink') {
			E(lnk).erase();
		}
	}*/
	
	
}

// ---------------------------------------------------------------------- //

function newsMaxiMini() {
	var newsHead = document.getElementsByTagName('h3');
	newsHead = I(newsHead).filter(function(i, e) { return (e.className && e.className == 'pubTitle liteNews') } );
	newsHead.each(
		function(i, e) {
			//~ if(e.className && e.className == 'pubTitle liteNews') {
				if(i > (_NUM_NEWS_OPEN_ -1)) {
					var nx = e.nextSibling;
					if(nx.nodeType == 3) nx = nx.nextSibling;
					//.nextSibling;
					if(nx.className && nx.className == 'pubText')
						nx.style.display = 'none';
				}
				e.style.cursor = 'pointer';
				e.title   = 'Cliquez pour voir la news' +i;
				e.onclick = function() {
					var nx = this.nextSibling; //.nextSibling;
					if(nx.nodeType == 3) nx = nx.nextSibling;
					if(nx.className && nx.className == 'pubText')
						nx.style.display = ((nx.style.display=='none') ? 'block' : 'none');
					
					return false;
				}
			//~ }
		}
	);
}

// ---------------------------------------------------------------------- //


if(typeof Elmediator == 'undefined') var Elmediator = new Object();

Elmediator = {
	pub: null,
	init: function() {
		launchTiltAnim();
		getFlashAudios();
		Elmediator.pub = new PopPub();
		Elmediator.pub.send();
		
		newsMaxiMini();
	},
	
	credits : 'Scriptin By N.Z.R. www.noizer-bug.net, except Google App and SWFObject'
};


var _NUM_NEWS_OPEN_ = 3; // défini le nbre de news  ouverte sur la page d'accueil

// ---------------------------------------------------------------------- //

Event.add(window, 'load', Elmediator.init);