// Common JS

Event.onDOMReady(function() {
	styleRadios();
	styleChecks();
	if (navigator.userAgent.match('Firefox/2')) subcatFixFF2();
	$$('input[type="submit"]').each(function(el) {
		el.observe('mouseover', function(e, elx) { elx.addClassName('inphover') }.bindAsEventListener(this,el));
		el.observe('mouseout', function(e, elx) { elx.removeClassName('inphover') }.bindAsEventListener(this,el));
	});
	ie6noFlick();
});

function ie6noFlick(){
	if(document.all && !window.opera){
		document.execCommand("BackgroundImageCache", false, true);
		
	}
}

/*
 * fix for centering in Firefox 2 subcategory arrows in dynamic divs 
 */
function subcatFixFF2() {
	$$('.subcat-c').each(function(el) {
		el.style.width = el.getStyle('width');
	});
}

/*
 * Handler for opening panel onclick, just add the id at the element that will be
 * clicked and the same id+'-panel' to the element you want to display when the
 * first element is clicked. the panel will close when you click outside of it.
 */
function panelHandler(x) {
    Event.observe(x, 'click', function(e){
    	Event.stop(e);
		if (!x.hasClassName('clicked')) {
			x.addClassName('clicked');
			Element.show($(x.id+'-panel'));
			$$('body', '#search').each(function(el) {
				Event.observe(el, 'click', function(e){
					if (Event.element(e) != x && Event.element(e) != $(x.id+'-panel') && !Event.element(e).descendantOf($(x.id+'-panel'))) {
						Element.hide($(x.id+'-panel'));
						x.removeClassName('clicked');
						if (navigator.userAgent.match('MSIE 6'))
							x.style.filter = x.style.filter.sub('_clicked.png','.png');
					}
				});
			});
			if (navigator.userAgent.match('MSIE 6'))
				x.style.filter = x.style.filter.sub('.png','_clicked.png');
		}
	}, false);
}

/*
 * Styles radios with a custom grafic, it needs the structure: 
 * label.radio -> input[type=radio]
 */
function styleRadios() {
	$$('label.radio').each(function(el) {
		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();
			}
		});
	});
}

/* Customized crossbrowsing checkbox */
function styleChecks() {
    $$('label.check').each(function(e) {
        if (e.getElementsByTagName('input')[0].checked)
        	e.addClassName('checked');
        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));
    });
}

/* vertical centering of an image (el) inside a contenitor up to (i) level(s) from it */ 
function vCenterMe(el,i) {
	marg = (($(el).up(i).getHeight()-$(el).getHeight())/2);
	$(el).style.marginTop = marg+'px';
}

/*  Usage:
 *	var dim = Position.GetWindowSize();
 *	dim.width is the window width
 *	dim.height is the window height
 */
Position.GetWindowSize = function(w) {
	var width, height;
        w = w ? w : window;
        this.width = w.innerWidth || (w.document.documentElement.clientWidth || w.document.body.clientWidth);
        this.height = w.innerHeight || (w.document.documentElement.clientHeight || w.document.body.clientHeight);
        
        return this;
}