(function($) {
	$.fn.imageRotator = function(options){
		// Settings
		options.myClass = $(this).attr('id');
		options.listItems = $('ul#'+options.myClass+' li');
		options.listItemParams = new Array();
		options.counter = -1;
		options.myInterval = null;
		options = $.extend({}, $.fn.imageRotator.defaults, options);
		
		$.fn.imageRotator.defaults = options;
		
		//set ul to become frame for the rotator
		$('ul#'+options.myClass).css({display:'block', overflow:'hidden', padding:'0px', margin:'0px', position:'relative', width:options.width, height:options.height});
		
		//Set all images to the correct alignment; Set the opacity of all images to 0
		$('ul#'+options.myClass+' li').css({opacity: 0.0, padding:'0px', margin:'0px', display:'block', position:'absolute', left:0, top:0, zIndex:1});
		
		//get default sizes for the images to reset images later
		var counter = 0;
		options.listItems.each(
			function(){
				options.listItemParams[counter] = {width:$(this).children('img').width(), height:$(this).children('img').height()};
				counter++;
			}
		)
		
		//Get the first image and display it (gets set to full opacity)
		//$('ul#'+options.myClass+' li:first').css({opacity: 1.0, zIndex:2});
			
		//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds		
		if(counter>1){
			$.fn.imageRotator.startTimer();
		}else{
			$.fn.imageRotator.rotate();	
		}

	};
	
	$.fn.imageRotator.rotate = function(){
			
		var current = null;
		var quadrants = new Array('lt','rt','lb','rb');
		
		//get current image
		/*options.listItems.each(
			function(){
				if($(this).css('opacity') == 1){
					current = $(this);
					aCounter = bCounter;
				};
				bCounter++;
			}
		);*/
		
		//if counter exceeds length of items reset to -1
		//if((aCounter+1) == options.listItems.length) aCounter = -1;
		
		//get next item
		if($.fn.imageRotator.defaults.counter != -1){
			var current = $.fn.imageRotator.defaults.listItems[$.fn.imageRotator.defaults.counter];
		}
		
		if($.fn.imageRotator.defaults.counter+1 == $.fn.imageRotator.defaults.listItems.length){
			var next = $.fn.imageRotator.defaults.listItems[0];
			$.fn.imageRotator.defaults.counter = 0;
		}else{
			var next = $.fn.imageRotator.defaults.listItems[$.fn.imageRotator.defaults.counter+1];
			$.fn.imageRotator.defaults.counter = $.fn.imageRotator.defaults.counter+1;
		}
		
		//prepare next and swap z-index so that next is on top of current
		$(next).css({opacity: 0.0, zIndex:2})
		if(current){
			$(current).css({zIndex:1});
		}
		
		//reset sizes for next img
		$(next).children('img').css({width:$.fn.imageRotator.defaults.listItemParams[$.fn.imageRotator.defaults.counter].width, height:$.fn.imageRotator.defaults.listItemParams[$.fn.imageRotator.defaults.counter].height});
		
		//create random number for quadrant zoom
		var randNum = Math.floor(Math.random()*4);

		//selects what quadrant to anchor next img to based on random number
		switch(quadrants[randNum]){
			case 'lt':
				$(next).css({left:0, top:0, right:'', bottom:''});
			break;
			case 'rt':
				$(next).css({left:'', top:0, right:0, bottom:''});
			break;
			case 'lb':
				$(next).css({left:0, top:'', right:'', bottom:0});
			break;
			case 'rb':
				$(next).css({left:'', top:'', right:0, bottom:0});
			break;
		}
		
		//figure out proportional width and height based on ul frame width and height
		var nextWidth = $.fn.imageRotator.defaults.width;
		var nextHeight = Math.round((nextWidth*$(next).children('img').height())/$(next).children('img').width());

		if(nextHeight < $.fn.imageRotator.defaults.height){
			nextHeight = $.fn.imageRotator.defaults.height;
			nextWidth = Math.round((nextHeight*$(next).children('img').width())/$(next).children('img').height());
		}

		//fade in li holder of img
		$(next).animate({opacity: 1.0}, 
			{
				duration:$.fn.imageRotator.defaults.fadeIn, 
				easing:'linear',
				complete:function(){
					$.fn.imageRotator.defaults.listItems.each(
						function(){
							$(this).css({opacity:0.0});
						}
					);
					
					$(this).css({opacity:1.0});
				}
			});
		
		//animate scale of img
		$(next).children('img').animate({width:nextWidth, height:nextHeight},
			{
				duration:($.fn.imageRotator.defaults.timer+1000), 
				easing:'linear'
			});
	 
		//for crossfade uncomment below
		//$(current).animate({opacity: 0.0}, options.fadeIn)
	}
	
	$.fn.imageRotator.stopTimer = function(){
		clearInterval(myInterval);	
	}
	
	$.fn.imageRotator.startTimer = function(){
		myInterval = setInterval('$.fn.imageRotator.rotate()',$.fn.imageRotator.defaults.timer);	
	}
	
	$.fn.imageRotator.defaults = {
		myClass: 'gallery',
		timer:3000,
		fadeIn:1000,
		width:350,
		height:350,

		listItems:null,
		listItemParams:null,
		counter:-1,
		myInterval:null
	};
})(jQuery);
