function setAsCurrent(that) {
    $("#img_main").addClass('loading');
    $('#throbber').show();
    // get img to load in
    $("#other_photos a.thumbnail").removeClass('selected');
    that.parent().addClass('selected');

    img_src = that.attr('src').replace('_mm', '_main');
    var img = new Image();
    $(img)
    // once the image has loaded, execute this code
    .load(function () {
        // set the image hidden by default
        $("#main_link").attr('href', that.attr('src').replace('_mm', '') );
        $("#img_main").hide();
        $("#img_main").attr('src', img_src);
        $("#throbber").hide();
        $("#img_main").removeClass('loading');

        // fade our image in to create a nice effect
        $("#img_main").fadeIn();
    })
    // if there was an error loading the image, react accordingly
    .error(function () {
        // notify the user that the image could not be loaded
    })

    // *finally*, set the src attribute of the new image to our image
    .attr('src', img_src);

}

function changePhoto(){
    $("a.thumbnail").click( function() {
        var img = $(this).children('img.img_thumnbail');
        setAsCurrent(img);
        return false;
    });
}

function setFocusBlurAction() {
    $("#comment_body").focus( function() {
        $(this).removeClass('correct');
        $(this).addClass('focus');
    });

    $("#comment_body").keypress( function(e){
        // allow special keys
        allowed_keys = new Array(0, 8, 99, 120);
        for( key in allowed_keys ){
            if( allowed_keys[key] == e.which ){
                /*console.debug("poprawny: " + e.which);*/
                return true;
            }
        }

        if( $(this).val().length >= 500) {
            /*console.debug("tutaj");*/
            return false;
        }
        return true;
    });

    $("#comment_body").blur( function() {
        $(this).removeClass('focus');
        if( !$(this).val() || $(this).val().length > 500 ) {
                $(this).removeClass('correct');
                $(this).addClass('error');
        } else {
            $(this).removeClass('error');
            $(this).addClass('correct');
        }
    });
}

function clearForm() {
    $("#comment_body").val('');
}

function submitAction() {
    $("#comment_form").submit( function() {
        // Force validation
        $("#comment_body").blur();
        if( $(this).find('.error').length > 0 ) {
            return false;
        }
    });
}

function confirmCommentDelete(){
    $("#comments a.delete_comment").click( function() {
        if( !confirm('Czy napewno chcesz usunąć ten komentarz?') ){
            return false;
        }
    });
}

function toolbar_actions(){
/*    $("#admin_toolbar li.delete_action").click( function() {
        if( confirm('Czy napewno chcesz usunąć ten profil?') ){
            var profile_id =  $(this).parent().attr('id').match(/\d+/);
            document.location = '/profile/delete/' + profile_id + '?url=/profile/view/' + profile_id;
        }
    });*/

    $("#admin_toolbar li.unblock_action").click( function() {
        var profile_id =  $(this).parent().attr('id').match(/\d+/);
        document.location = '/profile/activate/' + profile_id + '?url=/profile/view/' + profile_id;
    });

    $("#admin_toolbar li.block_action").click( function() {
        var profile_id =  $(this).parent().attr('id').match(/\d+/);
        $("#reason_box").show();
//        $(form) = '/profile/block/' + profile_id;
    });

    $("#admin_toolbar li.set_thumbnails").click( function() {
        var profile_id =  $(this).parent().attr('id').match(/\d+/);
        document.location = '/admin/profile/index/' + profile_id;
    });
}

function reason_box(){
    $("#rb_close").click( function() {
        $("#reason_box").hide();
        return false; //stop bubbling (li has also click action: look above)
    });

    $("#btn_send").click( function(){
        $("#i_reason").val( $("#reason_text").val() );
        if( !$("#i_reason").val() ) {
            alert('Podaj powód zablokowania');
            return false;
        }
        $("#frm_block").submit();
    });
}

var show_abuse_form = false;
function report_abuse(){
    $("#trigger-abuse").click( function(){
        if(show_abuse_form){
            $("#report-abuse").hide();
            show_abuse_form = false;
	    $("#submit_abuse").val(0);
        }else{
            $("#report-abuse").show();
            show_abuse_form = true;
	    $("#submit_abuse").val(1);
        }
    });
}

function handleLightBox() {
	$('#main_link').lightBox({imageBtnClose:'/images/lightbox/lightbox-btn-close.gif', imageBtnFullSize:'/images/lightbox/lightbox-btn-lupe.gif', imageLoading:	'/images/lightbox/lightbox-ico-loading.gif'});
}

function howtovote(){
    $("span.howtovote-trigger").click( function(){
	$("div#howtovote").show();
    });

    $("#close-howtovote").click( function(){
	$("div#howtovote").hide();
	return false;
    });
}

$(document).ready( function() {
    clearForm();
    changePhoto();
    setFocusBlurAction();
    submitAction();
    confirmCommentDelete();
    reason_box();
    toolbar_actions();
    report_abuse();
    handleLightBox();
    howtovote();
});
