var init = {
	cubeNumber:0,
	coefGlobal:2,
	objetXml:null,
	arrayImages:null,
	'go':function(){	
		this.animBg();
	},
	'createAllCube':function(objetX){
		this.arrayImages = [];
		this.objetXml = objetX.responseXML;
		var photos = this.objetXml.getElementsByTagName('photos')[0].getElementsByTagName('photo');
		this.cubeNumber = photos.length/2;
		for(var i=0; i<this.cubeNumber; i++){
			var objet = { "id":photos[i].getAttribute("id") , "image":photos[i].getElementsByTagName("grande")[0].firstChild.nodeValue , "imageSmall" : photos[i].getElementsByTagName("petite")[0].firstChild.nodeValue , "titre":photos[i].getElementsByTagName("titre")[0].firstChild.nodeValue};
			this.arrayImages.push(objet);
			cube.make(i, objet);
		}
		
	},
	'zoom':function(delta){
		this.coefGlobal = this.coefGlobal + delta/10;	
		coef=this.coefGlobal;
		for(var i=0; i< this.cubeNumber; i++){
			idCalque = "cube"+i;
			YAHOO.util.Dom.setStyle(idCalque, 'opacity', (i*(0.01*coef)));
			YAHOO.util.Dom.setStyle(idCalque, 'height', i*coef);
			YAHOO.util.Dom.setStyle(idCalque, 'width', i*coef);
			YAHOO.util.Dom.setXY(idCalque, [Math.floor(Math.sin(i*coef/10)*i*coef*2)+YAHOO.util.Dom.getViewportWidth()/2-(i*coef/2) ,Math.floor(Math.cos(i*coef/10)*i*coef*2)+YAHOO.util.Dom.getViewportHeight()/2-(i*coef/2) ]);	
				
		}	
	},
	'animBg':function(){
		var attributes = {
			backgroundColor: { to:  'rgb('+Math.floor(Math.random()*255)+', '+Math.floor(Math.random()*255)+', '+Math.floor(Math.random()*255)+')' }
		};
		var anim = new YAHOO.util.ColorAnim('fond', attributes, 10);
		anim.onComplete.subscribe(init.animBg); 
		anim.animate();
	},
	'charge':function(){
			flux = document.getElementById("keyword").value;
			xmlRequest.get("getData.php", "key="+flux, callbackSearch, "POST");
			
	}
}


var cube= {
	'make' : function (id, objet){
		idCalque = "cube"+id;
		if(document.getElementById(idCalque)){
			document.body.removeChild(document.getElementById(idCalque));
		}
		if(window.ActiveXObject){
			var calqueIo = document.createElement('<div id="'+idCalque+'" class="cube">');
		}
		else{
			var calqueIo = document.createElement('div');
			calqueIo.id = idCalque;
			calqueIo.className = "cube";
		}
		
		calqueIo.style.zIndex=id;
		
		var imgIo = document.createElement("img");
		imgIo.src = objet.imageSmall;
		
		imgIo.onmouseover = function(){
			imgIo.src = objet.image;
		}
		
		imgIo.onclick = function(){
			if(window.ActiveXObject){
				var calqueIoPrev = document.createElement('<div id="visualisation">');
			}
			else{
				var calqueIoPrev = document.createElement('div');
				calqueIoPrev.id = "visualisation";
			}
			var imgIoPrev = document.createElement("img");
			imgIoPrev.src = objet.image;
			imgIoPrev.onclick = function(){
				document.body.removeChild(imgIoPrev.parentNode)
			}
			calqueIoPrev.appendChild(imgIoPrev);	
			document.body.appendChild(calqueIoPrev);
		}		
		
		calqueIo.appendChild(imgIo);
		document.body.appendChild(calqueIo);
	
		coef=2;
		YAHOO.util.Dom.setStyle(idCalque, 'opacity', (id*(0.01*coef)));
		YAHOO.util.Dom.setStyle(idCalque, 'height', id*coef);
		YAHOO.util.Dom.setStyle(idCalque, 'width', id*coef);
		YAHOO.util.Dom.setXY(idCalque, [Math.floor(Math.sin(id*coef/10)*id*coef*2)+YAHOO.util.Dom.getViewportWidth()/2-(id*coef/2) ,Math.floor(Math.cos(id*coef/10)*id*coef*2)+YAHOO.util.Dom.getViewportHeight()/2-(id*coef/2) ]);	
	}
}

//Chargement des flux XML distants
var xmlRequest = {
	handleSuccess:function(o){
		init.createAllCube(o);
	},
	handleFailure:function(o){
		alert( o.status + " " + o.statusText)
	},
	get:function(url, flux, callback, type) {
		if(type == "POST"){
			YAHOO.util.Connect.asyncRequest('POST', url , callback, flux);
		}else{
			YAHOO.util.Connect.asyncRequest('GET', url , callback, flux);
		}


	}
};
//Callback
var callbackSearch ={
	success: xmlRequest.handleSuccess,
	failure: xmlRequest.handleFailure,
	scope:xmlRequest,
	argument: "ok"
};


function wheel(event){
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
                /** In Opera 9, delta differs in sign as compared to IE.
                 */
                if (window.opera)
                        delta = -delta;
        } else if (event.detail) { /** Mozilla case. */
                /** In Mozilla, sign of delta is different than in IE.
                 * Also, delta is multiple of 3.
                 */
                delta = -event.detail/3;
        }
        /** If delta is nonzero, handle it.
         * Basically, delta is now positive if wheel was scrolled up,
         * and negative, if wheel was scrolled down.
         */
        if (delta)
                init.zoom(delta);
        /** Prevent default actions caused by mouse wheel.
         * That might be ugly, but we handle scrolls somehow
         * anyway, so don't bother here..
         */
        if (event.preventDefault)
                event.preventDefault();
	event.returnValue = false;
}

if (window.addEventListener)
        window.addEventListener('DOMMouseScroll', wheel, false);
window.onmousewheel = document.onmousewheel = wheel;