/**
 * $Id: jquery.informationController.js 2281 2009-04-21 14:08:41Z Bjoern.Moenikes $
 **/

(function($){

  $.fn.extend({
    informationController : function(options)
    {
      if (!$.event._icCache) $.event._icCache = [];
      // initialise the controller with the relevant settings...
      options = $.extend(
      {
        id : '',
        $context : ''
      }
      , options
      );
        
      return this.each(
        function()
        {
          var $this = $(this);
          var alreadyExists = true;

          if (!this._icId) {
            this._icId = $.event.guid++;
            $.event._icCache[this._icId] = new InformationController(this);
            alreadyExists = false;
          }

          var controller = $.event._icCache[this._icId];
          controller.init(options);
        }
        )
    },

    icGetID : function()
    {
      var c = _getController(this[0]);
      if (c) {
        return c.getID();
      }
      return null;
    },
    icGetContext : function()
    {
      var c = _getController(this[0]);
      if (c) {
        return c.getID();
      }
      return null;
    },

    icToggleByURL : function(url, onSuccess, target, caption)
    {
      C.log("icToggleByURL");
      var id = '#'+$(this).attr("id");
      var $id = $(id);
      var con = id+'-container';
      var $con = $(con);

      var wrapOnSuccess = function(result)
      {
        $("#bookingArea").amGetSlideManager().slideToggle(id);
        if (onSuccess)
          onSuccess(result);
      }
      if (caption != undefined)
        $("div.header span.name", $con).text(caption);

      if ($con.is(':hidden'))
      {
        if (target)
          $(target).load(url, null, wrapOnSuccess);       
      }
      else      
        $("#bookingArea").amGetSlideManager().slideToggle(id);      
    },

    //                            -       -    pdc(response, header)         osc(response)
    icToggleByJSON : function (sendData, json, processDataCallback, onSuccessCallback, header)
    {
      var id = '#'+$(this).attr("id");
      var con = id+'-container';
      var $con = $(con);
      var $sm = getSlideManager();

      //      C.log("--------------");
      //      C.log(id);
      //      C.log(con);
      //      C.log("--------------");

      if ($con.is(':hidden'))
      {
        var res = getShowResponse();
        C.log(res);
        doJSON (json, sendData, function(response)
        {
          $("div.header span.name", $con).text(header);
          if (processDataCallback && $.isFunction(processDataCallback))
            response = processDataCallback(response, header);
          else // if there is no process routine, just display payload data
          {
            $("div.content", $con).text(response["data"]);
          }

          $sm.slideShow(id, function(){
            if (onSuccessCallback && $.isFunction(onSuccessCallback))
              onSuccessCallback(response);

          });
        }, res, res );
      }
      else
      {        
        $sm.slideHide(id);
      }
    },

    _icDestroy : function()
    {
    // TODO - implement this?
    }
  });

  // private internal function to cut down on the amount of code needed where we forward
  // dp* methods on the jQuery object on to the relevant DatePicker controllers...
  var _w = function(f, a1, a2, a3)
  {
    return this.each(
      function()
      {
        var c = _getController(this);
        if (c) {
          c[f](a1, a2, a3);
        }
      }
      );
  };

  function InformationController(ele)
  {
    // initial values...
    this.ele = ele;
    this.id				      =	$(this.ele).attr("id");
    //    this.name           = $(this.ele).attr("name");
    this.$context		  	=	$(this.ele);

    if (this.id=="" || this.name=="")
    {
      C.log("Error: InformationController() without id and/or name inited: ");
      C.log(this.ele);
    }
  };
  $.extend(
    InformationController.prototype,
    {
      init : function(s)
      {
        this.id = s.id;
        this.$context = $(s.id);
        var $close = $("#information2 .close a",this.$context);
        var closeClick = function(ev){
          var id = $(ev.target).parents("div[class*='from']").attr("id");
          $("#bookingArea").amGetSlideManager().slideToggle("#"+id);
        };
        $close.click(closeClick);
      },
      getID : function()
      {
        return this.id;
      },

      getContext : function()
      {
        return this.$context;
      }
    }
    );


  function _getController(ele)
  {
    if (ele._icId) return $.event._icCache[ele._icId];
    return false;
  };

  // make it so that no error is thrown if bgIframe plugin isn't included (allows you to use conditional
  // comments to only include bgIframe where it is needed in IE without breaking this plugin).
  if ($.fn.bgIframe == undefined) {
    $.fn.bgIframe = function() {
      return this;
    };
  };


  // clean-up
  $(window)
  .bind('unload', function() {
    var els = $.event._icCache || [];
    for (var i in els) {
      $(els[i].ele)._icDestroy();
    }
  });

})(jQuery);