﻿/*
=====================================================
 DLE Forum
-----------------------------------------------------
 http://dle-files.ru/
-----------------------------------------------------
 Copyright (c) 2011 DLE Files Group
=====================================================
 Данный код защищен авторскими правами
=====================================================
 Файл: dle_forum.js
=====================================================
*/

var cache_post = new Array();

// ********************************************************************************
// POST EDIT
// ********************************************************************************
function ajax_post_edit(id)
{
    ShowLoading();
    
    if ( ! cache_post[id] || cache_post[id] == '' )
    {
        cache_post[id] = $('#post-id-' + id).html();
    }
    
    $.post(forum_ajax + 'editpost.php', {id: id, action: 'edit'}, function(data) { $('#post-id-' + id).html(data); });
    
    HideLoading();
    
    return false;
}

function ajax_cancel_post_edit(id)
{
    $('#post-id-' + id).html(cache_post[id]);
    
    return false;
}

// ********************************************************************************
// POST SAVE
// ********************************************************************************
function ajax_save_post_edit(id, forum_wysiwyg)
{
    ShowLoading();
    
    var post_txt = '';
    
    if (forum_wysiwyg == "1")
    {
        post_txt = $('#forum_post_' + id).html();
    }
    else
    {
        post_txt = $('#forum_post_' + id).val();
    }
    
    $.post(forum_ajax + 'editpost.php', {id: id, action: 'save', wysiwyg: forum_wysiwyg, post_text: post_txt}, function(data) { $('#post-id-' + id).html(data); });
    
    cache_post[id] = '';
    
    HideLoading();
    
    return false;
}

// ********************************************************************************
// POST ADD
// ********************************************************************************
function doAddPost()
{
    var form        = document.getElementById('forum-post-form');
    var post_text   = '';
    var editor_mode = '';
    var sec_code    = '';
    
    if (forum_wysiwyg == "1")
    {
        post_text = $('#post_text').html();
        editor_mode = 'wysiwyg';
    }
    else
    {
        post_text = $('#post_text').val();
    }
    
    if (form.sec_code)
    {
        sec_code = form.sec_code.value;
    }
    
    if (post_text == '')
    {
        DLEalert ('Bütün vacib sətirləri doldurun', 'Xəta');
        return false;
    }
    
    ShowLoading();
    
    var topic_title = $('#topic_title').val();
    var forum_id    = $('#forum_id').val();
    var topic_id    = $('#topic_id').val();
    var post_id     = $('#post_id').val();
    
    $.post(forum_ajax + 'addpost.php', {post_text: post_text, topic_title: topic_title, forum_id: forum_id, topic_id: topic_id, post_id: post_id, editor_mode: editor_mode, name: form.name.value, mail: form.mail.value, sec_code: sec_code, skin: dle_skin}, function(data) { $('#ajax-post').html(data); $('#post_text').val(''); });
    
    HideLoading();
    
    return false;
}

// ********************************************************************************
// POST DEL
// ********************************************************************************
function postDelete(url) {
    DLEconfirm('Siz ismarıcı silmək istəyinizdən əminsiniz?', dle_confirm, function () { document.location = url; } );
};


// ********************************************************************************
// TOPIC DEL
// ********************************************************************************
function topicDelete(url) {
    DLEconfirm('Siz mövzunu silmək istəyinizdən əminsiniz?', dle_confirm, function () { document.location = url; } );
};

function rowDelete(url) {
    DLEconfirm('Siz ismarıcı silmək istəyinizdən əminsiniz?', dle_confirm, function () { document.location = url; } );
};

// ********************************************************************************
// TOPIC MENU
// ********************************************************************************
function TopicMenu( tid, forum_url, moderation ){

var menu=new Array();

if (moderation)
{
    menu[0]='<a href="' + forum_url + 'act=moderation&code=calc&tid=' + tid + '">Hamısın mövzuda hesabla</a>';
}

menu[1]='<a href="' + forum_url + 'act=subscription&code=add&tid=' + tid + '">Mövzuya abunə ol</a>';

menu[2]='<a href="' + forum_url + 'act=_topic&code=forward&tid=' + tid + '">Dostuna xəbər et</a>';

menu[3]='<a href="' + forum_url + 'act=_topic&code=print&tid=' + tid + '">Çap versiya</a>';

return menu;
};

// ********************************************************************************
// FORUM MENU
// ********************************************************************************
function ForumMenu( fid, moderation, forum_url ){

var menu=new Array();

if (moderation){

menu[0]='<a href="' + forum_url + 'showforum=' + fid + '&code=hidden">Bütün gizli mövzuları göstər</a>';

menu[1]='<a href="' + forum_url + 'act=getforum&code=calc&fid=' + fid + '">Hamısın forumda hesabla</a>';

}

menu[3]='<a href="' + forum_url + 'showforum=' + fid + '&code=today">Aktiv mövzular</a>';

menu[4]='<a href="' + forum_url + 'showforum=' + fid + '&code=noreply">Cavabsız mövzular</a>';

return menu;
};

// ********************************************************************************
// POST EDIT MENU
// ********************************************************************************
function PostEditMenu( pid, forum_url, page, post_n ){

var menu=new Array();

menu[0]='<a onclick="ajax_post_edit(\'' + pid + '\'); return false;" href="#">' + menu_short + '</a>';
menu[1]='<a href="' + forum_url + 'act=post&code=02&pid=' + pid + '&p=' + page + '&pn=' + post_n + '">' + menu_full + '</a>';

return menu;
};

// ********************************************************************************
// USER MENU
// ********************************************************************************
function FUserMenu( url, m_id, group, forum_url ){

var menu=new Array();

	menu[0]='<a href="' + site_dir + 'index.php?subaction=userinfo&user=' + url + '">' + menu_profile + '</a>';

	menu[1]='<a href="' + site_dir + 'index.php?do=pm&doaction=newpm&user=' + m_id + '">' + menu_send + '</a>';
	menu[2]='<a href="' + forum_url + 'act=getforum&code=user&n=' + url + '">İstifadəçinin mövzuların tap</a>';
    menu[3]='<a href="' + forum_url + 'act=posts&user=' + url + '">İstifadəçinin ismarıcların tap</a>';

	if (group == '1') {
		menu[4]='<a onclick="window.open(\'' + site_dir + dle_admin + '?mod=editusers&action=edituser&id=' + m_id + '\', \'User\',\'toolbar=0,location=0,status=0, left=0, top=0, menubar=0,scrollbars=yes,resizable=0,width=540,height=500\'); return false;" href="#">' + menu_uedit + '</a>';
	}

return menu;
};

// ********************************************************************************
// WARN ADD
// ********************************************************************************
function FWarn(action, user_id, post_id, forum_id)
{
	ShowLoading();
    
    $('#warn-form').remove();

    $('body').append("<div id='warn-form' title='Xəbərdarlıq' style='display:none'></div>");
    
    $.post(forum_ajax + 'warn.php', {user_id: user_id, action: 'get_form', forum_id: forum_id, post_id: post_id}, function(data)
    {
        $('#warn-form').dialog({
            autoOpen: true, width: 470,
            buttons: {
                "Rədd et": function() { $(this).dialog("close"); $("#warn-form").remove(); },
                "Daxil et": function()
                {
                    if ($('#cause').val())
                    {
                        ShowLoading();
                        
                        $.post(forum_ajax + 'warn.php', {user_id: user_id, action: action, forum_id: forum_id, post_id: post_id, cause: $('#cause').val(), type: $('#warn_type').val(), time: $('#warn_time').val(), skin: dle_skin}, function(data) {
                            $('#warn-' + post_id).html(data);
                            $(this).dialog("close"); $("#warn-form").remove();
                            HideLoading();
                        });
                    }
                }
            }
        });
        
        $('#warn-form').html(data);
    });
    
    HideLoading();

    return false;
};

// ********************************************************************************
// REPUTATION
// ********************************************************************************
function FRep(action, user_id, post_id, forum_id)
{
	DLEprompt('Reputasiyasının dəyişilmə səbəbin qeyd et:', '', 'Reputasiya', function(response)
    {
        if (response)
        {
            ShowLoading();
            
            $.post(forum_ajax + 'reputation.php', {user_id: user_id, action: action, forum_id: forum_id, post_id: post_id, cause: response, skin: dle_skin}, function(data) { $('#rep-' + post_id).html(data); });
            
            HideLoading();
        }
    });
    
    return false;
};

// ********************************************************************************
// NAVIGATION
// ********************************************************************************
function navigation(pages_count, url)
{
	DLEprompt('', '', 'Səhifəyə keç', function(page)
	{
		if (pages_count >= page)
		{
			window.location.href = url + page;
		}
	});
};

function PostLink(link)
{
	var message = window.location + '#post-' + link;
    
    DLEalert(message, 'Keçidi kopyala:');
};

// ********************************************************************************
// SELECT ID
// ********************************************************************************
function select_id( sid )
{	
	var saved = new Array();
	var clean = new Array();
	var add   = 1;
	
	tmp = document.modform.selected_id.value;
	
	if( tmp != "" )
	{
		saved = tmp.split(",");
	}
	
	for( i = 0 ; i < saved.length; i++ )
	{
		if ( saved[i] != "" )
		{
			if ( saved[i] == sid )
			{
				 add = 0;
			}
			else
			{
				clean[clean.length] = saved[i];
			}
		}
	}
	
	if ( add )
	{
		clean[ clean.length ] = sid;
	}
	
	newvalue = clean.join(',');
	
	document.modform.selected_id.value = newvalue;	
};

// ********************************************************************************
// SHOW HIDE
// ********************************************************************************
function ShowHide( name, open ){
	
	if ($('#' + name).is(':hidden') || open)
    {
        $('#' + name).show(1000);
    }
    else
    {
        $('#' + name).hide(1000);
    }
};

// ********************************************************************************
// POST PREVIEW
// ********************************************************************************
function PostPreview()
{
    var post_text   = '';
    var editor_mode = '';
    
    if (forum_wysiwyg == "1")
    {
        post_text   = $('#post_text').html();
        editor_mode = 'wysiwyg';
    }
    else
    {
        post_text = $('#post_text').val();
    }
    
    if (post_text == '')
    {
        DLEalert ( 'Bütün vacib sətirləri doldurun', 'Xəta' );
        return false
    }
    ShowLoading();
    
    $.post(forum_ajax + 'post.preview.php', {post_text: post_text, wysiwyg: forum_wysiwyg, skin: dle_skin}, function(data) { $('#post-preview').html(data); });
    
    HideLoading();
    
    return false;
}

// ********************************************************************************
// UPLOADS FORM
// ********************************************************************************
function uploadsform(open_url)
{
	ShowLoading();
    
    $("#uploads-form").remove();

    $("body").append("<div id='uploads-form' title='Serverə fayl yüklənməsi' style='display:none'></div>");
    
    $.post(forum_ajax + 'uploads.form.php', {open_url: open_url}, function(data)
    {
        $('#uploads-form').dialog({
            autoOpen: true, width: 470,
            buttons: {
                "Pəncərəni bağla": function() { $(this).dialog("close"); $("#uploads-form").remove(); }
            }
        });
        
        $('#uploads-form').html(data);
    });
    
    HideLoading();

    return false;
};

// ********************************************************************************
// FORUM INS
// ********************************************************************************
function forum_ins(name)
{
	var input = document.getElementById('forum-post-form').post_text;
	var finalhtml = "";
	
	if (forum_wysiwyg !== "1")
	{
		if (dle_txt!= "")
		{
			input.value += dle_txt;
		}
		
		else
		{
			input.value += "[b]"+name+"[/b],"+"\n";
		}
	}
	
	else
	{
		if (dle_txt!= "")
		{
			finalhtml = dle_txt;
		}
		
		else
		{
			finalhtml = "<b>"+name+"</b>,"+"<br />";
		}
		
		tinyMCE.execInstanceCommand('post_text', 'mceInsertContent', false, finalhtml, true);
	}
};

// ********************************************************************************
// CtrlEnter
// ********************************************************************************
function CtrlEnter(event, form)
{
	if((event.ctrlKey) && ((event.keyCode == 0xA)||(event.keyCode == 0xD)))
	{
		form.submit.click();
	}
};

// ********************************************************************************
// Copyright (c) 2011 DLE Files Group
// ********************************************************************************	
