window.addEvent('domready', function() {

	
	// Scroller for thumbs
	var scrollFx = new Fx.Scroll($('PhotoThumbs'));
	
	// Hide Controls until Click
	$('PhotoC').set('opacity', 0);
	
	// Clicking a club
	$$('.Box img').each(function(element) {
		element.addEvent('click', function() {
			ClubID = this.get('alt');
			$('ptid').set('value', 'pt'+1);
						
			load_thumbs(ClubID, '1');
			load_galleries(ClubID);
			
			$('PhotoC').set('opacity', 1);
		});
	});



	// Build Galleries
	function load_galleries(clubID) {
		
		new Request.JSON({
			url: '/galleryData.php',
			method: 'get',
			onComplete: function(data){
			
				$('MoreGalleries').tween('opacity', 1, 0);
				$('MoreGalleries').set('html', '');
				$('MoreGalleries').setStyles({
					'overflow': 'auto',
					'height': '210px'
				});
				
				var cName = data.getLast();
				
				$('ClubName').set('html', cName.clubname);
				
				
				data.each(function(gal){
				
					new Element('div').set({
						'class': 'galLink',
						'alt': gal.gallery_id,
						'html': gal.name + "<br>" + gal.date
					}).addEvent('click', function(){
						
						$$('.galLink').each(function(element){
										element.setStyles({'background': '#AAA', 'border': '0', 'color': '#000'});
									});
						this.setStyles({'background': '#000', 'border': '2px solid white', 'color': '#FFF'});
						$('ptid').set('value', 'pt'+1);
						load_thumbs(gal.gallery_id, '3');
						
					}).inject($('MoreGalleries', 'bottom'));
					
				});
				$('MoreGalleries').tween('opacity', 0, 1);
			}
		}).get({
			'ID': clubID,
			's': '2'
		});
		
	};
	

	// Build Thumbs
	function load_thumbs(ID, method) {
		
		new Request.JSON({
			url: '/galleryData.php',
			method: 'get',
			onComplete: function(data){
				
				$('PhotoThumbs').setStyles({
							'overflow': 'auto', 
							'height': '304px',
							'border-bottom': '5px solid #000'
							});
							
				$('PhotoThumbs').tween('opacity', 1, 0);
				$('PhotoThumbs').set('html', "");
				var nfPic = data.getLast();
				update_main(nfPic.photo_id);
				$('eventName').set('html', nfPic.galname);
				
				var count = 1;
				data.each(function(pic) {
					
					if(pic.width > pic.height) {
						var width = '78';
					} else {
						var width = '34';
					}
					
					new Element('img').setStyle('margin', '4px').set({'class': 'galPic',
						'alt': pic.photo_id,
						'src': '/photos/gallery/180/'+pic.photo_id+'.jpg',
						'width': width,
						'height': '52',
						'id': 'pt' + count
						}).addEvent('click', function() {
							PicID = this.get('alt');
							PicID2 = this.get('id');
							
							$$('.galPic').each(function(element) {
								element.setStyle('border', '1px solid black');
							});
							this.setStyle('border', '1px solid red');
							
							$('ptid').set('value', PicID2);
							
							
							update_main(PicID);
						}).inject($('PhotoThumbs', 'top'));
						$('ptid3').set('value', count);
						count++;
				});
				$('PhotoThumbs').tween('opacity', 0, 1);
			}	
		}).get({
			'ID': ID,
			's': method
		});
		
	};
	
	//Update MainPhoto
	function update_main(photoID) {
		$('MainPhoto').set('opacity', 0);
		$('PhotoFrame').set('opacity', 0);
		$('MainPhoto').dispose();
		
		new Element('img').set({'src': '/photos/gallery/570/'+photoID+'.jpg', 'alt': photoID, 'id': 'MainPhoto'})
		.inject($('PhotoFrame', 'top'));
		
		$('PhotoFrame').tween('opacity', 0, 1);
	}
	
	// Gallery Movement
	function gallery_move(direction) {
		var thisPID = $('ptid').get('value');
		var lastPID = $('ptid3').get('value');
		
		thisPID = thisPID.replace(/pt/, "");
		$('ptid2').set('value', thisPID);
		
		if(direction == "forward") {
			thisPID++;
		
			if(thisPID <= lastPID) {
				$('pt'+thisPID).fireEvent('click');
				scrollFx.toElement($('pt'+thisPID));
			} else {
				$('pt1').fireEvent('click');
				scrollFx.toElement($('pt1'));
			}
		} else {
			thisPID--;
		
			if(thisPID > 0) {
				$('pt'+thisPID).fireEvent('click');
				scrollFx.toElement($('pt'+thisPID));
			} else {
				$('pt'+lastPID).fireEvent('click');
				scrollFx.toElement($('pt'+lastPID));
			}
		}
		
	};

	// Gallery Next
	$('PhotoF').addEvent('click', function() {
		gallery_move("forward");
	});
	
	// Gallery Previous
	$('PhotoB').addEvent('click', function() {
		gallery_move("back");
	});
	
	// Slideshow Stop
	$('PhotoR').addEvent('click', function() {
		var status = $('PhotoR').get('src');
		if(status == "/photos/pause.jpg") {
			player = $clear(player);
			$('PhotoR').set('src', "/photos/play.jpg");
		} else {
			gallery_move("forward");
			player = play.periodical(5000);
			$('PhotoR').set('src', "/photos/pause.jpg");
		}
		
	});
	
	
	var play = function slideshow() {
			
			gallery_move("forward");
		
		};
	var player = play.periodical(5000);
	
	// Club Rotator
	var rot = function rotator() {
		
		$$('.Box img').each(function(element) { 
			
			var spot = element.get('id');
			var eid = element.get('alt');
			
			new Request.JSON({
				url: '/clubData2.php'
				,method: 'get'
				,onComplete: function(club) {
					
					if(eid != club.club_id) {
						//Update
					element.set('opacity', 0);
					element.set('src', '/photos/clubs/logo/'+club.club_id+'.jpg');
					element.set('alt', club.club_id);
					element.tween('opacity', 0, 1);
					}
					
				}
			}).get({'eid': eid, 'spot': spot});
		
		});
		
	};
	rot.periodical(5000);
	
});