/*//////////////////////////////////////////////////////////////////////////////////////////
	Global Variables
//////////////////////////////////////////////////////////////////////////////////////////*/


	var pruebas = false;
	var debug = false;



/*//////////////////////////////////////////////////////////////////////////////////////////

	Functions
	
//////////////////////////////////////////////////////////////////////////////////////////*/
	
	
	$(document).ready(function(){
		readyHandler()
	});
	
	
	function readyHandler()
	{

		initMenu();
		setColumnHeights();
		
		if(debug) $('#trace-wrapper').css('display', 'block');
		
		try{
			customPageReadyHandler();
		}catch(err){
		}
		
	}

	function getUrlHash()
	{
		return $(location).attr('href').split('#')[1];
	}

	function setColumnHeights()
	{
		var contentHeight = $('#content .box-wrapper').height();
		var sidebarHeight = $('#sidebar').height();
		var columnsHeight = Math.max(contentHeight, sidebarHeight);
		
		if(contentHeight >= sidebarHeight){
			
			//alert("Aumentar Sidebar");
			//$('#sidebar').css('height', contentHeight+15);
		}else{
			
			//alert("Aumentar Content");
			$('#content .box-wrapper').css('height', sidebarHeight-5);
		}
		
		//alert(contentHeight+" - "+sidebarHeight+" - "+columnsHeight);
		
	}

	function scrollPageTo(id){
		//alert("scrollPageTo "+id);
		
		$target = $(id);
		var targetOffset = $target.offset().top;
		$('html,body').animate(
			{
				scrollTop: targetOffset
			},
			700,
			"easeOutQuint",
			function(){
				//Animation complete
				location.href = id;	
			}
		);
		
	}

	var traceCurrentLine = 1;
	function trace(content){

		$('#trace').append("<p>"+traceCurrentLine+"|"+content+"</p>");
		$("#trace-wrapper").scrollTop($("#trace")[0].scrollHeight);
		traceCurrentLine++;
		
	}


/*//////////////////////////////////////////////////////////////////////////////////////////

	Noticias
	
//////////////////////////////////////////////////////////////////////////////////////////*/


	function newsReadyHandler()
	{
		//alert("newsReadyHandler");
		
		/*
		$('.news-item h2').click(function(){
			
			var newsItem = $(this).parent();
			if(newsItem.hasClass("news-closed"))
			{
				// Actions for the items closed
				
				$('.news-item').addClass('news-closed');
				//scrollPageTo("#"+newsItem.attr("id"));
				newsItem.removeClass('news-closed');
				
			}else{
				// Actions for the items opened
				
				$('.news-item').addClass('news-closed');
				//scrollPageTo("#"+newsItem.attr("id"));
			}
			
		});
		
		// Go to news item defined by url hash or, if undefined, to the first one
		if(getUrlHash()){
			$('#'+getUrlHash()+' h2').click();
		}else{
			$('.news-item:first-of-type h2').click();
		}
		*/
		/*
		
		$('#el-tipo').addClass('active-fixed');
		$('#el-tipo > a').addClass('active-fixed');
		$('#el-tipo-what').addClass('active-fixed');
		$('#el-tipo-categories').addClass('menu-fixed');
		*/
		
		remakeNews();
	}
	
	function remakeNews()
	{
		

		// Media
		
		$('.news-open object').each(function(){
			
			$('#news-content').append($(this));
			
		});
		

		// Create Images Array & remove from Left Column
		
		var images = new Array();
		
		$('.news-open img').each(function(){
			images.push($(this));
			$(this).remove();
		});
		
		
		// When images are removed, show Left Column
		
		$('.news-open').css('display', 'block');
		
		
		
		// SI HAY GALERÍA...
		
		if(images.length > 0){
			
			for(var i=0; i<images.length; i++){
				
				var image = images[i];
				var totalImg = images.length;
				var ind = i+1;
				var nextInd = i+2;
				nextInd = (nextInd > totalImg) ? 1 : nextInd;
				
				
				// Create Gallery Counter
				
				var a = "";
				a += '<a href="#'+ind+'" id="gallery-counter-'+ind+'">';
				a += '</a>';
				
				$('#gallery-counter').append(a);
				
				
				
				// Fill Gallery

				var li = "";
				li += '<li id="gallery-img-'+ind+'">';
				li += '<a href="#'+nextInd+'">';
				li += '<img style="max-width:'+image.attr('width')+'px;" src="'+image.attr('src')+'" />';
				li += '</a>';
				li += '</li>';
				
				$('#gallery-images').append(li);
				
			}
			
			$('.news .gallery-top').css('display', 'block');
			$('#news-gallery').css('display', 'block');
			
			galleryReadyHandler();

		}

		
	}
	

/*//////////////////////////////////////////////////////////////////////////////////////////

	Gallery
	
//////////////////////////////////////////////////////////////////////////////////////////*/


	function galleryReadyHandler()
	{
		
		//alert("galleryReadyHandler");

		$('#gallery-counter a').click(function(){

			showImage($(this).attr('href').substring(1));
			return false;
			
		});
		
		$('#gallery-images a').click(function(){

			showImage($(this).attr("href").substring(1));
			return false;
			
		});
		
		// Go to news item defined by url hash or, if undefined, to the first one
		if(getUrlHash()){
			showImage(getUrlHash());
		}else{
			showImage(1);
		}
		
	}

	function showImage(id){
		
		$('#gallery-images li').removeClass('current');
		$('#gallery-img-'+id).addClass('current');
		
		$('#gallery-counter a').removeClass('current');
		$('#gallery-counter-'+id).addClass('current');
		
		location.href = "#"+id;
		
	}




/*//////////////////////////////////////////////////////////////////////////////////////////

	Projects
	
//////////////////////////////////////////////////////////////////////////////////////////*/


	function indexReadyHandler()
	{
		switch(node){
			case 1:
				$('#el-tipo').addClass('active-fixed');
				$('#el-tipo > a').addClass('active-fixed');
				$('#el-tipo-what').addClass('active-fixed');
				$('#el-tipo-categories').addClass('menu-fixed');
				break;
			
			case 6:
				$('#el-tipo').addClass('active-fixed');
				$('#el-tipo > a').addClass('active-fixed');
				$('#el-tipo-suscripcion').addClass('active-fixed');
				$('#el-tipo-categories').addClass('menu-fixed');
				break;
			
			case 9:
				$('#acerca-de').addClass('active-fixed');
				$('#acerca-de > a').addClass('active-fixed');
				break;
			
			case 7:
				$('#contacto').addClass('active-fixed');
				$('#contacto > a').addClass('active-fixed');
				break;
			
		}
		
		
		
	}



/*//////////////////////////////////////////////////////////////////////////////////////////

	Index
	
//////////////////////////////////////////////////////////////////////////////////////////*/


	function projectsReadyHandler()
	{
		//trace("projectsReadyHandler");
		$('#projects').addClass('active-fixed');
		$('#projects > a').addClass('active-fixed');
		
		$('#projects-categories').addClass('menu-fixed');
		
		
		//$('#projects-categories').css('display', 'block !important');
		
		//$('#projects > ul ').showMenuAhoyeds($('#projects > ul '));



	}



/*//////////////////////////////////////////////////////////////////////////////////////////

	Comprar
	
//////////////////////////////////////////////////////////////////////////////////////////*/


	function comprarReadyHandler()
	{
		$('#el-tipo').addClass('active-fixed');
		$('#el-tipo > a').addClass('active-fixed');
		$('#el-tipo-comprar').addClass('active-fixed');
		
		$('#el-tipo-categories').addClass('menu-fixed');
	}





/*//////////////////////////////////////////////////////////////////////////////////////////

	Menu
	
//////////////////////////////////////////////////////////////////////////////////////////*/

	
	
	function initMenu(){
	
	
		// COMMON
		
		$('#menu > ul > li').mouseenter(function(){
			$(this).removeClass('inactive');
			$(this).addClass('active');
		});
		
		$('#menu > ul > li').mouseleave(function(){
			$(this).removeClass('active');
			if(!$(this).hasClass('active-fixed')){
				$(this).addClass('inactive');
				showCurrentProjectsList();
			}
		});
		
		
		// PROJECTS
		
		$('#menu #projects > ul > li > a').mouseenter(function(){
			var categoryId = $(this).parent().attr("id");
			showProjectsList(categoryId);
		});
		
		$('#menu #projects > ul').mouseleave(function(){
			showCurrentProjectsList();
		});
		
		$('#menu #projects > ul > li.active-fixed > a').each(function(){
			var categoryId = $(this).parent().attr("id");
			showProjectsList(categoryId);
		});
		
		
		// EL TIPO
		
		$('#menu #el-tipo').mouseover(function(){
			hideProjectsList();
		});
										
	}

	function resetMenuToDefault(){
		//trace("resetMenuToDefault");
		$('.active-fixed').removeClass('inactive');
	}

	function hideProjectsList(){
		$('li#projects > ul > li').addClass('inactive');
	}
	
	function showProjectsList(id){
		
		// Hide all the menus else than projects
		//$('#menu > ul > li[id!="projects"] > ul').css('display', 'none');
		$('#menu > ul > li[id!="projects"]').addClass('inactive');
		
		// Hide all the projects List
		////$('#menu #projects > ul > li.active-fixed > ul').css('display', 'block'); // Show current projects List
		//$('#menu #projects > ul > li.active-fixed[id!="'+id+'"] > ul').css('display', 'none'); // Hide the current project List
		$('#menu #projects > ul > li.active-fixed[id!="'+id+'"]').addClass('inactive'); // Hide the current project List
		$('#menu #projects > ul > li').removeClass('active'); // remove class 'active' to all the project Lists
		
		// Show the requested projects List
		$('#'+id).removeClass('inactive');
		$('#'+id).addClass('active');
	}
	
	function showCurrentProjectsList(){
		
		
		$('#menu #projects > ul > li').removeClass('active'); // Remove class 'active' to all the project Lists
		//$('#menu #projects > ul > li.active-fixed > ul').css('display', 'block'); // Show the current projects List
		$('#menu #projects > ul > li.active-fixed').removeClass('inactive'); // Show the current projects List
		
		if(!$('#menu #projects').hasClass("active-fixed")){
			// if we're not in projects --> show all the submenus
			//$('#menu > ul > li > ul').css('display', 'block');
			$('#menu > ul > li').removeClass('inactive');
		}
		
	}

