/*
 * Styles radios with a custom grafic, it needs the structure: 
 * label.radio -> input[type=radio]
 */
 
document.observe('dom:loaded', function() {
	if ($$('label.radio'))
		styleRadios();
	if ($$('label.check'))
		styleChecks();
	$$('.quick-links a','.user-links a').each(function(a) {
		a.href = a.href.gsub('index/','');
		if (location.href.match(a.href))
			a.addClassName('selected');
	});
  if (navigator.platform.match(/Mac/g))
    $('fb_like').setStyle({'width': '74px'});
});

function styleRadios() {
	$$('label.radio').each(function(el) {
		if (el.down('input').getAttribute('checked'))
			el.addClassName('selected');
		el.observe('click', function(e) {
			if (!el.hasClassName('selected')) {
				el.up('form').select('input[name="'+el.down('input').getAttribute('name')+'"]').each(function(el2) {
					el2.up().removeClassName('selected');
				});
				el.addClassName('selected');
				el.down('input').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"
}