Community = {}
Community.submitForm = function(){
    document.forms['form_add_mat'].submit();
}

Community.publishForm = function (){
    document.forms['form_add_mat'].elements['draft'].value = 0;
    Community.submitForm();
}

Community.getURL = function(){
    oForm = document.forms['form_add_mat'].elements;
    id = parseInt( oForm['id'].value, 10 );
    title = encodeURIComponent( oForm['title'].value );
    $.get("/community/getURL/" + id + "/json/", {
        'title':title
    }, function(data){
        if( data.success == 1 ){
            oForm['story_url'].value = data.url;
            var u = $.trim( $("#domain").html() ) + data.url + ".html";
            $("#url a").html( u );
//            $("#url a").attr('href', url);
        }
    });
}

Community.redirect = function(url){
    setTimeout("window.location.href =\"" + url +"\";",  3000);
}

Community.selectedPictureSRC = false;
Community.selectedPictureID = 0;

Community.align = null;

Community.showDialog = function ( onCloseFunction ){
    html = $('#picture_list').html();
    $('#picture_list').empty();

    $.modal(html, {
        overlayClose:true,
        position: [50],
        minHeight:550,
        minWidth:600,
        onShow: function(){
            $('ul.pic_list').html("");
            $('.picture_list').show( );
            Community.loadPictures();
        },
        onClose: onCloseFunction
    });
}

Community.selectLeatPicture = function (){
    this.showDialog( function (){
        if( Community.selectedPictureSRC !== false ){
            Community.setPreviewPicture( Community.selectedPictureSRC );
            $('#fam_lead_pic').attr('value', Community.selectedPictureID );
        }
        Community.closeDialog();
    });
}

Community.closeDialog = function (){
    Community.selectedPictureSRC = false;
    Community.selectedPictureID = 0;
    $('#picture_list').html( $('#simplemodal-data').html());
    $('#picture_list').hide();
    $.modal.close();
}

Community.setPreviewPicture  = function ( picture ){
    sp = picture.split(/\//g);
    image = sp.pop();
    sp.pop();
    sp.push('220_145', image );
    $("#leat_pic_selected a.pic_path").attr('href', sp.join( "/" ) ).html( image );
    $( '#leat_pic_select').hide();
    $( '#leat_pic_selected').show();
}

Community.clearPicture = function(){
    $('#fam_lead_pic').attr('value', 0);
    $( '#leat_pic_select').show();
    $( '#leat_pic_selected').hide();
    return false;
}
Community.loadPictures = function( url ){
    AjaxLoading('pu_userfile_button');
    if( url == null ){
        url = "/community/userpictures/json/";
    }
    $.get(url, {}, function(data){
        var html = "";
        if( data.files.length == 0 ){
            html = "<li>" +  lang.userfiles.no_pics + "</li>";
        } else {
            for( i in data.files){
                sp = data.files[i].path.split(/\//g);
                sp.push('200_200', sp.pop());

                html += "<li class='round_corners shadow_ccc'><a href=\"#\"><img src=\"" + data.userfiles_path + sp.join('/') + "\" width=\"128\" id=\"userpic_" + data.files[i].id + "\"/></a></li>";
            }
            Community.showPages( data.paginator );
        }
        $('ul.pic_list').html(html);
        Community.loadPicturesDone();
    }, 'json');
}

Community.loadPicturesDone = function(){
    AjaxLoading('pu_userfile_button', false);
    $('.pic_list').show();
    $('.pic_list a').click( function(){
        Community.selectedPictureSRC = $(this).children('img').attr('src');
        tmp = $(this).children('img').attr('id').split('_');
        Community.selectedPictureID = tmp[1];
        $.modal.close();
        return false;
    });
}

Community.errorMessage = function( text ){
    AjaxLoading('pu_userfile_button');
    $('.error_div').html(text).fadeIn('slow', function(){
        setTimeout("$('.error_div').fadeOut('slow')", 3000);
    });

}
Community.uploadFile = function(){
    AjaxLoading('pu_userfile_button');
    return true;
}

Community.showPages = function ( paginator ){
    if( paginator.prev_url == "" && paginator.next_url == "" ){
        html = "";
    } else {
        html = '<p class="pages">';

        html += ( paginator.prev_url != '') ? '<a href="#" onclick="Community.loadPictures(\'' + paginator.prev_url + '\'); return false;">' + lang.pages.prev + '</a>' : "<span>" + lang.pages.prev + "</span>";
        html += " ";
        html += ( paginator.next_url != '') ? '<a href="#" onclick="Community.loadPictures(\'' + paginator.next_url + '\'); return false;">' + lang.pages.next + '</a>' : "<span>" + lang.pages.next + "</span>";

        html += "</p>";
    }
    $("#files_pages").html( html );
};

Community.editorPicture = function ( editor, path ){
    //    html = "<p><ima</p>";
    sp = path.split(/\//g);
    image = sp.pop();
    sp.pop();
    sp.push( '200_350', image );
    var element= editor.document.createElement( 'img' );
    element.setAttribute( 'src', sp.join('/') );

    editor.insertElement( element );
}

Community.editorPictureProperties = function ( editor ){
    var selection = editor.getSelection();
    if ( selection.getType() == CKEDITOR.SELECTION_ELEMENT ){
        if( selection.getSelectedElement().getName() == 'img'){
            element = selection.getSelectedElement();
            src = element.getAttribute('src');
            align = element.getAttribute('align');
            className = element.getAttribute('class');

            $('#pic_prew').attr( 'src', src);
            
            html = $('#picture_wrapping').html();
            $('#picture_wrapping').empty();

            $.modal(html, {
                overlayClose:true,
                position: [50],
                onShow: function(){
//                    $('#pic_prew').attr( 'src', src);
                    switch( align ){
                        case "left":
                            id = "wrapping_l";
                            break;
                        case "right":
                            id = "wrapping_r";
                            break;
                        default:
                            id = "wrapping_n";
                            break;
                    }
                    $('#' + id).attr('checked',true);
                },
                onClose: function (){
                    if( Community.align !== null ){
                        element.setAttribute('style', "");
                        switch( Community.align ){
                            case "l":
                                align = "left";
                                className = "cm_pic_a_l";
                                break;
                            case "r":
                                align = "right";
                                className = "cm_pic_a_r";
                                break;
                            default:
                                align = "";
                                className = "";
                                break;
                        }
                        element.setAttribute('align', align);
                        element.setAttribute('class', className);
                    }
                    Community.align = null;
                    $('#picture_wrapping').html( $('#simplemodal-data').html());
                    $('#picture_wrapping').hide();
                    $.modal.close();
                }
            });
            
        }
    }
}
Community.alignImageOk = function (){
    Community.align = 'n';
    if ( $("#wrapping_r").attr('checked') ){
        Community.align = 'r';
    }
    if ( $("#wrapping_l").attr('checked') ){
        Community.align = 'l';
    }
    $.modal.close();
    return false;
};

Community.checkAutorship = function(){
    $("#fam_source").attr('disabled', ( $("#fam_i_authorship").attr('checked') == true ) );
}