﻿/// <reference path="~/js/jquery-1.3.2.js" />

(function($)
{
  $.fn.iContentPopup = function(o)
  {
    return this.each(function()
    {
      new $jc(this, o);
    });
  };

  var defaults = {
    speed: "slow",
    position: "center",
    initCallback: null,
    closeCallback: null
  };

  $.iContentPopup = function(e, o)
  {
    this.options = $.extend({}, defaults, o || {});
    this.container = $(e);
    this.blnOpen = false;
    this.contentcontainer = $("#contentcontainer", this.container);

    var self = this;

    // close 'button'
    $(".closewindow a").click(function()
    {
      self.close();
    });

    // define initial position and be aware that parent can be resized...
    this.resize();
    if (this.options.position == "center")
    {
      this.funcResize = function() { self.resize(); };
      $(window).unbind('resize', this.funcResize).bind('resize', this.funcResize);
    }

    // tell the world about this new born
    if (this.options.initCallback != null)
      this.options.initCallback(this, 'init');
  };

  // Create shortcut for internal use
  var $jc = $.iContentPopup;

  $jc.fn = $jc.prototype = {
    iContentPopup: '0.1'
  };

  $jc.fn.extend = $jc.extend = $.extend;

  $jc.fn.extend(
	{
	  close: function()
	  {
	    if (this.blnOpen)
	    {
	      this.blnOpen = false;
	      this.container.fadeOut("slow");
	      if (this.options.closeCallback != null)
	        this.options.closeCallback(this);

	    }
	  },

	  open: function(strFile)
	  {
	    if (strFile != undefined)
	    {
	      var self = this;

	      $.ajax(
				{
				  method: "get",
				  url: strFile,
				  dataType: "html",
				  success: function(strHtml)
				  {
				    self.contentcontainer.html(strHtml);
				  },
				  error: function()
				  {
				    self.contentcontainer.html("Er is een fout opgetreden.");
				  }
				});




	    }
	    if (!this.blnOpen)
	    {
	      this.blnOpen = true;
	      this.container.fadeIn("slow");
	    }
	  },

	  resize: function()
	  {
	    if (this.options.position == "center")
	    {
	      var intWindowWidth = document.documentElement.clientWidth;
	      var intWindowHeight = document.documentElement.clientHeight;
	      var intPopupHeight = this.container.height();
	      var intPopupWidth = this.container.width();
	      //centering
	      this.container.css({
	        "position": "absolute",
	        "top": intWindowHeight / 2 - intPopupHeight / 2,
	        "left": intWindowWidth / 2 - intPopupWidth / 2
	      });
	    }

	  }


	});
})(jQuery);