// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

Ajax.Responders.register({
  onCreate: function() {

	enable_loader();
  },
  onComplete: function() {
	  init_links();
	  disable_loader();
  }
});

function pt(el1, el2)
{
	new Effect.toggle(el1,'appear',{afterFinish:function(){ }});
	if(el2){
		new Effect.toggle(el2,'appear',{afterFinish:function(){ }});	
	}	
}


Event.observe(window, 'load', function() {
	init_links();
})

function init_links()
{
	$$('.inside').each(function(el){
		add_nj_link(el);
	});
}

var loading = false;

function nj_to(url)
{
	if(loading==true)
	 return '' // already loading
	 
	loading = true;
	$('loading').innerHTML=url;
	new Ajax.Updater('content', url, {method: 'get', evalScripts: 'true'})	
	
}

function enable_loader(url)
{
	
   
   $('loader').show();
}

function disable_loader()
{
	$('loader').hide();
	
	loading = false;
}


function add_nj_link(element)
{	
	//new_element = new Element('a', { src: '#', onClick: "return false;" });
	
	//element = Element.replace(element, "<a href='#' onClick='return false;' class='"+element.className+"'>"+element.innerHTML+"</a>");
	
	Event.observe(element, 'click', function(){		
		nj_to(element.href);
		
	})
}

var cleared = false;

function clear_search(id)
{
	if(cleared)
		return;
	
	$(id).value="";
	var cleared = true;
}

var domWrite = (function(){            // by Frank Thuerigen
	 // private 

	 var dw = document.write,              // save document.write()
	          myCalls = [],                // contains all outstanding Scripts
	          t = '';                      // timeout
	 
	 function startnext(){                 // start next call in pipeline
	  if ( myCalls.length > 0 ) {
	   if ( Object.watch ) console.log( 'next is '+myCalls[0].f.toString() );
	   myCalls[0].startCall();
	   }
	  }

	 function evals( pCall ){            // eval embedded script tags in HTML code
	  var scripts = [],
	      script,
	      regexp = /<script[^>]*>([\s\S]*?)<\/script>/gi;
	  while ((script = regexp.exec(pCall.buf))) scripts.push(script[1]);
	  scripts = scripts.join('\n');
	  if (scripts) {
	   eval(scripts);
	   }
	  }

	 function finishCall( pCall ){
	   pCall.e.innerHTML = pCall.buf;             // write output to element
	   evals( pCall );
	   document.write=dw;                        // restore document.write()
	   myCalls.shift();
	   window.setTimeout( startnext, 50 );
	   }

	 function testDone( pCall ){
	   var myCall = pCall;
	   return function(){
	    if ( myCall.buf !== myCall.oldbuf ){
	     myCall.oldbuf = myCall.buf;
	     t=window.setTimeout( testDone( myCall ), myCall.ms );
	     }
	    else {
	     finishCall( myCall );
	     }
	    }
	   }  
	   
	 function MyCall( pDiv, pSrc, pFunc ){                    // Class
	  this.e = ( typeof pDiv == 'string' ? 
	             document.getElementById( pDiv ) :
	             pDiv ),                     // the div element
	  this.f = pFunc || function(){},
	  this.stat = 0,                         // 0=idle, 1=waiting, 2=running, 3=finished
	  this.src = pSrc,                       // script source address
	  this.buf = '',                         // output string buffer
	  this.oldbuf = '',                      // compare buffer
	  this.ms = 100,                         // milliseconds
	  this.scripttag;                        // the script tag 
	  }
	 
	 MyCall.prototype={
	  startCall: function(){
	   this.f.apply( window );                 // execute settings function
	   this.stat=1;
	   var that = this;                            // status = waiting
	   document.write = (function(){
	    var o=that,
	        cb=testDone( o ),
	        t;
	    return function( pString ){            // overload document.write()
	     window.clearTimeout( t );
	     o.stat=2;                             // status = running
	     window.clearTimeout(t);
	     o.oldbuf = o.buf;
	     o.buf += pString;                     // add string to buffer
	     t=window.setTimeout( cb, o.ms );
	     };
	    })();
	   var s=document.createElement('script');
	   s.setAttribute('language','javascript');
	   s.setAttribute('type','text/javascript');
	   s.setAttribute('src', this.src);
	   document.getElementsByTagName('head')[0].appendChild(s);
	   }
	  }
	  
	 return function( pDiv, pSrc, pFunc ){  // public
	  var c = new MyCall( pDiv, pSrc, pFunc );
	  myCalls.push( c );
	  if ( myCalls.length === 1 ){
	   startnext();
	   }
	  }
	 })();
	 
function save_pinboard()
{
	new Ajax.Updater('pinboard_form', '/pinboard/create', {
	  method: 'get',
	  evalScripts: 'true',
	  parameters: $('pboard_form').serialize()
	});
}

function load_lyric(id)
{
	new Ajax.Updater('text', '/lyrics/lyricwiki/'+id, {method: 'get', evalScripts: 'true'})
}

function delete_pinboard(id)
{
	new Ajax.Updater('pinboard_entry_'+id, '/pinboard/delete/'+id, {method: 'get', evalScripts: 'true'})
}	 