/* 
 * ajax.js
 * Derived from structured ajax handling within ECom V1.
 * This script attempts to deliver a standard way of handling ajax
 * button events and the returning of content, including a standard
 * for falling back to non-ajax events when javascript is not available.
 */

jQuery(document).ready(function(){
  
  /*
   * To be correctly set up by this function, ajaxable links should include a 
   * number of querystring parameters which are recognised by the back-end.
   * 
   * panel - This is the DIV ID we wish to push returned content into, it is also
   * used in the back end to determine how the ajax request should be handled.
   * 
   * action - Optional, for a back end which handles multiple actions per panel
   * you will need to specify which ajax action it should carry out.
   * 
   * append true/false - optional, if you wish to append the result to the panel
   * 
   * 
   */
  setup_buttons();
  
});

// Necessary functions

function setup_buttons()
{
  $('.ajax_button').live('click',
    function(e){
      e.preventDefault();
      var url     = jQuery(this).attr('href');
      var target  = getQSKey(url,'panel');
      var append  = ( getQSKey(url,'append') == 'true' ) ? true : false;

      url         = url.replace('?','?ajax=true&'); // Inject the ajax property into the URL, to inform the back-end we're sending an ajax request

      loadPanel(target,url,append); // Load URL contents into target panel
     }
  );		
    
  $('.buttonYes').live('click',function(e){
      e.preventDefault();
      var href = $(this).attr('href');
      href += '&ajax=true';
      $(this).parent().parent().find('.ajax_helpful').load(href,function(responseText){$(this).html(responseText);});
      $(this).parent().fadeOut();
    });
			
  $('.buttonNo').live('click',function(e){
      e.preventDefault();
      var href = $(this).attr('href');
      href += '&ajax=true';
      $(this).parent().parent().find('.ajax_helpful').load(href,function(responseText){$(this).html(responseText);});
      $(this).parent().fadeOut();
    });	
}

function getQSKey(url,key)
{
	var params = url.replace('?','&').split('&');
		for(var x = 0;x < params.length;x++)
		{
			var param = params[x];
			if(param.substring(0,key.length) == key)
				{
					return unescape(param.substring(key.length+1,param.length));
				}
		}
}

/*
 * This function assumes that all ajaxable buttons in the loaded content
 * will be handled by an appropriate live() binding
 */
function loadPanel(target,url,append)
{
  if( append ) // We specifically want the results of this ajax call to be appended to the target
  {
    jQuery.get(url,function(data){jQuery(data).appendTo('#'+target);});
  }
  else
  {
    $('#'+target).load(url,'',function(){});
  }
}

function replacePanel(target,url)
{
  jQuery.get(url,function(data){jQuery('#'+target).replaceWith(data);});
}



jQuery.fn.moduleRefresh = function() {
  return jQuery(this).each(function(){
    
    if( jQuery(this).attr('class') )
    {

      var target = this;

      var moduleRef = jQuery(this).attr('class').replace('-','/');

      if( moduleRef.indexOf( 'module/' ) == 0 )
      {
        var url = '/ajax/' + moduleRef + '/?ajax=true';

        jQuery.get(url,function(data){
          jQuery(target).replaceWith(data);
        });
      }
    
    }
  });
}
