
var showHideContentInfo = false;
function showHideContent (id) {
	var from = 100, to = 0, dir = 'down', el = $(id);
	var aLogo = $('aLogoEmpresa'), h4Logo = $('h4LogoEmpresa'), main = $('main'), top = $('topCont');
	
	if (!!showHideContentInfo && showHideContentInfo.dir == 'down') { from = 0, to = 100, dir = 'up'; }
	
	if (showHideContentInfo) {
		if (!showHideContentInfo.end) {
			showHideContentInfo.anim.stop();
			from = parseInt(el.style.opacity);
			if (!Nav.esIE) { from = (parseFloat(el.style.opacity) * 100); }
		}
	}
	if (dir == 'up') {
		top.style.visibility = el.style.visibility = 'visible';
		h4Logo.appendChild(aLogo);
		aLogo.removeAttribute('style');
		main.removeAttribute('style');
	}
	
	showHideContentInfo = {};
	showHideContentInfo.end = false;
	showHideContentInfo.dir = dir;
	if (!Nav.esIE) {
		showHideContentInfo.anim = new Tween(el.style, 'opacity', Tween.strongEaseOut, from, to, 1.5, '');
		showHideContentInfo.anim.onMotionChanged = function (args) {
			if (dir == 'up') { opa = (args.target._pos / to * 100).toFixed(0); }
			else { opa = (args.target._pos / from * 100).toFixed(0); }
			setOpacity(opa, el);
			setOpacity(opa, top);
		}
		showHideContentInfo.anim.onMotionFinished = function () {
			if (dir == 'down') {
				top.style.visibility = 
				el.style.visibility = 'hidden';
				
				main.insertBefore(aLogo, main.firstChild);
				aLogo.style.position = 'fixed';
				aLogo.style.top = '20px';
				aLogo.style.left = '20px';
				var anim = new Tween(aLogo.style, 'opacity', Tween.strongEaseOut, 0, 100, .25, '');
				anim.onMotionChanged = function (args) { setOpacity(args.target._pos, aLogo); }
				anim.start();
			}
			showHideContentInfo.end = true;
		}
		showHideContentInfo.anim.start();
	}
	else {
		if (dir == 'down') {
			top.style.visibility = 
			el.style.visibility = 'hidden';
			
			main.insertBefore(aLogo, main.firstChild);
			aLogo.style.position = 'fixed';
			aLogo.style.top = '20px';
			aLogo.style.left = '20px';
		}
		showHideContentInfo.end = true;
	}
}


/* Eventos para acomodar el pie al tamaņo de la ventana */
function HBody2(){ return (Nav.esIE)? document.body.scrollHeight : document.body.offsetHeight; }
AddEvent(window, 'load', function(){
	var el = $('principal');
	var h = el.offsetHeight + HScreen() - HBody2();
	if (h > el.offsetHeight) { el.style.height = (h - 25) + 'px'; }
	
	var count = 0;
	AddEvent(window, 'resize', function(){
		if (Nav.esIE && count == 2) { return count = 0; }
		count++;
		
		el.style.height = '';
		var h = el.offsetHeight + HScreen() - HBody2();
		if (h > el.offsetHeight) { el.style.height = (h - 25) + 'px'; }
	});
});

/******************************************/
var SiteNavImages = new function () {
	
	var sclass = this;
	var nav;
	var navTit;
	var navLoad;
	var bg1;
	var bg2;
	var aOn; // a activo
	var img; // img precarga
	var dirIm = DIR_ROOT_THEME_CENTRAL+'img/bg/';
	var anim;
	var time;
	var first = true;
	
	//
	this.images = [];
	var imgsloaded = {}
	
	//
	var cEl = function (tag) {
		return document.createElement(tag);
	}
	var initNav = function () {
		var i = 0;
		
		nav = $('navFoto');
		navTit = $('navFotoTit');
		navLoad = $('navFotoLoading');
		bg1 = $('bgTop1');
		bg2 = $('bgTop2');
		
		for(i; i < sclass.images.length; i++) { createImage(sclass.images[i], i); }
		
		i = Math.floor(Math.random()*sclass.images.length);
		FireEvent(nav.childNodes[i], 'click');
	}
	var createImage = function (conf, index) {
		var a, sp, im;
		a = cEl('a');
		a._index = index;
		a.title = conf.title;
		a.style.backgroundImage = 'url(' + dirIm + conf.name + 'm.jpg)';
		nav.insertBefore(a, nav.firstChild);
		
		sp = cEl('span');
		a.appendChild(sp);
		
		im = cEl('img');
		im.src = dirIm + conf.name + 'm.jpg';
		im.alt = im.title = conf.title;
		sp.appendChild(im);
		
		conf.a = a;
		AddEvent(a, 'click', function () { changeBgImg(conf); });
	}
	var changeBgImg = function (conf) {
		if (!img) { img = new Image(); }
		if (!!time) { window.clearTimeout(time); }
		
		var i = dirIm + conf.name + '.jpg';
		if (!imgsloaded[i]) { navLoad.style.display = 'inline'; }
		img.onload = function() { imageBgLoaded(conf); };
		img.src = i;
		
		imgsloaded[i] = true;
	}
	var imageBgLoaded = function (conf) {
		navLoad.style.display = 'none';
		bg2.style.display = 'block';
		
		if (!!anim) { anim.stop(); }
		
		if (bg2.style.backgroundImage == '' && conf.title != '') {
			navTit.innerHTML = conf.title;
			navTit.style.display = 'block';
		}
		
		if (first) {
			anim = new Tween(bg1.style, 'opacity', Tween.regularEaseIn, 0, 100, 1.5, '');
			anim.onMotionChanged = function (args) {
				opa = (args.target._pos / 100 * 100).toFixed(0)
				setOpacity(opa, bg1);
			}
			anim.onMotionFinished = function () {
				bg2.style.backgroundImage = bg1.style.backgroundImage;
				time = window.setTimeout(function () { changeBgTimeout(); }, 6000);
			}
			anim.start();
			first = false;
		}
		else {
			anim = new Tween(bg2.style, 'opacity', Tween.regularEaseIn, 100, 0, 1.5, '');
			anim.onMotionChanged = function (args) {
				opa = (args.target._pos / 100 * 100).toFixed(0)
				setOpacity(opa, bg2);
			}
			anim.onMotionFinished = function () {
				navTit.style.display = 'none';
				if (conf.title != '') {
					navTit.innerHTML = conf.title;
					navTit.style.display = 'block';
				}
				
				bg2.style.backgroundImage = bg1.style.backgroundImage;
				bg2.style.display = 'none';
				time = window.setTimeout(function () { changeBgTimeout(); }, 6000);
			}
			anim.start();
		}
		bg1.style.backgroundImage = 'url('+img.src+')';
		
		if (!!aOn) { aOn.removeAttribute('class'); }
		aOn = conf.a;
		aOn.setAttribute('class', 'on');
	}
	var changeBgTimeout = function  () {
		var i = aOn._index, t = sclass.images.length, c = (i + 1) % t;
		var conf = sclass.images[c];
		FireEvent(conf.a, 'click');
	}
	
	// Event
	AddEvent(window, 'load', function () {
		var w = window.screen.width, images = false, i = 0;
		for(i; i < sclass.images.length; i++){
			if (sclass.images[i].wMax >= w) {
				sclass.images = sclass.images[i].images;
				break;
			}
		}
		initNav();
	});
}