/*global jQuery, document, setTimeout, EventStack */
/*members addListener, bind, click, each, event, executeStack, fn, html, 
    key, length, message, origin, params, publish, push, queue, 
    removeListener, subscribe, trigger, type, unbind, val, unSubscribe
*/
$(function () {
  /**
   * Setup the event stack
   */
  // EventStack.executeStack();
  
  // $('#ui form').restify();
  
  $('#loginfoo').dialog({  
    autoOpen: false,
    draggable: false,
    resizable: false
  });
  
  $('#signinlink').click(function (e) {  
    var self = $(this);

    $('#loginfoo').dialog('option', 'position', [e.pageX - 295, e.pageY + 10]).dialog('open');
    
    return false;
  });
  
});

// Plugin Dir
(function ($) {

  $.extend($.ui.tinkyWindow, {  
    defaults: {    
      width: 300,
      height: 100,
      top: 0,
      left: 0,
      open: false
    }        
  });
  
  $.widget('ui.tinkyWindow', {  
    _init: function () {
      this.element
        .wrap('<div id="tinkyWindow"></div>');
      
      $('#tinkyWindow')
        .append('<div class="ui-overlay"><div class="ui-widget-overlay"></div><div class="ui-widget-shadow ui-corner-all"></div></div>');
        
      $('#tinkyWindow .ui-widget-shadow').css({      
        width: 300,
        height: 100,
        position: 'absolute',
        left: this.options.left,
        top: this.options.top
      });
      
      this.element
        .addClass('ui-widget ui-widget-content ui-corner-all')
        .css({        
          position: 'absolute',
          width: 288,
          height: 88,
          top: this.options.top,
          left: this.options.left,
          padding: 10
        });
      
    },
    
    destroy: function () {    
      $('#tinkyWindow').remove();
    },
    
    open: function () {    
      
    },
    
    close: function () {    
      
    }
  });
  
  $.fn.publish = function (msg, payLoad) {    
    return this.each(function () {        
      $(this).trigger({
        type: 'app:event',
        params: payLoad,
        message: msg,
        origin: this 
      });
    });    
  };
  
  $.fn.subscribe = function (message, callBack) {
    return this.each(function () {
      EventStack.addListener(this, message, callBack);
      
      return this;
    });
  };
  
  $.fn.unSubscribe = function (message) {
    EventStack.removeListener(this, message);
    
    return this;
  };
  
  /*
   * Makes all forms rest-alike
  */
  $.fn.restify = function (options) {            
    return this.each(function () {
      var self = $(this);
      var method = self.attr('method') || 'get';
      
      if ($.inArray(method, ['put', 'delete']) ){
        method = 'post';
      }
      
      self.submit(function () {
        $('<input type="hidden" name="_method">')
          .attr('value', method)
          .prependTo($(this));
          
        return false;
      });
    });
  };
  
})(jQuery);

// Code dir
var EventStack = EventStack || {};  
(function () {

  var stack = [];
  
  EventStack.addListener = function (element, message, callBack) {
    var handle = {key: element, val: callBack};
    
    if (stack.length > 0) {
      $.each(stack, function (i, item) {
        if (item.event == message) {
          stack[i].queue.push(handle);
        }
        
      });
      
    } else {
      stack = [{event: message, queue: [handle]}];
      
    }
    
    
  };
  
  EventStack.removeListener = function (element, message) {    
    $.each(stack, function (i, item) {
      var queue = [];
      if (item.event == message) {        
        queue = $.grep(item.queue, function (q) {
          return q.key != element;
        });
        
        item.queue = queue;
      }
    });    
  };
  
  EventStack.executeStack = function () {
    setTimeout(function () {
      $(document).bind('app:event', function (event) {
        var queue = [];
        
        $.each(stack, function (i, item) {
          if (item.event == event.message) {
            queue = item.queue;
          }
        });
        
        if (queue.length > 0) {
          $.each(queue, function (i, item) {
            item.val(event.params);            
          });
        }
        
        $(document).unbind('app:event');
        
        return false;
      });
      
      EventStack.executeStack();
    }, 100);
  };  
  
})();
