$(document).ready( function() 
{
	base_url 		= $("base").attr("href");
	site_url 		= $("#site_url").val();
	active_theme 	= $("#active_theme").val();
	site_lang 		= $("#site_language").val();
	
	
	// filtro > localizaciones
	$.ajax({
		type: "POST",
		url: site_url+"/inmuebles/filtro_localizacion",
		data: $("#filtroForm").serialize(),
		dataType: "html",
		beforeSend: function()
		{
			$("#filtro_localizacion").html("<img src=\"img/ajax icon (13).gif\" />");
		},
		success: function(msg)
		{
			$("#filtro_localizacion").html(msg);
		}
	});
	
	
	// caso especial en EMPRESA, que despliega la documentacion necesaria para para una hipoteca
	$("a.documentacion_hipoteca").click( function() {
		$("#documentacion_hipoteca_datos").toggle("slow");
		return false;
	});


	// inhabilitar boton derecho
	$(document).bind("contextmenu",function(e){
        return false;
    });
	
	
		
	// Flash
	var OSName="Unknown OS";
	if (navigator.appVersion.indexOf("Win")!=-1) OSName="Windows";
	if (navigator.appVersion.indexOf("Mac")!=-1) OSName="MacOS";
	if (navigator.appVersion.indexOf("X11")!=-1) OSName="UNIX";
	if (navigator.appVersion.indexOf("Linux")!=-1) OSName="Linux";
	
	// header imagenes
	if (OSName=="Windows" || OSName=="MacOS")
	{
	    $("#header_image").flash({
			src: "swf/header.swf",
			width: 932,
			height: 191,
			// title: "Secuencia de fotos",
			scale: "scale", 
			wmode: "transparent",
			flashvars: 
			{
				// lang: site_lang,
				raiz: base_url
			}
		},
		{
			version: 9 
		});
	}
	// header logo
	if (OSName=="Windows" || OSName=="MacOS")
	{
	    $("#header_logo").flash({
			src: "swf/logo.swf",
			width: 236,
			height: 85,
			// title: "Logo de Inmobiliaria Garpe",
			scale: "scale", 
			wmode: "transparent",
			flashvars: 
			{
				// lang: site_lang,
				raiz: site_url
			}
		},
		{
			version: 9 
		});
	}
	
	
	// banners
	if (OSName=="Windows" || OSName=="MacOS")
	{
	   $("#banner_tenemos_su_hipoteca").flash({
			src: "swf/tenemos_su_hipoteca.swf",
			width: 153,
			height: 194,
			title: $("#banner_tenemos_su_hipoteca a").attr("title"),
			scale: "scale", 
			wmode: "transparent",
			flashvars: 
			{
				lang: site_lang,
				link: $("#banner_tenemos_su_hipoteca a").attr("href")
			}
		},
		{
			version: 8 
		});
		
	   $("#banner_trabaja_con_nosotros").flash({
			src: "swf/trabaja_con_nosotros.swf",
			width: 153,
			height: 194,
			title: $("#banner_trabaja_con_nosotros a").attr("title"),
			scale: "scale", 
			wmode: "transparent",
			flashvars: 
			{
				lang: site_lang,
				link: $("#banner_trabaja_con_nosotros a").attr("href")
			}
		},
		{
			version: 8 
		});
	}
	
			
	$("a[href^='http://youtube.com'], a[href^='http://www.youtube.com']").flash(
		{ height: 370, width: 450 },
		{ version: 8 },
		function(htmlOptions) {
			$this = $(this);
			// htmlOptions.src = $this.attr('href');
			
			// Para que funcione con este tipo de URL http://www.youtube.com/watch?v=n-SaC6PVzsc
			// Para que funcione tiene que ser asi: http://www.youtube.com/v/n-SaC6PVzsc
			url = $this.attr('href');
			var vid;
			var results;
			results = url.match("[\\?&]v=([^&#]*)");
			vid = ( results === null ) ? url : "http://youtube.com/v/"+results[1];
			htmlOptions.src = vid;
			
			$this.before($.fn.flash.transform(htmlOptions));				
		}
	);
	
	/*
	$("a.localplayer").flash(
		{ src: "<?php echo base_url() ?>swf/player.swf", height: 400, width: 500 },
		{ version: 8 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.file = "<?php echo base_url() ?>"+$this.attr("href");
			$this.before($.fn.flash.transform(htmlOptions));						
		}
	);*/

	
	
	// $("#catalog_tabs").tabs();

/*
	$('#catalog_tabs').bind('tabsselect', function(event, ui) {
		// Objects available in the function context:
		ui.tab     // anchor element of the selected (clicked) tab
		ui.panel   // element, that contains the selected/clicked tab contents
		ui.index   // zero-based index of the selected (clicked) tab

	});*/	




	// enviar formularios de contacto (Ficha del inmueble y Contacto)
	$("#form_contact input[name='submit']").click( function()
	{
		$("#form_contact img.processing").show();
		
		// *** TODO > validar los campos aqui antes de hacer la llamada AJAX
		
		$.ajax({
			type: "POST",
			url: site_url+"/contacta",
			data: $("#form_contact").serialize(),
			dataType: "json",
			error: function(result)
			{
				$("#form_contact_result").html("<p class=\"error\">GURU MEDITATION, ERROR #FF9900</p>");
				$("#form_contact img.processing").hide();
			},
			success: function(result)
			{
				if (result.status==true)
				{
					msg = result.msg;
					
					// limpiar campos
					$("#form_contact input[name='name']").val("");
					$("#form_contact input[name='email']").val("");
					$("#form_contact input[name='tel']").val("");
					$("#form_contact textarea[name='message']").val("");
					$("#form_contact input[name='legal']").val("");
					$("#form_contact input[name='cc']").val("");
					
				//	$("#comments").html(msg);
				}
				else if (result.status==false)
				{
					msg = result.msg;
				}
				else
				{
					msg = "<p class=\"error\">GURU MEDITATION, ERROR #FF9900</p>";
				}
				$("#form_contact_result").html(msg);
				$("#form_contact img.processing").hide();
			}
		});
	});	
});
