function GalleryButton(button_wrap, pos, gallery){
	var buttonInstance = this;
	this.gallery = gallery;
	this.button = $(button_wrap);
	this.position = pos;
	this.button.bind('click', function(){
		if(gallery.currentImg == pos){
			return false;
		}
			buttonInstance.gallery.changeImageTo(buttonInstance.position);
		});
}

function Gallery(container1, container2, button_prefix, images, links, blanks){
	this.container1 = $(container1);
	this.container2 = $(container2);
	this.images = images;
	this.links = links;
	this.blanks = blanks;
	this.nextImg = 1;
	this.currentImg = 0;
	this.visibleFirst = true;
	this.timeout = 5000;
	this.timer = 0;
	
	try{
		this.count = images.length;
	}
	catch(err){
		this.count = 0;
	}

	this.buttons = new Array();
	instance = this;
	var i=0;
	for(i=0;i<this.count;i++){
		this.buttons[i] = new GalleryButton(button_prefix + (i + 1), i, this);
	}
	
	this.rotate = function(){
		this.currentImg = this.nextImg;
		if(this.nextImg >= this.count-1){
			this.nextImg = 0;
		}
		else {
			this.nextImg++;
		}
		this.changeImageWithPreloadTo(this.nextImg);
	}
	
	this.start = function(){
		this.resetTimer();
	}
	
	this.stop = function(){
		clearTimeout(this.timer);
		this.timer = 0;
	}
	
	this.timer = 0;
	this.changeImageWithPreloadTo = function(imgnum){
		currentInstance = this;
		this.setActiveButton();
		if(this.visibleFirst){
				this.container1.fadeOut(600, function(){ 
					currentInstance.setBlank(currentInstance.container1, imgnum);
					currentInstance.container1.attr('href',currentInstance.links[imgnum]);
					currentInstance.container1.find('img').attr('src',currentInstance.images[imgnum]);
					currentInstance.resetTimer();
					});
			
			this.visibleFirst = false;
		}else{
			this.container1.fadeIn(600, function(){
				currentInstance.setBlank(currentInstance.container2, imgnum);
				currentInstance.container2.attr('href',currentInstance.links[imgnum]);
				currentInstance.container2.find('img').attr('src',currentInstance.images[imgnum]);
				currentInstance.resetTimer();
				});
			this.visibleFirst = true;
		}
	}
	
	
	this.changeImageTo = function(imgnum){
		currentInstance = this;
		this.currentImg = imgnum;
		this.nextImg = imgnum + 1;
		if(this.nextImg >= this.count){
			this.nextImg = 0;
		}

		this.setActiveButton();
		
		if(this.visibleFirst){
				this.setBlank(this.container2, imgnum);
				this.container2.attr('href',currentInstance.links[imgnum]);
				this.container2.find('img').attr('src',currentInstance.images[imgnum]);
				this.container1.fadeOut(600, function(){
					currentInstance.setBlank(currentInstance.container1, currentInstance.nextImg);
					currentInstance.container1.attr('href',currentInstance.links[currentInstance.nextImg]);
					currentInstance.container1.find('img').attr('src',currentInstance.images[currentInstance.nextImg]);
					currentInstance.resetTimer();
					});
			this.visibleFirst = false;
		}else{
			this.setBlank(this.container1, imgnum);
			this.container1.attr('href',currentInstance.links[imgnum]);
			this.container1.find('img').attr('src',currentInstance.images[imgnum]);
			this.container1.fadeIn(600, function(){
				currentInstance.setBlank(currentInstance.container2, currentInstance.nextImg);
				currentInstance.container2.attr('href',currentInstance.links[currentInstance.nextImg]);
				currentInstance.container2.find('img').attr('src',currentInstance.images[currentInstance.nextImg]);
				currentInstance.resetTimer();
				});
			this.visibleFirst = true;
		}
	}
	
	this.setActiveButton = function(){	
		this.buttons[this.currentImg].button.addClass('slide_button_active').siblings().removeClass('slide_button_active');
	}
	
	this.resetTimer = function(){
		clearTimeout(this.timer);
		var instance = this;
		this.timer = setTimeout(function(){instance.rotate();}, instance.timeout);
	}
	
	this.setBlank = function(container, imgnum){
		if(this.blanks[imgnum]==1){
			container.attr('target','_blank');
		}
		else{
			container.removeAttr('target');
		}
	}
	
}
