// Definizione dell'oggetto Shop che gestisce
// la sezione Shop

var Shop = {

	empty: true,
	dragmessage: true,
	MyBag: [],
	Spedizione: [],
	Open: true,
	mnt_message: '',
	colori: [],
	
	Opened: function()
	{
		this.Open = true;
	},
	
	Closed: function(message)
	{
		this.Open = false;
		this.mnt_message = message;
	},
	
	SetColor: function(colori)
	{
		this.colori = JSON.decode(colori);
	},
	
	StartShop: function()
	{
		var i=0;
		Sito.StopHoverTitoli();
		if (this.empty)
			{
			$('Q1').empty();
			$('Q2').empty();
			$('Q3').empty();
			$('Q1').removeEvents('mouseenter');
			$('Q1').removeEvents('mouseleave');
			$('Q2').removeEvents('mouseenter');
			$('Q2').removeEvents('mouseleave');
			$('Q3').removeEvents('mouseenter');
			$('Q3').removeEvents('mouseleave');
			$('Q1').set('styles',{'top':'21px','height':'417px','width':'836px'});
			$('Q2').set('styles',{'top':'21px','height':'417px','width':'272px'});
			$('Q3').set('styles',{'top':'21px','height':'417px','width':'272px'});
			$('Q1').setStyle('display', 'inline');
			$('Q2').setStyle('display', 'none');
			$('Q3').setStyle('display', 'none');
			$('Q4').setStyle('display', 'none');
			$('body').setStyles({'overflow' : 'auto'});
			$('piva').setStyles({'visibility' : 'visible'});
			
			// Preload immagini borsa
			
			var immagini_borsa = ['images/shop/bag/bianco_e.gif',
									'images/shop/bag/bianco_e_dx.gif',
									'images/shop/bag/bianco_e_sx.gif',
									'images/shop/bag/bianco_f.gif',
									'images/shop/bag/bianco_f_dx.gif',
									'images/shop/bag/bianco_f_sx.gif',
									'images/shop/bag/bianco_s.gif',
									'images/shop/bag/bianco_s_dx.gif',
									'images/shop/bag/bianco_s_sx.gif',
									'images/shop/bag/blu_e.gif',
									'images/shop/bag/blu_e_dx.gif',
									'images/shop/bag/blu_f.gif',
									'images/shop/bag/blu_f_dx.gif',
									'images/shop/bag/blu_f_sx.gif',
									'images/shop/bag/blu_s.gif',
									'images/shop/bag/blu_s_sx.gif',
									'images/shop/bag/nero_e.gif',
									'images/shop/bag/nero_e_dx.gif',
									'images/shop/bag/nero_f.gif',
									'images/shop/bag/nero_f_dx.gif',
									'images/shop/bag/nero_f_sx.gif',
									'images/shop/bag/nero_s.gif',
									'images/shop/bag/nero_s_sx.gif',
									'images/shop/bag/rosso_e.gif',
									'images/shop/bag/rosso_e_dx.gif',
									'images/shop/bag/rosso_f.gif',
									'images/shop/bag/rosso_f_dx.gif',
									'images/shop/bag/rosso_f_sx.gif',
									'images/shop/bag/rosso_s.gif',
									'images/shop/bag/rosso_s_sx.gif',
									'images/shop/bag/verde_e.gif',       
									'images/shop/bag/verde_e_dx.gif',    
									'images/shop/bag/verde_f.gif',
									'images/shop/bag/verde_f_dx.gif',    
									'images/shop/bag/verde_f_sx.gif',    
									'images/shop/bag/verde_s.gif',       
									'images/shop/bag/verde_s_sx.gif',
									'images/shop/bag/viola_e.gif',       
									'images/shop/bag/viola_e_dx.gif',    
									'images/shop/bag/viola_f.gif',
									'images/shop/bag/viola_f_dx.gif',    
									'images/shop/bag/viola_f_sx.gif',    
									'images/shop/bag/viola_s.gif',       
									'images/shop/bag/viola_s_sx.gif',
									'images/shop/bag/marrone_e.gif',       
									'images/shop/bag/marrone_e_dx.gif',    
									'images/shop/bag/marrone_f.gif',
									'images/shop/bag/marrone_f_dx.gif',    
									'images/shop/bag/marrone_f_sx.gif',    
									'images/shop/bag/marrone_s.gif',       
									'images/shop/bag/marrone_s_sx.gif'
									];
		
			for (i=0;i<immagini_borsa.length;i++)
				{
					var immagini_borsa_img = new Element('img',{'src':immagini_borsa[i],'alt':''});
					immagini_borsa_img.inject('preload');	
				}
			} 
	
		  //Sito.CambiaColore();
		  
		  /* Inserisco le cose che mi servono */
		  
		  var greybar = new Element('div',{'id':'greybar'});
		  
		  var makeyourbag = new Element('div',{'id':'makeyourbag','text':'Create your own Bag'});
		  makeyourbag.inject('Q1');
		  
		  var titolopalette = new Element('div',{'id':'titolopalette','text':'Colors'});
		  var titoloextension = new Element('div',{'id':'titoloextension','text':'Expansion'});
		  titolopalette.inject('Q1');
		  titoloextension.inject('Q1');
		  
		  var palettecolori = new Element('div',{'id':'palettecolori'});
		  var palettecolori_static = new Element('div',{'id':'palettecolori_static'});
		 
		  var mycolori = [];
		  var mycolori_static = [];
		 
		  var colorivar = ['rosso','nero','bianco','blu','verde','viola','marrone'];
		  var jcol = 0;
		  for (i=0;i<7;i++)
			{		
				if (this.colori[colorivar[i]] === '1')
					{
					mycolori[jcol] = new Element('div',{'id':'c'+(i+1),'class':'dragable','rel':colorivar[i]});
					mycolori_static[jcol] = new Element('div',{'id':'c_static_'+(i+1)});
					mycolori[jcol].inject(palettecolori);
					mycolori_static[jcol].inject(palettecolori_static);
					jcol = jcol + 1;
					}
			}
		  palettecolori.inject(greybar);
		  palettecolori_static.inject(greybar);
		  
		  var extension = new Element('div',{'id':'extension'});
		  var ext_n = new Element('input',{'type':'text','name':'ext_n','id':'ext_n','value':'1','readonly':'readonly'});
		  ext_n.inject(extension);
		  var extension_btn = new Element('div',{'id':'extension_btn'});
		  var aggiungi = new Element('div',{'id':'aggiungi'});
		  var elimina = new Element('div',{'id':'elimina'});
		  aggiungi.inject(extension_btn);
		  elimina.inject(extension_btn);
		  
		  extension.inject(greybar);
		  extension_btn.inject(greybar);
		  
		  var dragthecolor = new Element('div',{'id':'dragthecolor','text':'Drag the colors on the bag.'});
		  dragthecolor.inject(greybar);
		  
		  /*
		  dragthecolor.set('morph', {duration: 2500});
		  dragthecolor.get('morph').addEvents({
			'complete': function() {
									if (Shop.dragmessage)
										{
										if ($('dragthecolor') !== null)
											{
											$('dragthecolor').get('morph').start({
															'opacity': [1,0.2]
															});
											Shop.dragmessage = false;
											}
										}
									else
										{
										if ($('dragthecolor') !== null)
											{
											$('dragthecolor').get('morph').start({
																'opacity': [0.2,1]
																});
											Shop.dragmessage = true;
											}
										}
									}
									});*/
									
			if (this.Open)
				  {
				  var buy = new Element('div',{'id':'buy'});
				  var buy_button = new Element('a',{'class':'button', 'href':'#'});
				  var buy_text = new Element('span',{'text':'BUY'});
				  buy_text.inject(buy_button);
				  buy_button.inject(buy);
				  buy.inject(greybar);
				  buy.addEvents({
					click:function(){
									Carrello.Aggiungi(Shop.MyBag.Colori(),Shop.MyBag.prezzo);
									$('bag_n').set('value',Carrello.Contatore());
									Carrello.AggiornaTotale();
									Carrello.SalvaCarrello();
									Shop.Nascondi();
									Carrello.Mostra();
									//Sito.CambiaColore();
									}
								  });
				  var landing_text = new Element('div',{'id':'landing_text','text':''});
				  var landing_image = new Element('img',{'id':'landing_image','src':'images/shop/borsone.gif'});
				  var landing_space = new Element('div',{'id':'landing_space'});
				  landing_text.inject(landing_space);
				  landing_image.inject(landing_space);
				  landing_space.inject('Q1');
				  landing_space.addEvents({
					click:function(){
										this.setStyle('display', 'none');	
										//Sito.CambiaColore();
									}
								  });	
				  }
		  
		  greybar.inject('Q1');
		  
		  if (this.Open)
				  {
			  var blackbar = new Element('div',{'id':'blackbar'});
			  
			  var cost_n = new Element('input',{'type':'text','name':'cost_n','id':'cost_n','value':'0.00','readonly':'readonly'});
			  cost_n.inject(blackbar);
			  
			  var cost = new Element('div',{'id':'cost','text':'COST'});
			  cost.inject(blackbar);
			  
			  var order = new Element('div',{'id':'order'});
			  var order_button = new Element('a',{'class':'button_small', 'href':'#'});
			  var order_text = new Element('span',{'text':'order'});
			  order_text.inject(order_button);
			  order_button.inject(order);
			  order.inject(blackbar);
			  order.addEvents({	
					click:function(){
									Carrello.Aggiungi(Shop.MyBag.Colori(),Shop.MyBag.prezzo);
									$('bag_n').set('value',Carrello.Contatore());
									Carrello.AggiornaTotale();
									Carrello.SalvaCarrello();
									//Sito.CambiaColore();
									}
								  });
			  
			  var carrello = new Element('img',{'id':'carrello','src':'images/shop/carrello.gif'});
			  carrello.inject(blackbar);
			  carrello.addEvents({
					click:function(){
									Shop.Nascondi();
									Carrello.Mostra();
									//Sito.CambiaColore();
									}
								  });
			  
			  var bag_n = new Element('input',{'type':'text','name':'bag_n','id':'bag_n','value':'0','readonly':'readonly'});
			  bag_n.inject(blackbar);
			  
			  blackbar.inject('Q1');
				}
		  else
				{
				var maintenance = new Element('div',{'id':'maintenance','text':this.mnt_message});
				maintenance.inject('Q1');
				}
		  
		  aggiungi.addEvents({
					click:function(){
									Shop.MyBag.Aggiungi();
									$('ext_n').set('value',Shop.MyBag.Fasce());
									Shop.EsponiPrezzo();
									//Sito.CambiaColore();
									}
								  });
		  elimina.addEvents({
					click:function(){
									Shop.MyBag.Elimina();	
									$('ext_n').set('value',Shop.MyBag.Fasce());
									Shop.EsponiPrezzo();
									//Sito.CambiaColore();
									}
								  });
		  
		  /* Creo la borsa */
		  
		  this.MyBag = new Bag('Q1');
		  
		  /* Avvio l'animazione */
		  
									$('greybar').get('morph').start({
															'opacity': [0,1]
															});
									if (Shop.Open)
										{
										$('blackbar').get('morph').start({
															'opacity': [0,1]
															});
										Carrello.CaricaPrezzi();
										Carrello.StartCarrello();
										Carrello.CaricaCarrello();
										Login.StartLogin();
										Shop.EsponiPrezzo();
										}
									else
										{
										$('maintenance').get('morph').start({
															'opacity': [0,1]
															});
										}
										
									$('makeyourbag').get('morph').start({
															'opacity': [0,1]
															});
															
									$('titolopalette').get('morph').start({
															'opacity': [0,1]
															});
									$('titoloextension').get('morph').start({
															'opacity': [0,1]
															});
									$('extension').get('morph').start({
															'opacity': [0,1]
															});
									$('extension_btn').get('morph').start({
															'opacity': [0,1]
															});
									$('dragthecolor').get('morph').start({
															'opacity': [0,1]
															});															
									Shop.MyBag.CentraBorsa();
									Shop.MyBag.Show();
									Shop.MyBag.StartDnD();
									Sito.StartHoverTitoli();
									
									// #### DEBUG ####
									//Shop.Nascondi();
									//Carrello.Mostra();
									//Login.Mostra();
												
		  this.empty = false;
	},
	
	CloseShop: function()
	{
		if (!this.empty)
			{
			try
				{
				$('onlineshop').removeClass('titolorosso');
				$('onlineshop').addClass('titolonero');
				$('dragthecolor').removeEvents('complete');
				$('Q1').setStyles({'width' : '272px'});
				$('Q2').setStyles({'width' : '272px', 'left':'369px','display':'inline'});
				delete this.MyBag;
				this.empty = true;
				}
			catch(e)
				{
				alert(e.message);
				}
			}
	},
	
	EsponiPrezzo: function()
	{
		if (this.Open)
			{
			this.MyBag.CalcolaPrezzo();
			$('cost_n').set('value','\u20ac '+Shop.MyBag.Prezzo()+'.00');
			}
	},
	
	Mostra: function()
	{
	$$('Q1').each(function(el){
								el.setStyle('display', 'inline');	
							  });
    $('Q1').setStyle('display', 'inline');
	},
	
	Nascondi: function()
	{
	$$('Q1').each(function(el){
								el.setStyle('display', 'none');	
							  });
    $('Q1').setStyle('display', 'none');	
	},
	
	ThankYou: function()
	{
		Cookie.dispose('carrello');
		$('landing_text').erase('text');
		$('landing_text').set('html','The transaction was successfully completed. <span class="testorosso">Thank you and Merry Autumn</span>');
		$('landing_space').setStyle('display', 'inline');
	},
	
	Sorry: function()
	{
		$('landing_text').erase('text');
		$('landing_text').set('html','The transaction has failed. <span class="testorosso">Thank you and Merry Autumn</span>');
		$('landing_space').setStyle('display', 'inline');
	}
};
