/**
 * @author Erick
 */
var showroom;
var options = new Object(); 
options['w'] = 950;
options['h'] = 250;//540
//inutilises
//options['topFond'] = 15;//140;
//options['hFond'] = 250;

options['topShow'] = 0;//options['topFond']+options['hFond']+20;
options['hShow'] = 250
options['wu'] = options['w']-75;
options['hu'] = options['hShow']-120;
var beta = 2 * Math.PI;
var elipse = "M120,150 " + "a 280 130 0 1 1 500 100 " + "A 280 130 0 0 1 120 150";
var p, t;
var angle = 0;
var nPreview;
var yield = 15000;
var _isadmin=false;
var isIE = (document.all);
var timerA;

function myLoader(){
	//if(!isIE){
	bkg_draw();
	show('p1');
	new Effect.Appear('corps1', {
		duration: 2,
		from:0,
		to:100
	});
	showroom = Raphael(document.getElementById("showRoom"), options['w'], options['h']+55);

	/***** Creation du fond  ******/
	var r2 = showroom.rect(5, options['topShow']+30, options['w']-11, options['hShow'], 5);
	r2.attr({fill: "#1f2e33", stroke: "#463E2E", opacity: 0});	//463E2E
	r2.animate({opacity: 0.75}, 10000, ">" );

	/***** CHARGEMENT DES VIGNETTES  ******/
	var url = 'php/ax_showroom.php' ;
	debug(url);
	$('contactG').setAttribute('style','background-image: url(design/images/bkg_bas.png);');
	skin('contactG', 'bkg_bas2.png');
	doAjax(url, loadTimer);
	//}
}

function loadTimer(){
	debug('loadTimer');
	if (!this.req.responseXML) {
		debug("Request failed");
	}
	else {
		var xmlDoc = this.req.responseXML.documentElement;
		if (!xmlDoc) {
			debug("Failed loading links");
		}
		else {
			/********** test admin *****/
			_isadmin = (xmlDoc.getElementsByTagName('admin')[0].childNodes.length) ? (xmlDoc.getElementsByTagName('admin')[0].childNodes[0].nodeValue) : (false);
			debug('admin: ' + _isadmin);
			/********** AFFICHAGE DES LIENS *****/
			nPreview = xmlDoc.getElementsByTagName('show').length;
			var angle = 360 / nPreview;
			var angleN;
			var angleUnit = (2 * Math.PI) / nPreview;
			debug(nPreview + ' sites');
			p = new Array(nPreview);
			//t = new Array(nPreview);
			for (var i = 0; i < nPreview; i++) {

				var _titre = (xmlDoc.getElementsByTagName('titre')[i].childNodes.length) ? (xmlDoc.getElementsByTagName('titre')[i].childNodes[0].nodeValue) : (' ');
				var _lien = (xmlDoc.getElementsByTagName('lien')[i].childNodes.length) ? (xmlDoc.getElementsByTagName('lien')[i].childNodes[0].nodeValue) : (' ');
				var _img = (xmlDoc.getElementsByTagName('img')[i].childNodes.length) ? (xmlDoc.getElementsByTagName('img')[i].childNodes[0].nodeValue) : (' ');
				var _id = (xmlDoc.getElementsByTagName('id')[i].childNodes.length) ? (xmlDoc.getElementsByTagName('id')[i].childNodes[0].nodeValue) : (' ');

				debug(_titre);
				//angleN = (angle * i) * 0.017453293;
				angleN = angleUnit * i;//position de depart de cet element
				var x1 = (Math.cos(angleN) * (options['wu'] / 3)) + (options['wu'] / 2);
				var y1 = options['topShow'] + 50 + (Math.sin(angleN) * (options['hu'] / 3)) + (options['hu'] / 2);
				x1 = Math.abs( Math.round(x1) );
				y1 = Math.abs( Math.round(y1) );

				p[i] = showroom.image(_img, x1, y1, 120, 90, 2);
				p[i].node.id = 'pr_' + _id;
				p[i].node.onclick = siteOpen;

				if (_isadmin) {
					_titre = "Modifier " + _titre + ' [' + _id + ']';
				}
				document.getElementById('pr_' + _id).setAttribute("title", _titre);
				document.getElementById('pr_' + _id).setAttribute("class", "preview");
				document.getElementById('pr_' + _id).setAttribute("alt", _lien);

			}
			if (!isIE) {
				debug('setTimeout');
				timerA = window.setTimeout(animation, 500);
			}
			else{
				//timerA = window.setTimeout(animation, 0);
				window.clearTimeout(timerA);
				timerA = setInterval("animation()", 500);
			}
			skin('contactG', 'bkg_bas3.png');
			new Effect.Appear('f2', {duration:5, from:0, to:0.7})
		}
	}
}



function animation(){
	//debug('animat '+angle);
	var angleUnit = (2 * Math.PI) / nPreview;
	var angleN = 0;
	var x1, y1, opac, scal, zi;
	angle -= 0.08;
	//debug('animat '+angle);
	for (var i = 0; i < nPreview; i++) {//nPreview
		angleN  = angleUnit * i;//position de depart de cet element
		angleN += angle;
		angleN = angleN % (2*Math.PI);
		x1 = (Math.cos(angleN) * (options['wu'] / 3)) + (options['wu'] / 2) - 20;
		y1 = options['topShow'] + 50  +  (Math.sin(angleN) * (options['hu'] / 3)) + (options['hu'] / 2);
		x1 = Math.round(x1);
		y1 = Math.round(y1);
		opac = ( ( 3 + Math.sin(angleN) ) / 4);
		opac = (opac<.6)?opac-0.3:opac;
		scal = (3 + Math.sin(angleN))/3;
		zi =  15 + Math.round(Math.sin(angleN)*10);

		if (Math.sin(angleN) > 0) {
			p[i].toFront();
			if (Math.cos(angleN) > 0) {
				p[((i + 1) % nPreview)].toFront(); // NOP
			}
			else 
				if (i > 0) {
					p[i - 1].toFront();
				}
				else {
					p[nPreview - 1].toFront();
				}
		}
		p[i].animate({
			x: x1,
			y: y1,
			opacity: opac,
			scale: scal
		}, yield);
	}

	if (isIE) {//ie	
		window.clearInterval(timerA);
		timerA = setInterval("animation()", yield);
	}
	else {
		window.clearTimeout(timerA);
		timerA = window.setTimeout(animation, yield);
	}

}

function anime(element, x, y, opac, scal, zInd){
	x += $('showRoom').offsetLeft + $('corps1').offsetLeft + $('frame').offsetLeft;
	y += $('showRoom').offsetTop + $('corps1').offsetTop + 120;
	new Effect.Morph(element, {
		style: 	'top:'+y+'px; ' +
		'left:'+x+'px; ' +
		'width:'+ Math.round(scal*120)+'px; ' +
		'height:'+ Math.round(scal*90)+'px; ' +
		'opacity:'+opac+'; ' ,
		duration: Math.round(yield/1000), 
		fps: 100,
		queue: { position: 'end', scope: element, limit:5 }// ça n'a pas aidé à rendre l'anim fluide
	});
	$(element).style.zIndex = zInd;

}


function siteOpen(evt){
	if (_isadmin) {
		if (this.id) {
			var id = this.id;
			id = id.substring(3, 10);
		}
		else{
			var id = 0;
		}
		var url = 'http://at-ressources.com/admin/showroom_Edit.php?id=' + id ;
		window.open(url); 
	}
	else {
		//var atributs = this.attributes;
		var url = this.getAttribute("alt");
		window.open(url); 
	}

}

function bkg_draw(){
	if (isIE) {
		wScreen = document.body.clientWidth ;
		hScreen = $('contenu').offsetHeight + $('contenu').offsetTop + $('bas').offsetHeight + 80;
	}
	else{
		wScreen = document.body.clientWidth ;
		hScreen = document.body.clientHeight ;//$('contenu').offsetHeight + $('contenu').offsetTop + $('bas').offsetHeight + 65;//65
		wScreen = window.innerWidth - 30;
		hScreen = window.innerHeight - 30;
	}
	bkg = Raphael(document.body, wScreen, hScreen);

	// --> le degradé du fond de page
	var rJ = bkg.rect(wScreen/2, hScreen/2, 10, 10, 10);
	//rJ.attr({gradient: '105-#102030-#E0DCFF', fill: "#B3B7C4", stroke: "#FEFDE9", 'stroke-opacity': 0.60, 'stroke-width':3, opacity: 0.5});
	rJ.attr({gradient: '105-#B0C0D0-#D0DCDF', stroke: "#FEFDE9", 'stroke-opacity': 0.60, 'stroke-width':3, opacity: 0.5});
	rJ.animate({height: hScreen, width: wScreen, x:0, y:0}, 500, ">");

	// le cadre clair sous #bandeau 
	/*
	var cOrange = rvb2hex(240, 171, 10);
	var cGris   = rvb2hex(149, 155, 176);
	var bdo = document.getElementById('bandeau');
	if (document.all) {//ie
		var bdoLeft = bdo.offsetLeft - 8;
	}
	else{
		var bdoLeft = bdo.offsetLeft - 16;
	}
	var bdoTop = bdo.offsetTop-7;
	var rB = bkg.rect(bdoLeft, bdoTop, 1, bdo.offsetHeight, 3);
	rB.attr({fill: cOrange, stroke: cGris, 'stroke-opacity': 0.20, 'stroke-width':3, opacity: 0.65});
	//gradient: '105-'+rvb2hex(225, 102, 27)+'-'+rvb2hex(93, 246, 40), 
	rB.animate({width: bdo.offsetWidth}, 500, ">" );
	 */

}

function skin(element, bkgImg){
	debug('Skin: ' + element + ' to ' + bkgImg);
	$(element).setAttribute('style','background-image: url(design/images/'+bkgImg+');');

	/*new Effect.Morph($(element), {
		style: 'background-image: url(design/images/' + bkgImg + ');',
		duration: 1.5
	});
	 */
}

function pageLoad(page){
	debug('Loading page: '+page);
	var url = '/php/ax_page.php?page=' + page ;
	debug(url);
	hide( 'page');
	doAjax(url, pageDisplay);

}

function pageDisplay(){
	debug('pageDisplay');
	if (!this.req.responseXML) {
		debug("Request failed");
	}
	else {
		var xmlDoc = this.req.responseXML.documentElement;
		if (!xmlDoc) {
			debug("Failed loading page");
		}
		else {
			var _page = (xmlDoc.getElementsByTagName('pagename')[0].childNodes.length) ? (xmlDoc.getElementsByTagName('pagename')[0].childNodes[0].nodeValue) : ('sans titre');
			var _text = xmlDoc.getElementsByTagName('text')[0];
			//content('corps1', '');
			/*var nChild = _text.childNodes.length;	
			for (var i = 0; i < nChild; i++) {
				if (_text.childNodes[i]) {			
					alert(_text.childNodes[i].nodeValue);
					$('corps1').appendChild(_text.childNodes[i]);
				}
			}
			 */
			//alert(this.req.responseText);
			document.title = _page;
			content('page',this.req.responseText);
			new Effect.Appear('page', {duration:0.5});
		}
	}
}

function pageEdit(page){
	document.location='../../module/editor/editor.php?file='+page+'&width=950&height=600';
}

