/*
Cogumelo v0.1 - INNOTO SL
Copyright (C) 2010 Innoto Gestión para el Desarrollo Social S.L. <mapinfo@innoto.es>

modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
USA.
 */


var firstLoad = false;
var seccion_actual = 1;
var alto_max = 777;
var imgAUX;
var imgUpdater;
var fichaview = 0;
var my_glider;
var numSec = 1;
var numElem = 0;
var arrayImg = new Array();
var sec = 1;

//Inicializamos a web
Event.observe(window, "load", initHomePage, false);

function initHomePage() {
	chargeImages();	
	resize();
	if (contacto!==true){ 
		initialize();
	}
	initLangSelector();
	Event.observe(window, "resize", resize);
}

//creamos o elemento historial de navegación
function createHistory() {
	History.Observer.start();
	history.id = 'seccion';
	history.onStateChange = function(value) {
		this.Section(value);
	}.bind(this);
	History.Registry.set(history);
}

// creamos o ascensor e as funcións para o seu manexo
function createGlider() {
	my_glider = new Glider('my-glider', {
		duration : 0.1
	});
}

function resize() {
	$('filler1').style.height = '0px';
	$('filler2').style.height = '0px';
	alto = getPageSizes('height');
	if (alto > alto_max) {
		diferencia = alto - alto_max;
		$('filler1').style.height = diferencia / 2 + 'px';
		$('filler2').style.height = diferencia / 2 + 'px';
	}
}

//carrusel de exemplos para cada sección
function fillCarouselExamples(html, num) {
	var ul = $$("#examples_carousel" + num + " .container ul").first();
	// Fill carousel
	ul.update(html);
	a = new UI.Carousel("examples_carousel" + num);
}

//carga dos carruseles
function loadCarusel(id) {
	new Ajax.Request('index.php?q=examples&id=' + id, {
		method : 'get',
		onSuccess : function(request) {
			fillCarouselExamples(request.responseText, id);
		}
	});
}

//función de manexo das fichas dos exemplos
function VerFicha(num) {
	new Ajax.Updater('ficha', 'index.php?q=ficha&ficha=' + num + '&sec=' +seccion_actual, {
		method : 'get'
	});
	if ($('captura')){
		$('captura').style.backgroundImage = "url('Templates/images/loader.gif')";
        }
	imgAUX = new Image();
	imgAUX.src = 'Templates/images/loader.gif';
	fichaview = 1;
	if (imgUpdater) {
		imgUpdater.stop();
	}
	periodicalchangeImage(num);
	$('my-glider').hide();
	$('ficha').show();
	
	History.set('ficha', num);

	resize();
}

function goSection(id) {
	numSec=id;
	if (imgUpdater) {
		imgUpdater.stop();
	}
	if (History.get('seccion')) {
		sec = History.get('seccion');
		if (History.get('ficha')) {
			num = History.get('ficha');
			VerFicha(num);
		} else {
			my_glider.moveTo('section' + sec, $('my-glider').down(
					'div.scroller'), {
				duration : 0.6
			});
		}
	} else {
		my_glider.moveTo('section' + id, $('my-glider').down('div.scroller'), {
			duration : 0.6
		});
	}
	seccion_actual=sec;
	numSec = sec;
}

function Section(id) {
	numSec=id;
	if (fichaview == 1) {
		Return();
	}
	//seccion_actual = id;
	History.set('seccion', id);
	goSection(id);
}

function Return() {	
	if (ie==false){
		$('captura').style.backgroundImage = 'Templates/images/loader.gif';
		imgAUX = new Image();
		imgAUX.src = 'Templates/images/loader.gif';
		fichaview = 0;
		resetValues();
		$('ficha').hide();
		$('my-glider').show();
		Section(History.get('seccion'));
	}
	else{
		fichaview = 0;
		resetValues();
		$('ficha').hide();
		$('my-glider').show();
		Section(History.get('seccion'));
	}
}

function chargeImages() {
	
	if (lang =="lang_es"){
		Image1 = new Image(200,51);
		Image1.src = "Templates/images/botones/hover_divulgacion.jpg";
		Image2 = new Image(180,51);
		Image2.src = "Templates/images/botones/hover_gestion.jpg";
		Image3 = new Image(153,51);
		Image3.src = "Templates/images/botones/hover_participacion.jpg";
	}			
	if (lang =="lang_gl"){
		Image1= new Image(200,51);
		Image1.src = "Templates/images/botones/hover_divulgacion_gl.jpg";
		Image2 = new Image(186,51);
		Image2.src = "Templates/images/botones/hover_gestion_gl.jpg";
		Image3 = new Image(155,51);	
		Image3.src = "Templates/images/botones/hover_participacion_gl.jpg";		
		
	}	
	if (lang =="lang_en"){
		Image1= new Image(134,51);
		Image1.src = "Templates/images/botones/hover_divulgacion_en.jpg";
		Image2 = new Image(160,51);
		Image2.src = "Templates/images/botones/hover_gestion_en.jpg";	
		Image3 = new Image(154,51);		
		Image3.src = "Templates/images/botones/hover_participacion_en.jpg";				
	}
}

function initialize(){
	createGlider();
	if (History.get('seccion')) {
		seccion_actual = History.get('seccion');
		numSec=seccion_actual;
		Section(seccion_actual);
	}
	if (History.get('ficha')) {
		num = History.get('ficha');
		VerFicha(num);
	}
	
	if (Prototype.Browser.IE){
		if (oito){			
			loadCarusel(2);
			loadCarusel(3);
			loadCarusel(4);
		}
		else{//IE >8
			cargaCarrusel(2);
			cargaCarrusel(3);
			cargaCarrusel(4);
		}	
	}
	else{ 
		loadCarusel(2);
		loadCarusel(3);
		loadCarusel(4);
	}	
	createHistory();	
}





//************   Carga da ficha *************//

/*
 * Test if image exist
 */
function testImage(URL) {
	var tester = new Image();
	tester.onLoad = isGood;
	tester.onError = isBad;
	tester.src = URL;
}

// Comproba se existe unha imaxe.
function setIfExists(id, src) {
	var img = new Image();
	img.onload = function() {
		arrayImg[id] = src;
	};
	img.onerror = function() {
		arrayImg[id] = null;
	}
	img.src = src;
}

// función para rotar as imaxes nas fichas de exemplo
function periodicalchangeImage(id) {
	if (imgUpdater) {
		imgUpdater.stop();
	}

	// image number
	var i = 0;
	var max = 15;

	for (i = 1; i <= 15; i++) {
		setIfExists(i, "Fichas/images/prox" + id + "/cap" + i + ".jpg");
	}

	new PeriodicalExecuter(function(pe) {
		pe.stop();
		var cont = 2;
		imgUpdater = new PeriodicalExecuter(function(pe) {
			if (cont === max){
				cont = 1;
                        }
			while (arrayImg[cont] === null) {
				++cont;
				if (cont === max){
					cont = 1;
                                }
			}
			if (arrayImg[cont] !== null) {
				imgAUX.src = arrayImg[cont];
				$('captura').style.backgroundImage = "url('" + imgAUX.src + "')";
			}

			++cont;
		}, 3);
	}, 0.01);

}

// función que desactiva o PE e elimina a ficha do historial cando voltas ás
// seccións
function resetValues() {
	imgUpdater.stop();
	History.unset('ficha');
}

//////****************************   CARRUSEL IE6 e IE7   *********************************/////



function cargaCarrusel(id){	
	new Ajax.Updater('carrusel' + id,'index.php?q=examples&id='+id, {
		method:'get'});
	numSec = id;
}

function mueveLeft(i){
	if (History.get('seccion')){ 
		numSec = History.get('seccion');
	}
        left = i-1; 
	if (left<0){
                left=0;
        }
	new Ajax.Updater('carrusel' +numSec,'index.php?q=examples&id='+numSec+'&e='+left, {
		method:'get'});
}

function mueveRight(i){
	if (History.get('seccion')){ 
		numSec = History.get('seccion');
	}
        right = i+1;
	new Ajax.Updater('carrusel' +numSec,'index.php?q=examples&id='+numSec+'&e='+right, {
		method:'get'});
}

