var nin = 
{
	lat : null,
	lng : null,
	
	init : function()
	{
		// BUSCADOR --------------------------------------------------------------------
		// Substituye los campos de selección por enlaces y DIV flotantes.
		// Cuando se seleccina un elemento a la vez se selecciona en el SELECT oculto
		// -----------------------------------------------------------------------------
		$("#sname").focus(function()
		{
			if(this.value == "Nombre") this.value = "";
		});
		$("#sname").blur(function()
		{
			if(this.value == "") this.value = "Nombre";
		});
		
		$("#search SELECT").each(function()
		{
			var select = $(this);
				select.css("display", "none");
			
			var layer = '<div class="search_layer">';
				layer += '<ul>';
				
			var ops = select[0].options;
			for(var i=0; i<ops.length; i++)
			{
				layer += '<li><a href="javascript:void(0)" class="'+select[0].name+'_'+i+'">';
				if(i > 0) layer += '<b>';
				layer += ops[i].text;
				if(i > 0) layer += '</b>';
				layer += '</a></li>';
			}			
				layer += '</ul>';
				layer += '</div>';
			
			layer = $( layer ).insertBefore( "#search_bot" );
			var a = $( '<a href="javascript:void(0)" class="search_option">' + select[0].options[0].text + '</a>' ).insertAfter( layer );			
				a.click(function()
				{
					$(this).prev().show();
				})
		});		
		
		$("DIV.search_layer A").each(function()
		{
			var select = this.className.split("_")[0];
				select = $("#"+select)[0];
				select.selectedIndex = 0;
				
			$(this).click(function()
			{				
				select.selectedIndex = this.className.split("_")[1];
				var p = $(this).parent().parent().parent();
					p.hide();
					p.next().text($(this).text());			
			});
		});
		
		$("DIV.search_layer").mouseleave(function()
		{
			$(this).hide();
		});
		
		
		// PROYECTOS --------------------------------------------------------------------
		// Texto expandible
		// -----------------------------------------------------------------------------
		$("DIV.proy_info A").click(function()
		{
			var div = $(this).next();
			if(div.css("display") == "none")
				div.slideDown(200);
			else
				div.slideUp(200);
		});
		
		
		// PROYECTOS --------------------------------------------------------------------
		// Galería de imágenes
		// ------------------------------------------------------------------------------
		
		var gal_imgs = $("#proy_gallery_thumbs IMG").length,
			gal_current = 0;
			
		$("#proy_gallery_left").click(function()
		{
			if(gal_current > 0)
			{
				gal_current--;
				$('#proy_gallery_thumbs').animate({ scrollLeft: 106*gal_current}, 300);
			}
		});
		$("#proy_gallery_right").click(function()
		{
			if(gal_current < gal_imgs-3)
			{
				gal_current++;
				$('#proy_gallery_thumbs').animate({ scrollLeft: 106*gal_current}, 300);
			}
		});
		
		$("#proy_gallery_thumbs A").click(function()
		{
			$("IMG#proy_gallery_preview")[0].src = this.href;
			$("A#proy_gallery_lupa")[0].href = this.target;
			return false;
		});
		
		
		// PROYECTOS --------------------------------------------------------------------
		// Google Map
		// ------------------------------------------------------------------------------
		if(this.lat != null && this.lng != null && document.getElementById("proy_gmap") && GBrowserIsCompatible())
		{	
			var point = new GLatLng(this.lat, this.lng);
			//var icon = new GIcon(G_DEFAULT_ICON);
			//    icon.image = "http://gmaps-samples.googlecode.com/svn/trunk/markers/blue/blank.png";
			var map = new GMap2(document.getElementById("proy_gmap"));
				map.setCenter(point, 17);
				map.addOverlay(new GMarker(point)); //, { icon:icon }));
				map.setUIToDefault();
		}
	},
	
	check : function()
	{
		var ok = true;
		$("DIV.contacto FORM INPUT[name], DIV.contacto FORM TEXTAREA").each(function(i)
		{
			var this_ok = (this.value != "");
			
			if(this.name == "uemail")
				var this_ok = /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(this.value);
			
			$(this)[(!this_ok) ? 'addClass' : 'removeClass']("error");				
			if(!this_ok) ok = false;
		});
		
		return ok;
	}
}

//-------------------------------------------------------------------------------------

$(document).ready(function () {
    nin.init();
});