// Common JS loads all pages

Event.onDOMReady(function() {
	// Dynamic menu selection
	$$('#nav a').each(function(a) {
		a.href = a.href.gsub('index/','');
		if (location.href.match(a.href) || location.href.match('ci-presentiamo/') && a.href.match('ci-presentiamo/')) {
			a.addClassName('selected');
		} else if ((location.href.match('categoria') || location.href.match('sapore') || location.href.match('provenienza')) && a.href.match('catalogo/')) {
			a.addClassName('selected');
		}
	});
});

// Array remove method
Array.prototype.remove=function(s){
	var index = this.indexOf(s);
	if(this.indexOf(s) != -1)this.splice(index, 1);
}

/*
 * Styles radios with a custom grafic, it needs the structure: 
 * label.radio -> input[type=radio]
 */
function styleRadios() {
	$$('label.radio').each(function(el) {
		if (el.down('input').getAttribute('checked'))
			el.addClassName('selected');
		Event.observe(el, 'click', function(e) {
			if (!el.hasClassName('selected')) {
				Element.getElementsBySelector(el.up(), 'label').each(function(el2) {
					el2.removeClassName('selected');
				});
				el.addClassName('selected');
				Element.getElementsBySelector(el, 'input')[0].click();
			}
		});
	});
}

function styleRadiosInList() {
	$$('label.radio').each(function(el) {
		if (el.down('input').getAttribute('checked'))
			el.addClassName('selected');
		Event.observe(el, 'click', function(e) {
			if (!el.hasClassName('selected')) {
				Element.getElementsBySelector(el.up().up(), 'label').each(function(el2) {
					el2.removeClassName('selected');
				});
				el.addClassName('selected');
				Element.getElementsBySelector(el, 'input')[0].click();
			}
		});
	});
}

function vCenter(el,i) {
	marg = (($(el).up(i).getHeight()-$(el).getHeight())/2);
	$(el).style.marginTop = marg+'px';
}

/* Customized crossbrowsing checkbox */
function styleChecks() {
    $$('label.check').each(function(e) {
        if (e.getElementsByTagName('input')[0].checked)
        	e.addClassName('checked');
        	disableSelection(e);
        e.observe('click', function(ev, el) {
            if (el.getElementsByTagName('input')[0].checked) {
                el.removeClassName('checked');
                el.down('input').checked = null;
            } else {
                el.addClassName('checked');
                el.down('input').checked = true;
            }
            return false;
        }.bindAsEventListener(this,e));
    });
}

/*** disabilita la selezione del testo dell'elemento target ***/
function disableSelection(target){
	if (typeof target.onselectstart!="undefined") //IE route
		target.onselectstart=function(){return false}
	else if (typeof target.style.MozUserSelect!="undefined") //Firefox route
		target.style.MozUserSelect="none"
	else //All other route (ie: Opera)
		target.onmousedown=function(){return false}
	target.style.cursor = "default"
}


/*** funzione per selezionare i prodotti da inserire alla brochure uno alla volta ***/
function addToBrochure(check, id) {
	if ($(check).checked) 
	{
		if (prodIds.indexOf(id) == -1)
			prodIds.push(id);
		$('brochure_id').select('option').each(function(el) {
			if (el.value != 0) {
				el.value = el.value.replace(/item_id\/[0-9\,]*\//, 'item_id/'+prodIds.join(',')+'/');
			}
		});
	} 
	else 
	{
		if (prodIds.indexOf(id) != -1)
			prodIds.remove(id);
		$('brochure_id').select('option').each(function(el) {
			if (el.value != 0)
				el.value = el.value.replace(/item_id\/[0-9\,]*\//, 'item_id/'+prodIds.join(',')+'/');
		});
	}
}

/*** funzione per selezionare o deselezionare tutti i prodotti da inserire nella brochure ***/
function toggleAllProds() {
	if ($('toggle-all').innerHTML == selAll) { 
		$('toggle-all').innerHTML = delAll;
		$$('.add-to-brochure-small input').each(function(el) {
			if (!el.checked)
				el.click();
		});
	} else {
		$('toggle-all').innerHTML = selAll;
		$$('.add-to-brochure-small input').each(function(el) {
			if (el.checked)
				el.click();
		});
	}
}