$(document).ready(function() {
    $(".collapse_link").click(function(e){
        var href = $(this).attr('href');
        if(href.length > 0){
            $(href).slideToggle("slow");
        }
        return false;
    });
    
    if(window.location.hash.length > 0){
        $(window.location.hash).slideDown("slow");
    }

    /*$(".collapse_link").each(function(e){
        var href = $(this).attr('href');
        if(href.length > 0){
            $(href).slideUp();
        //$(href).show();
        }
    });*/

    $('#login_username').watermark({
        watermarkText: 'Nombre usuario',
        watermarkCssClass: 'watermark'
    });
    $('#login_password').watermark({
        watermarkText: 'Password',
        watermarkCssClass: 'watermark'
    });
    $('.comment_form input[name="generic_comment"]').watermark({
        watermarkText: 'Escriba un comentario y presione Enter...',
        watermarkCssClass: 'watermark'
    });
    $('#searchformjovenestxt').watermark({
        watermarkText: 'Buscar',
        watermarkCssClass: 'search_input_jovenes'
    });

    $('#txt_search').watermark({
        watermarkText: 'Buscar',
        watermarkCssClass: 'watermark'
    });
    $('#txt_searchRadio').watermark({
        watermarkText: 'Escriba una palabra relacionada con lo que busca',
        watermarkCssClass: 'watermark'
    });
    $('.search_listing:odd').addClass('even');

    $('#txt_search').focus(function(){
        $('#btn_search').show();
    });
    $('#txt_search').blur(function(){
        $(this).oneTime(1000, function() {
            $('#btn_search').hide();
        });
    });

    $('.report_link').each(function(){
        var href = $(this).attr('href');
        var current_url = location.href.substring(siteURL.length);
        href += '?address=' + encodeURIComponent(current_url) + '&title=' + encodeURIComponent(document.title);
        $(this).attr('href', href);
    });

    $('input.terms').each(function(){
        var $parentForm = $(this).closest("form");
        var $submits = $parentForm.find('input[type="submit"]')
        $submits.attr("disabled", "disabled");
        $submits.hide();
    });
    $('input.terms').click(function(){
        var $parentForm = $(this).closest("form");
        var $submits = $parentForm.find('input[type="submit"]')
        if ($(this).attr("checked")){
            $submits.removeAttr("disabled")
            $submits.show();
        } else {
            $submits.attr("disabled", "disabled");
            $submits.hide();
        }
    });

    

    $("#news_listing").ticker(75, true, true);
    $("#status_gente").ticker(75, true, true);

    //Para los forms en AJAX
    var options = {
        //target:        '',   // target element(s) to be updated with server response
        success:       processResponse,  // post-submit callback

        // other available options:
        //url:       url         // override for form's 'action' attribute
        type:      'post'       // 'get' or 'post', override for form's 'method' attribute
    //dataType:  'json' ,       // 'xml', 'script', or 'json' (expected server response type)
    // clear all form fields after successful submit
    //resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
    };
    var optionsLike = {
        //target:        '',   // target element(s) to be updated with server response
        success:       processResponseLike,  // post-submit callback

        // other available options:
        //url:       url         // override for form's 'action' attribute
        type:      'post'        // 'get' or 'post', override for form's 'method' attribute
    //dataType:  'json' ,       // 'xml', 'script', or 'json' (expected server response type)
    // clear all form fields after successful submit
    //resetForm: true        // reset the form after successful submit

    // $.ajax options can be used here too, for example:
    //timeout:   3000
    };
    
    // bind form using 'ajaxForm'
    $("form.comment_form").ajaxForm(options);
    $(".river_likeShare form").ajaxForm(optionsLike);
    
    
    $("form.comment_form input").keypress(function(event){
        if (event.which == '13') {
            $(this).parent().parent().find('.loadingGif').toggle();
            $(this).slideUp('normal');
            $(this).parent().ajaxSubmit(options);
            $(this).attr("value",".");
            event.preventDefault();
        }
    });

    function processResponse(responseText, statusText, xhr, $form){
        $parent = $form.parent().parent().parent().parent().parent();
        $parent .find('.los_comentarios').html(responseText);
        $('.loadingGif').slideUp("slow");
        $('.comment_form input')
        $parent.parent().find("#_watermark").css("display", "inline");
    }
    function processResponseLike(responseText, statusText, xhr, $form){
        $parent = $form.parent().parent().parent();
        $parent.find('.river_likeShare').html(responseText);
    }
    
});
