function createHome(){
	new Accordion($$('div.accorder2'), $$('div.accorded2'),{alwaysHide: false,fixedHeight: 380,display: 0});
	$$('div.vrant_cnt_thumbs').each(function(sing){
		sing.addEvent('mouseenter',function(){
			sing.setStyle('background' , 'transparent url(http://www.gruppopreziosi.com/inc/img/menus/dpz_vncnt_thumbs2.jpg) no-repeat bottom center');
		});
	});

	getBrAcc();
	getPdtHL(0,'newHL');
	getPdtHL(1,'rilievoHL');
	getPdtHL(2,'offerteHL');
	
	var myTabSwapper = new TabSwapper({
		selectedClass: 'dp_vnhead_on',
		deselectedClass: 'dp_vnhead_off',
		tabSelector: "#myTabs li",
		clickSelector: "#myTabs li a",
		sectionSelector: "#myContent div",
		name: "myTabSwapper",
		smooth: true
   });

	new Accordion($$('div.vetrina_c'), $$('div.vetrina_o'),{ //ACCORDION VETRINA
		alwaysHide: false,
		fixedHeight: '210px',
		opacity: false,
		onActive: function(toggler, element){
			toggler.removeClass('dp_vetrina_box_clsb');
			toggler.addClass('dp_vetrina_box_cls');
		},
		onBackground: function(toggler, element){
			toggler.removeClass('dp_vetrina_box_cls');
			toggler.addClass('dp_vetrina_box_clsb');
		}
	});
	$$('#dp_cnt_top span').each(function(sp){
		if(sp) sp.remove();
	});
}

function getPdtHL(pid,div){ //Prodotti d'impulso
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=6&p='+pid;
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		update : $(div)
	}).request();
}

function getContent(caso,p,tipo,p2,prof){
		var indice = 0;
		var uss = [];
		uss[indice] = caso;
		indice++;
		if(!p | p == 'undefined'){
		} else {
			uss[indice] = p;
			indice++;
		}
		if(!tipo | tipo == 'undefined'){
			uss[indice] = 0;
			indice++;
		} else {
			uss[indice] = tipo;
			indice++;
		}
		if(!p2 | p2 == 'undefined') {
			var p2 = '';
		} else {
			var p2 = p2;
			uss[indice] = p2;
			indice++;
		}
		if(!prof | prof == 'undefined') {
			var prof = 1;
			uss[indice] = 0;
			indice++;
		} else {
			var prof = prof
			uss[indice] = prof;
			indice++;
		}
		var his = '';
		var a = 0;
		uss.each(function(un){
			his = his+''+un;
			if(a!=(indice-1))
				his = his+'-';
			a++
		});
	if(caso == 12){
		createHome();
	}
	var url_param = Object.toQueryString({'s' : caso, 'p' : p, 'tipo' : tipo, 'p2' : p2, 'prof' : prof});
	var url = "http://www.gruppopreziosi.com/inc/ecomm.content.php?"+url_param;	
	if(caso == 10){
		var coun = 0;
		if($('vettore')){
			var vett = $('vettore').getProperty('value');
			coun++;
		}
		if($('pagamento')){
			var paga = $('pagamento').getProperty('value');
			coun++;
		}
		if(coun==2){
			url = url + '&vett='+vett+'&paga='+paga;
		} else {
			url = "http://www.gruppopreziosi.com/inc/ecomm.content.php?s=9";	
		}
	}
	
	if($('dp_vetrina')) $('dp_vetrina').remove();
	if($('dp_vn')){
		$('dp_vn').remove();
		$('dp_offerta').remove();
	}

	var sender = new Ajax(url,{
		autoCancel: true,
		method: 'get',
		evalScripts: true,
		onRequest: function(){
			goTop();
			if($('dp_gbl')){
				$('dp_gbl').setHTML('loading...');
			}
		},
		update: $('dp_gbl')
	});
	sender.request();
}

function createPath2(content,titolo,prof){
	if(prof == 1){
		$$('#dp_cnt_top span').each(function(sp){
			sp.remove();
		});
	}

	$$('#dp_cnt_top span').each(function(sp){
		if(sp){
			var numero = sp.getProperty('id');
			numero = numero.substr(14).toInt();
			if(numero > prof ){
				sp.remove();
			}
		}
	});

	if($('menu_temp_path'+prof)) $('menu_temp_path'+prof).remove();
	var menu = $('menu_path').innerHTML;
	if($(titolo)) var testo = $(titolo).getText(); else  var testo = titolo;
	var arr_hash = content.split(',');
	var str_hash = '';
	arr_hash.each(function(hashin){
		str_hash = str_hash + hashin+'-';
	});
	var le = str_hash.length.toInt()-1;
	var hash2 = str_hash.substring(0,le)
	$('menu_path').empty().setHTML(menu+'<span id="menu_temp_path'+prof+'"><a href="#'+hash2+'" onclick="window.hash(\''+hash2+'\');createPath2(\''+content+'\',\''+testo+'\','+prof+');getContent('+content+');"> &#8250; '+testo+'</a></span>');
}

function findThis(val){
	if(val.length != 0 & val.length > 2){
		getContent('find',val,'no_sel');
	} else if(val.length == 0){
		getContent('find','','no_sel');
	}
}

function selThis(id){
	if($(id)){
		if(id.substr(0,3)=='all'){
			$$('a.unsel').each(function(menu){
				menu.setStyles({
					'font-weight':'',
					'color' : '',
					'background':''
				});
			});
			return;
		}
		$$('a.unsel').each(function(menu){
			menu.setStyles({
				'font-weight':'',
				'color' : '',
				'background':''
			});
		});

		$(id).setStyles({
			'color': '#FFF',
			'font-weight':'bold',
			'background':'transparent url(http://www.gruppopreziosi.com/inc/img/menus/dpz_sx_catsel.jpg) no-repeat top center'
		});

	}
}

function getActions(action,sex,eta,price_min,price_max,prof){
	if(!sex) var sex = 9;
	if(!eta) var eta = 1000;
	if(!price_min) var price_min = 0;
	if(!price_max) var price_max = 0;
	if(!prof) var prof = 1;
	var url_param = Object.toQueryString({'s' : action, 'sex' : sex, 'eta' : eta, 'price_min' : price_min, 'price_max' : price_max, 'prof' : prof});
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	if($('dp_vetrina')) $('dp_vetrina').remove();
	if($('dp_vn')) $('dp_vn').remove();
	if($('dp_offerta')) $('dp_offerta').remove();
	if($('dp_gbl')) $('dp_gbl').remove();
	new Element('div').setProperties({'id': 'dp_gbl','class':'dp_gbl'}).injectAfter('dp_search');
	var jsonCart = new Ajax(url,{
		method : 'post',
		evalScripts: true,
		autoCancel : true,
		update: $('dp_gbl')
	}).request();
}

function goTop(){
	var winScroller = new Fx.Scroll(window);
	winScroller.toElement('dp_header_up');
}

function addCart(v,qty){
	window.location = 'http://www.gruppopreziosi.com/index.php?action=buy&s=3&p='+v+'&qty='+qty+'&m=1';
}

function chkButt(id,ele){ //elimina il bottone acquista se si esauriscono le scorte durante la navigazione
	var url_param = Object.toQueryString({'s' : 25,'v': id});
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	$$('.loader_gif_ajax').each(function(loa){
		loa.remove();
	});
	var loader;
	var jsonCart = new Ajax(url,{
		method : 'get',
		autoCancel : true,
		onRequest: function(){
			var loaderImg = '<img src="http://www.gruppopreziosi.com/inc/img/ajax-loader.gif" class="loader_gif_ajax" />';
			loader = new StickyWin({
				content: loaderImg,
				relativeTo: $(ele),
				position: 'center'
			});
		},
		onComplete: function(res){
			loader.hide();
			if(res == 'true'){
				if($(ele)) $(ele).remove();
			} else if(res == 'false'){
				stop();
			}
		}
	}).request();
}


function delCart(v){
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=4&p='+v;
	$$('.loader_gif_ajax').each(function(loa){
		loa.remove();
	});
	var loader;
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onRequest: function(){
			var loaderImg = '<img src="http://www.gruppopreziosi.com/inc/img/ajax-loader.gif" class="loader_gif_ajax" />';
			loader = new StickyWin({
				content: loaderImg,
				relativeTo: $('cart'),
				position: 'center'
			});
		},
		onComplete: function(){
			loader.hide();
			goToCart(7);
		},
		update : $('cart')
	}).request();
}

function viewCart(){
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=5';
	$$('.loader_gif_ajax').each(function(loa){
		if(loa){
			loa.remove();
		}
	});
	var loader;
	var jsonCart = new Ajax(url,{
		method : 'get',
		autoCancel : true,
		evalScripts: true,
		onRequest: function(){
			var loaderImg = '<img src="http://www.gruppopreziosi.com/inc/img/ajax-loader.gif" class="loader_gif_ajax" />';
			loader = new StickyWin({
				content: loaderImg,
				relativeTo: $('cart'),
				position: 'center'
			});
		},
		onComplete: function(){
			loader.hide();
		},
		update : $('cart')
	}).request();
}

function bancRis(a,b,c){
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=22&a='+a+'&b='+b+'&c='+c;
	$$('.loader_gif_ajax').each(function(loa){
		if(loa){
			loa.remove();
		}
	});
	if($('dp_vetrina')){
		$('dp_vetrina').remove();
	}
	if($('dp_vn')){
		$('dp_vn').remove();
	}
	if($('dp_offerta')){
		$('dp_offerta').remove();
	}
	if($('dp_gbl')){
		var target =  $('dp_gbl');
	}else {
		var target = new Element('div').setProperties({'id':'dp_gbl','class':'dp_gbl'}).injectInside('contents');
	}
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onRequest: function(){
			var loaderImg = '<img id="temp_loader" src="http://www.gruppopreziosi.com/inc/img/ajax-loader.gif" class="loader_gif_ajax" />';
			loader = new StickyWin({
				content: loaderImg,
				relativeTo: target,
				position: 'center'
			});
		},
		onComplete: function(){
			if($('temp_loader')){
				$('temp_loader').hide();
			}
		},
		update : target
	}).request();
}

function goToCart(num){
	var id = (num == '' | num == null | num.length == 0) ? 7: num;
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s='+id;
	$$('.loader_gif_ajax').each(function(loa){
		if(loa){
			loa.remove();
		}
	});
	if($('dp_vetrina')){
		$('dp_vetrina').remove();
	}
	if($('dp_vn')){
		$('dp_vn').remove();
	}
	if($('dp_offerta')){
		$('dp_offerta').remove();
	}
	if($('dp_gbl')){
		var target =  $('dp_gbl');
	}else {
		var target = new Element('div').setProperties({'id':'dp_gbl','class':'dp_gbl'}).injectInside('contents');
	}
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onRequest: function(){
			var loaderImg = '<img id="temp_loader" src="http://www.gruppopreziosi.com/inc/img/ajax-loader.gif" class="loader_gif_ajax" />';
			loader = new StickyWin({
				content: loaderImg,
				relativeTo: target,
				position: 'center'
			});
		},
		onComplete: function(){
			if($('temp_loader')){
				$('temp_loader').hide();
			}
		},
		update : target
	});
	jsonCart.cancel();
	jsonCart.request();
}

function getVectors(){
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=8';
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onRequest: function(){
			$('dp_gbl').setHTML('loading...');
		},
		update : $('dp_gbl')
	}).request();
}

function getPayments(){
	var ok = 0;
	var arr = $$('input[name=shipping]').getProperty('checked');
	arr.each(function(val){
		if(val == true){
			ok = 1;
		}
	});
	if(ok == 1){
		var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=9';
		var jsonCart = new Ajax(url,{
			method : 'get',
			evalScripts: true,
			autoCancel : true,
			onRequest: function(){
				if($('dp_gbl')){
					$('dp_gbl').setHTML('loading...');
				}
			},
			update : $('dp_gbl')
		}).request();
	} else {
		if(!$('errorMsg')){
			var err = new Element('p',{'id': 'errorMsg'}).setHTML('Devi scegliere un valore prima di continuare').injectAfter($('dp_gbl'));
		}
	}
}

function saveVector(valore){
	$('vettore').setProperty('value',valore);
}

function savePayment(valore){
	$('pagamento').setProperty('value',valore);
}

function getBrAcc(){ //costruisce gli accordion per i brand e i prodotti correlati
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=11';
	var jsonCart = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		update : $('dp_dx_box1')
	}).request();
}

function tC(id,id2){ //Swappa le classi per i prodotti novità/in offerta
	$(id).setProperties({
		'onclick' : 'return false;'
	});
	$(id2).setProperties({
		'onclick' : 'tC(\''+id2+'\',\''+id+'\');return false;'
	});
	$('pn').toggleClass('dp_vnhead_sx');
	$('pr').toggleClass('dp_vnhead_dx');
	$('pn').toggleClass('dp_vnhead_sxsel');
	$('pr').toggleClass('dp_vnhead_dxsel');
}

/*********************/
/*Rating delle schede*/
/*********************/
var NUMBER_OF_STARS = 5;
function init_rating(vid){
	var ratings = $('star'+vid);
	if(ratings){
		var rating = ratings.getText().toInt();
		ratings.empty();
		for (var j = 0; j < NUMBER_OF_STARS; j++){
			var star = document.createElement('img');
			if (rating >= 1){
				star.setAttribute('src', 'http://www.gruppopreziosi.com/inc/img/rating/dp_star_on.jpg');
				star.className = 'on';
				rating--;
			}else if(rating == 0.5){
				star.setAttribute('src', 'http://www.gruppopreziosi.com/inc/img/rating/dp_star_half.jpg');
				star.className = 'half';
				rating = 0;
			}else{
				star.setAttribute('src', 'http://www.gruppopreziosi.com/inc/img/rating/dp_star_off.jpg');
				star.className = 'off';
			}
			star.setAttribute('id', 'star_'+vid+'_'+j);
			ratings.appendChild(star);
		}
	}
}

function displayHover(ratingId, vid,star){
	for (var i = 0; i <= star; i++){
		$('star_'+ratingId+vid+'_'+i).setAttribute('src', 'http://www.gruppopreziosi.com/inc/img/rating/dp_star_over.jpg');
	}
}

function displayNormal(ratingId,vid, star){
	for (var i = 0; i <= star; i++){
		var status = $('star_'+ratingId+vid+'_'+i).className;
		$('star_'+ratingId+vid+'_'+i).setAttribute('src', 'http://www.gruppopreziosi.com/inc/img/rating/dp_star_'+status+'.jpg');
	}
}

function mooInit(vid){
	init_rating(vid);
}

function getRatingVar(vid){
	var n = Object.toQueryString({'m':0, 'vid': vid});
	var url = 'http://www.gruppopreziosi.com/inc/rating.php?'+n;
	var star = 'star'+vid;
	if($(star)){
		var request = new Ajax(url, {
			method : 'get',
			evalScripts: true,
			update : $(star),
			onComplete: function() {
				mooInit(vid);
			}
		}).request();
	}
}

function viewOrder(){
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=23';
	var jsonCart = new Ajax(url,{
		method : 'post',
		evalScripts: true,
		autoCancel : true,
		update : $('dp_gbl')
	}).request();
}

function useAgreement(code){ //USA CODICE CONVENZIONE
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=agreement&c='+ code;
	var disc = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onComplete: function(ret){
			if(ret != 0){
				$('discount').setHTML(ret);
				viewCart();
				goToCart(10);
			} else if(ret == 0){
				alert('codice errato!');
				$('discount').setProperty('value','');
			}
		}
	}).request();
}

function useDiscount(code){ //USA CODICE SCONTO
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=discount&c='+ code;
	var disc = new Ajax(url,{
		method : 'get',
		evalScripts: true,
		autoCancel : true,
		onComplete: function(ret){
			if(ret != 0){
				$('discount').setHTML(ret);
				viewCart();
				goToCart(10);
			} else if(ret == 0){
				alert('codice errato!');
				$('discount').setProperty('value','');
			}
		}
	}).request();
}

function remPromo(){ //RIMUOVI CODICE PROMOZIONE
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=remPromo';
	var disc = new Ajax(url,{
		method : 'get',
		autoCancel : true,
		onComplete : function(){
			viewCart();
			goToCart(10);
		}
	}).request()
}

function tryStep2(email,giorno,mese,anno){ //primo step reset password: invia la mail e va sul secondo form
	var url_param = Object.toQueryString({'s': 'reset_pwd2', 'email' : email, 'giorno' : giorno, 'mese': mese, 'anno' : anno});	
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	var disc = new Ajax(url,{
		method : 'POST',
		autoCancel : true,
		evalScripts: true,
		update : $('dp_login')
	}).request()
}

function tryStep3(pwd,uid,code){ //secondo step reset password: invia i valori delle nuove password per il controllo
	var url_param = Object.toQueryString({'s': 'reset_pwd4', 'pwd' : pwd, 'uid' : uid, 'code' : code});	
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	var disc = new Ajax(url,{
		method : 'POST',
		autoCancel : true,
		evalScripts: true,
		onComplete: function(res){
			if(res == 1){
				$('reset_pwd_2').setHTML('<p class="przgreen3" style="text-align:center !important;">PASSWORD CORRETTAMENTE RESETTATA!</p>');
			} else {
				$('reset_pwd_2').setHTML('<p class="przred3" style="text-align:center !important;">SI E\' VERIFICATO UN ERRORE.</p>');
			}
		}
	}).request()
}

function openTicket(){ //SERVIZIO CLIENTI
	var url_param = Object.toQueryString({'s': 'support'});
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	var ticket = new Ajax(url,{
		method : 'POST',
		autoCancel : true,
		evalScripts: true,
		update : $('dp_gbl')
	}).request($('servizio_clienti'))
}

function newRegister(){ //NUOVA REGISTRAZIONE
	var url_param = Object.toQueryString({'s': 'register'});
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	var ticket = new Ajax(url,{
		method : 'POST',
		autoCancel : true,
		onRequest: function(){ console.log('ok'); },
		data : $('form_maggiore'),
		evalScripts: true,
		update : $('form_gormiti')
	}).request()
}

function trytotell(id){ //INIZIO PAGINA DI "SEGNALA AD UN AMICO"
	$('tellform').show();
	new StickyWin({
	  relativeTo: $('img_centrale'),
	  content: stickyWinHTML('Invia la pagina', $('tellform'), {
	    width: '400px',
	    buttons: [
				{
		        text: 'close', 
		        onClick: function(){javascript:void(0);}
		      },
		      {
		        text: 'ok', 
		        onClick: function(){tellafriend(id);return false;}
		      }
			]
	  })
	});
}

function tellafriend(vid){ //INVIO PAGINA "SEGNALA AD UN AMICO"
	var url_param = Object.toQueryString({'s': 'tellafriend','vid' : vid});
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?'+url_param;
	var ticket = new Ajax(url,{
		method : 'POST',
		evalScripts: false,
		onComplete: function(res){
			new StickyWin({
			  relativeTo: $('img_centrale'),
			  content: stickyWinHTML('',res , {
			    width: '400px',
			    buttons: [{
				        text: 'ok', 
				        onClick: function(){javascript:void(0);}
				      }]
			  })
			});
		}
	}).request($('tellafriend'))
}

function liveSaver(what,where){ //salva in automatico i campi dello step due del carello
	var url = 'http://www.gruppopreziosi.com/inc/ecomm.content.php?s=8';
	var ticket = new Ajax(url,{
		method : 'POST',
		onRequest: function(){
			new Confirmer({
				msg: 'Attendere.... Salvataggio in corso...',
				positionOptions: {
					relativeTo: $(where),
					position: "bottomLeft",
					offset: {x: 0, y: 0}
			  	}
			}).prompt();
		},
		onComplete: function(){
			new Confirmer({
				msg: 'Salvato!',
				positionOptions: {
					relativeTo: $(where),
					position: "bottomLeft",
					offset: {x: 0, y: 0}
			  	}
			}).prompt();			
		}
	}).request($('controllo_dati'));
}
