// JavaScript Document

var botsArray=new Array();
botsArray.push({bot:'bot_inicio',content:'modulo_inicio',nombre:'INICIO',ruta:'inicio.php'});
botsArray.push({bot:'bot_conocenos',content:'modulo_conocenos',nombre:'CONOCENOS',ruta:'conocenos.php'});
botsArray.push({bot:'bot_comollegar',content:'modulo_comollegar',nombre:'COMO_LLEGAR',ruta:'como_llegar.php'});
botsArray.push({bot:'bot_actividades',content:'modulo_actividades',nombre:'ACTIVIDADES',ruta:'actividades.php'});
botsArray.push({bot:'bot_viajes',content:'modulo_viajes',nombre:'VIAJES_Y_EXCURCIONES',ruta:'viajes.php'});
botsArray.push({bot:'bot_galeria',content:'modulo_galeria',nombre:'GALERIA',ruta:'galeria.php'});
botsArray.push({bot:'bot_contacto',content:'modulo_contacto',nombre:'CONTACTO',ruta:'contacto.php'});

var separador='#/';
var currentBotID=0;
var loaded=false;
var intevalo;

//////////////VIJAES////////////
var currentViajeID;

function onClickViaje(ID){
	currentViajeID=ID;
	//alert(document.viajeFormatoForm.nombre);
	document.viajeFormatoForm.nombre.value="";
	document.viajeFormatoForm.apellido.value="";
	document.viajeFormatoForm.mail.value="";
	
	var mat=new Array('nombre','apellido','mail');
	for(var i=0;i<mat.length;i++){
		$(document.viajeFormatoForm[mat[i]]).removeAttr('style');
	}
	
	$(".viajeSMS").css({'background':'none'});
	$(".viajeSMS").html('');
	
	var posY=$(".viaje"+viajeContentArray[currentViajeID].id).position().top;
	
	$(".formViaje").css({'visibility':'visible','top':posY});	
}

function onClickCerrarViajeForm(){
	$(".formViaje").css({'visibility':'hidden'});	
}

function sendViajeForm(){
	var obj=new Object();
	
	$(".viajeSMS").css({'background':'none'});
	
	obj.nombre=document.viajeFormatoForm.nombre.value;
	obj.apellido=document.viajeFormatoForm.apellido.value;
	obj.mail=document.viajeFormatoForm.mail.value;
	
	obj.titulo=viajeContentArray[currentViajeID].titulo;
	obj.texto=viajeContentArray[currentViajeID].texto;
	obj.imagen=viajeContentArray[currentViajeID].imagen;
	
	var mat=new Array('nombre','apellido','mail');
	var send=true;
	for(var i=0;i<mat.length;i++){
		if(document.viajeFormatoForm[mat[i]].value==""){
			$(document.viajeFormatoForm[mat[i]]).css({'border':'1px solid #F00'});
			send=false;
		}else{
			$(document.viajeFormatoForm[mat[i]]).removeAttr('style');
		}
	}
	
	if(send){
		if(isMail(obj.mail)){
			$(".viajeSMS").html('enviando...');
			
			$.post('php/sendViajeContacto.php',obj,function(data){
				$(".viajeSMS").css({'background':'#000000'});
				$(".viajeSMS").html(data);
			});
			
		}else{
			$(".viajeSMS").html('El e-mail no es válido.');
		}
	}else{
		$(".viajeSMS").html('Complete todos los campos.');	
	}
}

//////////////ACTIVIDADES////////////
var currentActividadID;

function onClickActividadDetalle(ID){
	currentActividadID=ID;
	
	document.actividadFormatoForm.nombre.value="";
	document.actividadFormatoForm.apellido.value="";
	document.actividadFormatoForm.mail.value="";
	$(".actividadSMS").css({'background':'none'});
	$(".actividadSMS").html('');
	
	var posY=$(".actividad"+ID).position().top;
	
	$("#modulo_actividades_form").css({'visibility':'visible','top':posY});	
}

function onClickCerrarActividadForm(){
	$("#modulo_actividades_form").css({'visibility':'hidden'});	
}

function sendActividadForm(){
	var obj=new Object();
	
	$(".actividadSMS").css({'background':'none'});
	
	obj.nombre=document.actividadFormatoForm.nombre.value;
	obj.apellido=document.actividadFormatoForm.apellido.value;
	obj.mail=document.actividadFormatoForm.mail.value;
	
	obj.titulo=actividadContentArray[currentActividadID].titulo;
	obj.texto=actividadContentArray[currentActividadID].texto;
	obj.imagen=actividadContentArray[currentActividadID].imagen;
	
	var mat=new Array('nombre','apellido','mail');
	var send=true;
	for(var i=0;i<mat.length;i++){
		if(document.actividadFormatoForm[mat[i]].value==""){
			$(document.actividadFormatoForm[mat[i]]).css({'border':'1px solid #F00'});
			send=false;
		}else{
			$(document.actividadFormatoForm[mat[i]]).removeAttr('style');
		}
	}
	
	if(send){
		if(isMail(obj.mail)){
			$(".actividadSMS").html('enviando...');
			
			$.post('php/sendActividadContacto.php',obj,function(data){
				$(".actividadSMS").css({'background':'#000000'});
				$(".actividadSMS").html(data);
			});
			
		}else{
			$(".actividadSMS").html('El e-mail no es válido.');
		}
	}else{
		$(".actividadSMS").html('Complete todos los campos.');	
	}
}

///////////////MAPA/////////////

function comoLlegarAvion(){
	texto='<iframe width="710" height="357" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com.ar/maps?f=d&amp;source=s_d&amp;saddr=aeropuerto+cordoba+argentina&amp;daddr=Entre+R%C3%ADos+435,+C%C3%B3rdoba&amp;geocode=FYwwIv4dQD0s_CFlFZzKo0w9zQ%3BFayRIP4dtbMs_CmzvjRGmqIylDExIeUy4INi-A&amp;aq=&amp;sll=-31.348653,-64.222641&amp;sspn=0.087815,0.154324&amp;vpsrc=6&amp;hl=es&amp;mra=ltm&amp;ie=UTF8&amp;t=m&amp;ll=-31.368295,-64.197235&amp;spn=0.209304,0.514297&amp;z=11&amp;output=embed"></iframe>';	
	
	$("#modulo_comollegar_mapa").html(texto);
}

function comoLlegarBus(){
	texto='<iframe width="710" height="357" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="http://maps.google.com.ar/maps?f=d&amp;source=s_d&amp;saddr=Av+Leopoldo+Lugones&amp;daddr=-31.4230743,-64.1771551+to:Entre+R%C3%ADos+435,+C%C3%B3rdoba&amp;geocode=FceCIP4ddMQs_A%3BFZ6FIP4d_bss_Cmn8TU1l6IylDETtFCFfdb4KA%3BFayRIP4dtbMs_CmzvjRGmqIylDExIeUy4INi-A&amp;aq=&amp;sll=-31.421885,-64.176705&amp;sspn=0.005484,0.009645&amp;vpsrc=0&amp;hl=es&amp;dirflg=w&amp;mra=dpe&amp;mrsp=1&amp;sz=17&amp;via=1&amp;ie=UTF8&amp;t=m&amp;ll=-31.421888,-64.1767&amp;spn=0.006537,0.015213&amp;z=16&amp;output=embed"></iframe>';	
	
	$("#modulo_comollegar_mapa").html(texto);
}

////////////CLOUD////////////////
var cloudArray=new Array();
var cloudIDArray=new Array();
function setCloudArray(matriz){
	cloudArray=matriz;
}

function setCloud(){
	
	var total=15;
	if(cloudArray.length<total){
		total=cloudArray.length;	
	}
	
	maxFicha=Math.round(total/3);
	//alert("total "+total+" maxFicha: "+maxFicha);
	var texto='';
	var cont=0;
	var fila=0;
	var isOpen=false;
	var isClose=false;
	for(var i=0;i<total;i++){
		
		var elID=getCloudD();
		cloudIDArray.push(elID);
		
		if(cont==0 && fila<3){
			texto+='<div id="modulo_txt_individuales">';
			isOpen=true;
			isClose=false;	
		}
		
		texto+='<p>'+cloudArray[elID]+'</p>';	
		
		cont++;
		
		if(cont>=maxFicha && fila<2){
			texto+='</div>';
			cont=0;
			fila++;
			
			isOpen=false;
			isClose=true;	
		}
		
	}
	if(!isClose){
		texto+='</div>';
	}
	
	$("#modulo_txt").html(texto);
}

function getCloudD(){
	var retNum=Math.floor(Math.random()*cloudArray.length);
	
	if(isInArray(retNum,cloudIDArray)){
		retNum=getCloudD();	
	}
	
	return retNum;	
}

function isInArray(dato,matriz){
	for(var i=0;i<matriz.length;i++){
		if(matriz[i]==dato){
			return true;	
		}
	}
	return false;
}

/////////////NEWSLETTER/////////////////

function sendNewsLetter(){
	var obj=new Object();
	
	
	obj.mail=document.newsletter.mail.value;
	if(obj.mail==""){
		$("#newsletterMSN").html('Ingrese un mail.');	
	}else{
		if(!isMail(obj.mail)){
			$("#newsletterMSN").html('El mail no es válido.');
		}else{
			$("#newsletterMSN").html('enviando...');
			$.post('php/insertNewsLetter.php',obj,function(data){
				
				$("#newsletterMSN").html(data);
			});
			
		}
	}
}

/////////////WORK/////////////////

function sendWorkMail(){
	$(".workMSN").css({'background':'none'});
	var obj=new Object();
	obj.mail=document.workForm.mail.value;
	if(obj.mail==""){
		$(".workMSN").html('Ingrese un mail.');	
	}else{
		if(!isMail(obj.mail)){
			$(".workMSN").html('El mail no es válido.');
		}else{
			$(".workMSN").html('enviando...');
			$.post('php/insertWorkMail.php',obj,function(data){
				$(".workMSN").css({'background':'#000'});
				$(".workMSN").html(data);
			});
			
		}
	}
}

////////////CONTACTO///////////

function sendContacto(){
	var obj=new Object();
	obj.nombre=document.contactoForm.nombre.value;
	obj.apellido=document.contactoForm.apellido.value;
	obj.mail=document.contactoForm.mail.value;
	obj.consulta=document.contactoForm.consulta.value;	
	//alert($(document.contactoForm.nombre).width());
	var nombresArray=new Array('nombre','mail','consulta');
	var send=true;
	for(var i=0;i<nombresArray.length;i++){
		if(document.contactoForm[nombresArray[i]].value==""){
			$(document.contactoForm[nombresArray[i]]).css({'border':'1px solid #FF0000'});
			send=false;	
		}else{
			$(document.contactoForm[nombresArray[i]]).removeAttr("style");
			
		}
	}
	
	if(!send){
		$("#modulo_contacto_messaje").html('Complete los campos marcados con <font color="#FF0000">*</font>.');	
	}else{
		if(!isMail(obj.mail)){
			$(document.contactoForm.mail).css({'border':'1px solid #FF0000'});
			$("#modulo_contacto_messaje").html('El mail ingresado no es válido.');
		}else{
			$(document.contactoForm.mail).removeAttr("style");
			$("#modulo_contacto_messaje").html('enviando...');
			
			$.post("php/sendContacto.php",obj,function (data){
				$("#modulo_contacto_messaje").html(data);	
			});
		}
	}
	
}



function isMail(pMail){
	
	var matriz1=pMail.split("@");
	if(matriz1.length!=2){
		return false;	
	}else{
		var parte2=matriz1[matriz1.length-1];
		var matriz2=parte2.split('.');
		
		var comar=matriz2[matriz2.length-1];
		
		if(comar.length>3){
			return false;	
		}
		
	}
	return true;
}
/////////////GALERIA///////////
var matrizGaleria;
var galeriaID=0;

function setMatrizGaleria(matriz){
	
	matrizGaleria=matriz;
}

function setGaleria(){
	var texto='';
	//alert('setGaleria');
	for(var i=0;i<matrizGaleria.length;i++){
		if(galeriaID!=i){
			texto+='<a href="javascript:void()" onclick="onClickGalBot('+i+')">'+matrizGaleria[i].nombre+'</a>';
		}else{
			texto+='<span id="modulo_galeria_content_links_selected">'+matrizGaleria[i].nombre+'</span>';
		}
	}
	$("#modulo_galeria_content_links").html(texto);
	showGalImages();
	
	
}

function showGalImages(){
	var matriz=matrizGaleria[galeriaID].galeria;
	var texto='';
	for(var i=0;i<matriz.length;i++){
		texto+='<a href="javascript:void()" onclick="onclickGalImage('+i+')"><img src="img/dinamic/'+matriz[i]+'" width="150" height="100" /></a>';
	}
	
	if(matriz.length>12){
		
		var falta=Math.round((matriz.length - 12)/3);
		
		ancho= 625+falta*150;
		//Ualert(ancho);
		$('#contenedor').css({'height':ancho});
		$('#modulo1').css({'height':ancho});
		
	}
	
	$("#modulo_galeria_content_images").html(texto);
}

function onClickGalBot(id){
	galeriaID=id;
	setGaleria();
}

function closeGalImage(){
	$("#negro").css({'visibility':'hidden'});	
	$("#big_image").css({'visibility':'hidden'});
	
	if(visiDiv!=undefined){
		$(visiDiv).css({'visibility':'visible'});	
	}
}

function onclickGalImage(id){
	
	var textIni='<a href="javascript:void();" onclick="closeGalImage()" id="big_image_close"></a><div id="big_image_content"><div id="big_image_content_preloader"></div></div>';
	$("#big_image").html(textIni);
	$("#big_image").css({'width':400,'height':400});
	
	doPosIniGal();
	var matriz=matrizGaleria[galeriaID].galeria;
	var imagen=new Image();
	imagen.src='img/dinamic/'+matriz[id];
	imagen.onLoad=onLoadGaleriaImagen(imagen);
}

var visiDiv;

function onclickOpenImagen(pRuta ,vDiv){
	
	var textIni='<a href="javascript:void();" onclick="closeGalImage()" id="big_image_close"></a><div id="big_image_content"><div id="big_image_content_preloader"></div></div>';
	$("#big_image").html(textIni);
	$("#big_image").css({'width':400,'height':400});
	
	doPosIniGal();
	var imagen=new Image();
	imagen.src=pRuta;
	imagen.onLoad=onLoadGaleriaImagen(imagen);
	
	if(vDiv != undefined){
		visiDiv=vDiv;
		$(vDiv).css({'visibility':'hidden'});
		
	}
}


function onLoadGaleriaImagen(img){
	var galImg=img;
	//$("#big_image_content").html(galImg);
	//alert("height "+galImg.height+"width "+galImg.width)
	var alto=$(window).height();
	var ancho=$(window).width();
	if(galImg.height+40>alto){
		galImg.height=galImg.height-40;
	}
	
	var wFin=galImg.width+20;
	var hFin=galImg.height+40;
	
	var posx=(ancho - wFin ) / 2;
	var posy=(alto - hFin ) / 2;
	
	
	$('#big_image_content').animate({
		width:galImg.width,
		height:galImg.height,
	  }, 500, function() {
		  
		$("#big_image_content").css({'width':galImg.width,'height':galImg.height});
		
	  });
	  
	  
	
	$('#big_image').animate({
		width:wFin,
		height:hFin,
		top:posy,
		left:posx
	  }, 500, function() {
		$("#big_image").css({'width':wFin,'height':hFin,'top':posy,'left':posx});
		$("#big_image_content").html(galImg);
		
	  });
	
}

function onTimeoutGalTimeOut(){
		
}

function doPosIniGal(){
	var alto=$(window).height();
	var ancho=$(window).width();
	
	var posx=(ancho - $("#big_image").width() ) / 2;
	var posy=(alto - $("#big_image").height() ) / 2;
	
	$("#big_image").css({'top':posy,'left':posx,'visibility':'visible','opacity': 0,'-moz-opacity':0, 'filter':'alpha(opacity=0)'});
	$("#negro").css({'height':alto,'visibility':'visible'});
	
	
	 $('#big_image').animate({
			opacity:1,
			'-moz-opacity':1,
			filter:'alpha(opacity=100)'
		  }, 500, function() {
			$("#big_image").css({'opacity': 1,'-moz-opacity':1, 'filter':'alpha(opacity=100)'});
		  });
}

/////////////CONOCENOS///////////
var conocenosID=0;

function setConocenos(){
	//alert('setConocenos');
	for(var i=0;i<4;i++){
		var clasBot='.secBot'+i;	
		var clasSec='.secInfo'+i;	
		
		$(clasBot).removeAttr("style");	
		$(clasSec).css({'visibility':'hidden','height':'0','overflow':'hidden'});
		if(i==conocenosID){
			$(clasSec).removeAttr("style");	
			$(clasSec).css({'visibility':'visible'});
			$(clasBot).css({'color':'#000','background':'url(img/secciones_bot.png) center bottom no-repeat'});
		}
	}
}

function onClickConocenos(id){
	conocenosID=id;
	setConocenos();
	
}

/////////////BANNER//////////////
var matrizBanner;
var bannerCont=-1;
var bannerInterval;

function setTopBanner(matriz){
	
	matrizBanner=matriz;
	bannerInn();
	
	bannerInterval=setInterval(bannerInn,10000);
}

function bannerInn(){
	bannerCont++;
	if(bannerCont>=matrizBanner.length){
		bannerCont=0;
	}
	
	var imagen=new Image();
	imagen.src='img/banner/'+matrizBanner[bannerCont];
	//alert("imagen.src "+imagen.src);
	imagen.onLoad=onLoadBanner(imagen);
		
			
}

function onLoadBanner(imagen){
	//alert('onLoad');
		 $('#banner').animate({
			opacity:0,
			'-moz-opacity':0,
			filter:'alpha(opacity=100)'
		  }, 500, function() {
			$("#banner").css({'opacity': 0,'-moz-opacity':0, 'filter':'alpha(opacity=0)'});
			$("#banner").html(imagen);
			
			 $('#banner').animate({opacity:1,'-moz-opacity':1,filter:'alpha(opacity=100)'}, 500, function() { $("#banner").css({'opacity': 1,'-moz-opacity':1, 'filter':'alpha(opacity=100)'}); });
			
		  });
}

///////////BOTONERA/////////////

jQuery(document).ready(iniciar);

$.history.init(function(url) {
		
	var ruta=document.location.href;

	if(ruta.split(separador).length<2){
		document.location.href=ruta+separador+"INICIO";
	}
	evalURLLocation();
	
});

function loadPage(){
	evalURLLocation();
}

function iniciar(){
	
		/*$.post("content/contenido.php",{},function(data){
			$("#contenedor").html(data);
			loaded=true;
			setConocenos();
			setGaleria();
		});*/
		
		loaded=true;
		//setConocenos();
		//setGaleria();
		
		setCloud();
		
}

function onClickTopBot(id){
	//currentBotID=id;
	var ruta=document.location.href;
	
	var rutaReal=ruta.split(separador)[0]+separador+botsArray[id].nombre;
	//alert("rutaReal "+rutaReal);
	document.location.href=rutaReal;
	
}

function cleanBotClass(){
	
	$("."+botsArray[currentBotID].bot).removeAttr("style");	
}

function evalURLLocation(){
	
	var ruta=document.location.href;
	var where=ruta.split(separador)[1];
	var elID=getIDbyWhere(where);
	setBotoneraByID(elID);
	loadByID(elID);
	//moveByID(elID);
	
	
		
	
}

var loaded=false;
currentID=0;
function loadByID(id){
	var ruta='content/'+botsArray[id].ruta;
	loaded=false;
	currentID=id;
	$("#contenedor").html('<div id="preloader"></div>');
	setTimeout(onTimeoutLoad,2000);
	
	$.post(ruta,{},function(data){
		loaded=true;
		
		$("#contenedor").html(data);
		moveByID(id);
		if(id==1){
			setConocenos();	
		}
		if(id==5){
			setGaleria();	
		}
		$("."+botsArray[id].bot).css({'color':'#FFFF00'});
	});
}

function onTimeoutLoad(){
	//alert("onTimeoutLoad "+loaded);
	if(!loaded){
		loadByID(currentID);
	}
}

function moveByID(id){
	var moveClase="#"+botsArray[currentBotID].content;	
	
	var alto=$(moveClase).height()+$("#scroll").height();
	//var pos=-1003*id;
	//alert("moveClase: "+moveClase+" alto: "+alto+" pos: "+pos);
	var medidaAlto=alto - $("#scroll").height();
	 $("#contenedor").css({'height':alto});
	 
	 
	  $("#modulo1").animate({
			height:alto
		  }, 1000, function() {});
		  
	 /*$("#contenedor").animate({
		left:pos,
		height:alto
	  }, 1000, function() {
		//$("#contenedor").css({'left':pos,'height':alto});
		
		 $("#modulo1").animate({
			height:alto
		  }, 1000, function() {
			//$("#modulo1").css({'height':alto});
		  });
		
	  });*/
	  
	 
	
}

function setBotoneraByID(id){

	cleanBotClass();
	currentBotID=id;
	
	$("."+botsArray[currentBotID].bot).css({'color':'#FFFF00'});
}

function getIDbyWhere(pWhere){
	for(var i=0;i<botsArray.length;i++){
		if(botsArray[i].nombre==pWhere){
			return i;	
		}
	}
	return 0;
}
