window.name = 'principale';

function majTitres(e) {
	new PeriodicalExecuter(function(){
		new Ajax.Request('/includes/xml/live.xml', {
			onSuccess: function(req){
				titre = req.responseXML.firstChild.getElementsByTagName('title')[0].firstChild.nodeValue;
				artiste = req.responseXML.firstChild.getElementsByTagName('artist')[0].firstChild.nodeValue;
				img = req.responseXML.firstChild.getElementsByTagName('cover')[0].firstChild.nodeValue;
				link = req.responseXML.firstChild.getElementsByTagName('link')[0].firstChild.nodeValue;
				$('txt_titre').update(artiste+' - '+titre);
				$('img_live').setAttribute('src', img.replace('.jpg', '_t.jpg'));
				$('img_live_grd').setAttribute('src', img);
				Live.goal = link;
			}
		});
	}, 10);
}//majTitres

function addFlash(){
	//if(FlashReplace.checkForFlash(7) && $('entete'))
	//$('entete').setStyle({backgroundImage: "url('/img/header_noflash.png')"});
	//FlashReplace.replace("divanim1", "/includes/flash/anim_prun_haut.swf", "anim1", 498, 266, 7, {quality: "high"});
	//FlashReplace.replace("divanim2", "/includes/flash/anim_prun_bas.swf", "anim2", 324, 162, 7, {quality: "high"});
	if($('div_prun_player') && prun_player != null){
		FlashReplace.replace(prun_player.div_id, prun_player.url, prun_player.id, prun_player.width, prun_player.height, prun_player.flashv, prun_player.options); //prun_player definie dans article.php
		if(!FlashReplace.checkForFlash(10))
			$('div_prun_player').update('<p>Le Prun Player nécessite au moins <a href="http://get.adobe.com/flashplayer/" target="_blank">la version 10 de Flash Player</a>.</p>')
	}
}



//---- WebTicker by Mioplanet
//---- www.mioplanet.com
 
TICKER_RIGHTTOLEFT = false;
TICKER_SPEED = 1;
TICKER_STYLE = "font-family:Tahoma; font-size:14px; color:#FFFFF";
TICKER_PAUSED = false;

function ticker_start() {
	var tickerSupported = false;
	TICKER_CONTENT = $('titres').innerHTML;
	TICKER_WIDTH = $('titres').getStyle('width');

	// Firefox Safari et Mozilla
	if (navigator.userAgent.indexOf("Firefox")!=-1 || navigator.userAgent.indexOf("Safari")!=-1 || navigator.userAgent.indexOf("Mozilla")!=-1) {
		$("titres").innerHTML = "<TABLE  cellspacing='0' cellpadding='0' width='100%'><TR><TD nowrap='nowrap'>"+"<SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' width='100%'>&nbsp;</SPAN>"+"</TD></TR></TABLE>";
		tickerSupported = true;
	}
	// IE
	if (navigator.userAgent.indexOf("MSIE")!=-1 && navigator.userAgent.indexOf("Opera")==-1) {
		document.getElementById("titres").innerHTML = "<DIV nowrap='nowrap' style='width:100%;'>"+"<SPAN style='"+TICKER_STYLE+"' ID='TICKER_BODY' width='100%'></SPAN>"+"</DIV>";
		tickerSupported = true;
	}
	if(tickerSupported){
		$('titres').scrollLeft = TICKER_RIGHTTOLEFT ? $('titres').scrollWidth - $('titres').offsetWidth : 0;
		$('TICKER_BODY').innerHTML = TICKER_CONTENT;
		$('titres').setStyle({'display': 'block'});
		new Event.observe('titres', 'mouseover', function(){TICKER_PAUSED=true});
		new Event.observe('titres', 'mouseout', function(){TICKER_PAUSED=false});
		new PeriodicalExecuter(TICKER_tick, 0.05);
	}
}//ticker_start

function TICKER_tick() {
	if(!TICKER_PAUSED) $('titres').scrollLeft += TICKER_SPEED * (TICKER_RIGHTTOLEFT ? -1 : 1);
	if(TICKER_RIGHTTOLEFT && $('titres').scrollLeft <= 0) $('titres').scrollLeft = $('titres').scrollWidth - $('titres').offsetWidth;
	if(!TICKER_RIGHTTOLEFT && $('titres').scrollLeft >= $('titres').scrollWidth - $('titres').offsetWidth) $('titres').scrollLeft = 0;
	
}//TICKER_tick

//FlashReplace is developed by Robert Nyman, http://www.robertnyman.com. License and downloads: http://code.google.com/p/flashreplace/
var FlashReplace = {
	elmToReplace : null,
	flashIsInstalled : null,
	defaultFlashVersion : 7,
	replace : function (elmToReplace, src, id, width, height, version, params){
		this.elmToReplace = $(elmToReplace);
		this.flashIsInstalled = this.checkForFlash(version || this.defaultFlashVersion);
		if(this.elmToReplace && this.flashIsInstalled){
			var obj = '<object' + ((window.ActiveXObject)? ' id="' + id + '" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" data="' + src + '"' : '');
			obj += ' width="' + width + '"';
			obj += ' height="' + height + '"';
			obj += '>';
			var param = '<param';
			param += ' name="movie"';
			param += ' value="' + src + '"';
			param += '>';
			param += '';
			var extraParams = '';
			var extraAttributes = '';
			for(var i in params){
				extraParams += '<param name="' + i + '" value="' + params[i] + '">';
				extraAttributes += ' ' + i + '="' + params[i] + '"';
			}
			var embed = '<embed id="' + id + '" src="' + src + '" type="application/x-shockwave-flash" width="' + width + '" height="' + height + '"';
			var embedEnd = extraAttributes + '></embed>';
			var objEnd = '</object>';
			this.elmToReplace.innerHTML = obj + param + extraParams + embed + embedEnd + objEnd;			
		}
	},
	
	checkForFlash : function (version){
		this.flashIsInstalled = false;
		var flash;
		if(window.ActiveXObject){
			try{
				flash = new ActiveXObject(("ShockwaveFlash.ShockwaveFlash." + version));
				this.flashIsInstalled = true;
			}
			catch(e){
				// Throws an error if the version isn't available			
			}
		}
		else if(navigator.plugins && navigator.mimeTypes.length > 0){
			flash = navigator.plugins["Shockwave Flash"];
			if(flash){
				var flashVersion = navigator.plugins["Shockwave Flash"].description.replace(/.*\s(\d+\.\d+).*/, "$1");
				if(flashVersion >= version){
					this.flashIsInstalled = true;
				}
			}
		}
		return this.flashIsInstalled;
	}
};
// ---


var Ident = {
	cnx: function(e){
		Event.stop(e);
		Form.disable('form_cnx');
		new Ajax.Request('includes/xml/cnx.php',{
			postBody: $H({'pseudo':$F('pseudo'),'mdp':$F('mdp')}).toQueryString(),
			onLoading: Spinner.toggle_cnx,
			onFailure: Ident.cnx_indispo,
			onSuccess: function(req){
				if(req.responseText == '403 login failed'){
					$('fields_cnx').replace('<span id="alert_cnx">Mauvais pseudo/mdp<a href="#" id="close_alert"><img src="img/close_cnx.png" /></a></span>');
					new Event.observe('close_alert', 'click', Ident.close_alert);
				}
				else{
					$('form_cnx').replace(req.responseText);
					new Event.observe('link_decnx', 'click', Ident.decnx);
					if($F('redirect'))
						document.location.href=$F('redirect');
					
				}
			},
			onComplete: function(req){
				Form.enable('form_cnx');
			}
		});
	},
	decnx: function(e){
		Event.stop(e);
		new Ajax.Request('includes/xml/cnx.php',
		{
			postBody: 'logout=true',
			onLoading: Spinner.toggle_cnx,
			onFailure: Ident.cnx_indispo,
			onSuccess: function(req){
				$('links_account').replace(req.responseText);
				new Event.observe('link_cnx', 'click', Ident.fields_cnx);
			}
		});
	},
	cnx_indispo: function(){
		$('fields_cnx').replace('<span id="alert_cnx">Service indisponnible<a href="#" id="close_alert"><img src="img/close_cnx.png" /></a></span>');
		new Event.observe('close_alert', 'click', Ident.close_alert);
	},
	fields_cnx: function(e){
		Event.stop(e);
		$('link_cnx').replace('<span id="fields_cnx"><label for="pseudo">Pseudo : </label><input type="text" name="pseudo" id="pseudo" /> <label for="mdp">Mot de passe : </label><input type="password" name="mdp" id="mdp" /> <input type="submit" name="submit" value="Go !" /><img src="img/spinner_cnx.gif" id="spinner_cnx" style="display:none" /><a href="#" id="close_cnx"><img src="img/close_cnx.png" width="16" /></a></span>');
		$('pseudo').activate();
		new Event.observe('form_cnx', 'submit', Ident.cnx);
		new Event.observe('close_cnx', 'click', Ident.close_cnx);
	},
	close_cnx: function(e){
		Event.stop(e);
		$('fields_cnx').replace('<a href="#" id="link_cnx">Se connecter</a>');
		new Event.observe('link_cnx', 'click', Ident.fields_cnx);
	},
	close_alert: function(e){
		Event.stop(e);
		$('alert_cnx').replace('<a href="#" id="link_cnx">Se connecter</a>');
		new Event.observe('link_cnx', 'click', Ident.fields_cnx);
	}
}

var Spinner = {
	toggle: function(){
		Element.toggle('spinner');
	},
	toggle_cnx: function(){
		Element.toggle('spinner_cnx');
	}
}



function horloge(){
	new PeriodicalExecuter(function(pe){
		today = new Date();
		hours = today.getHours();
		minutes = today.getMinutes();
		seconds = today.getSeconds();
		if(hours < 10)
			hours = '0'+hours;
		if(minutes < 10)
			minutes = '0'+minutes;
		if(seconds < 10)
			seconds = '0'+seconds;
		$('time').update(hours+':'+minutes+':'+seconds);
	}, 1);
}


function sort_photos(){
	data = Sortable.serialize('list_photos');
	new Ajax.Request('includes/xml/sortincats.php', {
		method: 'get',
		parameters : data+'&cat='+$F('mc_id')
	});
}
function create_sortable_photos(){
	Sortable.create('list_photos', {onUpdate: function(){ sort_photos();}});
}

function player(sons, titre, lien, width, height){
	window.open('http://www.prun.net/player.php?sons='+sons+'&titre='+titre+'&lien='+lien,'player','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=1,copyhistory=0,menuBar=0,width='+width+',height='+height+',top=100,left=200,screenY=100,screenX=200');
}
function player_live(e){
	Event.stop(e);
	window.open('/player.php','player_direct','toolbar=0,location=0,directories=0,status=0,scrollbars=0,resizable=1,copyhistory=0,menuBar=0,width=600,height=400,top=100,left=200,screenY=100,screenX=200');
}

function activate_search_field(){
	new Ajax.Autocompleter('recherche', 'recherche_suggestions', 'recherche.php?ajax', 
	{
		indicator:'search_spinner',
		minChars: 2,
		afterUpdateElement: function(text, li){
			document.location.href = 'http://www.prun.net/articles/'+li.id+'/';
		}
	});
	Field.activate('recherche');
}

var ArtworkZoom = {
	timeout: null,
	on: function(){
		new Effect.Grow('img_live_grd', {
			beforeStart: function(){Event.stopObserving('img_live', 'mouseover', ArtworkZoom.on);},
			afterFinish: function(){
				ArtworkZoom.timeout = setTimeout('ArtworkZoom.off()', 5000);
				new Event.observe('img_live_grd', 'mouseout', ArtworkZoom.off);}
		});
	},
	off: function(){
		clearTimeout(ArtworkZoom.timeout);
		new Effect.Puff('img_live_grd', {
			beforeStart: function(){Event.stopObserving('img_live_grd', 'mouseout', ArtworkZoom.off);},
			afterFinish: function(){new Event.observe('img_live', 'mouseover', ArtworkZoom.on);}
		});
	}
}
var Live = {
	goal: '/playlist/',
	click: function(){
		document.location.href=Live.goal;
	}
}


function loadingPage(){
	if($('google')&&Element.visible('google')){
		Element.hide('google_submit');
		Field.activate('google_q');
	}
	
	if($('live')&&Element.visible('live')){
		new Event.observe('live', 'click', Live.click);
		new Event.observe('img_live', 'mouseover', ArtworkZoom.on);
		
		majTitres();
		ticker_start();
	}
	if($('player_live_link'))
		new Event.observe('player_live_link', 'click', player_live);
	
	if(is_admin == true)
		loading_admin();
	
	if($('list_photos')&&$('list_musiques')){
		Sortable.create('list_musiques');
		create_sortable_photos();
	}
	if($('color-picker')){
		pickColor('color-picker-preview');
		relateColor('color-picker-preview', getObj('color-picker-field').value);
		new Event.observe('color-picker-field', 'keypress', function(){relateColor('color-picker-field', this.value)});
		new Event.observe('color-picker-field', 'change', function(){relateColor('color-picker-field', this.value)});
	}
	if($('link_cnx'))
		new Event.observe('link_cnx', 'click', Ident.fields_cnx);
		
	if($('links_account'))
		new Event.observe('link_decnx', 'click', Ident.decnx);

	
	if($('bandeau')){
		new Ajax.Request('includes/xml/pub.php?xml', {
			onSuccess: function(req){
				pub = req.responseXML.getElementsByTagName('pub');
				var i=0;
				pub_active = true;
				var time = pub[0].getElementsByTagName('time')[0].firstChild.nodeValue;
				fct_pub = function(){
					pub_active = false;
				}
				setTimeout(fct_pub, (time*1000));
				if($A(pub).length >1){
					new PeriodicalExecuter(function(){
						if(!pub_active){
							i++;
							if(i>= pub.length)
								i=0;
							var lien = pub[i].getElementsByTagName('lien')[0].firstChild.nodeValue;
							var titre = pub[i].getElementsByTagName('titre')[0].firstChild.nodeValue;
							var code = pub[i].getElementsByTagName('code')[0].firstChild.nodeValue;
							var time = pub[i].getElementsByTagName('time')[0].firstChild.nodeValue;
							$("lien_bandeau").update(code);
							$("lien_bandeau").setAttribute('href', lien);
							$("lien_bandeau").setAttribute('title', titre);
							pub_active = true;
							setTimeout(fct_pub, (time*1000));
						}
					}, 1);//interval en sec pour le changement de pub
				}
			}
		});	
	}
	if($('principale_acc')){
		$$('.bloc_random').each(function(bloc){
			new PeriodicalExecuter(function(){
				new Ajax.Request('index.php',{
					postBody: $H({'id_bloc':bloc.id.split('_')[1],'prev_id':$F(bloc.id.split('_')[1]+'_prev_id')}).toQueryString(),
					onSuccess: function(req){
						new Effect.Opacity($(bloc).select('.art_princ')[0], {
							duration:0.5, from:1, to:0, 
							afterFinish: function(){
								Element.update($(bloc).select('.art_princ')[0], req.responseText);
								new Effect.Opacity($(bloc).select('.art_princ')[0], {duration:2, from:0, to:1});
							}
						});
					}
				});
			}, 8);//interval en sec pour le changement d art princ
		});
	}
	if($('player_externe_link')){
		new Event.observe('player_externe_link', 'click', function(e){
			Event.stop(e);
			Element.setStyle('player_externe', {cursor: 'pointer'});
			player(player_sons, player_titre, player_lien, 480, 140);
		});
	}
	if($('form_perso'))
		perso_init();
	
	if($('time'))
		horloge();
		
	if($('recherche')){
		activate_search_field();
	}
	
	if($('search_link')){
		new Event.observe('search_link', 'click', function(e){
			Event.stop(e);
			$('search_link').replace('<img src="img/top_search_spinner.gif" id="search_spinner" style="display:none;"/> <input type="text" name="recherche" id="recherche" size="40" maxlenght="100" value="Tapez votre recherche"/> <div class="autocomplete" id="recherche_suggestions" style="display:none;"></div>');
			activate_search_field();
		})
	}
	
	if($('download-podcast')){
		if($('select_mstyle')){
			new Event.observe('select_mstyle', 'change', function(e){
				document.location.href="/download-podcast/"+$F('theme_podcast')+"/style/"+$F('select_mstyle')+"/";
			});
		}
		if($('select_emission')){
			new Event.observe('select_emission', 'change', function(e){
				document.location.href="/download-podcast/"+$F('theme_podcast')+"/"+$F('select_emission')+"/";
			});
		}
	}
	
	addFlash();
	
}

new Event.observe(document, 'dom:loaded', loadingPage);
