if ($.browser.msie && $.browser.version < 7)
{
    $(document).ready(initIE6);
    window.onload = function () {
        changeHeightIE6();
    };
}
else
{
    $(document).ready(init);
    window.onload = function () {
        changeHeight();
    };
}

var formActive = false;

function init()
{
	$("#header div.pasador img:first").fadeIn();
	if ($("#header div.pasador img").size() > 1) {
		var PAS_IMG = $("#header div.pasador img:first");
		setInterval(function () {
			PAS_IMG = PAS_IMG.next();
			if (!PAS_IMG.is("img")) {
				PAS_IMG = $("#header div.pasador img:first")
			}
			$("#header div.pasador img").fadeOut();
			PAS_IMG.fadeIn();
		},5000);
	}
	// FORMULARIO DE CONTACTO
	contacto();

	var hover = false;
	// LOGIN
	$("a.login").click(function () {
		if ($("a.login").hasClass("active"))
			$("a.login").removeClass("active");
		else $("a.login").addClass("active");
		$("#login").toggle();
	});

	// IMAGENES, PASAR LA PRIMERA DEL DETALLE ARRIBA
	$("div.content div.detalle div.buttons + div.info + div.imagenes a.alimpio:first-child img").parent().insertBefore("div.content div.detalle div.buttons + div.info").addClass("primeraImagen");
	if ($.trim($("div.content div.detalle div.buttons + img.primeraImagen + div.info + div.imagenes").html()) == '')
	{
		$("div.content div.detalle div.buttons + img.primeraImagen + div.info + div.imagenes").remove();
		$("div.content div.detalle div.buttons + img.primeraImagen + div.info").addClass("todo");
	}

	// ESQUINAS MENU VERDE
	$('#green_menu').append('<div class="corner_left"></div>');
	$('#green_menu .corner_left').css({"position":"absolute", "top":"0", "left":"0", "width":"8px", "height":"12px", "background":"url('/images/gr_left.png') no-repeat top left"});
	$('#green_menu').append('<div class="corner_right"></div>');
	$('#green_menu .corner_right').css({"position":"absolute", "top":"0", "right":"0", "width":"8px", "height":"12px", "background":"url('/images/gr_right.png') no-repeat top right"});

	// TABLERO DE CONTACTO
	$("#green_menu #contact").hide();
	$("#green_menu .contact:first").click(showContact);

	// MAPA
	$("#green_menu #contact .map").hide();
	$("#green_menu #contact .map .closemap").click(hideMap);
	$("#green_menu #contact .button_map").click(showMap);
	$("#green_menu #contact div.where input").keyup(function (e) {
		if (e.keyCode == 13)
		{
			showMap();
		}
	});

	// BORDE DEL MENU
	$('#left_container').prepend('<div style="width:100%;height:12px;background:url(/images/bg_menu_top.png) no-repeat top center; position: absolute; top: -12px; left: 0;"></div>');
	$('#left_container').prepend('<div style="width:100%;height:12px;background:url(/images/bg_menu_bottom.png) no-repeat bottom center; position: absolute; bottom: -12px; left: 0;"></div>');
	$('#left_container').css("margin-top","12px");

	//BORDES DEL CONTENIDO
	// abajo #body
	$('#body').append('<div style="width:729px; height: 10px; position: absolute; bottom: -10px; left: -2px; background: url(/images/body-content-bottom.png) no-repeat top left; z-index: 20"></div>');
	$('#body .content').append('<div style="width:725px; height: 13px; position: absolute; bottom: 0; left: 0; background: url(/images/content-bottom.png) no-repeat bottom left; z-index: 50"></div>');
	$('#body .content:last').append('<div style="width:725px; height: 13px; position: absolute; bottom: 0; left: 0; background: url(/images/content-bottom2.png) no-repeat bottom left; z-index: 50"></div>');
	// degradado de .content sin ser el primero
	$('#body .content:not(:first)').css("background","white url('/images/content-bg.png') repeat-x top left");
	$('#body .content:not(:first)').css("z-index","10");

	// BOTON ENLACE
	$(".content a.enlace")
		.css("position","relative")
		.append('<img class="left" src="/images/link_left.png" style="position:absolute;top:0;left:-9px;border: none;" />')
		.append('<img class="right" src="/images/link_right.png" style="position:absolute;top:0;right:-19px;border: none;" />');

	// IMAGENES INFERIORES
	$('#left_container').append('<div style="width:100%;height:50px;background:url(/images/left-bottom.png) no-repeat bottom center; position: absolute; bottom: -66px; left: 0; z-index: 5"></div>');
	$('#body').append('<div style="width:100%;height:50px;background:url(/images/right-bottom.png) no-repeat bottom center; position: absolute; bottom: -58px; left: 0; z-index: 5"></div>');

	$('#green_menu li.talk').click(talk);
}

function initIE6()
{
    // FORMULARIO DE CONTACTO
    contacto();

    $("div.content div.listado_submenu ul:first-child").addClass("first-child");
    $("div.content ul.menu li").mouseover(function () {
        $(this).addClass("hover");
    });
    $("div.content ul.menu li").mouseout(function () {
        $(this).removeClass("hover");
    });
    var hover = false;
    // LOGIN
    $("a.login").click(function () {
        if ($("a.login").hasClass("active"))
            $("a.login").removeClass("active");
        else $("a.login").addClass("active");
        $("#login").toggle();
    });

    // IMAGENES, PASAR LA PRIMERA DEL DETALLE ARRIBA
    $("div.content div.detalle div.buttons + div.info + div.imagenes img:first-child").insertBefore("div.content div.detalle div.buttons + div.info").addClass("primeraImagen");
    if ($.trim($("div.content div.detalle div.buttons + img.primeraImagen + div.info + div.imagenes").html()) == '')
    {
        $("div.content div.detalle div.buttons + img.primeraImagen + div.info + div.imagenes").remove();
        $("div.content div.detalle div.buttons + img.primeraImagen + div.info").addClass("todo");
    }

    // ESQUINAS MENU VERDE
    $('#green_menu').append('<div class="corner_left"></div>');
    $('#green_menu .corner_left').css({"position":"absolute", "top":"0", "left":"0", "width":"8px", "height":"12px", "background":"url('/images/gr_left.png') no-repeat top left"});
    $('#green_menu').append('<div class="corner_right"></div>');
    $('#green_menu .corner_right').css({"position":"absolute", "top":"0", "right":"0", "width":"8px", "height":"12px", "background":"url('/images/gr_right.png') no-repeat top right"});

    // TABLERO DE CONTACTO
    $("#green_menu #contact").hide();
    $("#green_menu .contact").click(showContact);

    // MAPA
    $("#green_menu #contact .map").hide();
    $("#green_menu #contact .map .closemap").click(hideMap);
    $("#green_menu #contact .button_map").click(showMap);
    $("#green_menu #contact div.where input").keyup(function (e) {
        if (e.keyCode == 13)
        {
            showMap();
        }
    });

    // BORDE DEL MENU
    $('#left_container').prepend('<div style="width:204px;height:300px;background:url(/images/bg_menu_topie6.png) no-repeat top center; position: absolute; top: 0; left: 0; z-index:9;"></div>');
    $('#left_container').prepend('<div style="width:204px;height:11px;background:url(/images/bg_menu_bottomie6.png) no-repeat bottom center; position: absolute; bottom: -1px; left: 0;"></div>');
    $('#left_container').css("padding","12px 0 12px 0");

    //BORDES DEL CONTENIDO
    // abajo #body
    $('#body').append('<div style="width:729px; height: 10px; position: absolute; bottom: -5px; left: -2px; background: url(/images/body-content-bottomie6.png) no-repeat top left; z-index: 20"></div>');
    $('#body .content').append('<div style="width:725px; height: 13px; position: absolute; bottom: -1px; left: 0; background: url(/images/content-bottom.png) no-repeat bottom left; z-index: 500"></div>');
    $('#body .content:last').append('<div style="width:725px; height: 13px; position: absolute; bottom: -1px; left: 0; background: url(/images/content-bottom2.png) no-repeat bottom left; z-index: 500"></div>');
    // degradado de .content sin ser el primero
    $('#body .content:not(:first)').css("background","white url('/images/content-bg.png') repeat-x top left");
    $('#body .content:not(:first)').css("z-index","10");

    // BOTON ENLACE
    $(".content a.enlace")
        .css("position","relative")
        .append('<img class="left" src="/images/link_left.png" style="position:absolute;top:0;left:-9px;border: none;" />')
        .append('<img class="right" src="/images/link_right.png" style="position:absolute;top:0;right:-19px;border: none;" />');

    // IMAGENES INFERIORES
    $('#left_container').append('<div style="overflow:visible;width:100%;height:50px;background:url(/images/left-bottomie6.png) no-repeat bottom center; position: absolute; bottom: -66px; left: 0; z-index: 5"></div>');
    $('#body').append('<div style="overflow:visible;width:100%;height:50px;background:url(/images/right-bottomie6.png) no-repeat bottom center; position: absolute; bottom: -58px; z-index: 5"></div>');

    $('#green_menu li.talk').click(talk);

    var waux = $("div.content div.detalle img.primeraImagen").width();
    if (waux != null)
    {
        if (waux >= 645)
        {
            $("div.content div.detalle img.primeraImagen").css({
                "width":"645px",
                "height":"auto"
            });
        }
    }
    $("div.content div.detalle img + div.info").css({
        "width":"470px",
        "float":"right"
    });

    $("div.content div.detalle .primeraImagen + div.info + div.imagenes").css({
        "float":"left",
        "width":"152px"
    });
    $("div.content div.detalle .primeraImagen + div.info + div.imagenes img").css({
        "height":"auto",
        "margin":"0 23px 23px 0",
        "width":"152px"
    });
}

// FUNCIONES BLOQUE CONTACTO
function showContact()
{
	$("#green_menu #contact").slideToggle("normal");
	$("#green_menu #contact .close").click(hideContact);
}

function hideContact()
{
	$("#green_menu #contact").slideToggle("slow");
	$("#green_menu #contact .map").hide(); // tambien cerramos el mapa
}

// FUNCIONES MAPA
function showMap()
{
    $("#green_menu #contact .map").fadeIn("fast");
    loadMap('calle uruguay 13, 30169, murcia');
}

function hideMap()
{
	$("#green_menu #contact .map").fadeOut("slow");
}

function changeHeight()
{
	h1 = $('#left_block').outerHeight() + 18; // le sumamos 18 para compensar los bordes
	h2 = $('#body').outerHeight();
	if (h1 > h2)
	{
		$(".content:last").height($('.content:last').height() + h1 - h2 );
	}
	else if (h2 > h1)
		$('#left_block').height(h2 - 48); // le restamos 48 para compensar los 18 del borde y 30 del padding de la izquierda

	var h = $('#body').outerHeight() - 30;
	$('#body').append('<div style="width:3px; height: '+h+'px; position: absolute; top: 3em; left: -3px; background: url(/images/content-bl.png) repeat-y top left; z-index: 15"></div>');
	$('#body').append('<div style="width:3px; height: '+h+'px; position: absolute; top: 3em; right: -3px; background: url(/images/content-br.png) repeat-y top left; z-index: 15"></div>');
}

function changeHeightIE6()
{
    h1 = $('#left_block').height() + 0; // le sumamos 18 para compensar los bordes
    h2 = $('#body').height();
    if (h1 > h2)
    {
        $(".content:last").height($('.content:last').height() + h1 - h2 );
    }
    else if (h2 > h1)
        $('#left_block').height(h2 + 33); // le restamos 48 para compensar los 18 del borde y 30 del padding de la izquierda

    var h = $('#body').height() - 30;
    $('#body').append('<div style="width:3px; height: '+h+'px; position: absolute; top: 3em; left: -3px; background: url(/images/content-bl.png) repeat-y top left; z-index: 15"></div>');
    $('#body').append('<div style="width:3px; height: '+h+'px; position: absolute; top: 3em; right: -3px; background: url(/images/content-br.png) repeat-y top left; z-index: 15"></div>');
}

function toggleV(elem)
{
	if (elem.css("visibility") == "visible")
		elem.css("visibility","hidden");
	else elem.css("visibility","visible");
}

function talk()
{
	if (!$('#green_menu li.talk .iframetalk').is('.iframetalk'))
	{
		var cod = '<iframe class="iframetalk" src="http://www.google.com/talk/service/badge/Show?tk=z01q6amlq9c0ej9dqoo427kfo7p1odhmnqkpuef2i7tjidt93doam0ho76ntvq3agbuuofel60phqm7dve9bnk0qamj510vbotvu7rlcphs7anbianl44202scei6mfhl16r819kc5k5ssoikjs3enoq9ilbjsbm0p2n2u88c&amp;w=200&amp;h=60" allowtransparency="true" width="200" frameborder="0" height="60"></iframe>'
		$('#green_menu li.talk').append(cod);
	}
	else $('#green_menu li.talk iframe').remove();
}

function contacto()
{
	$('#contact div.formulario div.button_enviar a').click(function (e) {
		if (compruebaForm())
		{
			formActive = true;
			var auxData = $(this).closest('form').serialize();
			$.ajax({
				data:		auxData,
				url:		'/envio_mail.php',         // override for form's 'action' attribute
				type:		'post',        // 'get' or 'post', override for form's 'method' attribute
				dataType:	'script',
				success:	function () {formActive = false;}
			});
		}
		e.preventDefault();
	});
}

function compruebaForm()
{
	if (formActive == true) return false;
	var nombre = $.trim($('#contact div.formulario input[name=contacto-nombre]').val());
	var email = $.trim($('#contact div.formulario input[name=contacto-email]').val());
	var mensaje = $.trim($('#contact div.formulario textarea[name=contacto-mensaje]').val());
	$('#contact div.formulario label').css('color','white');
	var bien = true;
	if (! $('#contact div.formulario div.check input').is("input:checkbox:checked"))
	{
		$('#contact div.formulario div.check a').css('color','red');
		bien = false;
	}
	else
	{
		$('#contact div.formulario div.check a').css('color','white');
	}
	if (nombre == '')
	{
		$('#contact div.formulario input[name=contacto-nombre]').prev('label').css('color','red');
		bien = false;
	}
	if (mensaje == '')
	{
		$('#contact div.formulario textarea[name=contacto-mensaje]').prev('label').css('color','red');
		bien = false;
	}
	if (!mail(email))
	{
		$('#contact div.formulario input[name=contacto-email]').prev('label').css('color','red');
		bien = false;
	}
	return bien;
}

function mail(txt)
{
	var s = txt;
	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;
	else
		return false;
	return false;
}
