var links = {

	 1: 'sheffield-theatres',
	 2: 'the-cogency',
	 3: 'cultural-olympiad',
	 4: 'arts-council-england',
	 5: 'loughborough-university',
	 6: 'greenwich-students-union',
	 7: 'greenwich-students-union',
	 8: 'fabulous',
	 9: 'dancing-strong',
	 10: 'fabulous',
	 11: 'new-art-exchange',
	 12: 'foot-in-hand-dance-company',
	 13: 'sheffield-theatres',
	 14: 'sheffield-theatres',
	 15: 'new-art-exchange',
	 16: 'pashley',
	 17: 'chat',
	 18: 'adobe',
	 19: 'adobe',
	 20: 'adobe',
	 21: 'the-mighty-creatives',
	 22: 'arts-for-rutland',
	 23: 'boxfresh',
	 24: 'corbis',
	 25: 'cultural-olympiad',
	 26: 'de-montfort-university',
	 27: 'donald-edwards',
	 28: 'lcb-depot',
	 29: 'rich-mix',
	 30: 'the-mighty-creatives',
	 31: 'schiffer-books-NY',
	 32: 'sheffield-theatres',
	 33: 'pedal-nation',
	 34: 'recent-alexander',
	 35: 'recent-sheffield',
	 36: 'recent-sheffield',
	 37: 'recent-sheffield',
	 38: 'recent-sheffield',
	 39: 'lrhf',
	 40: 'recent-nae',
	 41: 'recent-nae',
	 42: 'the-mighty-creativese',
	 43: 'adobe',
	 44: 'recent-sheffield',
	 45: 'sheffield-theatres',
	 46: 'sheffield-theatres',
	 47: 'sheffield-theatres'
	 

};

var home = {

	numImages: 47,
	gridSize: 12,
	thumbs: [],
	chosenAlready: [],
	numBlanks: 0,
	maxBlanks: 0,
	
	init: function() {
	
		//create thumbnail divs
		for( var i = 0; i < this.gridSize; i++ ) {
		
			var className = 'thumb';
			if( (i+1) % 4 == 0 ) className += ' last';
			if( i >= 8 ) className += ' last-line';
			if( i % 4 == 0 ) {
				className += ' first';
				$('thumbnails').insert( '<div class="clearer"></div>' );
			}
		
			this.thumbs[i] = new Element( 'div', { className: className } );
			$('thumbnails').insert( this.thumbs[i] );
			
			//populate thumb
			var randomImg = this.getImage();
			if( randomImg == '' ) continue;
			
			var img = new Element( 'img', { src: 'images/thumbs/' + randomImg + '.jpg' } );
			
			//image or image with link
			if( links[ randomImg ] != '' && links[ randomImg ] != undefined ) {
			
				//add link and image
				var a = new Element( 'a', { href: links[ randomImg ] } );
				this.thumbs[i].insert( a );
				a.insert( img );
				
				//add handler
				a.observe( 'click', ajaxLoader.loadLink.bindAsEventListener(ajaxLoader) );

			
			} else {
			
				//image
				this.thumbs[i].insert( img );
			
			}
		
		}
	
	},
	
	getImage: function() {
	
		//random chance of it being blank
		if( this.numBlanks < this.maxBlanks && Math.ceil( Math.random() * 5 ) == 5 ) {
			this.numBlanks ++;
			return '';
		}
	
		do {
			
			var r = Math.ceil( Math.random() * 47 );
	
			// has this already been used? if not return it
			if( this.chosenAlready.indexOf( r ) == -1 ) {
			
				this.chosenAlready.push( r );
				return r;
			
			}
	
		} while( this.chosenAlready.length < this.numImages );
	
	}

}

document.observe( 'dom:loaded', home.init.bind(home) );
