(function($) {
    $.fn.holePuncher = function(options) {
        var date = new Date();
        var timestamp = date.getTime();
        var defaults = {
            pageName: '',
            dataString: '',
            targetElement: 'none',
            containerClass: 'hpContainer',
            containerID: timestamp,
            ajaxMethod: 'GET',
            beforeLoad: function() {},
            beforeAjax: function() {},
            afterAjax: function() {},
            afterLoad: function() {},
            frameAttributes: '',
            wrapAttributes: '',
            fluidSlider: false
        };
        var opts = $.extend(defaults, options);
        return this.each(function() {
            var $this = $(this);
            bindClick($this);
        });
        function bindClick(thisObj) {
            $(thisObj).click(function() {
                opts.beforeLoad(this);
                
                var $holeContainer = $('<div />').attr('class', opts.containerClass).attr('id', opts.containerID)
                if(opts.targetElement == 'none') { var targetHash = $(this).attr('rel'); } else { var targetHash = opts.targetElement; 
                }
                $(targetHash).append($holeContainer);
                
                
                dataString = 'pageName='+opts.pageName+'&'+opts.dataString;
                $.ajax({type: opts.ajaxMethod,
    	  	      url: '/gyrobase/ajaxTunnel.html',
		          data: dataString,
		          beforeSend: function (xhr) { 
   		            opts.beforeAjax.call(this); 
   		          },
		          success: function(c){
  		            var containerIDHash = '#' + opts.containerID;
			        $(containerIDHash).append(c);
	                opts.afterAjax.call(this);      
		          },
		          error: function (XMLHttpRequest, textStatus, errorThrown) {
			        this; // the options for this ajax request 
		          }
		        });
                opts.afterLoad(this);
            });
        }
    };
})(jQuery);