$(document).ready( function() 
{
	base_url = $("base").attr("href");
	site_url = $("input[name='site_url']").val();
	site_lang = $("meta[name='lang']").attr("content");
		
	// $('.gallery a').lightBox(); // Select all links in object with gallery class
	// $("a.downloadCatalog").fancybox({ "hideOnContentClick": false, "frameWidth": 400 }); 
    // $('#gallery a').lightBox(); // Select all links in object with gallery ID
	
	
	$("a[rel^='prettyPhoto']").prettyPhoto();
	

	// Download form *INICIO*
	$(".catalogos a.download").click(function(){			
			$("#downloadWindow").dialog("open");

			title = $(this).html();
			$("#ui-dialog-title-downloadWindow").html(title);

			download_url = $(this).attr("href");
			// $("#downloadWindow input[name='download_url']").val($(this).attr("href"));
			$("#downloadWindow input[name='motivo']").val(title);

			return false;
		});
	$("#downloadWindow").dialog({
		autoOpen: false,
		width: 500,
		buttons: {
			/*"Ok": function() { 
				$(this).dialog("close"); 
			},*/ 
			" X ": function() { 
				$(this).dialog("close"); 
			} 
		}
	});

	
	$("#downloadForm input[type='submit']").click( function()
	{
		if (form_validation())
		{
			$("#downloadForm img.loading").css("display", "inline");
			$.ajax({
				type: "POST",
				url: site_url+"/contact",
				data: $("#downloadForm").serialize(),
				dataType: "text", // "json",
				complete: function(result, textStatus)
				{
					/* solo funciona si usamos json
					if (textStatus=="parsererror")
					{
						alert("ERROR GRAVE, GURU MEDITATION");
					}*/
				},
				success: function(result, textStatus)
				{
					// if (result.status==true)
					if (result.substr(0, 6)=="TRUE  ")
					{
						window.location = base_url+download_url;
						$("#downloadWindow").dialog("close");
					}
					// else if (result.status==false)
					else if (result.substr(0, 6)=="FALSE ")
					{
						msg = result.substr(6);
						// msg = result.msg;
						alert(msg);
					}
					else
					{
						alert("ERROR GRAVE, GURU MEDITATION");
					}
					$("#downloadForm img.loading").css("display", "none");
				}
			});
		}
		return false;
	});
	// Download form *FIN*	
	
	$("#contactForm input[type='submit']").click( function()
	{
		if (form_validation())
		{
			$("#contactForm img.loading").css("display", "inline");
			$.ajax({
				type: "POST",
				url: site_url+"/contact",
				data: $("#contactForm").serialize(),
				dataType: "text", // "json",
				complete: function(result, textStatus)
				{
					/* solo funciona si usamos json
					if (textStatus=="parsererror")
					{
						alert("ERROR GRAVE, GURU MEDITATION");
					}*/
				},
				success: function(result, textStatus)
				{
					// if (result.status==true)
					if (result.substr(0, 6)=="TRUE  ")
					{
						msg = result.substr(6);
						// msg = result.msg;
						
						$("input[name='name']").val("");
						$("input[name='email']").val("");
						$("input[name='empresa']").val("");
						$("textarea[name='comments']").val("");
					}
					// else if (result.status==false)
					else if (result.substr(0, 6)=="FALSE ")
					{
						msg = result.substr(6);
						// msg = result.msg;
					}
					else
					{
						msg = "ERROR GRAVE, GURU MEDITATION";
					}
					$("#contactForm img.loading").css("display", "none");
					alert(msg);
				}
			});
		}
		return false;
	});
	
	
	
	
			
	$("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: "swf/player.swf", height: 400, width: 500 },
		{ version: 8 },
		function(htmlOptions) {
			$this = $(this);
			htmlOptions.flashvars.file = $this.attr("href");
			$this.before($.fn.flash.transform(htmlOptions));						
		}
	);
	
	
	$('#innerfade').innerfade({ speed:"slow", timeout:4000, type:"random", containerheight:"424px", animationtype:"fade" }); 
	
	
	// 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";
	

	// logotipo membrete
	if (OSName=="Windows" || OSName=="MacOS")
	{
	    $('#header_logo').flash(
			{
				src: "swf/logo.swf",
		        width: 400,
		        height: 120,
				title: "Logo Tubacero",
				scale: "scale", 
				wmode: "transparent",
		        flashvars: 
				{
					raiz: site_url
				}
		    },
			{
				version: 9 
			}
		);
	}
	
	// logotipo membrete
	if (OSName=="Windows" || OSName=="MacOS")
	{
	    $('#home_intro').flash(
			{
				src: "resources/home/intro.swf",
		        width: 663,
		        height: 257,
				title: "Intro Tubacero",
				scale: "scale", 
				wmode: "transparent",
		        flashvars: 
				{
					lang: site_lang
				}
		    },
			{
				version: 9 
			}
		);
	}
});



// validar formularios de contacto
function form_validation()
{
	name        = $("input[name='name']").val();
	email       = $("input[name='email']").val();
	empresa     = $("input[name='empresa']").val();
	comments	= $("textarea[name='comments']").val();
	
	name_title 		= $("input[name='name']").attr("title");
	email_title 	= $("input[name='email']").attr("title");
	empresa_title 	= $("input[name='empresa']").attr("title");
	comments_title 	= $("textarea[name='comments']").attr("title");
	
	if (name=="" && name_title!=="")
	{
		alert(name_title);
		$("input[name='name']").select();
	}
	else if (email=="" && email_title!=="")
	{
		alert(email_title);
		$("input[name='email']").select();
	}
	else if (empresa=="" && empresa_title!=="")
	{
		alert(empresa_title);
		$("input[name='empresa']").select();
	}
	else if (comments=="" && comments_title!=="")
	{
		alert(comments_title);
		$("textarea[name='comments']").select();
	}
	else
	{
		return true;
	}
	return false;
};