/*
 * Copyright (c) 2007 yovisto.com - Search Web Search App. All Rights Reserved.
 * Autor: Joerg Waitelonis
 */

///////////////////////////////////
///  SHOW/HIDE WIKIMENU
///////////////////////////////////

function wikiMenuHideAll(){
  try{
    $('wikiArticle').style.display='none';
    $('wikiArticleEdit').style.display='none';
    $('wikiComments').style.display='none';
    $('wikiCommentsEdit').style.display='none';
    $('wikiHistory').style.display='none';
    $('wikiHistoryItem').style.display='none';
   }catch(e){;}
   finally{
    $('wikiArticle').style.display='none';
    $('wikiComments').style.display='none';
    $('wikiHistory').style.display='none';
    $('wikiHistoryItem').style.display='none';
    }

}

function wikiShowArticle(){
    wikiMenuHideAll();
    $('wikiArticle').style.display='inline';
}
function wikiShowArticleEdit(){
    wikiMenuHideAll();
    startEditor();
    $('wikiArticleEdit').style.display='inline';    
}
function wikiShowComments(){
    wikiMenuHideAll();
    $('wikiComments').style.display='inline';
}
function wikiShowCommentsEdit(){
    wikiMenuHideAll();
    startEditor();
    $('wikiCommentsEdit').style.display='inline';
}
function wikiShowHistory(){
    wikiMenuHideAll();
    $('wikiHistory').style.display='inline';    
}
function wikiShowHistoryItem(){
    wikiMenuHideAll();
    $('wikiHistoryItem').style.display='inline';    
}

///////////////////////////////////
// LOAD WIKI
///////////////////////////////////
var wiki_object=null;
function wikiAjaxLoadWiki(id, showmode, object){
    wiki_object=object;
    var url = '/WikiPageAjax;jsessionid=' + jsid;

	var pars = 'id=' + id+ '&load=1&showmode='+showmode+'&object='+wiki_object ; 

    var myAjax = new Ajax.Request( url, {
	        method: 'get', 
	        parameters: pars, 
	 	    onComplete: wikiAjaxLoadWikiCallback
	       });
}

function wikiAjaxLoadWikiCallback(transport){
    // clear editors
    oFCKeditor=null;
	oFCKeditor2=null;
    
    var msg = transport.responseText;
    
    $("wikiPage").innerHTML = msg; 

    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }       
    
    if ($('wikiArticleText')!=null && $('wikiCommentsText')!=null){
        startEditor();
    }
    
}

///////////////////////////////////
// SAVE WIKIPAGE  
///////////////////////////////////


/* btw. savecomment und savearticle sind fast gleich, 
*  es wird jedesmal beides übergeben und abgespeichert
*  nur in der callback Funktion wird ein anderer Darstellungsmodus gewählt
*/

function wikiAjaxSaveArticle(){
    if ($('commitMessageArticle').value.trim()!="" ){
          
		var id=  "id="+$('wiki_id').value;

        var text = FCKeditorAPI.GetInstance('wikiArticleText').GetXHTML( true );
        var comment= FCKeditorAPI.GetInstance('wikiCommentsText').GetXHTML( true );
        var message= $('commitMessageArticle').value;
            
        var url = '/WikiPageAjax;jsessionid=' + jsid ;
        
		var pars = id+ '&save=1&article='+_urlEscape(text)+'&comment='+_urlEscape(comment)+"&message="+_urlEscape(message)+"&comment_edit=0&object="+wiki_object ; 

        var myAjax = new Ajax.Request( url, {
	        method: 'post', 
	        parameters: pars, 
	 	onComplete: wikiAjaxSaveArticleCallback
	       });
    }else{
     $('commitMessageArticle').style.background = '#FFD5D5';
    }
}

function wikiAjaxSaveArticleCallback(transport){
    var msg = transport.responseText;
    
    //$("wikiPage").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }
    wikiAjaxLoadWiki($('wiki_id').value, 'article', wiki_object);  
}



function wikiAjaxSaveComment(){

    if ($('commitMessageComments').value.trim()!="" ){
    
    var id=  "id="+$('wiki_id').value;
	
        //var text = tinyMCE.getContent('wikiArticleText');
        //var comment= tinyMCE.getContent('wikiCommentsText');
        var text = FCKeditorAPI.GetInstance('wikiArticleText').GetXHTML( true );
        var comment= FCKeditorAPI.GetInstance('wikiCommentsText').GetXHTML( true );
        var message= $('commitMessageComments').value;

        var url = '/WikiPageAjax;jsessionid=' + jsid;
        
	var pars = id+ '&save=1&article='+_urlEscape(text)+'&comment='+_urlEscape(comment)+"&message="+_urlEscape(message)+"&comment_edit=1&object="+wiki_object ; 
	
        var myAjax = new Ajax.Request( url, {
	        method: 'post', 
	        parameters: pars, 
	 	    onComplete: wikiAjaxSaveArticleCallback
	       });

    }else{
     $('commitMessageComments').style.background = '#FFD5D5';
    }
}

function wikiAjaxSaveCommentCallback(transport){
    var msg = transport.responseText;
    
    //$("wikiPage").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }
    wikiAjaxLoadWiki($('wiki_id').value, 'comment', wiki_object);
}


///////////////////////////////////
// EDITOR
///////////////////////////////////

var oFCKeditor=null;
var oFCKeditor2=null;

function startEditor(){
	//alert("editor:" + oFCKeditor);
	if (oFCKeditor==null){
		oFCKeditor = new FCKeditor( 'wikiArticleText' ) ;
		oFCKeditor.BasePath = "/js/fckeditor/" ;
		oFCKeditor.Height='800';
		oFCKeditor.ReplaceTextarea() ;
	}

	if (oFCKeditor2==null){
		oFCKeditor2 = new FCKeditor( 'wikiCommentsText' ) ;
		oFCKeditor2.BasePath = "/js/fckeditor/" ;
		oFCKeditor2.Height='800';
		oFCKeditor2.ReplaceTextarea();
	}
    
    /*try{
        tinyMCE.execCommand('mceRemoveControl', false, "wikiArticleText");
        tinyMCE.execCommand('mceRemoveControl', false, "wikiCommentsText"); 
    }catch(e){

    }
    tinyMCE.idCounter=0;
    tinyMCE.execCommand('mceAddControl', false, "wikiArticleText");
    tinyMCE.execCommand('mceAddControl', false, "wikiCommentsText");     
   */
}


///////////////////////////////////
// HISTORY
///////////////////////////////////



function loadRevision(rev){
    wikiMenuHideAll();
    $('wikiHistoryItem').style.display='inline';
    wikiAjaxGetRevision(rev);
}

function wikiAjaxGetRevision(rev){

        var url = '/WikiPageAjax;jsessionid=' + jsid;
	var pars = 'rev=' + rev; 
	
        var myAjax = new Ajax.Request( url, {
	        method: 'get', 
	        parameters: pars, 
	 	    onComplete: wikiAjaxGetRevisionCallback
	       });
}

function wikiAjaxGetRevisionCallback(transport){

    var msg = transport.responseText;
    
    $("wikiHistoryItem").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }
}


///////////////////////////////////
// WIKI DIFF 
///////////////////////////////////

function wikiShowDiff(){
    wikiMenuHideAll();
    $('wikiHistoryItem').style.display='inline';

    from = checkRadioFrom();
    to = checkRadioTo();

    wikiAjaxGetDiff(from, to);
}

function checkRadioFrom(){
        for (i=0; i<$('formDiff').radioDiffFrom.length; i++){
			if($('formDiff').radioDiffFrom[i].checked == true){
				return $('formDiff').radioDiffFrom[i].value;
			}
	}
}
function checkRadioTo(){
        for (i=0; i<$('formDiff').radioDiffTo.length; i++){
			if($('formDiff').radioDiffTo[i].checked == true){
				return $('formDiff').radioDiffTo[i].value;
			}
	}
}

function wikiAjaxGetDiff(from, to){

        var url = '/WikiPageAjax;jsessionid=' + jsid;
	var pars = 'from=' + from +'&to='+to; 
	
        var myAjax = new Ajax.Request( url, {
	        method: 'get', 
	        parameters: pars, 
	 	    onComplete: wikiAjaxGetDiffCallback
	       });
}

function wikiAjaxGetDiffCallback(transport){

    var msg = transport.responseText;
    
    $("wikiHistoryItem").innerHTML = msg; 
    if (msg == "false"){
        // nothing
    } else {
        // nothing
    }
}

