// JavaScript Document
function t_init(parentDiv, imageHeight, imageWidth){
	galleryImages = new Array();
	galleryTitles = new Array();
	if(document.getElementById(parentDiv)){
	galleryImagesPrep = document.getElementById(parentDiv).getElementsByTagName('a');
	for (var i=0; i<galleryImagesPrep.length; i++){
		if(galleryImagesPrep[i].className=='galleryImage'){
			
			galleryImages.push(galleryImagesPrep[i].firstChild.src);
			galleryTitles.push(galleryImagesPrep[i].getAttribute("title"));
		}
	}
	g_imageHeight = imageHeight;
	g_imageWidth = imageWidth;
	divId = parentDiv;
	new case_Gallery();
	}
}
var case_Gallery = Class.create();
case_Gallery.prototype = {
	initialize: function (){
		this.imageHeight = g_imageHeight;
		this.imageWidth = g_imageWidth;
		this.parentDiv = divId;
		this.currentImage = 0;
		this.imageArray = new Array();
		this.titlesArray = new Array();
		this.titlesArray = galleryTitles;
		this.imageArray = galleryImages;
		
		
		
		
		this.upperLimit = this.imageArray.length-1;
		$(this.parentDiv).appendChild(
							Builder.node('div', {id: this.parentDiv+'_outer'},[
								Builder.node('div', {id: this.parentDiv+'_main'},
									Builder.node('a', {id: this.parentDiv+'_lightboxLink', href:'#', rel:'caseStudy['+this.parentDiv+']'},
										Builder.node('div', {id: this.parentDiv+'_inner'},[
											Builder.node('img', {id: this.parentDiv+'_loading', src:'images/loading.gif'}),
											Builder.node('div', {id: this.parentDiv+'_caption'})
										])
									)
								),
									Builder.node('div', {id: this.parentDiv+'_minis'},[
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_minus', src: 'images/scroll_left.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_mini1', src: 'images/loading.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_mini2', src: 'images/loading.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_mini3', src: 'images/loading.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_mini4', src: 'images/loading.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_mini5', src: 'images/loading.gif'})),
										Builder.node('a', {href:'#'},
											Builder.node('img', {id: this.parentDiv+'_plus', src: 'images/scroll_right.gif'}))
									])
									]));
	$(this.parentDiv).style.textAlign="center";
	$(this.parentDiv+'_caption').className = 'caption';
	$(this.parentDiv+'_inner').hide();
	$(this.parentDiv+'_minus').observe('click', (function(event) { event.stop(); this.changeImages(-1); }).bindAsEventListener(this));
	$(this.parentDiv+'_mini1').observe('click', (function(event) { event.stop(); this.changeImages(-2); }).bindAsEventListener(this));
	$(this.parentDiv+'_mini2').observe('click', (function(event) { event.stop(); this.changeImages(-1); }).bindAsEventListener(this));
	$(this.parentDiv+'_mini3').observe('click', (function(event) { event.stop(); }).bindAsEventListener(this));
	$(this.parentDiv+'_mini4').observe('click', (function(event) { event.stop(); this.changeImages(1); }).bindAsEventListener(this));
	$(this.parentDiv+'_mini5').observe('click', (function(event) { event.stop(); this.changeImages(2); }).bindAsEventListener(this));
	$(this.parentDiv+'_plus').observe('click', (function(event) { event.stop(); this.changeImages(1); }).bindAsEventListener(this));
	$(this.parentDiv+'_main').setStyle({height: this.imageHeight+'px'});
	
	$(this.parentDiv+'_mini1').style.opacity = '0.4';
	$(this.parentDiv+'_mini2').style.opacity = '0.4';
	$(this.parentDiv+'_mini4').style.opacity = '0.4';
	$(this.parentDiv+'_mini5').style.opacity = '0.4';
	
	if(this.imageArray.length <5){
		$(this.parentDiv+'_mini5').style.visibility = 'hidden';
		$(this.parentDiv+'_mini1').style.visibility = 'hidden';
	}
	if(this.imageArray.length <3){
		$(this.parentDiv+'_mini4').style.visibility = 'hidden';
		
	}
	if(this.imageArray.length <2){
		$(this.parentDiv+'_minis').hide();		
	}
	this.changeImages(0);
	this.preL();
	},
	changeImages: function(changeNum){
		$(this.parentDiv+'_loading').show();
		$(this.parentDiv+'_inner').hide();
		this.nextImage = this.currentImage;
		this.nextImage += changeNum;
		
		this.mini = new Array();
		this.mini[1] = this.nextImage-2;
		this.mini[2] = this.nextImage-1;
		this.mini[3] = this.nextImage;
		this.mini[4] = this.nextImage+1;
		this.mini[5] = this.nextImage+2;
	if (this.upperLimit <3){
		this.bottomLimit = 2;
		var top = 3+this.upperLimit;
	}
	else{
		this.bottomLimit = 1;
		var top = 6;
	}
	for(var n=this.bottomLimit; n<top; n++){
		if (this.mini[n] > this.upperLimit){ this.mini[n] -= this.upperLimit+1;}
		else if (this.mini[n] < 0){ this.mini[n] += this.upperLimit+1;}
		$(this.parentDiv+'_mini'+n).src = this.imageArray[this.mini[n]].replace('/images/', '/images/thumbs/');
		$(this.parentDiv+'_mini'+n).className = 'mini';
	}
		this.currentImage = this.mini[3];
		
		
		
		var backgroundImage = 'url("'+this.imageArray[this.mini[3]]+'") no-repeat';
		
		var imgPreloader = new Image();
        
        // once image is preloaded, resize image container


        imgPreloader.onload = (function(){
        
		$(this.parentDiv+'_inner').setStyle({background: backgroundImage, height: this.imageHeight-2+'px', border: 'solid 1px #AAA', width:this.imageWidth-2+'px', margin: '0px', marginLeft: '0px'});
		$(this.parentDiv+'_lightboxLink').href = this.imageArray[this.mini[3]].replace('/images/', '/images/large/');
		$(this.parentDiv+'_caption').setStyle({color:'white', width: this.imageWidth-12, top: this.imageHeight-37+'px'});
		$(this.parentDiv+'_caption').innerHTML = this.titlesArray[this.mini[3]];
		$(this.parentDiv+'_loading').hide();
		
		
		new Effect.Appear($(this.parentDiv+'_inner'));
		if (this.titlesArray[this.mini[3]] != "") { new Effect.Appear($(this.parentDiv+'_caption'), {from:0, to:0.8}); }
		else { $(this.parentDiv+'_caption').hide(); }
		
        }).bind(this);
		$(this.parentDiv+'_inner').hide();
		$(this.parentDiv+'_loading').show();
        imgPreloader.src = this.imageArray[this.mini[3]];
		
		
	
		
		
	},
	preL: function(){
		this.preLimg = new Array();
		for(var i=0; i<this.imageArray.length; i++){
			this.preLimg[i] = new Image();
			this.preLimg[i].src = this.imageArray[i];
		}
	}
	
}


document.observe('dom:loaded', function () { t_init('gallery1', 165, 250); });
document.observe('dom:loaded', function () { t_init('gallery2', 165, 250); });