/*
adRotator javascript
*/

function ImgRotator(id,time){
	
	/* Attributes */
	this.container = $(id);
	this.ads = container.children('img');
	this.timer = null;
	this.time = time;
	this.currentAd = null;
	
	this.init = function(){
		
		this.currentAd = $(ads.get(0));
		
		ads.removeClass('active').hide();
		this.currentAd.addClass('active').show();
		
		this.timer = setTimeout(this.rotate,this.time);
	}
	
	this.rotate = function(){
	
		this.next();
	
	}
	
	this.next = function(){
		
		clearTimeout(this.timer);	// could be called asynchronously
		
		var next = currentAd.next();
		
		if (next.size() == 0){
			next = $(ads.get(0)); // start back at the beginning
		}
		
		currentAd.fadeOut('slow').removeClass('active');
		next.fadeIn('slow').addClass('active');
		
		this.currentAd = next;
		this.timer = setTimeout(this.rotate, this.time);
	
	};
	

		$(".thumbs img").hover(function(){

		clearTimeout(this.timer);

		var path = $(this).attr('class');
		var hoverimg = $("#home-gallery-pic img[class="+path+"]");
		
		currentAd.fadeOut('fast').removeClass('active');
		hoverimg.fadeIn('fast').addClass('active hover');
		
		this.currentAd = hoverimg;
		this.timer = setTimeout(this.rotate, this.time);
		
		},
		function(){	
		
		clearTimeout(this.timer);
		
		var path = $(this).attr('class');
		var hoverimg = $("#home-gallery-pic img[class="+path+"]");
		
		$('.hover').fadeOut('slow').removeClass('active hover');
		$('img.img1').fadeIn('slow').addClass('active');

		this.currentAd = next;
		this.timer = setTimeout(this.rotate, 5000);
		
		}
	);

	
	this.init();
	return this;

}


