/* victor at cruzate dot com */

var section, subsection;
var cstate = (Cookie.get('cstate'))? Cookie.get('cstate'): 0;

/* Fix for IE background image flicker 
 if (document && document.execCommand) {
        try { document.execCommand("BackgroundImageCache",false,true); }
        catch (oh) { }
} 
*/

// suckerfish css IE hover emulator
startList = function() {
	if (document.all&&document.getElementById) {
		navRoot = document.getElementById("nav");
		for (i=0; i<navRoot.childNodes.length; i++) {
			node = navRoot.childNodes[i];
			if (node.nodeName=="LI") {
				node.onmouseover=function() {
					this.className+=" over";
				}
				node.onmouseout=function() {
					this.className=this.className.replace(" over", "");
				}
			}
		}
	}
}
window.onload=startList;

//gets element's text
Element.extend({
	getText: function() {
	return ($pick(this.innerText, this.textContent))
	}
});

window.addEvent('domready', function()
{
neform = ($E('form', 'lcontent'))? $E('form', 'lcontent') : null;

// sets initial styles and variables
$$('body').setStyle('background','#333');
$$('.opaco').setOpacity(0.8);
if (neform) neform.setOpacity(0.8);
if (window.gecko) {$('calcontent').setOpacity(0.7);} else {$('calcontent').setOpacity(0.5);}
if (cstate==0) $('calcontent').setStyle('visibility','hidden');

/*
//draggable tshirt button for Safari
if (window.webkit) {
$('camiseta').setStyles({'position':'relative','top':'395px','left':'10px'});
} else {

//draggable tshirt button for the rest
var box = $('camiseta'), coordsX = $('coordsX'), coordsY = $('coordsY');

var settings = {
	coords: {"y": 395, "x": 10}
}

var HashCookie = new Hash.Cookie('Camiseta');

box.makeDraggable({
	container: 'drag',	
	onDrag: function() {
		var pos = {"x": this.element.getStyle('left').toInt() + 1, "y": this.element.getStyle('top').toInt() + 1};
		settings['coords'] = pos;
	},
	onComplete:function(){		
		HashCookie.extend(settings);	
	}.bind(box)
});

//sets draggable button position
	if (HashCookie.get('coords')) {
		settings['coords'] = HashCookie.get('coords');
	} else {
		settings['coords'] = {"y": 385, "x": 10};
	}
 
	var loadStyles = settings;
	loadStyles['left'] = loadStyles.coords.x - 1; loadStyles['top'] = loadStyles.coords.y - 1;
	delete loadStyles.coords;
	box.setStyles(loadStyles);
}
*/

// show/hide content functions
var tango = { 

	hideall: function() { 
		$$('div.hide').each(function(el){
			el.setStyles({'display': 'block','visibility':'hidden'});
		});
		if (neform) neform.setStyle('visibility','hidden');
//		$('calcontent').setStyle('visibility','hidden');
		if (window.gecko) {
			$$('div.textexpand').each(function(el){
			el.setStyle('visibility','hidden');
			});
		}
	},

	showall: function() {
		$$('div.hide').each(function(el){
			el.setStyles({'display': 'block','visibility':'visible'});
		});
		if (neform) neform.setStyle('visibility','visible');
//		if (cstate==1) $('calcontent').setStyle('visibility','visible');
		if (window.gecko) {
			$$('div.textexpand').each(function(el){
			el.setStyle('visibility','visible');
			});
		};
	},
	
//calendar functions
	calstate: function() {	

	var state = $('calcontent').getStyle('visibility');
	if (state =='hidden') {
		$('calcontent').setStyle('visibility','visible');
		cstate=1;
		Cookie.set('cstate', 1);
		} else {
		$('calcontent').setStyle('visibility','hidden');
		cstate=0;
		Cookie.set('cstate', 0);
		}
	},

	brightup: function() {
		if (cstate==1){
		el = $('calcontent');
		el.setStyle('background','#FFF');
		if (window.gecko) {el.setOpacity(1);} else {el.setOpacity(0.8);}
		}
	},

	brightdown: function() {
	if (cstate==1){
		el = $('calcontent');
		if (window.gecko) {el.setOpacity(0.7);} else {el.setOpacity(0.5);}
		}
	},

//initialise tango functions	
	init: function(){
	this.hideall();
	this.showall();
	this.calstate();
	this.brightup();
	this.brightdown();
	}	
};

//more info accordion
var showmore = new Accordion($$('span.moreinfo'), $$('div.textexpand'),{
		alwaysHide: true, 
		show: 999,
		onActive: function(toggler, element){
			var toggletext = toggler.getText();
			if (toggletext=='+ INFO'){
				toggler.setHTML('CERRAR')}
			},
		onBackground: function(toggler, element){
			var toggletext = toggler.getText();
			if (toggletext=='CERRAR'){
				toggler.setHTML('+ INFO')}
			}			
		}).stop();

// effect activation buttons
$('toggletop').addEvent('mouseover', function(e){
	e = new Event(e);
	tango.hideall();
	e.stop();
});

$('bg').addEvent('mouseover', function(e){
	e = new Event(e);
	tango.showall();
	e.stop();
});

/*
$('caltoggle').addEvent('click', function(e){
	e = new Event(e);
	tango.calstate();
	e.stop();
});

$('cal').addEvent('mouseover', function(e){
	e = new Event(e);
	tango.brightup();
	e.stop();
});

$('cal').addEvent('mouseleave', function(e){
	e = new Event(e);
	tango.brightdown();
	e.stop();
});
*/

/* 
draggable tshirt button
$('camiseta').makeDraggable({
	container: 'drag'
});
*/

//displays relevant subsection if present
$('nav').getElements('li').each(function(lis){
var liname = lis.getProperty('name');
	if (liname==subsection) {
		lis.setProperty('id', 'subsection');
		$('menu').setStyle('background', 'url(/assets/images/smenubgon.png) repeat-x');
		$('subsection').getFirst().setStyle('text-decoration','underline');
		}
	if (liname==section) {
		lis.setProperty('id', 'section');
		$('section').getFirst().setStyle('color','#099');
		if (subsection) $E('ul', 'section').setStyle('display', 'block');
		}		

//menu rollovers
	$$(lis.getElements('ul')).each(function(liuls){
		if (liuls){
		
			lis.addEvent('mouseover', function(e){
				e = new Event(e);
				$('menu').setStyle('background', 'url(/assets/images/smenubgon.png) repeat-x');
				e.stop();
			});
			
			if (!subsection){
			lis.addEvent('mouseleave', function(e){
				e = new Event(e);
				$('menu').setStyle('background', 'url(/assets/images/smenubgoff.png) repeat-x');
				e.stop();
			});	
			}
		}
	});		
});

});