function loadswf(file, id, width, height, bgcolor, flashvars)
{
	var so = new SWFObject(file, id + "_swf", width, height, 9, bgcolor);
	so.addParam("wmode", "transparent"); 
	so.addParam("flashVars", flashvars);
	so.addParam("allowFullScreen", "true");
	so.write(id);
}

function marcas(item) {
	$.ajax({
		url: "includes/carousel.inc.php",
		async: false,
		success: function (html) {
			$("div.carousel").html(html);
			loadswf("galeria/carousel.swf?cache=" + Math.random(), "swfcarousel", 1634, 600, "#FFFFFF", "&screenWidth=" + screenWidth + "&xmlfile=galeria/xml/" + item + ".xml");

			$("div#lightbox").css("display", "block");
			$("div.carousel").css("display", "block");
		}
	})
}

function lightbox_close()
{
	$("div#lightbox").css("display", "none");
	$("div.lightbox").css("display", "none");
	$("div.carousel").css("display", "none");
}

function tienda()
{
	$.ajax({
		url: "includes/tienda.inc.php",
		async: false,
		success: function (html) {
			$("div.lightbox").html(html);

			$("div#lightbox").css("display", "block");
			$("div.lightbox").css("display", "block");

			$.ajax({
				url: "includes/mapas.inc.php",
				data: "&region=" + 15,
				success: function (html) {
					$("div.mapa").html(html);

					$("img.btn_close").click(function () {
						lightbox_close();
					});
				}
			});


			$("select#ubicacion").change(function () {
				$.ajax({
					url: "includes/mapas.inc.php",
					async: false,
					data: "&region=" + $("select#ubicacion").val(),
					success: function (html) {
						$("div.mapa").html(html);
					}
				});
			});
			
			$("img.btn_close").click(function () {
				lightbox_close();
			});
			
		}
	})
}

function comercial()
{
	$.ajax({
		url: "includes/comercial.inc.php",
		async: false,
		success: function (html) {
			$("div.lightbox").html(html);

			$("div#lightbox").css("display", "block");
			$("div.lightbox").css("display", "block");

			loadswf("swf/player.swf", "swfcomercial", 450, 387, "#FFFFFF", "&file=comercial.flv&autoplay=true&preview=images/450x387_foto.jpg");

			$("img.btn_close").click(function () {
				lightbox_close();
			});
		}
	})
}

function isEmail(theElement)
{
	var s = theElement;
	var filter=/^[A-Za-z][A-Za-z0-9_\.]*@[A-Za-z0-9_]+\.[A-Za-z0-9_.]+[A-za-z]$/;
	if (s.length == 0 ) return false;
	if (filter.test(s)) return true;

	return false;
}

function isTelefono(elem)
{
	var s = elem;
	var filter=/^[0-9\s-]*$/;
	if (s.length > 10) return false;
	if (s.length == 0 ) return false;
	if (filter.test(s)) return true;

	return false;
}

function isNombre(elem)
{
	var s = elem;
	var filter=/^[A-Za-z\sáéíóúñÁÉÍÓÚÑ]*$/;
	if (s.length == 0 ) return false;
	if (filter.test(s)) return true;

	return false;
}

function dv(T)
{
	if (T.length == 0) return 'N';
	var M=0,S=1;for(;T;T=Math.floor(T/10))S=(S+T%10*(9-M++%6))%11;return S?S-1:'K';
}

function validar() {
	if (!isNombre($("input#nombre").val()))
	{
		alert("Debes ingresar correctamente tu nombre.");
		return false;
	}

	rut = $("input#rut").val();
	rut = rut.replace("-", "");
	rut = rut.replace(".", "");
	if (rut.substr(rut.length - 1, 1) != dv(rut.substr(0, rut.length - 1)))
	{
		alert("Debes ingresar correctamente tu rut.");
		return false;
	}

	if (!isEmail($("input#email").val()))
	{
		alert("Debes ingresar correctamente tu email.");
		return false;
	}
	
	if (!isTelefono($("input#telefono").val()))
	{
		alert("Debes ingresar correctamente tu telefono.");
		return false;
	}

	nacimiento = $("select#dia").val() + "/" + $("select#mes").val() + "/" + $("select#anio").val();
	$.ajax({
		url: "http://www.lapolar.cl/2009/formulario_externo",
		data: "&nombre=" + $("input#nombre").val() + "&rut=" + $("input#rut").val() + "&email=" + $("input#email").val() + "&telefono=" + $("input#telefono").val() + "&sexo=" + $("select#sexo").val() + "&historia=" + $("textarea").val() + "&nacimiento=" + nacimiento,
		type: "POST",
		success: function (html) {
			if (html == "ok")
				alert("Tus datos se han registrado correctamente");
			else
				alert(html);
			lightbox_close();
		}
	});
}

function eventos()
{
	$.ajax({
		url: "includes/evento.inc.php",
		async: false,
		success: function (html) {
			$("div.lightbox").html(html);

			$("div#lightbox").css("display", "block");
			$("div.lightbox").css("display", "block");

			$("img.btn_close").click(function () {
				lightbox_close();
			});
		}
	})
	
}

function juego()
{
	$.ajax({
		url: "includes/juego.inc.php",
		async: false,
		success: function (html) {
			$("div.carousel").html(html);

			$("div#lightbox").css("display", "block");
			$("div.carousel").css("display", "block");

			$("img.btn_close").click(function () {
				lightbox_close();
			});

			$("div#next").click(function () {
				$.ajax({
					url: "includes/juego2.inc.php",
					data: "&historia=" + $("textarea#historia").val(),
					type: "GET",
					success: function (html) {
						$("div.carousel").html(html);
					   
						$("div.submit").click(function () {
							validar();
						});

						$("img.btn_close").click(function () {
							lightbox_close();
						});
						
				   }
				});
			});
		}
	});
}

function galeria()
{
}

function catalogo()
{
	//http://www.kzines.com/chile/clientes/lapolar/zapatillas_urbanas_120309/FlipK.html
}

function registro()
{
}

function getViewportDimensions() {
    var intH = 0, intW = 0;
    
    if(self.innerHeight) {
       intH = window.innerHeight;
       intW = window.innerWidth;
    } 
    else {
        if(document.documentElement && document.documentElement.clientHeight) {
            intH = document.documentElement.clientHeight;
            intW = document.documentElement.clientWidth;
        }
        else {
            if(document.body) {
                intH = document.body.clientHeight;
                intW = document.body.clientWidth;
            }
        }
    }

    return {
        height: parseInt(intH, 10),
        width: parseInt(intW, 10)
    };
}


$(document).ready(function () 
{
    var viewport = getViewportDimensions();
	elem = $("div#swfhome")[0];
    screenWidth = viewport.width; //(viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
    screenHeight = viewport.height; //(viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);

	$("div#swfhome").css("width", screenWidth + "px"); 
	$("div#swfhome").css("left", 0 + "px"); 
	$("div#swfhome").css("top", (screenHeight - 600) / 2 + "px");
	
	$("div#lightbox").click(function () {
		lightbox_close();
	});

	$("div#lightbox").css("height", screenHeight + "px"); 
	$("div#lightbox").css("width", screenWidth + "px"); 

	$("div.carousel").css("width", screenWidth + "px"); 
	$("div.carousel").css("top", (screenHeight - 600) / 2 + "px"); 

	loadswf("swf/site.swf?cache=" + Math.random(), "swfhome", 1634, 600, "#FFFFFF", "&screenWidth=" + screenWidth);
	
	$(window).resize(function () {
		var viewport = getViewportDimensions();
		elem = $("div#swfhome")[0];
		screenWidth = viewport.width; //(viewport.width == 0) ? 50 : parseInt((viewport.width - elem.offsetWidth) / 2, 10);
		screenHeight = viewport.height; //(viewport.height == 0) ? 50 : parseInt((viewport.height - elem.offsetHeight) / 2, 10);
	
		$("div#swfhome").css("width", screenWidth + "px"); 
		$("div#swfhome").css("left", 0 + "px"); 
		$("div#swfhome").css("top", (screenHeight - 600) / 2 + "px");
		
		document.getElementById("swfhome_swf").resize(screenWidth);
	});
	
});
