function addReply(id, title, user) {
    if (id === undefined) {
        $('id_comments').value = '';
        $('in_reply').style.display = 'none';
        $('c_title').value = '';
    } else {
        $('id_comments').value = id;
        $('in_reply_txt').innerHTML = '<b>'+title+'</b> by ' + user+' &nbsp; <a href="javascript:addReply()">remove reply</a>';
        $('in_reply').style.display = '';
        $('c_title').value = 'Re: '+title;
    }
}


function cancelCommentEdit() {
	// restore box content
	document.getElementById('comment_box_' + document.editingCommentId).innerHTML = document.editingCommentBoxHTMLContent;
	
	// clear storage
	document.editingCommentBoxHTMLContent = '';
	document.editingCommentId = false;
}

function editComment(id) {
	// save comment box content
	// if one comment is editing currently
	if ( document.editingCommentId ) {
		// replace form with old content
		document.getElementById('comment_box_' + document.editingCommentId).innerHTML = document.editingCommentBoxHTMLContent;
	} 
	// store current comment
	document.editingCommentBoxHTMLContent = document.getElementById('comment_box_' + id).innerHTML;
	document.editingCommentId = id;
	// save comment Title and body
	commentTitle = document.getElementById('c_'+id+'_title').innerHTML;
	commentBody = new String( document.getElementById('c_'+id+'_body').innerHTML );
	
	// show form instead of comment block
	document.getElementById('comment_box_' + id).innerHTML = document.editCommentFormHTML;
	document.getElementById('comment_box_' + id).style.display = 'none';
	var editCommentForm = document.getElementById('editCommentForm');
	// replace back "<br>" to "\n"
	editCommentForm['body'].value = commentBody.replace(/(<br>)/ig,"\n");
	editCommentForm['title'].value = commentTitle;
	editCommentForm['comment_id'].value = id;
	
//	alert(editCommentForm['body'].value + ' ' + editCommentForm['title'].value + ' ' + editCommentForm['comment_id'].value);
	document.getElementById('comment_box_' + id).style.display = 'block';
}



function postCommentEdit(id_post) {
	new Ajax.Request('/action/post_comment?p='+id_post, {method:'post', parameters: $('editCommentForm').serialize(true), onSuccess:postCommentEditSuccess, onFailure:ajaxFailure});
}

/* AJAX: edit comment -------------------------------------------------------- */
function postCommentEditSuccess(t) {
    eval('var res = '+t.responseText);
    if (res.code) {
			// alert(res.code + ' ' + res.editedCommentId + ' ' + res.editedCommentTitle);
    	// put comment block on it's place
    	commentId = res.editedCommentId;
			document.getElementById('comment_box_' + commentId).innerHTML = document.editingCommentBoxHTMLContent;
			document.getElementById('c_'+commentId+'_title').innerHTML = res.editedCommentTitle;
			document.getElementById('c_'+commentId+'_body').innerHTML = res.editedCommentBody;
			// clear storage 
			document.editingCommentId = false;
			document.editingCommentBoxHTMLContent = '';
    } else {
        if (!res.msgs.length) {
            alert('Error on adding of comment.');
        } else {
            msg = '';
            for(var i=0; i<res.msgs.length; i++) {
                msg += res.msgs[i]+"\n";
            }
            alert(msg);
        }
    }
}



function ajaxFailure() {
    alert('Failure of ajax request!');
}


/* AJAX: hot post ----------------------------------------------------------- */
function postHotSuccess(t) {
    eval('var res = '+t.responseText);
    if (res.code) {
        $('hot_value_'+res.id_posts).innerHTML = $('hot_value_'+res.id_posts).innerHTML*1 + 1;
        $('hot_btn_'+res.id_posts).innerHTML = '<img src="/images/btn_plus_gray.gif" />';
    } else {
        alert('Error on hotting of the blog or story.');
    }
}



function postHot(id) {



    new Ajax.Request('/action/hot?p='+id, {method:'get', onSuccess:postHotSuccess, onFailure:ajaxFailure});



}







/* AJAX: delete comment ----------------------------------------------------- */



function postCommentDeleteSuccess(t) {



    eval('var res = '+t.responseText);



    if (res.code) {



        location.href = '/view/'+res.id_posts+'#comments';



        location.reload();



    } else {



        alert('Error on deleting of comment.');



    }



}



function postCommentDelete(id_post, id) {



    if (!confirm('Do you want to delete comment?')) {



        return;



    }



    new Ajax.Request('/action/delete_comment?p='+id_post+'&id='+id, {method:'get', onSuccess:postCommentDeleteSuccess, onFailure:ajaxFailure});



}







/* AJAX: add comment -------------------------------------------------------- */
function postCommentAddSuccess(t) {
    eval('var res = '+t.responseText);
    if (res.code) {
        location.href = '/view/'+res.id_posts+'#comments_form';
        location.reload();
    } else {
        if (!res.msgs.length) {
            alert('Error on adding of comment.');
        } else {
            msg = '';
            for(var i=0; i<res.msgs.length; i++) {
                msg += res.msgs[i]+"\n";
            }
            alert(msg);
        }
    }
}



function postCommentAdd(id_post) {
	new Ajax.Request('/action/post_comment?p='+id_post, {method:'post', parameters: $('newComment').serialize(true), onSuccess:postCommentAddSuccess, onFailure:ajaxFailure});
}







/* AJAX: wish list ---------------------------------------------------------- */



function wl_remove(type, id) {



    if (!confirm('Do you really want to remove this '+type+' from your watch list?')) {



        return;



    }



    new Ajax.Request('/action/wl_remove_'+type+'?id='+id, {method:'get', onSuccess:wl_remove_success, onFailure:ajaxFailure});



}



function wl_remove_success(t) {



    eval('var res = '+t.responseText);



    if (res.code) {



        var elId = 'wl_'+res.type+'_'+res.id;



        Element.hide(elId);



    } else {



        if (!res.msgs.length) {



            alert('Error on remove of an item from wacth list.');



        } else {



            msg = '';



            for(var i=0; i<res.msgs.length; i++) {



                msg += res.msgs[i]+"\n";



            }



            alert(msg);



        }



    }



}







function wl_add(type, id) {



    new Ajax.Request('/action/wl_add_'+type+'?id='+id, {method:'get', onSuccess:wl_add_success, onFailure:ajaxFailure});



}



function wl_add_success(t) {



    eval('var res = '+t.responseText);



    if (res.code) {



        if (res.type == 'user') {



            alert('User was successfully added to your watch list.');



        } else {



            alert('Tag was successfully added to your watch list.');



        }



        var elClass = 'wl_'+res.type+'_'+res.id;



        var els = $A(document.getElementsByClassName(elClass));



        for(i=0; i<els.length; i++) {



            Element.hide(els[i]);



        }







        elList = $('wl_'+res.type+'s_list');



        elNoList = $('wl_no_'+res.type+'s');



        if (elList) {



            if (elNoList) {



                Element.hide(elNoList);



            }



            var param = res.name;



            if (res.type == 'user') {



                param = res.id;



            }



 						elList.innerHTML += '<span id="'+elClass+'" class="nowrap"><a href="/search/?'+res.type+'='+encodeURIComponent(param)+'">'+res.name+'</a><a href="javascript:void(0);" onclick="wl_remove(\''+res.type+'\', '+res.id+')" class="plusIcon">-</a></span>';



        }



    } else {



        if (!res.msgs.length) {



            alert('Error on adding of an item to wacth list.');



        } else {



            msg = '';



            for(var i=0; i<res.msgs.length; i++) {



                msg += res.msgs[i]+"\n";



            }



            alert(msg);



        }



    }



}











/* AJAX: post management ---------------------------------------------------- */



function postDelete(id) {



    if (!confirm('Do you really want to remove this post?')) {



        return;



    }



    new Ajax.Request('/action/post_delete?p='+id, {method:'get', onSuccess:postDeleteSuccess, onFailure:ajaxFailure});



}



function postDeleteSuccess(t) {



    eval('var res = '+t.responseText);



    if (res.code) {



        if (location.href.indexOf('/view/') == -1) {



            location.reload();



        } else {



            history.back(-1);



        }



    } else {



        if (!res.msgs.length) {



            alert('Error on remove of the post.');



        } else {



            msg = '';



            for(var i=0; i<res.msgs.length; i++) {



                msg += res.msgs[i]+"\n";



            }



            alert(msg);



        }



    }



}







var lastPostId = null;



function postUpdate(id) {



    lastPostId = id;



    var elButton = $('submit_button');



    elButton.orig_value = elButton.value;



    elButton.value = 'Processing...';



    elButton.disabled = 1;



    



    //picQueueCheck()



    postUpdateProc(id);



}



function postUpdateProc(id) {



    new Ajax.Request('/action/post_update', {method:'post',  parameters: $('postForm').serialize(true), onSuccess:postUpdateSuccess, onFailure:ajaxFailure});



}



function postUpdateSuccess(t) {



    eval('var res = '+t.responseText);



    



    if (res.code) {



        location.href = '/view/'+res.id_posts;



    } else {



        if (!res.msgs.length) {



            alert('Error on update or add of the post.');



        } else {



            msg = '';



            for(var i=0; i<res.msgs.length; i++) {



                msg += res.msgs[i]+"\n";



            }



            alert(msg);



        }



    }



    



    var elButton = $('submit_button');



    elButton.disabled = 0;



    elButton.value = elButton.orig_value;



}















/* AJAX: WL search tags ----------------------------------------------------- */



function wlTagsSearchSubmit() {



    var url = '/action/search_tags?'+$('search_tags').serialize();



    new Ajax.Request(url, {



        method: 'get',



        onSuccess: function(resp) {



            $('search_tags_button').disabled = false; 



            wlTagsSearchResults(resp.responseText);



        },



        onFailure: function() {



            $('search_tags_button').disabled = false; 



            alert('Request failure!');



        },



        onLoading: function() {



            $('search_tags_button').disabled = true; 



        }



    });



    return false;



}    



function wlTagsSearchResults(response) {



    $('tag_results').innerHTML = response;



    $('tag_results').innerHTML += '<div class="search_results_close"><a href="javascript:void(0);" onClick="Effect.Fade(\'tag_results\', {duration:0.1});">[ close ]</a></div>';



     Effect.Appear('tag_results', {duration:0.1});



}







/* AJAX: WL search users ---------------------------------------------------- */



function wlUsersSearchSubmit() {



    var url = '/action/search_users?'+$('search_users').serialize();



    new Ajax.Request(url, {



        method: 'get',



        onSuccess: function(resp) {



            $('search_users_button').disabled = false; 



            wlUsersSearchResults(resp.responseText);



        },



        onFailure: function() {



            $('search_users_button').disabled = false; 



            alert('Request failure!');



        },



        onLoading: function() {



            $('search_users_button').disabled = true; 



        }



    });



    return false;



}    



function wlUsersSearchResults(response) {



    $('user_results').innerHTML = response;



    $('user_results').innerHTML += '<div class="search_results_close"><a href="javascript:void(0);" onClick="Effect.Fade(\'user_results\', {duration:0.1});Effect.Appear(\'search_users_details\', {duration:0.1})">[ close ]</a></div>';



    Effect.Fade('search_users_details', {duration:0.1});



    Effect.Appear('user_results', {duration:0.1});



}











/* Post Pictures ------------------------------------------------------------ */



var lastSlotId = 0;



var uploads = new Array();



function picAddSlot() {



    lastSlotId++;







    elForm = document.createElement('form');



    elForm.id = 'file_'+lastSlotId;



    elForm.enctype = 'multipart/form-data';



    elForm.encoding = 'multipart/form-data';



    elForm.action = '/uploader.php';



    elForm.method = 'post';



    elForm.target = 'uploadForm';



    



    elCell = document.createElement('td');



    elCell.appendChild(elForm);



    elRow = document.createElement('tr');



    elRow.id = elForm.id+'_row';



    elRow.appendChild(elCell);



    







    elField = document.createElement('input');



    elField.type = 'hidden';



    elField.name = 'el';



    elField.value = elForm.id;



    elForm.appendChild(elField);







    elFile = document.createElement('input');



    elFile.type = 'file';



    elFile.name = 'file';



    elFile.id = elForm.id+'_file';



    elFile.onchange = function() {



        if (!picIsAllowedExt(this.id)) {



            this.value = '';



        }



    }



    elForm.appendChild(elFile);







    if (uploads.length) {



        var els = document.getElementsByClassName('attach_button');



        for(i=0; i<els.length; i++) {



            el = els[i];



            Element.hide(el);



        }



    }



    elButton = document.createElement('input');



    elButton.type = 'button';



    elButton.value = uploads.length ? 'Attach All' : 'Attach'; 



    elButton.className = 'attach_button';



    elButton.style.marginLeft = '10px';



    elButton.style.fontSize = '95%';



    elButton.style.height = '100%';



    elButton.style.width = '70px';



    elButton.onclick = function () {



        picCheckQueue();



    }



    elForm.appendChild(elButton);







    elCell.id = elForm.id+'_fbox';



    



    elCell = document.createElement('td');



    elCell.style.display = 'none';



    elCell.id = elForm.id+'_loading';



    elCell.style.fontSize = '120%';



    elCell.style.fontWeight = 'bold';



    elCell.style.color = '#68b8ff';



    elCell.innerHTML = '<img src="/images/loading_16x16.gif" align="absmiddle" style="margin-right:10px;" /> <span style="vertical-align:middle;">Loading...</span>';



    elRow.appendChild(elCell);







    elCell = document.createElement('td');



    elCell.style.display = 'none';



    elCell.id = elForm.id+'_loaded';



    elCell.style.fontSize = '110%';



    elCell.style.paddingTop = '6px';



    elCell.style.color = '#2599ff';



    elCell.style.borderBottom = '1px dashed #65B7FF';



    elCell.innerHTML = 'Loaded';



    elRow.appendChild(elCell);







    uploads.push(elForm.id);



    



    $('new_entries').appendChild(elRow);



}



function picIsAllowedExt(el) {



    var filename = $F(el);



    if (filename == "") {



        return 1;



    }



    if (!filename.match(/(\.jpeg)|(\.jpg)|(\.gif)|(\.png)$/i)) {



        alert("Sorry, only default image files are allowed: jpeg, gif, png.");



        return 0;



    }



    return 1;



}











function picCheckQueue() {



    if (el = uploads.shift()) {



        r = picUploadFile(el);



        if (!r) {



            picCheckQueue();



        }



    } else {



        picAddSlot();



    }



}



function picUploadFile(el) {



    if ($F(el+'_file') == '') {



        Element.hide(el+'_row');



        return 0;



    }



    



    Element.hide(el+'_fbox');



    Element.show(el+'_loading');



    



    $(el+'_loaded').innerHTML += ' ('+$F(el+'_file')+')';



    $(el).submit();







    return 1;



}



function picUploadHandler(el, state, file, size) {



    Element.hide(el+'_loading');



    if (state != 'ok') {



        $(el+'_loaded').innerHTML = 'Error uploading';



        alert('Error uploading, check size of photo!');



    } else {



        var elForm = $('postForm');



        elField = document.createElement('input');



        elField.type = 'hidden';



        elField.name = 'new_files[]';



        elField.value = file;



        elForm.appendChild(elField);



        var sSize = '<div style="float:right">'+size+'Kb</div>';



        $(el+'_loaded').innerHTML = sSize + $(el+'_loaded').innerHTML;



    }



    Element.show(el+'_loaded');



    picCheckQueue();



}




