function urlNumeral (url){
	
	url = url.split('#')
	return url[1];
}

$.fn.bgPosition = function() {
    var bgp = "background-position";
    return $.browser.msie ? [$(this).css(bgp + "-x").split(" ")[0], $(this).css(bgp + "-y").split(" ")[0]] : [$(this).css(bgp).split(" ")[0], $(this).css(bgp).split(" ")[1]];
}

$.fn.placeholder=function(o){var d={placeholderTextColour:"#CCC"};var p=$.extend(d,o);return this.each(function(){var t=$(this);var a=t.attr("placeholder");if (typeof a!=="undefined"&&a!==false&&this.nodeName.toLowerCase()=="input"){var q=$.metadata?$.extend({},p,t.metadata()):p;var y=q.placeholderTextColour; var z="color"; f(t,a,"",z,y);t.focus(function(){f(t,"",a,z,"");});t.blur(function(){f(t,a,"",z,y);});}});};function f(a,b,c,d,e){if(a.val()==c){a.val(b).css(d,e);}}

lfc = {};

lfc.global = {};

lfc.log = function(salida){
	
	if($.browser.mozilla == true){
		console.log(salida)
	}else{
		//$("body").append("<div>" +salida+"</div>" )
	}
}
// aca guardo todos los punteros de jq
lfc.$ = {}

lfc.global.ini = function(){

	lfc.$.body = $("body");
	lfc.$.header = $("header");
	lfc.$.footer = $("footer");
	lfc.$.btnFooter = lfc.$.footer.find("#btnHideShowFooter");
	lfc.$.triangulo = $("#triangulo");
	lfc.$.secciones = $("section");
	lfc.$.nav = $("#float");
	lfc.$.h1Nav = lfc.$.nav.find("h1")
	lfc.$.nav.leftOrg = parseInt(lfc.$.nav.css("left"));
	lfc.$.nav.topOrg = parseInt(lfc.$.nav.css("top"));
	lfc.$.window = $(window);
	lfc.$.document = $(document);
	lfc.$.frameActivo = $()
	lfc.estadoAnimate = false;
	
	if( $.browser.msie == true && $.browser.version == 6 ){
	
		if( lfc.$.header.length == 1){
			
			lfc.$.header.css("position","absolute");
			lfc.global.posHeader();

			lfc.$.window.scroll(function(){	lfc.global.posHeader();	})
			lfc.$.window.resize(function(){	lfc.global.posHeader();	})
		}// end IF header
		
		if( lfc.$.nav.length == 1){
			
			lfc.$.nav.css("position","absolute");
			lfc.global.posNav(true);
			lfc.$.window.scroll(function(){	lfc.global.posNav(true);	})
			lfc.$.window.resize(function(){	lfc.global.posNav(true);	})
		}// end if NAV
		
		if( lfc.$.triangulo.length == 1){
			
			lfc.$.triangulo.css("position","absolute");
			lfc.global.posTriangulo();
			lfc.$.window.scroll(function(){	lfc.global.posTriangulo();	})
		}
		if( lfc.$.footer.length == 1){
			
			lfc.$.footer.css("position","absolute");
		}
	
	}else{
			
		if( lfc.$.nav.length == 1){
	
			lfc.global.posNav(false);
			lfc.$.window.resize(function(){	lfc.global.posNav(false);	})
		}
	}// END if IE

	
	lfc.global.posSecciones();
	
	lfc.$.window.resize(function(){
		lfc.global.posSecciones();
		lfc.global.posFooter(0);
		lfc.global.goto(lfc.$.frameActivo,true);
	})
	
	// genero las mariposas
	lfc.$.secciones.each(function(){
		
		var $seccion = $(this);
		var $articulo= $seccion.children("article");
		
		lfc.global.mariposear($articulo,8)
	})
	
	lfc.$.window.scroll(function(){
	
		var array = []
		var menu = false;
		var altoWin = parseInt( lfc.$.window.height() );
		var altoDoc = parseInt( lfc.$.document.height() );
		var pos = "";
		
		var porcScroll  =  ( lfc.$.window.scrollTop() ) / (altoDoc - altoWin)  * 100;
		var porcScrollinv  =  100 - porcScroll;
		
		
		/*
		lfc.$.body.css("background-position",
			lfc.$.body.attr("data-bgLeft") + " " +
				( 50 * (porcScrollinv / 100) + 150 ) + "px"
		);
		*/
		
		lfc.$.secciones.each(function(){
		
			var $seccion = $(this);
			var $articulo= $seccion.children("article");
			var $mariposas = $articulo.children(".mariposa");
			var distancia = $seccion.offset().top - lfc.$.window.scrollTop();
			
			var porc = parseInt(distancia / $seccion.offset().top * 100);
			if( isNaN(porc) ){
				porc = 0;
			}
			
			lfc.log( $seccion.attr("id") + ": "+ distancia + "("+ porc +")");
		
			array.push(
				Array(
					$seccion,
					$seccion.offset().top + 0 - lfc.$.window.scrollTop()
				)
			)
			
			pos = parseInt($seccion.offset().top) - parseInt(lfc.$.window.scrollTop()) + parseInt($seccion.attr("data-bgTop"));
			
			$seccion.css("background-position","0px "+pos+"px");

			$articulo.css("background-position", $articulo.attr("data-bgLeft")+"px "+ (
				parseInt(
					$seccion.offset().top - lfc.$.window.scrollTop()
					+ ( ( $seccion.offset().top - lfc.$.window.scrollTop() ) / 2 )
					+ parseInt( $articulo.attr("data-bgTop") )
				)
			)+"px");
			
			$mariposas.each(function(){
				
				$m = $(this);
				var org = -330;
				
				if( $m.attr("data-org") == "bottom" ){
					org = 330;
				}
				
				$m.css("top",
				
						 (
						 org + parseInt( $m.attr("data-posy") )
						 )
						* porc / 100
						+
						(
						parseInt( $m.attr("data-posy") )
						)
				)
				
			})
			
			
			// averiguo si lo postulo para marca en la botonera
			if(
				$seccion.offset().top + 185 > lfc.$.window.scrollTop()
				&&
				$seccion.offset().top + 185 <= lfc.$.window.scrollTop() + altoWin
			){
			
				menu = $seccion;
			}
			
		})
		
		if(menu){
		
			if(lfc.estadoAnimate == false ){
	
				lfc.global.activarSeccion(menu);
			}
		}
		
	}) // SCROLL ************************************************************************************************************************************

	// atributos para la botonera
	lfc.$.nav.find('a.block').click(function(e){
		e.preventDefault()
		lfc.global.goto( $("#"+urlNumeral($(this).attr("href"))) )
	})
	
	lfc.$.btnFooter.click(function(e){ e.preventDefault(); lfc.global.showFooter(0); })
	
	lfc.$.footer.hover(function(){},function(){
	
		if( lfc.$.footer.attr("data-viable") != "1" ){
			
			lfc.global.hideFooter(0)
		}
	});
	
	
	
	
	// Asigno los data- ***********************************************************************************************************************
	
	var partes = [];
	
	// guarda en data alguna info importante
	lfc.$.secciones.each(function(){
		
		var $seccion = $(this);
		var $articulo= $seccion.children("article");
		
		partes = $seccion.bgPosition()
		$seccion.attr("data-bgTop", parseInt(partes[1]) );
		
		
		partes = $articulo.bgPosition();
		$articulo.attr("data-bgLeft", parseInt(partes[0]) );
		$articulo.attr("data-bgTop", parseInt(partes[1]) );
	})
	
	partes = (lfc.$.body.bgPosition());
	lfc.$.body.attr("data-bgLeft", partes[0] );
	lfc.$.body.attr("data-bgTop", partes[1] );
	

	// *****************************************************************************************************************************************

	
	// seteo el primer frame activo
	lfc.global.goto( $("#home") );
	lfc.$.window.scroll();

	lfc.$.body.css("visibility","visible");
}

lfc.global.posHeader = function (){
	
	lfc.$.header.css("top",
		lfc.$.window.scrollTop()
	)
}
lfc.global.posFooter = function (dy){
	

	if( $.browser.msie == true && $.browser.version == 6 ){

		lfc.global.showFooter(0)
		return true;
	}

	
//	if(lfc.$.frameActivo.attr("id") != lfc.$.footer.attr("data-section") ){

		var $articulo = lfc.$.frameActivo.find("article");
	
		// evaluo si hay lugar para mostrar el footer
		if( parseInt($articulo.attr("data-altoReal")) + lfc.$.header.height() + lfc.$.footer.outerHeight() < lfc.$.window.height() ){
		
			lfc.global.showFooter(dy);
			// marco al footer como que es viable que esta visible
			lfc.$.footer.attr("data-viable","1");
			
			if(dy>0){
			
				lfc.$.footer.addClass("alpha-333");
			}
		
		}else{
		
			lfc.global.hideFooter(dy);
			lfc.$.footer.removeAttr("data-viable");
		}
		
		lfc.$.footer.attr("data-section",
			lfc.$.frameActivo.attr("id")
		)
//	}
	

}
lfc.global.showFooter = function(dy){
	
	$("#avisoFooter").remove();
	
	if( $.browser.msie == true && $.browser.version == 6 ){
		lfc.$.footer.css("top", lfc.$.window.scrollTop() +lfc.$.window.height() - lfc.$.footer.outerHeight() );
		return false;
	}
	
	if(!dy){ var dy = 1; }
	
	if( ! lfc.$.footer.attr("data-isUp") ) {

		lfc.$.footer.attr("data-isUp","1");
		lfc.$.btnFooter.stop().delay(dy).fadeTo(250,0);

		lfc.$.footer.stop().delay(dy).animate({"bottom":0},{
			duration	: 1000,
			easing	: "easeOutBounce",
			complete	: function(){
				lfc.$.footer.removeAttr("data-isUp");
				lfc.$.footer.removeAttr("data-isDown");
			}
		});
	}
}
lfc.global.hideFooter = function(dy){
	
	if( $.browser.msie == true && $.browser.version == 6 ){
		return true
	}
	
	if(!dy){ var dy = 0; }
	
	if( ! lfc.$.footer.attr("data-isDown") ) {
	
		lfc.$.footer.attr("data-isDown","1");
		lfc.$.btnFooter.stop().delay(dy).fadeTo(250,100);
		
		lfc.$.footer.stop().delay(dy).animate({"bottom": lfc.$.footer.outerHeight() * -1 },{
			duration	: 1000,
			easing	: "easeOutBounce",
			complete	: function(){
				lfc.$.footer.removeAttr("data-isUp");
				lfc.$.footer.removeAttr("data-isDown");
			}
		});
	}
}

lfc.global.posNav = function (top){
	
	lfc.$.nav.css("left",
		( lfc.$.window.width() - 990 ) / 2 + lfc.$.nav.leftOrg
	)
	
	if(top){
		lfc.$.nav.css("top",
			lfc.$.window.scrollTop() + lfc.$.nav.topOrg
		)
	}
}

lfc.global.posTriangulo = function(){
		
		lfc.$.triangulo.css("top",
			lfc.$.window.scrollTop()
		)
}

lfc.global.posSecciones = function (){
	
	lfc.$.secciones.css("padding-top",
		lfc.$.header.height()
	);
	
	lfc.$.secciones.each(function(){

		var $seccion = $(this);
		var $article = $seccion.children("article");

		if( lfc.$.window.height() < parseInt( $article.attr("data-altoReal") ) + lfc.$.header.height() ){

			$article.height(
				parseInt( $article.attr("data-altoReal") ) //+ lfc.$.footer.outerHeight() + 20
			);

		}else{

			$article.height(
				lfc.$.window.height() - lfc.$.header.height()
			);
		
		} // end if
	})
}

lfc.global.goto = function ($target, rapido){
	
	if(!rapido){
		var tiempo = 1200;
	}
	
	lfc.estadoAnimate = true;
	
	$('html, body').stop().animate({scrollTop : $target.offset().top },{
		duration:tiempo,
		easing: 'easeInOutQuint',
		complete: function(){
			
			lfc.estadoAnimate = false;
		}
	})// and SCROLL ANIMATE

	lfc.global.activarSeccion($target)
}

lfc.global.activarSeccion = function ($target){
	
	if( $target.attr("id") != lfc.$.frameActivo.attr("id") ){
	
		// limpio la botonera
		lfc.$.nav.find('a.block').removeClass("selected");
		
		var v = 0
		lfc.$.nav.find('a.block').each(function(vuelta){
			var $btn = $(this);
			if( urlNumeral($btn.attr("href")) == $target.attr("id") ){
				$btn.addClass("selected");
				v = vuelta;
			}
		})
	
		lfc.$.h1Nav.stop().animate({backgroundPosition : "0px "+ (-83*v) +"px"},{
			duration	: 1000,
			easing	: "easeOutBounce"
		});
	
		lfc.$.frameActivo = $target;
	}
	lfc.global.posFooter(500);
}

lfc.global.mariposear = function($target,total){
	
	for( i=0 ; i < total ; i++ ){
	
		$target.append("<div class='mariposa'></div>");
		$m = $target.children(".mariposa:last-child");
		
		$m.attr("a",i);
		
		// tipo de mariposa
		var tipo = Math.floor(Math.random()*6);
		var org = Math.floor(Math.random()*2);
		var posy = -60 + Math.floor(Math.random()* (225 - 35));
		var posx = Math.floor(Math.random()*(981-35))
		
		//$m.addClass("t"+tipo);		
		$m.css("background-position", -35 * tipo+"px top")
		
		$m.css({
			top:	posy,
			left:	posx
		})
		
		$m.attr("data-posy", posy );
		$m.attr("data-posx", posx );
		$m.attr("data-avance", 1 + Math.floor(Math.random()*6) );
		
		if(org == 1){
			$m.attr("data-org", "top" );
		}else{
			$m.attr("data-org", "bottom" );
		}
			
		
	}
}
