// JavaScript Document
var EuronicsAcquistaOnline = new Class.create();
EuronicsAcquistaOnline.prototype = {
	initialize : function (oArgs){
		this.div			= oArgs.div ? oArgs.div : 'AcquistaOnline';
		this.wait 			= oArgs.wait ? oArgs.wait : 5000;
		this.duration		= oArgs.duration ? oArgs.duration : 0.5;
		this.autostart		= (typeof(oArgs.autostart)=='undefined') ? true : oArgs.autostart;
		this.xml			= oArgs.xml ? oArgs.xml : 'http://www.euronics.it/prodotti-promozione-hp/elenco_prodotti_promozione.xml';
		this.prodsToShow		= oArgs.prodsToShow ? oArgs.prodsToShow : 5;
		this.prods			= new Array();
		this.prod			= 0;
		this.prodDiv 		= oArgs.slides;
//		this.counter		= oArgs.counter;
//		this.caption		= oArgs.caption;
//		this.playButton		= oArgs.playButton ? oArgs.playButton : 'PlayButton';
//		this.pauseButton	= oArgs.pauseButton ? oArgs.pauseButton : 'PauseButton';
//		this.iImageId		= this.start;
		this.playInt;
		this.auto			= oArgs.div ? true : false;
		this.debugDiv 		= $(this.div+"DEBUG")==null ? false : $(this.div+"DEBUG");
//		EuronicsAcquistaOnlineHomeDEBUG
		this.animation		= true;
		this.enableDebug	= false;
		

		if($(this.div)==null){
			alert('NO DIV?');
			return;
		}
//		if ( this.slides ) {
//			this.numOfImages	= this.slides.length;
//			if ( !this.numOfImages ) {
//				alert('No slides?');
//			}
//		}
		if ( this.autostart ) {
			this.loadXML();
		}
	},
	
	// The Fade Function
//	swapImage: function (x,y) {		
//		$(this.slides[x]) && $(this.slides[x]).appear({ duration: this.duration });
//		$(this.slides[y]) && $(this.slides[y]).fade({duration: this.duration });
//	},
	
	// the onload event handler that starts the fading.
	loadXML: function () {
		this.debug('loadXML', 'loadXML');
		var data = new Date();
		random = data.getYear()+(data.getMonth() + 1)+data.getDate();

		new Ajax.Request(this.xml+'?'+random,
		{
			method: "get",
			onComplete: function(req){
				xmlprods = req.responseXML.getElementsByTagName("data")[0].childNodes;
				for(i=0;i<xmlprods.length;i++){
					if(this.prods.length>=this.prodsToShow){
						break;
					}
					var currentNode = xmlprods[i]
					if(currentNode.nodeType==3){
						continue;
					}
					prod = {
						img: this.getxmlProdTag(currentNode,'img'),
						titolo: this.getxmlProdTag(currentNode,'titolo'),
						desc: this.getxmlProdTag(currentNode,'desc'),
						prezzo: this.getxmlProdTag(currentNode,'prezzo'),
						lnkS: this.getxmlProdTag(currentNode,'lnkS'),
						lnkC: this.getxmlProdTag(currentNode,'lnkC'),
						lnkF: this.getxmlProdTag(currentNode,'lnkF'),
						brand: this.getxmlProdTag(currentNode,'brand'),
						logo: this.getxmlProdTag(currentNode,'logo'),
						delivery: this.getxmlProdTag(currentNode,'delivery'),
						offerta: this.getxmlProdTag(currentNode,'offerta'),
						esclusivo: this.getxmlProdTag(currentNode,'esclusivo'),
						ecologico: this.getxmlProdTag(currentNode,'ecologico'),
						novita: this.getxmlProdTag(currentNode,'novita'),
						sottocosto: this.getxmlProdTag(currentNode,'sottocosto')
					}
					if(prod.desc.length>200){
						prod.desc = prod.desc.substring(0,200)+"...";
					}
					this.prods[ this.prods.length ] = prod;
				}
				this.createStructure();
			}.bind(this)
		});
	},
	createzampa: function(n){
		span = new Element('span',{});
		span.addClassName('zampa_'+n);
		img = new Element('img',{src:'/i/2012/AcquistaOnline/zampa_'+n+'.png'});
		br = new Element('br');
		span.insert(img);
		span.insert(br);
		return span;
	},
	createStructure: function(){
//		var img = new Element('img',{id:'logo',alt:'Hooray!'});
		br = new Element('br');
		
		AO = new Element('div',{id:'AO_Home'});
		AO.addClassName("AO");
			AO_NAV = new Element('div',{});
			AO_NAV.addClassName("AO_NAV");
			AO.insert(AO_NAV);
			
			for(i=0;i<this.prodsToShow;i++){
				prod = this.prods[i];
				AO_NAV.insert(this.createNavElement(i));
			}
			AO_NAVShadow = new Element('div',{});
			AO_NAVShadow.addClassName("AO_NAVShadow");
			AO_NAV.insert(AO_NAVShadow);
			
			this.debug("Prods",this.prods.length);

			AO_PROD = new Element('div',{});
			AO_PROD.addClassName("AO_PROD");
			this.prodDiv = AO_PROD;
			AO.insert(AO_PROD);

				AO_PRODTOP = new Element('div',{});
				AO_PRODTOP.addClassName("AO_PRODTOP");
				AO_PROD.insert(AO_PRODTOP);

					AO_ProdImg = new Element('div',{});
					AO_ProdImg.addClassName("AO_ProdImg");
					AO_PRODTOP.insert(AO_ProdImg);

					AO_Loghi = new Element('div',{});
					AO_Loghi.addClassName("AO_Loghi");
					AO_PRODTOP.insert(AO_Loghi);
					AO_Loghi.insert(this.createzampa('ecologico'));
					AO_Loghi.insert(this.createzampa('esclusivo'));
					AO_Loghi.insert(this.createzampa('novita'));
					AO_Loghi.insert(this.createzampa('offerta'));
					AO_Loghi.insert(this.createzampa('spedizionegratuita'));

				AO_PRODBOTTOM = new Element('div',{});
				AO_PRODBOTTOM.addClassName("AO_PRODBOTTOM");
				AO_PROD.insert(AO_PRODBOTTOM);

					AO_PRODBOTTOML = new Element('div',{});
					AO_PRODBOTTOML.addClassName("AO_PRODBOTTOML");
					AO_PRODBOTTOM.insert(AO_PRODBOTTOML);

//						AO_ProdBrandLogo = new Element('div',{});
//						AO_ProdBrandLogo.addClassName("AO_ProdBrandLogo");
//						AO_PRODBOTTOML.insert(AO_ProdBrandLogo);

//							AO_ProdBrandLogo.insert(new Element('img'));

						AO_ProdBrand = new Element('span',{});
						AO_ProdBrand.addClassName("AO_ProdBrand");
						AO_PRODBOTTOML.insert(AO_ProdBrand);
						AO_PRODBOTTOML.insert(br);

						AO_ProdTitle = new Element('span',{});
						AO_ProdTitle.addClassName("AO_ProdTitle");
						AO_PRODBOTTOML.insert(AO_ProdTitle);

						AO_ProdDesc = new Element('div',{});
						AO_ProdDesc.addClassName("AO_ProdDesc");
						AO_PRODBOTTOML.insert(AO_ProdDesc);

					AO_PRODBOTTOMR = new Element('div',{});
					AO_PRODBOTTOMR.addClassName("AO_PRODBOTTOMR");
					AO_PRODBOTTOM.insert(AO_PRODBOTTOMR);

						AO_ProdPrice = new Element('div',{});
						AO_ProdPrice.addClassName("AO_ProdPrice");
						AO_PRODBOTTOMR.insert(AO_ProdPrice);

							numbers = new Element('div',{});
							numbers.addClassName("numbers");
							AO_ProdPrice.insert(numbers);

						AO_ProdBtn = new Element('div',{});
						AO_ProdBtn.addClassName("AO_ProdBtn");
						AO_PRODBOTTOMR.insert(AO_ProdBtn);

							a = new Element('a',{href:''});
							AO_ProdBtn.insert(a);

								a.insert(new Element('img',{src:'/i/2012/AcquistaOnline/btnAcquistaStore.png'}));
			
		$(this.div).replace(AO);

		this.prodDiv.observe('click', function(a) {
			location.href = this.prods[ this.prod ].lnkS;
		}.bind(this));
		

		this.initPosition();
	},
	initPosition: function(){
		
		$(this.div).select('.AO_NAVel').each(function(a){
			a.setStyle({left: '-100px', position:'relative'})
		})
		
		$(this.div).select('.AO_NAVShadow').each(function(a){
			a.setStyle({opacity: 0, position: 'relative',zIndex:1, left:'-10px'})
		})

		$(this.div).select('.AO_ProdImg').each(function(a){
			a.setStyle({opacity: 0})
		})
		
		$(this.div).select('.AO_Loghi span').each(function(a){
			a.setStyle({left: '100px', position:'relative'})
		})
		
		$(this.div).select('.AO_PRODBOTTOML').each(function(a){
			a.setStyle({left: '400px', position:'relative'})
		})
		
		$(this.div).select('.AO_PRODBOTTOMR').each(function(a){
			a.setStyle({left: '170px', position:'relative'})
		})
		this.startSlideShow();
	},
	getxmlProdTag:function(currentNode, tag){
		return(currentNode.getElementsByTagName( tag )[0].childNodes[0].nodeValue);
//		return currentNode.getElementsByTagName( tag ).item(0).firstChild.nodeValue;
	},
	
	debug: function(l, v, r){
		if(!this.enableDebug){
			return;
		}
		if(this.debugDiv===false){
			return;
		}
		if(v===undefined){
			v = '';
		}

		if(r===true){
			this.debugDiv.update('');
		}
		this.debugDiv.update(this.debugDiv.innerHTML+"<strong>"+l+":</strong>"+v+"<br />");
	},

	startSlideShow: function () {
		if(this.prods.length<this.prodsToShow){
			return;
		}

//		$(this.div).select('.AO_NAVel').each(function(a){
//			new Effect.Move(a, { x: "0px", duration:1, delay:(Math.ceil(a.getAttribute('rel'))*1) });
//		})

		this.debug("Prods",this.prods.length);
		this.debug("StartSlideShow",'OK');
		this.animation = false;
		this.showProd();
//		this.play = setInterval(this.play.bind(this),this.wait);
//		$(this.playButton).hide();
//		$(this.pauseButton).appear({ duration: 0});
//
//		this.updatecounter();
//									
	},
	createNavElement: function(i){
		prod = this.prods[i];
		var NavEl = new Element('div',{ rel:i});
		NavEl.addClassName("AO_NAVel");
		var NavElImg = new Element('div',{ rel:i });
		NavElImg.addClassName("AO_NAVelImg");
		var NavElBrand = new Element('div',{ rel:i });
		NavElBrand.addClassName("AO_NAVelBrand");

		var NavImg = new Element('img',{ rel:i  });
//		NavImg.hide();
		NavImg.onload = function(t){
			maxw = 50;
			maxh = 35;
			w = this.width;
			h = this.height;

			step = (w/maxw>h/maxh ? w/maxw: h/maxh);
			this.width = Math.ceil(w/step);
			this.height = Math.ceil(h/step);
			
/*			this.setStyle({
				width: Math.ceil(w/step)+'px',
				height: Math.ceil(h/step)+'px'
			});*/
//			this.setStyle({
//				marginTop: Math.ceil( ((maxh-element.getHeight())/2)+25 )+'px'
//			});
			this.show();
		};
		NavImg.src = 'http://www.euronics.it'+prod.img;
		NavElImg.insert(NavImg);

//		NavElImg.update('<img rel="'+i+'" src="http://www.euronics.it'+prod.img+'" />';
		NavElBrand.update(prod.brand);
		NavEl.appendChild(NavElImg);
		NavEl.appendChild(NavElBrand);

		NavEl.observe('click', function(a) {
			relValue = a.element().getAttribute('rel');
			if(!this.animation){
				this.stop();
				this.prod = relValue;
				this.showProd();
			}
		}.bind(this));
		
		NavEl.observe('mouseover', function(a) {
			rel = a.element().getAttribute('rel');
			$(this.div).select('.AO_NAVel[rel='+rel+']').each(function(a){
				$(a).addClassName('AO_NAVelSelected');
			});
		}.bind(this));
		
		NavEl.observe('mouseout', function(a) {
			rel = a.element().getAttribute('rel');
			$(this.div).select('.AO_NAVel[rel='+rel+']').each(function(a){
				$(a).removeClassName('AO_NAVelSelected');
			});
		}.bind(this));

		return NavEl;
	},
	getEl: function(c){
		element = $(this.prodDiv).select(c);
//		this.debug("celement", c);
//		this.debug("element", element);
//		this.debug("elementCNT", element.length);
		return(element[0]);
	},
	showProd: function(){
		if(!this.animation){
			this.animProdUscita();
			this.fillProd.bind(this).delay(1);
			this.animProdEntrata.bind(this).delay(1);
		}
   	},
	animProdUscita: function(){
		this.debug("animProdUscita",'inizio');
		
		this.animation = true;

		$(this.div).select('.AO_NAVShadow').each(function(a){
			new Effect.Opacity(a, {from:1, to: 0, duration: 0.2});
			new Effect.Move(a, {x:this.getNewLeft(a,-10), duration: 0.5 });
		}.bind(this))

		$(this.div).select('.AO_NAVel').each(function(a){
//			this.debug('x',this.getNewLeft(a,0));
			new Effect.Move(a, {x: this.getNewLeft(a,-10), duration: 0.5})
		}.bind(this))


		$(this.div).select('.AO_ProdImg').each(function(a){
			new Effect.Opacity(a, { from: 1, to: 0, duration: 1 })
		}.bind(this))
		
		$(this.div).select('.AO_Loghi span').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,100)})
		}.bind(this))
		
		$(this.div).select('.AO_PRODBOTTOML').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,400)})
		}.bind(this))
		
		$(this.div).select('.AO_PRODBOTTOMR').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,400)})
		}.bind(this))
		this.debug("animProdUscita",'fine');
	},
	animProdEntrata: function(){

		this.debug("animProdEntrata",'inizio');
		$(this.div).select('.AO_NAVel').each(function(a){
			a.setStyle({zIndex:0})
		}.bind(this))

		$(this.div).select('.AO_NAVel[rel='+this.prod+']').each(function(a){
			a.setStyle({zIndex:2})
			new Effect.Move(a, {x: this.getNewLeft(a,0), duration: 0.5})
		}.bind(this))

		$(this.div).select('.AO_NAVShadow').each(function(a){
			a.setStyle({top: '-'+((63*(this.prods.length-this.prod))+18)+'px'})
//			alert(this.prod);
//-208px-252px
			new Effect.Opacity(a, {from:0, to: 1, duration: 0.2});
			new Effect.Move(a, {x:this.getNewLeft(a,0), duration: 0.5 });
		}.bind(this))

		$(this.div).select('.AO_ProdImg').each(function(a){
			new Effect.Opacity(a, { from: 0, to: 1, duration: 1 })
		}.bind(this))
		
		$(this.div).select('.AO_Loghi span').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,0)})
		}.bind(this))
		
		$(this.div).select('.AO_PRODBOTTOML').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,0)})
//			new Effect.Move(a, {x: -400})
		}.bind(this))
		
		$(this.div).select('.AO_PRODBOTTOMR').each(function(a){
			new Effect.Move(a, {x: this.getNewLeft(a,0)})
		}.bind(this))
		this.animation = false;
		this.debug("animProdEntrata",'fine');
		if(this.auto){
//			this.playInt = setInterval(this.play.bind(this),this.wait);
			this.playInt = setTimeout(this.play.bind(this),this.wait);
		}
	},
	fillProd: function(){
		this.debug("fillProd",this.prod);
		prod = this.prods[ this.prod ];

		this.getEl('.AO_ProdBrand').update(prod.brand);
		this.getEl('.AO_ProdTitle').update(prod.titolo);
		this.getEl('.AO_ProdDesc').update(prod.desc);
//		this.getEl('.AO_ProdBrandLogo').update('<img src="http://www.euronics.it'+prod.logo+'" />');
//		this.getEl('.AO_ProdPrice').update(prod.prezzo;
		this.getEl('.AO_ProdBtn a').setAttribute('href', prod.lnkS);
		if(prod.ecologico=='true'){
			this.getEl('.AO_Loghi .zampa_ecologico').show();
		} else {
			this.getEl('.AO_Loghi .zampa_ecologico').hide();
		}
		if(prod.esclusivo=='true'){
			this.getEl('.AO_Loghi .zampa_esclusivo').show();
		} else {
			this.getEl('.AO_Loghi .zampa_esclusivo').hide();
		}
		if(prod.novita=='true'){
			this.getEl('.AO_Loghi .zampa_novita').show();
		} else {
			this.getEl('.AO_Loghi .zampa_novita').hide();
		}
		if(prod.offerta=='true'){
			this.getEl('.AO_Loghi .zampa_offerta').show();
		} else {
			this.getEl('.AO_Loghi .zampa_offerta').hide();
		}
		if(prod.delivery=='true'){
			this.getEl('.AO_Loghi .zampa_spedizionegratuita').show();
		} else {
			this.getEl('.AO_Loghi .zampa_spedizionegratuita').hide();
		}
		if(this.getEl('.AO_ProdImg img')!==undefined){
			this.getEl('.AO_ProdImg img').remove()
		}
		var img = new Element('img',{});
		img.hide();
		img.onload = function(t){
			maxw = 250;
			maxh = 150;
			this.debug("t", t);
			this.debug("t", $(this));

			var element = $(this.getEl('.AO_ProdImg img'));
			this.debug("element", element);
			w = element.getWidth();
			h = element.getHeight();
			if(w>maxw || h>maxh){
				step = (w/maxw>h/maxh ? w/maxw: h/maxh);
				newwidth = Math.ceil(w/step)+'px';
				newheight = Math.ceil(h/step)+'px';
			} else {
				newwidth = Math.ceil(w);
				newheight = Math.ceil(h);
			}

			element.setStyle({
				width: newwidth,
				height: newheight 
			});
			element.setStyle({
				marginTop: Math.ceil( ((maxh-element.getHeight())/2)+25 )+'px'
			});
			element.show();
		}.bind(this);
		$(this.getEl('.AO_ProdImg')).insert(img);
		img.src = 'http://www.euronics.it'+prod.img;

		this.getEl('.AO_ProdPrice .numbers').update(this.generateNumbers(prod.prezzo))
	},
	getNewLeft: function(el,newpos){
		pos = el.getStyle('left');
		currentpos = Math.ceil(pos.substring(0,(pos.length-2)));
		return newpos-currentpos;
	},
	generateNumbers: function(euro){
		bigImages = true;
		html = '';
		for(var n=0;n<euro.length;n++){
			nn = euro.substring(n,n+1);
			if(nn==','){
				break;
				bigImages = false;
				html = '<div class="numS numV_S"></div>'+html;
			} else {
				if(bigImages){
					html = '<div class="numB num'+nn+'_B"></div>'+html;
				} else {
					html = '<div class="numS num'+nn+'_S"></div>'+html;
				}
			}
		}
		return html;
	},
	play: function () {
		this.prod = this.prod+1;
		if(this.prod>=this.prods.length){
			this.prod = 0;
		}
		this.showProd();
	},
	stop: function  () {
		clearInterval(this.playInt);				
	}
};

