var oldcontent;

function hcontent(word,content){
  var hc = content.replace(/([A-Z]\w+[A-Z]+\w+)\s+/g,'<a class="word" href="/wikiword/$1">$1</a> ');
  hc = hc.replace(/\n/g,' <br />');
  $('#content').html(content);
  return '<h2>' + word + '</h2><br />' + hc + ' <br /><a class="editr" href="/wiki/edit/'+word+'">Edit</a>' +
    '<br /><a id="backhome" href="/wiki">Home</a>';
}

function addlink(word,uri){
  var wido = $('#wido');
  var item = document.createElement('a');
  if (word == 'new'){
  	item.className = 'new';
    item.href = '/wiki/new';
  }else{
  	item.className = 'word';
    item.href = '/wikiword/'+uri;
  }
  item.innerHTML = word;
  wido.append(item);
  wido.append(document.createElement('br'));
}

function fireeditor(word,ww){
  $('#wido').hide();

  var iword,icontent,inew;
  if(word=='new'){
    iword = '<input type="text" id="wword" name="wword" value="" /><br />';
    icontent = '';
    inew = 'new';
  }else{
    iword = '<input type="text" id="wword" name="wword" readonly="readonly" value="'+word+'" />';
    icontent = $('#content').html();
    inew = 'old';
  }
  oldcontent = icontent;
  if(typeof(ww)!='undefined'){
  	icontent='';
  	inew = 'new';
  }

  var tmpdata = $('#tmpdata');
  tmpdata.html('<form id="frmedit" onsubmit="editSave('+
    "'"+inew+"','"+word+"');return false;"+'">'+iword+
    '<br /><textarea id="wcontent" name="wcontent">' + icontent +
    '</textarea><br /><input type="submit" value="save" />'+
    '<input type="reset" value="Cancel" onclick="cancelEdit();return false;" /></form>');
  tmpdata.show();
  return false;
}

function cancelEdit(){
  $('#wido').show();
  $('#tmpdata').html("");
  $('#wcontent').html(oldcontent);
}

function editSave(oldnew,word){
  var uri;
  if(oldnew == 'new'){ uri = "/wikinew"; }
  else{ uri = "/wikiupdate"; }
  //uri = "/wikiput";
  var wido=$('#wido');
  wido.empty();
  wido.html(hcontent($("#wword").val(),$("#wcontent").val()));
  wido.show();
  $('#tmpdata').hide();
  //dojo.lfx.html.highlight("wido", [0, 255, 0], 250).play(1000);
  prepareLinks();
  $.post(uri,{word:$("#wword").val(),content:$("#wcontent").val()},
      function(json){
      	if (json.ok==true){
      		//alert("good");
      	}else{
      		//alert("bad");
      	}
      },
      "json");
}

function wowi(uri) {
	//alert(uri);
  //node.show();
  $.getJSON(uri,function(json){
  	  if(json.content==''){
        if(confirm("The page '" + json.word + "' doesn't exist. Do you want to create it now?")==true){
        	fireeditor(json.word,1);
        }
      }
      else{
  		  var wido = $('#wido');
  		  wido.hide();
  		  wido.empty();
  		  wido.html(hcontent(json.word,json.content));
  		  prepareLinks();
  		  wido.slideDown();
  		}
    });
}

function prepareLinks() {
	  $("a.editr").click(function(){
        fireeditor($(this).attr("href").split('/').pop())
        return false;
      });
    $("a.word").click(function(){
        wowi($(this).attr("href"));
        return false;
      });
    $("a.new").click(function(){
    	fireeditor('new');
    	return false;
    	});
}
