
/*
 =============================================================
 $Author: Tom Potter
 $Id$
 =============================================================
*/

var timers = new Array();  
var base_url = ($("base").attr("href") || '');

$.fn.loader = function() {
  // INTIALISE AUTOLOAD OBJECT
  var autoload = {
    id: 't' + Math.random(),
    loaded: false,
    obj: $(this),
    url: base_url + 'client.php',
    data: $(this).attr('hash').replace('#!', ''),
    remove: $(this).hasClass('remove'),
    modal: $(this).hasClass('modal'),
    timer: $(this).hasClass('timer'),
    timer_id: null,
    timer_duration: 5000,
    timer_action: function() {
      console.log(this);
      ///this.obj.loader();
      
      // REMOVE OLD
      $('#'+this.id).remove();
      // ATTACH AND TRIGGER
      this.obj.loader();
      this.obj.trigger('click');
      
      
      //if(this.time_id == null) { }
      //alert(this);
      //if(this.timer_id != null) { 
//        clearTimeout(this.timer_id);
      //}
      //this.timer_id = window.setTimeout("alert('asdasd')", this.timer_duration);
      
      ///this.timer_id = window.setTimeout(this.timer_action(), this.timer_duration);
      
    },
    closed: {
      //'background' : 'transparent url(http://subversible.com/websvn/drupal/trunk/themes/garland/images/menu-collapsed.gif) no-repeat scroll 0 50%',
      //'padding-left' : '1.2em',
    },
    open: {
      //'background' : 'transparent url(http://subversible.com/websvn/drupal/trunk/themes/garland/images/menu-expanded.gif) no-repeat scroll 0 50%'
    },
    active: {
      //'background' : 'transparent url(http://subversible.com/websvn/drupal/trunk/misc/throbber.gif) no-repeat 0px -16px'
    }
  };
  // BIND XLOAD EVENTS
  $(this).one('click', function() {
    $.ajax({
      type: 'GET',
      timeout: 10000,
      url: autoload.url,
      data: autoload.data,
      beforeSend: function(XMLHttpRequest) {
      	autoload.obj.css(autoload.active);
      	autoload.obj.click(function() { return false; });
        if(autoload.modal) {
          $.blockUI();
        }      	
      },
      complete: function(XMLHttpRequest, textStatus) {
      	autoload.obj.css((autoload.loaded == true) ? autoload.open : autoload.closed);
      	if(textStatus == 'timeout') {
      	  autoload.obj.loader(); // REBIND ON TIMEOUT
      	}
      },
      success: function(data) {
      	if(typeof(data) == 'object') {
      	  data = (typeof(data.error) != 'undefined') ? data.message : data.toSource();
        }
        if(data != '') { 
          autoload.loaded = true;
          // REBIND MORE LOAD 'live()'
          if($('a.autoload',data).length > 0) {
            data = $(data);
            $('a.autoload', data).each(
              function() { $(this).loader(); }
            );
            $('a.exec', data).each(
              function() { $(this).trigger('click'); }
            );
          }
          // WHAT TO DO WITH THE RESULTS
          if(autoload.modal) {
          	$.unblockUI;
            $.blockUI({
            	message: data,
            	css: { 
                overflow: 'auto',
                top:  '40px', 
                left: '40px', 
                width: ($(window).width() - 80) + 'px', 
                height: ($(window).height() - 80) + 'px'
              }
            });
            $('div.blockPage').html(data).click($.unblockUI);
            $('.blockOverlay').attr('title','Click to unblock').click($.unblockUI);
          }
          else {
            //var result = $('<div></div>').attr('id', autoload.id).html(data).hide();
            var result = $('<div></div>').html(data).hide();
            autoload.obj.toggle(
              function() { 
              	/* console.log(autoload); */
              	$(this).css(autoload.open);
              	if(autoload.timer) {
                  console.log('timermode');
                  console.log(autoload.timer_id);
                  //autoload.timer_action();
                  
                  if(autoload.timer_id == null) {
                    autoload.timer_id = window.setTimeout('timers[1].timer_action()', autoload.timer_duration);
                    timers[1] = autoload;
                  }
                  
                  //autoload.obj.loader(); // REBIND ON TIMEOUT
                  //$(this).delay(100000).loader();
                  //$(this).delay(100000).trigger('click');
                  //$(this).removeClass('timer');
                }
              },
              function() { 
              	/* console.log('closed'); */
                $(this).css(autoload.closed);
              }
            );
            autoload.obj.click(function() {
              $(this).after(result.toggle());
              return false;
            }).trigger('click');
          }
        }
        if(autoload.remove) {
          autoload.remove = false;
          autoload.obj.remove();
        }
      },
      error: function() { }
    });
    return false;
  }).css(autoload.closed);
}

$(document).ready(function() {

  $('a.autoload').each(function() {
    $(this).loader();
  });

  $('a.exec').each(function() {
    $(this).trigger('click');
  });

})


