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

	var ClubID;
	var ThisClubID;
	
	var SlideGal = new Fx.Slide($('ClubGallery')).hide();
	
	$('allEvents').set('opacity', 0);
	$('Privs').set('opacity', 0);
	$('glistSubmit').set('opacity', 0);	
		
	// Load guestlist form
	// Retrieving HTML
	var req = new Request.HTML({url:'/guestlist.html',
				onSuccess: function(html) {
					$('guestlist').set('opacity', 0);
					$('guestlist').set('text', '');
					$('guestlist').adopt(html);
			}, onFailure: function() {
					$('newContent').set('html', 'Unvailable');
			}
		});
	
	// Send req
		req.send();	
			
	// Handle updating the main event and guestlist
	$$('.Box img').each(function(element) {
		element.addEvent('click', function() {
			ClubID = this.get('alt');
			ThisClubID = $('clubID').get('value');
			$('guestlist').set('opacity', 0);
			
			$('clubMainPhoto').dispose();
						
			new Element('img').set({'src': "/photos/clubs/main/"+ClubID+".jpg", 'id': 'clubMainPhoto', 'alt': ClubID})
			.inject($('ColLeftMain', 'top'));
				
			//$('clubMainPhoto').set('opacity', 0, 1);
			$('allEvents').set('opacity', 1);
			
			if (ClubID != ThisClubID) {
				
				SlideGal.hide();
				
				var CountThis = 1;
				$$('.ClubBox img').each(function(element) {
					element.set('src', "/photos/clubs/gallery/"+CountThis+"/thumb/" + ClubID + ".jpg");
					CountThis++;
				});
				
				SlideGal.slideIn();	
			
			}
			
			//$('clubMainPhoto').tween('opacity', 0, 1);
			//$('Privs').set('opacity', 0);
			//$('glistSubmit').set('opacity', 0);
	
			if (ClubID != ThisClubID) {
				// Import Form
				req.send();
				

				new Request.JSON({
					url: '/clubData.php',
					method: 'get',
					onComplete: function(event){
						if (event.event) {
							$('guestlist').set('opacity', 0);
							$('Privs').set('opacity', 0);
							//$('eventName').set('html', event.event);
							$('eventPriv').set('html', event.privileges);
							$('formID').set('value', event.event_id);
							$('clubName').set('html', event.club);
							$('clubID').set('value', ClubID);
							$('guestlist').tween('opacity', 0, 1)
							$('Privs').tween('opacity', 0, 1)
							$('glistSubmit').set('opacity', 1);
							
							$('clubThumb').set('html', '');
							new Element('img').set('src', '/photos/clubs/logo/'+event.club_id+'.jpg')
							.inject($('clubThumb'));
							
						}
						else {
							$('clubID').set('value', ClubID);
							$('Privs').set('opacity', 0);
							//$('eventName').set('html', "No Events");
							$('glistSubmit').set('opacity', 0);
							$('guestlist').set('html', "<p>This club does not currently have any events listed.</p>");
						}
						$('guestlist').set('opacity', 1);
					}
				}).get({
					'clubID': ClubID
				});
				guestlist_drop(ClubID);
			}
			$('guestlist').set('opacity', 1);
		})
		
	});
	
	// Handle viewing club gallery
	var PhotoID;
	$$('.ClubBox img').each(function(element) {
		element.addEvent('click', function() {
			PhotoID = this.get('alt');	
			ThisClubID = $('clubID').get('value');
			$('clubMainPhoto').set('opacity', 0);
			
			$('clubMainPhoto').dispose();
						
			new Element('img').set({'src': "/photos/clubs/gallery/"+PhotoID+"/main/"+ThisClubID+".jpg", 'id': 'clubMainPhoto', 'alt': ThisClubID})
			.inject($('ColLeftMain', 'top'));
			//$('clubMainPhoto').set('opacity', 0, 1);
			

			
		})
	});
		


	
	// Update from Guestlist dropdown
	$('allEvents').addEvent('change', function() {
		var IEevent = $('allEvents').get('value');
		update_main_event(IEevent);
	});
	
	//Update main event photo & guestlist
	function update_main_event(update_event_id) {

		// Import Form
		req.send();	
		
		new Request.JSON({
			url: '/eventData.php'
			,method: 'get'
			,onComplete: function(event) {
				$('guestlist').set('opacity', 0);
				$('Privs').set('opacity', 0);
				//$('eventName').set('html', event.event);
				$('eventPriv').set('html', event.privileges);
				$('formID').set('value', event.event_id);
				$('guestlist').tween('opacity', 0, 1);
				$('Privs').tween('opacity', 0, 1);
				$('glistSubmit').set('opacity', 1);
			}
		}).get({'eventID': update_event_id});		
	};
	
	//General guestlist dropdown
	function guestlist_drop(this_club_id) {
			new Request.JSON({
				url: '/clubData3.php'
				,method: 'get'
				,onComplete: function(data) {
					$('allEvents').set('html', '');
					
					data.each(function(ev) {
						
						if(this_club_id == ev.club_id)
						{
							new Element('option')
							.set({'value': ev.event_id, 'html': ev.date+" at "+ev.club_name, 'selected': ev.event_id})
							.inject($('allEvents'));
							$('formID').set('value', ev.event_id);
						} else {
							
							new Element('option')
							.set({'value': ev.event_id, 'html': ev.date+" at "+ev.club_name})
							.inject($('allEvents'));
							
						}
						
					});
				}
			}).get({'clubID': this_club_id});
	};

	// Handle submitting the guestlist

	$('glistSubmit').addEvent('click', function() {
		var formName = $('formName').get('value');
		var formGuests = $('formGuests').get('value');
		var formEmail = $('formEmail').get('value');
		var formPhone = $('formPhone').get('value');
		
		if($('formBottle').get('checked'))
		{
			var formBottle = "yes";
		} else { var formBottle = "no"; }
		
		if($('formLimo').get('checked'))
		{
			var formLimo = "yes";
		} else { var formLimo = "no"; }

		var formMsg = $('formMsg').get('value');
		var formID = $('formID').get('value');
		
		if(!formName) $('formName').setStyle('background-color', 'red');
		else { $('formName').setStyle('background-color', 'white'); }
		if(!formEmail) $('formEmail').setStyle('background-color', 'red');
		else { $('formEmail').setStyle('background-color', 'white'); }
		if(!formPhone) $('formPhone').setStyle('background-color', 'red');
		else { $('formPhone').setStyle('background-color', 'white'); }
		
		if(formName && formEmail && formPhone)
		 {
			new Request.JSON({
					url: '/eventGPost.php'
					,method: 'get'
				}).get({'eventID': formID, 'name': formName, 'guests': formGuests, 'email': formEmail, 'phone': formPhone, 'bottle': formBottle, 'limo': formLimo, 'msg': formMsg});
			
					
			$('glistSubmit').set('opacity', 0);
			$('Privs').set('opacity', 0);
			$('allEvents').set('opacity', 0);
			$('guestlist').set('opacity', 0);
			$('guestlist').set('html', "<p>Thank you for submitting!</p><p>See you at the party!</p>");
			$('guestlist').tween('opacity', 0, 1);
		}
	});
	
	
	// 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);
	
});