(function($){
    $.ajaxcontent = function(el, options){
        // To avoid scope issues, use 'base' instead of 'this'
        // to reference this class from internal events and functions.
        var base = this;
        // Access to jQuery and DOM versions of element
        base.$el = $(el);
        base.el = el; 
        // Add a reverse reference to the DOM object
        base.$el.data("ajaxcontent", base);
        base.init = function(){
            base.options = $.extend({},$.ajaxcontent.defaultOptions, options);
            // Get the container element
						base.container = $("#" + base.options.scrollContainer).wrapInner("<div class='ajaxShifter'></div>").find('.ajaxShifter:first');
						// Get the links of the menu and find the current one
						base.pageLinks = base.$el.find('li');
						var currentLink = selectCurrentLink(base.pageLinks);
						if (!currentLink) {
							currentLink = base.pageLinks.eq(0);
						}
						base.currentPosition = base.pageLinks.index(currentLink);

						// Build up all the previous panes
						currentLink.prevAll().each(function(i) {
							$("<div class='frame'></div>").load($(this).find('a:first').attr('href') + " #content>*", null, function() {
								if ($.isFunction(base.options.callbackOnLoad)) {
									base.options.callbackOnLoad();
								}
							}).prependTo(base.container);
						});
						// Build up all the next panes
						$(currentLink).nextAll().each(function(i) {
							$("<div class='frame'></div>").load($(this).find('a:first').attr('href') + " #content>*", null, function() {
								if ($.isFunction(base.options.callbackOnLoad)) {
									base.options.callbackOnLoad();
								}
							}).appendTo(base.container);
						});
						
						base.container.animate({height: base.container.find("#content").height()});
						
						base.pageLinks.find('a').click(function() {
							base.pageLinks.find('a').each(function() {
								$(this).removeClass('current');
							});
							$(this).addClass('current');
							base.currentPosition = base.pageLinks.index($(this).parent());
							base.container.animate({left: -(base.docWidth * base.currentPosition), height: base.container.find(".frame:eq(" + base.currentPosition + ")").height()}, 1000)
							return false;
						});
						$(window).bind('resize load', function() {
							setWidths();
						});
						
						
        }
				// Private functions
				selectCurrentLink = function(links) {
					var selected = false;
					links.each(function() {
						var u = $(this).find('a:first').attr('href');
						if( window.location.pathname.indexOf( u ) >= 0 && u.length > 0) {
							selected = $(this);
							return false;
						}
					});
					return selected;
				}
				
				setWidths = function() {
					base.docWidth = $(window).width();
					var scrollContainer = $("#" + base.options.scrollContainer);
					scrollContainer.css('width', base.docWidth);
					base.container.css('width', base.docWidth * base.pageLinks.size());
					scrollContainer.find('.frame').css('width', base.docWidth);
					base.container.css('left', -(base.docWidth * base.currentPosition));
					//alert(base.container.css('left'));
				}
				
        base.init();
    }

    $.ajaxcontent.defaultOptions = {
	   scrollContainer: "contentWrapper",
		 callbackOnLoad:	null
    }
	
    $.fn.ajaxcontent = function(options){
        return this.each(function(){
            (new $.ajaxcontent(this, options));
        });
    }
})(jQuery);