$(document).ready(function(){
	
	
// deshabilitar scrollbars 	
if (document.all && document.createAttribute && document.compatMode != 'BackCompat') {
     // IE6 (and above) in standards mode
     document.getElementsByTagName('html')[0].style.overflow = 'hidden'; // set to '' to show
} else {
     document.body.style.overflow = 'hidden'; // set to '' to show
}

// NAVEGACIÓN -------------------------------->>
	
	// Configuración
	maskMinOpacity = 0; // Opacidad mínima de máscara
	maskMaxOpacity = 0.5; // Opacidad máxima de máscara
	sectionMinOpacity = 0.3; //Opacidad mínima de sección
	sectionMaxOpacity = 1; // Opacidad máxima de sección

	// Posición central de máscara
	wrapperTop = $("header").height()+30;
	wrapperLeft = ($(window).width()/2)-($("#mask").width()/2);	
	
	//
	currentWindowWidth = $(window).width();
	
	navColors = Array("4b92b0","be3250","be328e","af32be","7132be","3532be","32be8e","32be46","71be32","a2be32","be9b32","c97e2f","c45a20","c43b20","c42020","2062c4","4b9b5a","4a7ca6","975ebb","c45a20","c43b20","c42020","2062c4","4b9b5a","4a7ca6","975ebb");
	navColors.shuffle(); // shuffle
	
	contactoRef = "#gop_21";
	
	modeMenu = true;
	
	textShadowOptions = {
	  x:      1, 
	  y:      1, 
	  radius: 1,
	  color:  "#666"
	}

	currentInteres = false;
	
	preloader1 = '<div style="width:160px;margin:170px auto 0 auto;filter:alpha(opacity=20);-moz-opacity:0.2;-khtml-opacity: 0.2;opacity: 0.2;z-index:10000"><img src="theme/img/preloader.gif" width="160" height="17" /></div>';
	preloader2 = '<div style="width:50%;filter:alpha(opacity=20);-moz-opacity:0.2;-khtml-opacity: 0.2;opacity: 0.2;z-index:10000;position:absolute;top:50px;left:25%;text-align:center"><img src="theme/img/preloader.gif" width="160" height="17" /></div>';
	//<<--
	
	// Init
	$("#mask").css({opacity:maskMinOpacity});
	$("#mask").css({"top":wrapperTop,"left":wrapperLeft});
	
	// navegacion
	$("nav a").click(function(){

		if($(this).attr("href")!="#"){
			if(!modeMenu) closeFullContentFM($("#ccc #fullContent #c1"));
			doNav(this);
			theHref = $(this).attr("href");
			modeMenu = true;
		}
		return false;	
	});
	
//  <<-------------------------------- NAVEGACIÓN




// CANVAS -------------------------------->>
$("#wrapperContent ul li").each(function(index){
	
	var gradStop1 = "#"+navColors[index];
	//var gradStop1 = $.xcolor.random();
	var gradStop2 = $.xcolor.lighten(gradStop1);
	var gradStop3 = $.xcolor.darken(gradStop1);
	doContainer("p_"+(index+1),gradStop1,gradStop2,gradStop3);	

});
	var MgradStop1 = "#000";
	var MgradStop2 = $.xcolor.lighten(MgradStop1);
	var MgradStop3 = $.xcolor.darken(MgradStop1);
	doContainer("maskCanvas",MgradStop1,MgradStop2,MgradStop3);	

//  <<-------------------------------- CANVAS

	
		var resizeTimer = null;
		$(window).bind('resize', function() {
			if(modeMenu){
				if (resizeTimer) clearTimeout(resizeTimer);
				resizeTimer = setTimeout(onResizeSTScreen, 1000);
			}
		});
	
		
});


// Listener cuando se reescala la ventana en la pantalla del menu
function onResizeSTScreen(){
	wrapperLeft = ($(window).width()/2)-($("#mask").width()/2);	
	$("#mask").css({"left":wrapperLeft});
	$(".current_page_item_two a").click();
}

// Listener cuando se reescala la ventana en la pantalla del contenido completo
function onResizeNDScreen(){
	
	$("#ccc").remove();
  	reloadedCanvas = "#"+fullScreenCanvas();
 	 getFullContent(reloadedCanvas);
}



// Navegación
function doNav(o){
	$("#wrapperContent li .content").html("");
	$("#mask").show().animate({opacity:maskMaxOpacity}); // muestra máscara
	wrapperX  =$("#wrapperContent").offset().left;
	thisID = $(o).attr("id");
	sectionID  = "#wrapperContent ul li#s"+thisID.substring(3,thisID.length);
	$("li").animate({opacity:sectionMaxOpacity})
	sectionX = $(sectionID).offset().left;
	maskX = $("#mask").offset().left;
	
	wrapperY  =$("#wrapperContent").offset().top;
	sectionY = $(sectionID).offset().top;
	maskY = $("#mask").offset().top;
	
	xFPos = wrapperX+(maskX-sectionX);
	yFPos = wrapperY+(maskY-sectionY); 
	
	$("#wrapperContent").stop().animate({"top":yFPos+"px","left":xFPos+"px"}, {easing: 'easeInOutExpo', duration:1500,complete:function(){targetDone()}});
	
}


// terminada la animacion de la navegación principal
function targetDone(){
	
	$("#mask").animate({opacity:maskMinOpacity}).hide();
	$("#wrapperContent li").not(sectionID).animate({opacity:sectionMinOpacity});
	selectedSectionID = thisID.substring(3,thisID.length);
	if($("#ccc"))$("#ccc").remove();
	loadContent(selectedSectionID);
	
	
}


// Cargar el contenido de la sección
function loadContent(ID){
	
	var $container = $("#s"+ID+" .content");
	if(currentInteres){
		url2Load = $("#gop"+ID).attr("href")+"&abstract=1&interes="+currentInteres;
		currentInteres = "";	
	}
	else{
		url2Load = $("#gop"+ID).attr("href")+"&abstract=1";
	}
	$container.html(preloader1).load(url2Load,function(){
		
		
		if($("#s"+ID+" .content div.goContact").length>0){
			$container.append('<div class="attButton"><a href="#">Solicitar Informaci&oacute;n &raquo;</a></div>');
			$(".attButton a").click(function(){goToContact($("#gop"+ID).html())});
		}
		
		if($("#s"+ID+" .content div.more").length>0){
			$container.append('<div class="continueButton"><a href="#">Seguir Leyendo &raquo;</a></div>');
			doInContent(true);
			$(".continueButton a").click(function(){showFullContent()});
		}
		else{
			doInContent(false);
		}
	});
	

}



// para dentro de contenido
function doInContent(showMore){
	
	$container = $("#p"+selectedSectionID);
	var secMainColor = (subMenuContButton)?subMenuContButton:$(".current_page_item_two a").attr("rel");
	if(showMore){
		$(".continueButton").css({
							"background-color":$.xcolor.combine(secMainColor,'#000'),
							"border":"solid 1px "+$.xcolor.opacity(secMainColor,"#FFF",0.2)
							});
	 	$(".continueButton").corner(10);						
	}
	
	$("#c1 a").css({color:$.xcolor.blue(secMainColor)});
	if($(".attButton").length>0){
	$(".attButton").css({
							"background-color":$.xcolor.combine(secMainColor,'#000'),
							"border":"solid 1px "+$.xcolor.opacity(secMainColor,"#FFF",0.2)
	});
	$(".attButton").corner(10);	
	}
	
	
	Cufon.replace('h1', { fontFamily: 'Bradley Hand ITC',textShadow: '2px 2px 2px rgba(0, 0, 0, 0.2)'});
	
	//Cufon.replace('.continueButton a', { fontFamily: 'Bradley Hand ITC',hover:true});
//	Cufon.replace('.attButton a', { fontFamily: 'Bradley Hand ITC',hover:true});					
}



// Contenido completo
function showFullContent(){
	modeMenu = false;
	var $canvasWrapper = "#"+fullScreenCanvas();
	$("#wrapperContent").animate({left:"+=3800px"},{easing: 'easeInOutExpo', duration:1500,complete:function(){
																									$(this).toggle();
																									getFullContent($canvasWrapper);
																										
																										var resizeTimer = null;
																										$(window).bind('resize', function() {
																											//firebugConsole("log",modeMenu);
																											if(!modeMenu){
																												if (resizeTimer) clearTimeout(resizeTimer);
																												resizeTimer = setTimeout(onResizeNDScreen, 500);
																											}
																										});
																									}
	});
	//firebugConsole("log",modeMenu);

}



// Obtiene el contenido completo
function getFullContent(c){
	
	$(c).append('<div id="fullContent"></div>');
	$(c+" #fullContent").html(preloader2).load(theHref,function(){fullContentActions(c)});
	$("#ccc").css({"z-index":1000});

	
}



// Acciones dentro del contenido completo
function fullContentActions(c){
	
	$c = $(c+" #fullContent #c1 h1").parent(); //WTF, pero fue la única forma para ie7 grrr!!
	$c.css({"z-index":1003});
	$c.offset({top:150,left:($(window).width()/2)-($c.width()/2)});
	if($(c+" #fullContent .content div.more").length>0){
			doInContent(true);
		}
		else{
			doInContent(false);
		}
	
	// cerrar
	var secMainColor = (subMenuContButton)?subMenuContButton:$(".current_page_item_two a").attr("rel");
	$c.prepend('<div id="closeButton"><a href="#">&laquo; Regresar</a></div>');
	$("#closeButton").css({
						"background-color":$.xcolor.combine(secMainColor,'#000'),
						"border":"solid 1px "+$.xcolor.opacity(secMainColor,"#000",0.2)
						});
	$("#closeButton").corner(10);						
						
	$("#closeButton a").click(function(){closeFullContent($c)});
	
	if($(".attButton").length>0){
			var selectedSectionID = thisID.substring(3,thisID.length);
			$c.prepend('<div id="attButton"><a href="#">Solicitar Informaci&oacute;n &raquo;</a></div>');
			$("#attButton").css({
						"background-color":$.xcolor.combine(secMainColor,'#000'),
						"border":"solid 1px "+$.xcolor.opacity(secMainColor,"#000",0.2)
						});
			$("#attButton a").click(function(){goToContact($("#gop"+selectedSectionID).html())});
			$("#attButton").corner(10);	
	}
	
	/*Cufon.replace('#closeButton a', { fontFamily: 'Bradley Hand ITC',hover:true});
	Cufon.replace('#attButton a', { fontFamily: 'Bradley Hand ITC',hover:true});*/
	$('.full').css({height:($(window).height()-$(".full").offset().top)-10+"px"});
	$('.full').jScrollPane({showArrows: true});
	//$(".jspHorizontalBar, .jspVerticalBar ").corner(10)
	
	
	
}


function closeFullContent($c){
	
	$c.animate({opacity:0},{easing: 'easeInOutSine', duration:500,complete:function(){$("#ccc").css({"z-index":-1});$("#wrapperContent").toggle()}});
	
	
	$("#wrapperContent").animate({left:"-=3800px"},{easing: 'easeInOutExpo', duration:1500,complete:function(){$("#ccc").remove()}});
	modeMenu = true;
	return false;
}

function closeFullContentFM($c){
	$c.animate({opacity:0},{easing: 'easeInOutSine', duration:500,complete:function(){$("#ccc").css({"z-index":-1});modeMenu = true;}});
	
	$("#wrapperContent").toggle();
	return false;
}

// Solicitar información
function goToContact(sectionStr){
	var $contacto = $(contactoRef);
	currentInteres = sectionStr.replace(/ /gi,"_");
	$contacto.click();
	$contacto.hover();
}



// dibuja canvas para contenido completo
function fullScreenCanvas(){
	
	var fCanvasWidth = $(window).width();
	var fCanvasHeight = $(window).height()-$("header").height();
	var fCanvasTop = $("header").height();
	var fCanvasLeft = 0;
	var canvasCConatiner = "ccc";
	
	$("body").append('<div id="ccc" style="position:absolute;top:'+fCanvasTop+'px;left:'+fCanvasLeft+';color:#fff;width:'+fCanvasWidth+'px;height:'+fCanvasHeight+'px;z-index:-1"></div>');
	
	var canvasElem = document.createElement('canvas'); 
    document.getElementById(canvasCConatiner).appendChild(canvasElem); 
     //if it is IE 
	 if (typeof G_vmlCanvasManager != 'undefined') {
		 canvasElem = G_vmlCanvasManager.initElement(canvasElem); 
    } 

    canvasElem.setAttribute("width", fCanvasWidth); 
	canvasElem.setAttribute("height", fCanvasHeight); 
	canvasElem.setAttribute("id",canvasCConatiner);
	
    ctx = canvasElem.getContext("2d"); 
	gradStop1 = (subMenuContButton)?subMenuContButton:$(".current_page_item_two a").attr("rel");
	gradStop2 = $.xcolor.lighten(gradStop1);
  	grad = ctx.createLinearGradient(0,100,0,700); 
  	grad.addColorStop(0, gradStop1);
  	grad.addColorStop(0.2, gradStop2);
	ctx.fillStyle = grad;
	ctx.fillRect(0,0,fCanvasWidth,fCanvasHeight); 
	ctx.save();
	return canvasCConatiner;
	
	/*	var fCanvasWidth = $(window).width();
	var fCanvasHeight = $(window).height()-$("header").height();
	var fCanvasTop = $("header").height();
	var fCanvasLeft = 0;
	var canvasCConatiner = "ccc";
		$("body").append('<div id="ccc" style="position:absolute;top:'+fCanvasTop+'px;left:'+fCanvasLeft+';color:#fff;width:'+fCanvasWidth+'px;height:'+fCanvasHeight+'px;z-index:-1"></div>');

	return "ccc";*/
	
}


// -------------------------------------- STD FUNCTIONS ---------------------------------- \\
// Canvas +  Gradient
function doContainer(canvasID,gradStop1,gradStop2,gradStop3){
	
	var ctx = document.getElementById(canvasID).getContext("2d");
  	grad = ctx.createRadialGradient(200,100,450,800,250,250); 
  	grad.addColorStop(0, gradStop1);
  	grad.addColorStop(0.2, gradStop2);
	grad.addColorStop(1, gradStop3);
	roundRectGradient(ctx, 0, 0,696, 393,30,grad);

}//


// Dibujar canvas de sección
function roundRectGradient(ctx, x, y, width, height, radius,gradient) {

  	ctx.beginPath();
  	ctx.moveTo(x + radius, y);
  	ctx.lineTo(x + width - radius, y);
  	ctx.quadraticCurveTo(x + width, y, x + width, y + radius);
  	ctx.lineTo(x + width, y + height - radius);
  	ctx.quadraticCurveTo(x + width, y + height, x + width - radius, y + height);
  	ctx.lineTo(x + radius, y + height);
  	ctx.quadraticCurveTo(x, y + height, x, y + height - radius);
  	ctx.lineTo(x, y + radius);
  	ctx.quadraticCurveTo(x, y, x + radius, y);
  	ctx.closePath();
	ctx.fillStyle = gradient;
	ctx.fill();
       
}


// do shadow
function textShadow(target){
	
	var option = {
	  x:      1, 
	  y:      2, 
	  radius: 3,
	  color:  "#ccff00"
	}
	
	$(target).textShadow(option);
	
}



// debug literals
function parseOLiterals(o){
	var result;
	$.each(o,function(k,v){
		result += k+" : "+v+"\n";
	})
	alert(result);
}

 //doAjax('bla.htm','div',function(){alert("hello")})
// ajax
function doAjax(url,target,callback){
	$.ajax({
	  url: url,
	  success: function(data) {
		$(target).html(data);
		callback;
	  }
	});
}



// shuffle array
Array.prototype.shuffle = function() {
	var s = [];
	while (this.length) s.push(this.splice(Math.random() * this.length, 1));
	while (s.length) this.push(s.pop());
	return this;
} 



function firebugConsole(type,message){if(window.console&&window.console.firebug)console[type](message)}









