// Oggetto bag

// Questo č l'oggetto che gestisce il drag & drop e la costruzione della borsa

// Ho predisposto una stringa di 7 caratteri cosė composta: 1 e 7 carattere Lati borsa - da due a 6 le varie fasce
// Possono essere cosė valorizzati:
// _ Vuoto
// W Bianco
// B Blu
// R Rosso
// G Verde
// N Nero
// P Viola
// M Marrone

function Bag(element)
{
	this.elementi = 1;
	this.prezzoborsa = 0;
    this.prezzofasce = 0;
	this.prezzo = 0;
	this.bag_string_default = "WW____W";
	this.bag_string = this.bag_string_default;
	this.colori = [];	
	this.colori._ = "_";
	this.colori.W = "bianco";
	this.colori.B = "blu";
	this.colori.R = "rosso";
	this.colori.G = "verde";
	this.colori.N = "nero";
	this.colori.P = "viola";
	this.colori.M = "marrone";
	this.colori.bianco = "W";
	this.colori.blu = "B";
	this.colori.rosso = "R";
	this.colori.verde = "G";
	this.colori.nero = "N";
	this.colori.viola = "P";
	this.colori.marrone = "M";
	
	this.offset = 130;
	
	/* Vista a sx */
	
	this.fasceborsa_sx = new Element('div',{'id':'fasceborsa_sx'});
	this.fasce_sx = [];
	this.fasce_img_sx = [];
	this.fascia_s_sx = new Element('div',{'id':'fascia_s_sx','class':'droppable','index':'sx'});
	this.fascia_s_img_sx = new Element('img',{'src':'images/shop/bag/bianco_s_sx.gif'});
	this.fascia_s_img_sx.inject(this.fascia_s_sx);
	this.fascia_s_sx.inject(this.fasceborsa_sx);
	var i=0;
	for (i=0;i<5;i++)
		{
			this.fasce_img_sx[i] = new Element('img',{'src':'images/shop/bag/bianco_f_sx.gif'});
			this.fasce_sx[i] = new Element('div',{'id':'f_sx'+(i+1),'class':'droppable f_sx','index':i});
			this.fasce_sx[i].setStyles({ 'z-index': 26-i });
			this.fasce_img_sx[i].setStyles({ 'margin-top': -7*(i+1) });
			this.fasce_img_sx[i].setStyles({ 'margin-left': -47 });
			this.fasce_img_sx[i].inject(this.fasce_sx[i]);
			this.fasce_sx[i].inject(this.fasceborsa_sx);
			this.fasce_sx[i].hide();
		}
	this.fascia_e_sx = new Element('div',{'id':'fascia_e_sx'});
	this.fascia_e_img_sx = new Element('img',{'src':'images/shop/bag/bianco_e_sx.gif'});
	this.fascia_e_img_sx.inject(this.fascia_e_sx);
	this.fascia_e_sx.inject(this.fasceborsa_sx);
	this.fascia_e_sx.setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	this.fasce_sx[0].show();
	this.fasceborsa_sx.inject(element);
	
	/* Vista centrale */
	
	this.fasceborsa = new Element('div',{'id':'fasceborsa'});
	this.fasce = [];
	this.fasce_img = [];
	this.fascia_s = new Element('div',{'id':'fascia_s'});
	this.fascia_s_img = new Element('img',{'src':'images/shop/bag/bianco_s.gif'});
	this.fascia_s_img.inject(this.fascia_s);
	this.fascia_s.inject(this.fasceborsa);
	for (i=0;i<5;i++)
		{
			this.fasce_img[i] = new Element('img',{'src':'images/shop/bag/bianco_f.gif'});
			this.fasce[i] = new Element('div',{'id':'f'+(i+1),'class':'droppable','index':i});
			this.fasce[i].setStyles({ 'z-index': 26-i });
			this.fasce_img[i].inject(this.fasce[i]);
			this.fasce[i].inject(this.fasceborsa);
			this.fasce[i].hide();
		}
	this.fascia_e = new Element('div',{'id':'fascia_e'});
	this.fascia_e_img = new Element('img',{'src':'images/shop/bag/bianco_e.gif'});
	this.fascia_e_img.inject(this.fascia_e);
	this.fascia_e.inject(this.fasceborsa);
	this.fasce[0].show();
	this.fasceborsa.inject(element);
	
	/* Vista dx */

	this.fasceborsa_dx = new Element('div',{'id':'fasceborsa_dx'});
	this.fasce_dx = [];
	this.fasce_img_dx = [];
	this.fascia_s_dx = new Element('div',{'id':'fascia_s_dx'});
	this.fascia_s_img_dx = new Element('img',{'src':'images/shop/bag/bianco_s_dx.gif'});
	this.fascia_s_img_dx.inject(this.fascia_s_dx);
	this.fascia_s_dx.inject(this.fasceborsa_dx);
	for (i=0;i<5;i++)
		{
			this.fasce_img_dx[i] = new Element('img',{'src':'images/shop/bag/bianco_f_dx.gif'});
			this.fasce_dx[i] = new Element('div',{'id':'f_dx'+(i+1),'class':'droppable f_dx','index':i});
			this.fasce_dx[i].setStyles({ 'z-index': 22+i });
			// Va fatto dinamicamente
			// this.fasce_img_dx[i].setStyles({ 'margin-top': -7*((4-i)+1) });
			this.fasce_img_dx[i].setStyles({ 'margin-right': 47 });
			this.fasce_img_dx[i].inject(this.fasce_dx[i]);
			this.fasce_dx[i].inject(this.fasceborsa_dx);
			this.fasce_dx[i].hide();
		}
	this.fascia_e_dx = new Element('div',{'id':'fascia_e_dx','class':'droppable','index':'dx'});
	this.fascia_e_img_dx = new Element('img',{'src':'images/shop/bag/bianco_e_dx.gif'});
	this.fascia_e_img_dx.inject(this.fascia_e_dx);
	this.fascia_e_dx.inject(this.fasceborsa_dx);
	this.fascia_s_dx.setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	this.fasce_dx[0].show();
	this.fasceborsa_dx.inject(element);
	// Sistemo il margin-top dinamicamente
	this.Scala();			
}

Bag.prototype.CentraBorsa = function()
{	
	this.posizioneBorsa =    (($('Q1').getWidth() * (1 / 2)) - this.fasceborsa.getWidth()/2);
	this.posizioneBorsa_sx = (($('Q1').getWidth() * (1 / 3)) - this.fasceborsa_sx.getWidth()/2) - this.offset;
	this.posizioneBorsa_dx = (($('Q1').getWidth() * (2 / 3)) - this.fasceborsa_dx.getWidth()/2) + this.offset;
	this.fasceborsa.setStyles({ 'left' : this.posizioneBorsa});	
	this.fasceborsa_sx.setStyles({ 'left' : this.posizioneBorsa_sx});	
	this.fasceborsa_dx.setStyles({ 'left' : this.posizioneBorsa_dx});
};

Bag.prototype.Show = function()
{
	this.fasceborsa.get('morph').start({
										'opacity': [0,1]
										});
	this.fasceborsa_sx.get('morph').start({
										'opacity': [0,1]
										});
	this.fasceborsa_dx.get('morph').start({
										'opacity': [0,1]
										});									
};

Bag.prototype.Fasce = function()
{
	return this.elementi;
};

Bag.prototype.Prezzo = function()
{
	return this.prezzo;
};

Bag.prototype.Colori = function()
{
	return this.bag_string;
};

Bag.prototype.Aggiungi = function ()
{
	if (this.elementi < 5)
		{
		this.SetColor(this.elementi,'bianco');
		this.elementi = this.elementi+1;
		$('f'+this.elementi).show();
		$('f_sx'+this.elementi).show();
		$('f_dx'+this.elementi).show();		
		}	
	$('fascia_e_sx').setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	$('fascia_s_dx').setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	this.Scala();
	this.CentraBorsa();
	this.CalcolaPrezzo();
};

Bag.prototype.Scala = function ()
{
	var i=0;
	for (i=0;i<this.elementi;i++)
		{
		this.fasce_img_dx[i].setStyles({ 'margin-top': -7*((this.elementi-i)) });	
		}
};

Bag.prototype.Elimina = function ()
{	
	if (this.elementi > 1)
		{
		this.SetColor(this.elementi-1,'_');
		$('f'+this.elementi).hide();
		$('f_sx'+this.elementi).hide();
		$('f_dx'+this.elementi).hide();
		this.elementi = this.elementi-1;
		}
	$('fascia_e_sx').setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	$('fascia_s_dx').setStyles({ 'margin-top' : 85-(this.elementi*9)});	
	this.Scala();
	this.CentraBorsa();
	this.CalcolaPrezzo();
};

Bag.prototype.SetBag = function(bag_string)
{
	this.SetColor("sx",this.colori[bag_string.charAt(0)]);
	var j=0;
	for (j=1;j<6;j++)
		{
		if (bag_string.charAt(j) !== "_")
			{
			if (this.elementi < j)
				{
				this.Aggiungi();
				}	
			this.SetColor(j-1,this.colori[bag_string.charAt(j)]);
			}
		else
			{
				this.Elimina();
			}
		}
	this.SetColor("dx",this.colori[bag_string.charAt(6)]);
	this.bag_string = bag_string;
};

Bag.prototype.SetBagString = function(index,color)
{
	if (index === 'sx')
		{
		index = 0;
		}
	else {
		if (index === 'dx')
			{
			index = 6;
			}
		else
			{
			index = parseInt(index,10) + 1;
			}
		}
	this.bag_string = this.bag_string.replaceAt(index,this.colori[color]);
};

Bag.prototype.SetColor = function(index,color)
{
	if ((index!=='sx')&&(index!=='dx'))
		{
		this.fasce_img[index].setProperty('src','images/shop/bag/' + color + '_f.gif');
		this.fasce_img_sx[index].setProperty('src','images/shop/bag/' + color + '_f_sx.gif');
		this.fasce_img_dx[index].setProperty('src','images/shop/bag/' + color + '_f_dx.gif');	
		}
	else
		{
		if (index === 'sx')
			{
			this.fascia_s_img_sx.setProperty('src','images/shop/bag/' + color + '_s_sx.gif');
			this.fascia_s_img.setProperty('src','images/shop/bag/' + color + '_s.gif');			
			}
		else
			{
			this.fascia_e_img_dx.setProperty('src','images/shop/bag/' + color + '_e_dx.gif');
			this.fascia_e_img.setProperty('src','images/shop/bag/' + color + '_e.gif');
			}
		}
	this.SetBagString(index,color);
	//Sito.CambiaColore();
};

Bag.prototype.SetPrice = function(borsa,fasce)
{
	this.prezzoborsa = borsa;
    this.prezzofasce = fasce;
};

Bag.prototype.CalcolaPrezzo = function()
{
	this.prezzo = this.prezzoborsa + (this.prezzofasce * (this.elementi-1));
};

Bag.prototype.StartDnD = function ()
{
	document.ondragstart = function () { return false; }; 
	$$('.dragable').each(function(drag) {
		var myMove = new Drag.Move(drag, { 
			droppables: '.droppable',
			onDrop: function(el,droppable) {
											if (droppable)
												{
												Shop.MyBag.SetColor(droppable.get('index'),el.get('rel'));
												drag.setStyles({ 'top':0, 'left':0, 'margin-right':'7px' });
												}
											else
												{
												drag.setStyles({ 'top':0, 'left':0, 'margin-right':'7px' });
												}
											} /*,
			onEnter: function(el,droppable) {
			},
			onLeave: function(el,droppable) {
			}*/
		});
	});
};
	

