var elem = "#menuFlotante";
var menuYloc = null;

// pre-submit callback
function disableSend(data, form_object, options) {
    $('.botonsubmit').css('margin-left', '204px').css('width', '7em').val('Enviando...').attr("disabled","disabled");
};

// post-submit callback
function showResponse(responseText, statusText)	 {
    $('.botonsubmit').removeAttr("disabled").css('margin-left', '230px').css('width', '5em').val('Enviar');
    alert(responseText.message);
	if (responseText.success){
		$('#send_comment_form textarea')[0].value="";
		$('#send_comment_form #id_name')[0].value="";
		$('#send_comment_form #id_email')[0].value="";
	};
};


// Imagen anterior y siguiente en las galerias
function prev_img(clickedImage) {
	deltaImages(clickedImage, false);
};
function next_img(clickedImage) {
	deltaImages(clickedImage, true);
};
function deltaImages(clickedImage, direction){
	var $visorFotos = $(clickedImage).parents('.centroSeccion');
	var $fotos = $visorFotos.find('.fotos');
	var $images = $fotos.find('img');
	var $pieDeFotos = $visorFotos.find('.pieDeFotos');
	var $current_photo_index = $visorFotos.find('.current_photo_index');

	if ($images.length == 1) return;

    if ((direction && $current_photo_index.html() != $images.length) || (!direction && $current_photo_index.html() != '1')) {
        var $current = $fotos.find('img:visible');
        $images.css('display', 'none');
        $current = direction ? $current.next() : $current.prev();
        $current.css('display', 'block');
        $pieDeFotos.text($current.attr('alt'));
        $current_photo_index.html(parseInt($current_photo_index.html()) + (direction ? 1 : -1));
    };
}
// fin


// Wrapper para jQuery.load() que muestra un gif animado mientras se ejecuta
function jload(object, url, indicator) {
    object.html("<div style=\'margin-right:0px; margin-left: 0px; width:100%\'><img id='indicador_carga' src=\'"+indicator+"\') /></div");
    object.load(url);
};
// fin

// Cuando se expande una seccion (ver todo) se colapsan las otras
var section_nav_urls = {'press_nav' : '/prensa/recientes/', 'agenda_nav' : '/eventos/recientes/', 'videos_nav' : '/videos/recientes' };

function collapse_sections(section_open) {
    for (section in section_nav_urls) {
        if (section != section_open) {
            $('#'+section).load(section_nav_urls[section]);
        };
    };
};

// fin
function contactar(nombre){
	this.location.href = 'mai' + 'lto:' + nombre + '@montevide' + 'ana.com' + '.uy';
}


function expandirTestimonio(source){
	$("blockquote.completo").hide();
	$("blockquote.resumen").show();
	$testimonio = $(source).parents(".testimonio");

	var testimonio = $testimonio.find("blockquote.resumen").hide();
	var testimonio = $testimonio.find("blockquote.completo").show();

	return false;
}







	/* form de prensa */
	function disableSendPrensa(data, form_object, options) {
	    $('.botonsubmit').val('Enviando...').attr("disabled","disabled");
	};
	function showResponsePrensa(responseText, statusText)	 {
	    $('.botonsubmit').removeAttr("disabled").val('Enviar');
	    
		alert(responseText.message + (responseText.error||''));
		
		if (responseText.success){
			$('#acreditacion_prensa').clearForm();
		}else{
			if (responseText.error_field){
				$('#acreditacion_prensa #id_' + responseText.error_field)[0].focus()
			}
			
		};
	};
    var optionsPrensa = {
        beforeSubmit: disableSendPrensa, // pre-submit callback
        success: showResponsePrensa,  // post-submit callback
        dataType: 'json', // response type
        clearForm: false // clear all form fields after successful submit
    };
	/* fin form de prensa */
	
	
	
	



$(document).ready(function() {
    // Efecto animacion del navegador omnipresente
    menuYloc = parseInt($(elem).css("top").substring(0,$(elem).css("top").indexOf("px")), 10);
	$(window).scroll(function() {
		var offset = menuYloc+$(document).scrollTop()+"px";
		$(elem).animate({top:offset},{duration:1000,queue:false});
	});

	var offset = menuYloc+$(document).scrollTop()+"px";
	$(elem).animate({top:offset},{duration:1000,queue:false});

    // fin

    // Manejo de forms con ajaxForm
    var options = {
        beforeSubmit: disableSend, // pre-submit callback
        success: showResponse,  // post-submit callback
        dataType: 'json', // response type
        clearForm: false // clear all form fields after successful submit
    };

	$('#send_comment_form').ajaxForm(options);
	$('#send_me_news_form').ajaxForm(options);
	$('#acreditacion_prensa').ajaxForm(optionsPrensa);
	



	// Rotacion de comentarios en seccion Ida y Vuelta
	$('#derechaComentarios').cycle({
        fx:    'fade', // transition effect
        speed:  4000,  // milliseconds
        pause:  1,     // pause when the mouse hovers over the slide?
        random:  1     // random order instead of sequential order?
    });

    // fin

	// Rotacion de fotos de apoyos
	$('#centroApoyos').cycle({
        fx:    'fade', // transition effect
        speed:  2000,  // milliseconds
        pause:  1,     // pause when the mouse hovers over the slide?
        random:  1     // random order instead of sequential order?
    });

    // fin

    $('#id_email').focus(function() {
        $(this).val("");
    });

});
