(function(jQuery){
	jQuery.fn.gallery = function(options){
		var defaults = {
			xmlSource: 'main/JHU-Vim-V3/home-gallery.xml',
			loaderImage: 'main/JHU-Vim-V3/images/loading.gif',
			rotateSpeed: 6000,
			autoRotate: true,
			startRotate: 1000,
			totalImages: 9
		};
		
		options = jQuery.extend(defaults, options);
		return this.each(function(){
		
			var xmlLength = 0;
			var rn = 0;
			var active = 0;
			var $this = jQuery(this);
			jQuery.ajax({
				type: 'GET',
				url: options.xmlSource,
				dataType: 'xml',
				success: function(xml){
					$this.append('<div id="main-image"></div><div id="loader" style="background:url('+options.loaderImage+') no-repeat 50% 50%;"></div><div id="load-image"><img src="" /></div>');
					$this.append('<div id="thumbs"><a href="#" id="previous">Previous Image</a><ul></ul><a href="#" id="next">Next Image</a></div>');
					$thumbs = $this.children('div#thumbs');
					$ul = $thumbs.children('ul');
					$mainImageDiv = jQuery('#main-image');
					$loadImageDiv = jQuery('#load-image');
					$loader = jQuery('#loader');
					$loadImage = $loadImageDiv.children('img');
					xmlLength = jQuery(xml).children().children().length;
					for(i=0; i<options.totalImages; i++){
						rn= Math.floor(Math.random()* xmlLength); 
						$current = jQuery(xml).children().children().eq(rn);
						$ul.append('<li><a href="'+$current.children('large').text()+'" rel="'+$current.children('url').text()+'"><img src="'+$current.children('thumb').text()+'" alt="" /></a></li>');
						if(i==0){
							$this.children('div#main-image').append('<a href="'+$current.children('url').text()+'"><img src="'+$current.children('large').text()+'" alt=""></a>');
						}
						$current.remove();
						xmlLength -= 1; 
					}
					
					$ul.children('li:first').addClass('current');
					active = 1;
					$this.children('div#main-image').children('a').children('img').load(function(){
						$thumbs.fadeIn('slow');
						$loader.fadeOut();
					});
					$lia = $ul.children('li').children('a');
					jQuery('a#next').click(function(){
						moveForward();
						jQuery.clearTimer(galleryTimer);
						jQuery.clearTimer(startTimer);
					});
				
					jQuery('a#previous').click(function(){
						if(active == 1){
							$ul.children('li').removeClass('current');
							$ul.children('li:last').addClass('current');
							active = options.totalImages;
						}else{
							$ul.children('li.current').removeClass('current').prev().addClass('current');
							active -= 1;
						}
						$getInfo = $ul.children('li.current').children('a');
						main = $getInfo.attr('href');
						where = $getInfo.attr('rel'); 
						loadIt(main,where);
						jQuery.clearTimer(galleryTimer);
						jQuery.clearTimer(startTimer);
						return false;
					});
				
					function moveForward(auto){
						if(active == options.totalImages){
							$ul.children('li').removeClass('current');
							$ul.children('li:first').addClass('current');
							active = 1;
							
						}else{
							$ul.children('li.current').removeClass('current').next().addClass('current');
							active += 1;
							
						}
						$getInfo = $ul.children('li.current').children('a');
						main = $getInfo.attr('href');
						where = $getInfo.attr('rel'); 
						loadIt(main,where);
						jQuery.clearTimer(galleryTimer);
						if(active == options.totalImages && options.cycle == 'once' && auto == 'auto'){
							autoGallery('last');
						}else if(auto == 'auto'){
							autoGallery();
						};
						return false;	
					}
				
					$lia.click(function(){
						jQuery.clearTimer(galleryTimer);
						jQuery.clearTimer(startTimer);
						$ul.children('li').removeClass('current');
						jQuery(this).parent().addClass('current');
						main = $(this).attr('href');
						where = $(this).attr('rel'); 
						loadIt(main,where);
						active = $(this).parent().prevAll().length + 1;
						return false;
					});
				
					function loadIt(main,wher){
						$loadImage.attr('src',main);
						$loader.fadeIn();
						$loadImage.load(function(){
							$loadImageDiv.fadeIn('slow', function(){loadMain(main,where);});
							$loadImage.unbind();
						});
					}
				
					function loadMain(main,where){
						$loader.fadeOut("fast");
						$mainImageDiv.html('<a href="'+where+'"><img src="'+main+'" alt="" /></a>');
						$mainImageDiv.children('a').children('img').load(function(){
							$loadImageDiv.css('display','none');
							$mainImageDiv.children('a').children('img').unbind();
						});
						
					}
					var galleryTimer = {};
					function autoGallery(last){
						galleryTimer = $.timer(options.rotateSpeed, function(){
							moveForward("auto");
							if(last == 'last'){
								jQuery.clearTimer(galleryTimer);
							}
						});
					}
					if(options.autoRotate){
						var startTimer = {};
						var startTime = options.startRotate - options.rotateSpeed;
						startTimer = $.timer(startTime, function(){
							autoGallery();
						});
					}
				} //end success
			}); //end ajax function	
		});	
		
	};
})(jQuery);

jQuery.timer = function(time,func,callback){
	var a = {timer:setTimeout(func,time),callback:null}
	if(typeof(callback) == 'function'){a.callback = callback;}
	return a;
};

jQuery.clearTimer = function(a){
	clearTimeout(a.timer);
	if(typeof(a.callback) == 'function'){a.callback();};
	return this;
};
