



// VARIABLES GLOBALES

var bloqueActual=0;

var viewportwidth;

var viewportheight;

var firstBgLoaded = false;

var id;







// INICIALIZA

$(document).ready(function(){

    

    		 $("#cube-1").hide();
			 $("#cube-2").hide();
			 $("#cube-3").hide();
    		 $("#cube-4").hide();
			 $('#btnIzq').hide();
			 
				
			/*Navegacion automatica bloques mouse over */	
			  $("div.overDer").mouseover(function() {
				 navToSecc(1);
			  });
			  $("div.overIzq").mouseover(function() {
			
				 navToSecc(-1);
			  });
			  
			 /*Descripciones proyectos QUI mouse over*/
			 $("#box-1").mouseover(function(){
		      	 $("#d1").css("visibility", "visible");

			  });
			  $("#box-1").mouseout(function(){
		      	 $("#d1").css("visibility", "hidden");

			  });
  		     $("#box-2").mouseover(function(){
			   $("#d2").css("visibility", "visible");
			   
			  });
			 $("#box-2").mouseout(function(){
		      	 $("#d2").css("visibility", "hidden");

			  });
			 $("#box-3").mouseover(function(){
				  $("#d3").css("visibility", "visible");
			   
			   });
			 $("#box-3").mouseout(function(){
		      	 $("#d3").css("visibility", "hidden");

			  });
			 $("#box-4").mouseover(function(){
				  $("#d4").css("visibility", "visible");
				 
			  });
			  $("#box-4").mouseout(function(){
		      	 $("#d4").css("visibility", "hidden");

			  });
			 $("#box-5").mouseover(function(){
			  	 $("#d5").css("visibility", "visible");
			  });
			  $("#box-5").mouseout(function(){
		      	 $("#d5").css("visibility", "hidden");

			  });

			
    $(window).scroll(function () { 

        checkScroll();

    });

	

    

    $(window).resize(function() {

        adjustSize()

    });

    adjustSize();

		

    $('#background').videobackground({

        resize:false,
		
		//poster: 'http://ekosistema.com/web/img/home/fondo.jpg',

        loop:true,

        videoSource: [
		
		'http://ekosistema.com/web/video/fondo_clouds.webm', 

        'http://ekosistema.com/web/video/fondo_clouds.mp4',

		'http://ekosistema.com/web/video/fondo_clouds.m4v', 
		//'Big_Buck_Bunny_Trailer.m4v',//'http://www.jplayer.org/video/m4v/Big_Buck_Bunny_Trailer.m4v',		

        'http://ekosistema.com/web/video/fondo_clouds.ogv'],
		 

        loadedCallback: function() {

            if(!firstBgLoaded){

                adjustBackground();

                $('#background video').fadeIn('slow');

                firstBgLoaded = true;

            }

        }

    });		

			

    /*Display o no la navegacion horizontal*/

    if(($("#level-1 .bloque").length * $(window).width())>($(window).width()))  {

        $('#nav').css("display","block");

    }

    else{

        $('#nav').css("display","none");

    }





    /*Fancybox*/

    $('.fancy-inline-link').each(function(){

        $(this).fancybox({

						

            'centerOnScroll': 'true',

            'autoDimensions': 'true',

            'autoScale': 'true',

            'scrolling' : 'no',

            'titlePosition'		: 'inside',

            'transitionIn'		: 'none',

            'transitionOut'		: 'none'

        });



    });	

			

			

		

    if($('body.ubi').length){

        

        $(window).unload(function(){

            GUnload();

        });

        initialize();

    }

    

    if(!$('body.home').length){



        $(window).scroll(function(){

            scrollParalax(); 

        });

        scrollParalax();

    }            

       

         

    setTimeout(adjustSize,200);

		

});





// fin --- INICIALIZA

function hoverMenu(id){
//alert(id);
  $("#"+id).addClass( "actual" );

}
/*Ajuste background*/

function adjustBackground(){

    var elBg = $("div#background");

    $(elBg).css("width",'100%');

    $(elBg).css("height",'100%');

		

    var elBgImg = $("div#background img#the_bg");

    $(elBgImg).css("height","auto");

    $(elBgImg).css("width",($(elBg).width())+"px");

    $(elBgImg).css("margin-left", 0);

		

    if($(elBg).height() > ($(elBgImg).height()-150)){

        $(elBgImg).css("height",($(elBg).height()+150)+"px");

        $(elBgImg).css("width","auto");

        $(elBgImg).css("margin-left",(($(elBg).width() - $(elBgImg).width())/2)+"px");

    }

    var elBgVideo = $("div#background video");

    if($(elBgVideo).length){

        var tempAlto = $(elBg).height();

        var tempAncho = $(elBg).width();

        var tempRatio;

        $(elBgVideo).css("margin-left", 0);



        if(tempAlto > tempAncho) {

            tempRatio = (Math.ceil((tempAlto / $(elBgVideo)[0].videoHeight)*10))/10;

            $(elBgVideo).css("height",($(elBgVideo)[0].videoHeight * tempRatio)+"px");

            $(elBgVideo).css("width",($(elBgVideo)[0].videoWidth * tempRatio)+"px");

            if($(elBgVideo).width() < tempAncho) {

                tempRatio = (tempAncho / $(elBgVideo)[0].videoWidth);

                $(elBgVideo).css("height",($(elBgVideo)[0].videoHeight * tempRatio)+"px");

                $(elBgVideo).css("width",($(elBgVideo)[0].videoWidth * tempRatio)+"px");

            }

        } else {

            tempRatio = (tempAncho / $(elBgVideo)[0].videoWidth);

            $(elBgVideo).css("height",($(elBgVideo)[0].videoHeight * tempRatio)+"px");

            $(elBgVideo).css("width",($(elBgVideo)[0].videoWidth * tempRatio)+"px");

            if($(elBgVideo).height() < tempAlto) {

                tempRatio = (tempAlto / $(elBgVideo)[0].videoHeight);

                $(elBgVideo).css("height",($(elBgVideo)[0].videoHeight * tempRatio)+"px");

                $(elBgVideo).css("width",($(elBgVideo)[0].videoWidth * tempRatio)+"px");

            }

        }

        if($(elBgVideo).width() > tempAncho)$(elBgVideo).css("margin-left",(($(elBg).width() - $(elBgVideo).width())/2)+"px");

    } 

}

/*Ajuste de elementos a tamaño de pantalla*/

function adjustSize(){

	//Animacion de cube txt box fades appear and hide 
	 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
	 if (typeof window.innerWidth != 'undefined')
	 {
		  viewportwidth = window.innerWidth,
		  viewportheight = window.innerHeight
	 }
	  
	// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)
	 else if (typeof document.documentElement != 'undefined'
		 && typeof document.documentElement.clientWidth !=
		 'undefined' && document.documentElement.clientWidth != 0)
	 {
		   viewportwidth = document.documentElement.clientWidth,
		   viewportheight = document.documentElement.clientHeight
	 }
	  
	 // older versions of IE
	  
	 else
	 {
		   viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		   viewportheight = document.getElementsByTagName('body')[0].clientHeight
	 }
	 
	 
	 
    var currViewport = viewport();

	

    $(".bloque").css("width",currViewport.width+"px");

    $(".bloques-container").each(function(){

        $(this).css("width",($(this).children(".bloque").length * currViewport.width)+"px");

    });

    

    $("#first-zone").css("width","0px");

    $(".deco-plantas").css("width","0px");

    $(".deco-plantas2").css("width","0px");

    $(".single-block").css("width","0px");

    



    

    var contentWidth = 0;

    $('#content').css("width","100%");

    $('#content').children().each(function(){

        contentWidth = $(this).width() >  contentWidth ? $(this).width() : contentWidth;

    });

    $('#content').css("width",contentWidth+"px");

    

    var domWidth = $(document).width();

    $("#first-zone").css("width",domWidth+"px");

    $(".deco-plantas").css("width",domWidth+"px");

    $(".deco-plantas2").css("width",domWidth+"px");

    $(".single-block").css("width",(domWidth -250)+"px");

     $(".double-block").css("width",(domWidth -600)+"px");

    
	

    $("#main").css("height",$('body').height()+"px");

    $("#first-zone").css("height",$('body').height()+"px");

    $(".bloque").css("height",$('body').height()+"px");

    $(".single-block").css("height",$('body').height()+"px");

    $(".double-block").css("height",$('body').height()+"px");
	

    var mitadAncho1=currViewport.width/2.5;

    var mitadAncho=currViewport.width/2.5;

    /*Distribucion black box*/

    $('.box').css("width",(currViewport.width * 0.29) +"px" );

    $('.box').css("height",((currViewport.width * 0.29)/1.3177)+"px" );

	 $('.boxF').css("min-width",200 +"px" );

    $('.boxF').css("max-height", 336+"px" );

    adjustBackground();

    

    $("#main").css("height",$('body').height()+"px");	

    $("#first-zone").css("height",$('body').height()+"px");

    $(".bloque").css("height",$('body').height()+"px");

    $(".single-block").css("height",$('body').height()+"px");

};





function viewport()

{

    var e = window

    , a = 'inner';

    if ( !( 'innerWidth' in window ) )

    {

        a = 'client';

        e = document.documentElement || document.body;

    }

    return {

        width : e[ a+'Width' ] , 

        height : e[ a+'Height' ]

    }

}









/*Mostrar y ocultar los boxTxt al hacer clic*/

function showTxtBox(proximo,actual){

	

    $("#"+proximo).show();

    $("#"+actual).hide();

	 

};



function toggleTextBox(elem){

    $(".textBox").each(function(){

        if(this == elem) $(elem).children("div").toggle();

        else{

            $(this).children(".boxBgOpen").hide();

            $(this).children(".boxBgClose").show();

        }

    });



    

};

	

/*Verificando posicion del scroll de la pantalla*/	
/*
function checkScroll(){

		

	

    var bloquesDeSeccion=$('#level-2 .bloque');//Bloques en la seccion actual de la web var bloquesDeSeccion

    var posicionScroll = $(window).scrollLeft();



    for(var i = 0 ; i < bloquesDeSeccion.length; i++){//i como el identificador de bloque

        var esEste = false;

        if(posicionScroll >= (i*viewportwidth) && posicionScroll < ((i+1)*viewportwidth)){

            esEste = true;

        }

        var arrBoxesPeque = $(bloquesDeSeccion[i]).children('.boxBgClose'); //txtBoxs con .boxBgClose hijo de cada .bloque	
alert("move"+arrBoxesPeque+" esEste"+esEste);
        var arrBoxesGran = $(bloquesDeSeccion[i]).children('.boxBgOpen');

        for(j = 0 ; j < arrBoxesPeque.length; j++){

            if(esEste){

					

                $(arrBoxesPeque[j]).delay(j*250).fadeOut('fast'); 

                $(arrBoxesGran[j]).delay(j*250).fadeIn('slow'); 

            }

            else{

					

                $(arrBoxesGran[j]).hide(); 

                $(arrBoxesPeque[j]).show(); 

            }

        }

			

			

			

    }

    //Bloque#1 muestra flecha der

    if((posicionScroll >= 0) && (posicionScroll < viewportwidth)) {

        $('#btnDer').show();

        $('#btnIzq').hide();

        bloqueActual=0;

    }//Bloque>#1 muestra flecha izq

    else if ((posicionScroll >= viewportwidth)&&(posicionScroll <= (2*viewportwidth))){

			

        $('#btnDer').hide();

        $('#btnIzq').show();

        bloqueActual=1;

    }

			

	

		

}

	*/
	
/*Verificando scroll en pantalla*/
	function checkScroll(){
		
	//Animacion de cube txt box fades appear and hide
		var bloquesDeSeccion=$('#level-2 .bloque');//Bloques en la seccion actual de la web var bloquesDeSeccion
		var posicionScroll = $(window).scrollLeft();
	
		for(var i = 0 ; i < bloquesDeSeccion.length; i++){//i como el identificador de bloque
			var esEste = false;
			if(posicionScroll >= (i*viewportwidth) && posicionScroll < ((i+1)*viewportwidth)){
				esEste = true;
			}
			var arrBoxesPeque = $(bloquesDeSeccion[i]).children('.boxBgClose'); //txtBoxs con .boxBgClose hijo de cada .bloque
			var arrBoxesGran = $(bloquesDeSeccion[i]).children('.boxBgOpen');
			for(j = 0 ; j < arrBoxesPeque.length; j++){
				if(esEste){
					
					$(arrBoxesPeque[j]).delay(j*250).fadeOut('fast'); 
					$(arrBoxesGran[j]).delay(j*250).fadeIn('slow'); 
				}
				else{
					
					 $(arrBoxesGran[j]).hide(); 
					 $(arrBoxesPeque[j]).show(); 
				}
			}
			
			
			
		}
			//Bloque#1 muestra flecha der
			if((posicionScroll >= 0) && (posicionScroll < viewportwidth)) {
				$('#btnDer').show();
				$('#btnIzq').hide();
				bloqueActual=0;
			}//Bloque>#1 muestra flecha izq
			else if ((posicionScroll >= viewportwidth)&&(posicionScroll <= (2*viewportwidth))){
			
				$('#btnDer').show();
				$('#btnIzq').show();
				bloqueActual=2;
			}
			else if ((posicionScroll >= 2*viewportwidth)&&(posicionScroll <= (3*viewportwidth))){
			
				$('#btnDer').hide();
				$('#btnIzq').show();
				bloqueActual=1;
			}
			
	
		
	}

	

function navToSecc(next){

		

    //Bloques en la seccion actual de la web var bloquesDeSeccion

    var bloquesDeSeccion=$('#level-2 .bloque');

    //alert(" " + viewportwidth+" "+next );

    if (next == -1){

        $.scrollTo(('0'), {

            duration: 100

        });

        bloqueActual=0;

    }

    else if (next == 1)

    {/*
		if((posicionScroll >= 0) && (posicionScroll < viewportwidth)) {
			$.scrollTo((viewportwidth), {

            duration: 100

        });

       			 bloqueActual=1;
		}
		else if ((posicionScroll >= viewportwidth)&&(posicionScroll <= (2*viewportwidth))){
			$.scrollTo((2*viewportwidth), {
	
				duration: 100
	
			});
	
					 bloqueActual=2;
		}
				 */
		
		$.scrollTo((viewportwidth), {

            duration: 100

        });

       			 bloqueActual=1;
		
		
			
       

    }



		

		

	

	

}

	

function widePic(urlPic,div ) {

		

    $(div).attr("src",urlPic);



}













/*

* SCROLL

*/





function scrollParalax(){   

    

    

    

    

    var posicionScroll = $(window).scrollLeft();

    var anchoPantalla = $(window).width();

    var ratio = (posicionScroll / anchoPantalla) - 0.5 ;

    

    $('.level-p4').css('background-position',(ratio * -(anchoPantalla*4))+'px 100%');

    $('.level-p3').css('background-position',(ratio * -(anchoPantalla*3))+'px 100%');

    $('.level-p2').css('background-position',(ratio * -(anchoPantalla*2))+'px 100%');

    $('.level-p1').css('background-position',(ratio * -(anchoPantalla*1))+'px 100%');

    $('.level-p0').css('background-position',(ratio * -(anchoPantalla*0.75))+'px 100%');

    
	 $('#box-3').css('right',((ratio + 0.5) * -(anchoPantalla*0.75))+'px');
    

    

    $('#level-2 .bloque').css('left',((ratio + 0.5) * -(anchoPantalla*0.75))+'px');

    $('#level-1 .bloque').css('left',((ratio + 0.5) * -(anchoPantalla*0.5))+'px');

    $('#level-0 .bloque').css('left',((ratio + 0.5) * -(anchoPantalla*0.25))+'px');

    

    

    $('.single-block div').css('left',((ratio + 0.5) * (anchoPantalla*0.10))+'px');

    

    $('.bloque .extr-right').each(function(){

        $(this).css('margin-right',$(this).parent().css("left"));

    });



//var contentWidth = 0;

//    var contentSub = 0;

//    $('#content').children().each(function(){

//        contentWidth = $(this).width() >  contentWidth ? $(this).width() : contentWidth;

//        var tempDesplaza = parseFloat(String($(this).children('.bloque').css("left")).replace("px",""));

//        contentSub = tempDesplaza < contentSub ? tempDesplaza : contentSub ; 

//    });

//    

//    

//    $('#main').css("width",(contentWidth + contentSub)+"px");



    

    

}







/*

*  MAPAS 

*/

/*

function initialize() {

    if (GBrowserIsCompatible()) {

        var map = new GMap2(document.getElementById("map_canvas"));

        var myCounter = new Counter();

        map.setCenter(new GLatLng(41.387696, 2.135247), 13);

        map.setUIToDefault();

        var point = new GLatLng(41.387696, 2.135247);

        map.addOverlay(new GMarker(point));

        GEvent.bind(map, "click", myCounter, myCounter.increment);

		

        var map2=new GMap2(document.getElementById("map_canvas2"));

        var myCounter2 = new Counter();

        map2.setCenter(new GLatLng(52.48967,13.363172),13);

        map2.setUIToDefault();

        var point2 = new GLatLng(52.48967,13.363172);

        map2.addOverlay(new GMarker(point2));

        GEvent.bind(map2, "click", myCounter2, myCounter2.increment);

		

        var map3=new GMap2(document.getElementById("map_canvas3"));

        var myCounter3 = new Counter();

        map3.setCenter(new GLatLng(52.48967,13.363172),13);

        map3.setUIToDefault();

        var point3 = new GLatLng(52.48967,13.363172);

        var point4 = new GLatLng(41.387696, 2.135247);

        map3.addOverlay(new GMarker(point3));

        map3.addOverlay(new GMarker(point4));

        GEvent.bind(map3, "click", myCounter3, myCounter3.increment);

		

    }

}

    */

function Counter() {

    this.counter = 0;

}

    

Counter.prototype.increment = function() {
/*
    this.counter++;

    alert("You have clicked the map " + this.counter +

        (this.counter == 1 ?" time":" times"));

    $("#inline13").delegate("a.simpletitle", "click", function(event){

        event.preventDefault();

        $.fancybox(this,{

            'titlePosition': 'inside'

        });

    }); 

*/

}









/*

* GESTIÓN DE FORMULARIOS

*/	

function datosCandidato(){

    var $form=$('#form');

    var data="nom="+ $form.find( 'input[name="nom"]' ).val() +"&apellidos="+ $form.find( 'input[name="apellidos"]' ).val() +"&mail="+ $form.find( 'input[name="mail"]' ).val() +"&telefono="+ $form.find( 'input[name="telefono"]' ).val() +"&file="+ $form.find( 'input[name="file"]' ).val();

    //alert(data);

    if($form.find( 'input[name="nom"]' ).val() != "" && $form.find( 'input[name="apellidos"]' ).val() != "" && $form.find( 'input[name="mail"]' ).val() != "" && $form.find( 'input[name="telefono"]' ).val() != "" && $form.find( 'input[name="file"]' ).val() != "" )
    {	

        str= ($form.find( 'input[name="mail"]' ).val()) ;        

        var err=false;

        if ($.trim(str)=='') return true;

	

        var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; 

				  

        if ( str.match(filter) ) {

            err=false;

            $.ajax({

                type: "POST",

                url:"php/datos.php", 

                data:  data,

                success:	 function(msg){

                   alert("Gracias por aplicar, sus datos se han enviado correctamente: "+msg);

						   

                },

                error: function(msg){

                   alert(msg);

                },

		

            });

            $form.find( 'input[name="nom"]' ).val("");

            $form.find( 'input[name="apellidos"]' ).val("");

            $form.find( 'input[name="mail"]' ).val("");

            $form.find( 'input[name="telefono"]' ).val("");

            $form.find( 'input[name="file"]' ).val("");

				

				  

        }

        else  {

            err=true;

				  

        }

				 

        if(err){

            alert(str + ' no es una direccion de correo.');

            $(this).val('');

				

        }

				

	

	

		

		

    }

    else alert("Por favor revise sus datos");

}

	

function datosContacto(){

	

    var $form=$('#formContacto');

    var data="empresa="+ $form.find( 'input[name="empresa"]' ).val() + "&nombreC="+ $form.find( 'input[name="nomC"]' ).val()+ "&correo="+ $form.find( 'input[name="correo"]' ).val()+"&motivo="+ $form.find( 'textarea[name="motivo"]' ).val();

	

    if($form.find( 'input[name="empresa"]' ).val() != ""  && $form.find( 'input[name="correo"]' ).val() != "")

    {	

        str= ($form.find( 'input[name="correo"]' ).val()) ;        

        var err=false;

        if ($.trim(str)=='') return true;

	

        var filter=/^(("[\w-\s]+")|([\w-]+(?:\.[\w-]+)*)|("[\w-\s]+")([\w-]+(?:\.[\w-]+)*))(@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$)|(@\[?((25[0-5]\.|2[0-4][0-9]\.|1[0-9]{2}\.|[0-9]{1,2}\.))((25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\.){2}(25[0-5]|2[0-4][0-9]|1[0-9]{2}|[0-9]{1,2})\]?$)/i; 

				  

        if ( str.match(filter) ) 

        {

            err=false;

            $.ajax({

                type: "POST",

                url:"php/contacto.php", 

                data:  data,

                success:	 function(msg){

                    alert("Gracias por contactarnos, en breve le responderemos");

							   

                },

                error: function(msg){

                    alert(msg);

                },

			

            });

            $form.find( 'input[name="empresa"]' ).val("");
			
			$form.find( 'input[name="nomC"]' ).val("");

            $form.find( 'input[name="correo"]' ).val("");

            $form.find( 'textarea[name="motivo"]' ).val("");

		

        }

        else  {

            err=true;

				  

        }

				 

        if(err){

            alert(str + ' no es una direccion de correo.');

            $(this).val('');

				

        }

    }

}
