﻿$(document).ready(function() {

    $("#leftLane").css({ width: "0px", top: $("#mainImage").position().top }).animate({ width: "200px" });

    if (document.getElementById("mainEpigraph") != undefined) {
        $("#mainEpigraph").hide();
        $("#mainEpigraphBack").hide();
    }
    if (document.getElementById("zoomPrint") != undefined) {
        $("#zoomPrint").hide();
        $("#zoomPrintBack").hide();
    }
    if (document.getElementById("txtLaneBack") != undefined) {
        $("#txtLane").hide();
        $("#txtLaneBack").css({
            top: $("#mainImage").position().top + $("#mainImage").height(),
            left: $("#mainImage").position().left + 202,
            width: $("#mainImage").width() - 202,
            height: "0px",
            opacity: 0
        }).animate({
            top: $("#mainImage").position().top,
            height: $("#mainImage").height(),
            opacity: 0.6
        },
    function() {
        $("#txtLane").css({
            top: $("#txtLaneBack").position().top + 10,
            left: $("#txtLaneBack").position().left + 10,
            width: $("#txtLaneBack").width() - 20,
            height: $("#txtLaneBack").height() - 20
        }).fadeIn()
    }
    );
        if (document.getElementById("contact") != undefined) {
            onSendMXres();
        }
    }


    /* bottomLane engaged */
    if (document.getElementById("bottomLane") != undefined) {
        $("#bottomLane").css({
            height: "0px",
            top: ($("#mainImage").position().top + $("#mainImage").height() - 2),
            left: ($("#mainImage").position().left + 202),
            opacity: 0,
            width: "749px"
        }).animate({
            height: 100,
            top: ($("#bottomLane").position().top - 100),
            opacity: 1
        }, function() {
            var epTop = $("#bottomLane").position().top - $("#mainEpigraph").height() - 2;
            var epHeight = $("#mainEpigraph").height();
            $("#mainEpigraphBack").css({
                height: "0px",
                opacity: "0",
                top: epTop,
                left: $("#bottomLane").position().left
            }).show().animate({
                width: "370px",
                height: epHeight,
                top: epTop - epHeight,
                opacity: 0.7
            },
        function() {
            $("#mainEpigraph").css({
                width: $("#mainEpigraphBack").width(),
                top: $("#mainEpigraphBack").position().top,
                left: $("#mainEpigraphBack").position().left
            }).fadeIn(500);

            /* zoom/print engaged */
            if (document.getElementById("zoomPrint") != undefined) {
                var zpHeight = $("#zoomPrintBack").height();
                $("#zoomPrintBack").css({
                    opacity: 0,
                    height: 0,
                    top: $("#mainImage").position().top,
                    left: $("#mainImage").position().left + $("#mainImage").width() - $("#zoomPrintBack").outerWidth(true)
                }).animate({
                    opacity: 0.7,
                    height: zpHeight
                },
                function() {
                    $("#zoomPrint").css({
                        top: $("#zoomPrintBack").position().top,
                        left: $("#zoomPrintBack").position().left
                    }).fadeIn();
                }
                )
            }
        });
        });
    };


    /* rightLane engaged */
    if (document.getElementById("rightLane") != undefined) {
        $("#rightLane").css({
            width: "0px",
            opacity: 0,
            height: $("#mainImage").height() - 2,
            top: $("#mainImage").position().top,
            left: ($("#mainImage").position().left + $("#mainImage").width())
        }).animate({
            width: "100px",
            opacity: 1,
            left: ($("#rightLane").position().left - 100)
        },
            function() {
                if (document.getElementById("mainEpigraph") != undefined) {
                    var epTop = $("#rightLane").position().top + $("#rightLane").height() - 8;
                    var epHeight = $("#mainEpigraph").height() + 10;
                    $("#mainEpigraph").css({ height: "0px", opacity: "0", top: epTop, left: $("#rightLane").position().left - $("#mainEpigraph").width() - 11 }).show().animate({ height: epHeight, top: epTop - epHeight, opacity: 1 });
                    $("#mainEpigraphBack").css({ height: "0px", opacity: "0", top: epTop, left: $("#rightLane").position().left - $("#mainEpigraph").width() - 11 }).show().animate({ height: epHeight, top: epTop - epHeight, opacity: 0.5 });
                }
            }
         );
    };

});

$(window).resize(function() {
    if (document.getElementById("mainEpigraphBack") != undefined) {
        if (document.getElementById("rightLane") != undefined) { $("#mainEpigraph").css("left", $("#rightLane").position().left - $("#mainEpigraph").width() - 11); }
        if (document.getElementById("bottomLane") != undefined) {
            $("#bottomLane").css({ left: $("#leftLane").position().left + $("#leftLane").width() + 2 });
            $("#mainEpigraph").css("left", $("#bottomLane").position().left);
        }
        $("#mainEpigraphBack").css("left", $("#mainEpigraph").position().left)
    };
    if (document.getElementById("txtLaneBack") != undefined) {
        $("txtLaneBack").css("left", $("#mainImage").position().left + 202);
        $("txtLane").css("left", $("#mainImage").position().left + 202);
    }
});


/** Image Gallery **/
$(function() {
    $("#imgList img").click(function() {

        //-- hide epigraph
        if (document.getElementById("mainEpigraph") != undefined) {
            var txt = $(this).attr("alt");
            $("#mainEpigraph").animate({ top: $("#mainEpigraph").position().top + 15, opacity: 0 }, function() { $("#mainEpigraph").text(txt) });
        }
        
        //-- load next image
        $("#mainImage").addClass("loading");
        showImage($(this).attr("longdesc"));

        if (document.getElementById("zoomPrint") != undefined) {
            $("#zoomPrint a").attr("href", $(this).attr("destination"))
        }

        return false;
    });
});

function showImage(src) {
    $("#mainImage img").fadeOut("normal").remove();
    var mImage = new Image();
    $(mImage).load(function() {
        $(this).hide();
        $("#mainImage").append(this).removeClass("loading");
        $(this).fadeIn("slow");
        if (document.getElementById("mainEpigraph") != undefined) {
            $("#mainEpigraph").animate({ top: $("#mainEpigraphBack").position().top, opacity: 1 });
        }
    });
    $(mImage).attr("src", src);
}

/* Contact validation */
function getQS(ji) {
    hu = window.location.search.substring(1);
    gy = hu.split("&");
    for (i = 0; i < gy.length; i++) {
        ft = gy[i].split("=");
        if (ft[0] == ji) { return ft[1]; }
    }
}

function onSendMXres() {
    switch (getQS('mxstat')) {
        case 'ok':
            document.getElementById('contact').style.display = 'none';
            document.getElementById('contactOk').style.display = 'inline';
            break;
        case 'fail':
            document.getElementById('contact').style.display = 'none';
            document.getElementById('contactFail').style.display = 'inline';
            break;
    }
}

function validateMX() {
    var oForm = document.contactForm;
    if (genIsEmpty(oForm.mxNombre_y_apellido, "su nombre")) { return true; };
    if (genIsEmpty(oForm.mxEmail, "su dirección de correo")) { return true; };
    if (genIsEmail(oForm.mxEmail, "una dirección de correo real")) { return true; };
    if (genIsEmpty(oForm.mxTelefonoPais, "el código del pais (54 para Argentina)")) { return true; };
    if (genIsEmpty(oForm.mxTelefonoRegion, "el código del area (11 para Capital Federal)")) { return true; };
    if (genIsEmpty(oForm.mxTelefonoLocal, "su numero de teléfono")) { return true; };
    if (genIsEmpty(oForm.mxTexto_Ingresado, "escriba aqui el motivo de su consulta")) { return true; };
    $("#btSend").fadeOut();
    oForm.submit();
}

function genIsEmail(oField, sMessage, sPrefix){
	var filter=/^([\w-]+(?:\.[\w-]+)*)@((?:[\w-]+\.)*\w[\w-]{0,66})\.([a-z]{2,6}(?:\.[a-z]{2})?)$/i
	if (!filter.test(oField.value))
	{
		genShowError(oField, sMessage, sPrefix);
		return true;
	}
	return false;
}

function genIsNumber(oField, sMessage, sPrefix){
	var filter=/(^\d+$)/
	if (!filter.test(oField.value))
	{
		genShowError(oField, sMessage, sPrefix);
		return true;
	}
	return false;
}

function genIsEmpty(oField, sMessage, sPrefix){
	var isOk = false;
	var fType= new String();
	fType=oField.type
	if (fType==undefined){fType=oField[0].type}

	switch(fType){
		case 'textarea':
		case 'text':
			isOk=(oField.value!='');
			break;
		
		case 'select-one':
			isOk=(!(oField[0].selected)); 
			break;

		case 'radio':
		case 'checkbox':
			if(oField.length>0){
				for(i=0; i<oField.length; i++){if(oField[i].checked){isOk=true}}
			}else{
				isOk = (oField.checked)
			}
			break;
		
		default:
			alert ('field: ' + oField.name + '\ntype: ' + fType + '\nvalue: ' + oField.value);
			alert (oField.type)

	}

	if(!(isOk)){
		genShowError(oField, sMessage, sPrefix);
		return true;
	}
	return false;
}
function genShowError(oField, sMessage, sPrefix){
	
	if(sPrefix==undefined)
	{alert('Por favor ingrese ' + sMessage);}
	else
	{alert(sPrefix + sMessage);}
	oField.focus();
}
