// json.js
(function($){$.toJSON=function(o)
{if(typeof(JSON)=='object'&&JSON.stringify)
return JSON.stringify(o);var type=typeof(o);if(o===null)
return"null";if(type=="undefined")
return undefined;if(type=="number"||type=="boolean")
return o+"";if(type=="string")
return $.quoteString(o);if(type=='object')
{if(typeof o.toJSON=="function")
return $.toJSON(o.toJSON());if(o.constructor===Date)
{var month=o.getUTCMonth()+1;if(month<10)month='0'+month;var day=o.getUTCDate();if(day<10)day='0'+day;var year=o.getUTCFullYear();var hours=o.getUTCHours();if(hours<10)hours='0'+hours;var minutes=o.getUTCMinutes();if(minutes<10)minutes='0'+minutes;var seconds=o.getUTCSeconds();if(seconds<10)seconds='0'+seconds;var milli=o.getUTCMilliseconds();if(milli<100)milli='0'+milli;if(milli<10)milli='0'+milli;return'"'+year+'-'+month+'-'+day+'T'+
hours+':'+minutes+':'+seconds+'.'+milli+'Z"';}
if(o.constructor===Array)
{var ret=[];for(var i=0;i<o.length;i++)
ret.push($.toJSON(o[i])||"null");return"["+ret.join(",")+"]";}
var pairs=[];for(var k in o){var name;var type=typeof k;if(type=="number")
name='"'+k+'"';else if(type=="string")
name=$.quoteString(k);else
continue;if(typeof o[k]=="function")
continue;var val=$.toJSON(o[k]);pairs.push(name+":"+val);}
return"{"+pairs.join(", ")+"}";}};$.evalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);return eval("("+src+")");};$.secureEvalJSON=function(src)
{if(typeof(JSON)=='object'&&JSON.parse)
return JSON.parse(src);var filtered=src;filtered=filtered.replace(/\\["\\\/bfnrtu]/g,'@');filtered=filtered.replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g,']');filtered=filtered.replace(/(?:^|:|,)(?:\s*\[)+/g,'');if(/^[\],:{}\s]*$/.test(filtered))
return eval("("+src+")");else
throw new SyntaxError("Error parsing JSON, source is not valid.");};$.quoteString=function(string)
{if(string.match(_escapeable))
{return'"'+string.replace(_escapeable,function(a)
{var c=_meta[a];if(typeof c==='string')return c;c=a.charCodeAt();return'\\u00'+Math.floor(c/16).toString(16)+(c%16).toString(16);})+'"';}
return'"'+string+'"';};var _escapeable=/["\\\x00-\x1f\x7f-\x9f]/g;var _meta={'\b':'\\b','\t':'\\t','\n':'\\n','\f':'\\f','\r':'\\r','"':'\\"','\\':'\\\\'};})(jQuery);
// lexems.js
$(document).ready(function() {
	  var sel = "<select class='span-5' name='lexems' size=20 > \n";
	  $.each($("div#lexems a"), function(link_id, link) {
	    sel += '  <option value="'+$(link).attr("name")+'" >'+$(link).text()+'</option> \n';
	  });
	  sel += "</select>\n";
	  $("div#lexems").html(sel);
	  path = location.pathname.split("/")
	  if (path[1]=='show') {
	    val = decodeURIComponent(path[2]+"/"+path[3]);
	    $("div#lexems select option[value="+val+"]").attr("selected", "selected");
	  } else {
	    $("div#lexems select").attr("selectedIndex", 0);
	  }
	  $("select[name=lexems]").livequery("change", function() {
	    var lexem = $(this).val();
	    $.get(encodeURI("/show/"+lexem, ""), function(data) {
	      $("div#main_content").html(data);
	      tokenize();  // tokenize.js
	      //expand_tags();
		  //attachTabSynonyms();
	      $(document).attr("title", lexem.split("/")[0]+" | "+get_base_title());
	    });
	    return false;
	  })
	  $("h1.main_header").click(function() {
		 location.href="/"; 
	  });
	  $("span.logo").click(function(){
	  	 location.href="/";
	  });
});
// tokenize.js
$(document).ready(function() {
	  tokenize();
	  //expand_tags();
	});

function tokenize() {

  var to_tokenize = { meaning: [], example: [], idiom: [], derivation_example: [] };
  
  $.each($("div.meaning"), function(id, div) {
     var semem = $(div).text();
     semem = semem.replace("+", "%2B"); 
     to_tokenize.meaning.push( semem); 
  });
	 
  $.each($("div.example"), function(id, div) { 
     var clone = $(div).clone();
	 $(clone).find("span").remove();
     to_tokenize.example.push($(clone).text() ) });
	 
  $.each($("div.idiom"), function(id, div) { 
     to_tokenize.idiom.push($(div).text() ) });
  
  $.each($("div.derivation span.example"), function(id, div){
  	 to_tokenize.derivation_example.push($(div).text())  });
  
  q = $("input[name=q]").val()
  q_string = util_get_query();
  
  var search_base = location.pathname.split("/")[1];  
  
  $.post("/tokenize","text="+$.toJSON(to_tokenize)+"&where="+q_string['where']+
         "&position="+q_string['position']+
         "&highlight="+q+"&search_base="+search_base,
    function(tokenized){
      $.each($("div.meaning"), function(id, div) {
    	  $(div).html(tokenized.meaning[id]); 
          //$(div).children("a").removeClass("highlight");
      });
	  
      $.each($("div.example"), function(id, div) {
		  var span = $(div).find("span").get(0);

          $(div).html(tokenized.example[id]);
		  
		  $(div).append(span);
      });
	  
      $.each($("div.idiom"), function(id, div) { 
          $(div).html(tokenized.idiom[id]); 
      });
      
      $.each($("div.derivation span.example"), function(id, div) { 
          $(div).html(tokenized.derivation_example[id]); 
      });
	  
  }, "json");

  $.each($("div.derivation b span.definer"), function(id, div) {
	  var der_text = $(div).text();
	  token = der_text.split(" ")[0];
	  replaced = "<a href='/search/"+token+"?where="+q_string['where']+"' >"+token+"</a>";
	  
	  der_text= der_text.replace(token, replaced);
	  $(div).html(der_text);
	  
  });
  
  // collapse flexion
  var flx_span = $("div.flexion i");
  var old_flx = $.trim(flx_span.text());
  var old_flx_arr = old_flx.split(' ');
  var new_flx = ''; var i=0;
  while (new_flx.length < 10 && i<old_flx_arr.length) {
  	new_flx = new_flx +' '+ old_flx_arr[i];
	i=i+1;
  } 
  new_flx = $.trim(new_flx);
  if (new_flx!=old_flx) {
  	  console.debug(new_flx);
  	  console.debug(old_flx);
	  flx_span.html(new_flx + "  <a href='#' id='expand_flx' >+</a>");
	  $("a#expand_flx").livequery("click", function(){
	  	flx_span.html(old_flx);
	  });
  }
 
  
  
}


