_oldsearch = '';

$(document).ready(function (){
	$("img.rolloverimagen").mouseover(function (){
			var newImage = $(this).attr('src').replace(/^(.*?)(\.(?:gif|jpg|png))$/, "$1_over$2");
			$(this).attr('src', newImage);
	});
	$("img.rolloverimagen").mouseout(function (){
			var newImage = $(this).attr('src').replace('_over', '');
			$(this).attr('src', newImage);
	});
	
	$(document).pngFix(); //-- Para png's transparentes
	
	$("#home_suscripcion").css({ background: "url(img/"+_lang+"_bg_div_suscripcion.jpg) no-repeat left top" });
	/*
	$("#flash").css({ background: "url(img/"+_lang+"_img_flash.jpg) no-repeat left top" });
	$("#home_casos").css({ background: "url(img/"+_lang+"_bg_div_casos.jpg) no-repeat left top" });
	$("#home_proyectos").css({ background: "url(img/"+_lang+"_bg_div_proyectos.jpg) no-repeat left top" });
	$("#flash_quienes").css({ background: "url(img/"+_lang+"_img_flash_quienes_somos.jpg) no-repeat left top" });
	$("#flash_casos").css({ background: "url(img/"+_lang+"_img_flash_casos_exito.jpg) no-repeat left top" });
	*/

	$("#search").focus(function(ev){if(_oldsearch == '') _oldsearch = ev.target.value; ev.target.value = ''});
	$("#search").blur(function(ev){if(ev.target.value == '') ev.target.value = _oldsearch});
	$("#search").keydown(searchKey);

	$("#contactsend").click (sendContactForm);
	$("#frmcontact").submit (sendContactForm);

	$("#sendsubscription").click (sendSubscription);
	$("#subscriptionemail").keydown (subscriptionKey);
});

function subscriptionKey (ev){
    var keycode = ev.which? ev.which: ev.keyCode;
    if (keycode == 13) {
        sendSubscription();
        ev.stopPropagation();
        return false;
    }
}

function sendSubscription (ev) {
    var email = document.frmsubscription.subscriptionemail.value;
    if (email != '') {
        if (wbvfIsEmail (email)) {
        $.ajax({
            url: "subscribe_ajax.php",
            type: "POST",
            dataType: "text",
            data: {email: email, wlang: _lang},
            success: function(txt) {
                if (txt == "OK") {
                    alert (_subsok);
                } else {
                    alert (_subserr + ' - ' + txt);
                }
            },
            error: function(req, err, obj) {
                alert (_subserr + ' - ' + err + ' ' + req.status);
            }
        });
            
        } else {
            alert (g_wbVFalertMap[_lang]["mail"].replace ("%e", "e-mail") );
        }
    }

    if (ev != null) ev.stopPropagation();
    return false;
}

function searchKey (ev){
    var keycode = ev.which? ev.which: ev.keyCode;
    if ( (keycode == 13) && (ev.target.value != '') ) {
        self.location = ev.target.form.action + '?' + ev.target.value;
        ev.stopPropagation();
        return false;
    }
    return true;    
}

function sendContactForm(ev) {
    if (wbValidateFormL (document.frmcontact, _lang)) {
        $("#senderr").hide();
        $("#sendform").hide();
        $("#sending").show();

		var _nombre = document.frmcontact.nombre.value;
		var _telefono = document.frmcontact.telefono.value;
		var _empresa = document.frmcontact.empresa.value;
		var _comentario = document.frmcontact.comentario.value;
		var _email = document.frmcontact.email.value;
        
        $.ajax({
            url: "contact_ajax.php",
            type: "POST",
            dataType: "text",
            data: {nombre: _nombre, empresa: _empresa, telefono: _telefono, comentario: _comentario, email: _email, wlang: _lang},
            success: function(txt) {
                if (txt == "OK") {
                    $("#sendok").show();
                    $("#sendform").hide();
                    $("#sending").hide();
                    $("#senderr").hide();
                } else {
                    $("#sending").hide();
                    $("#sendform").show();
                    $("#senderr").show();
                    $("#senderrtxt").html (txt);
                }
            },
            error: function(req, err, obj) {
                $("#sending").hide();
                $("#sendform").show();
                $("#senderr").show();
                $("#senderrtxt").html (err + ' ' + req.status);
            }
        });
    }
    ev.stopPropagation();
    return false;
}