if(!jsFrontend) { var jsFrontend = new Object(); }

jsFrontend = {
	// datamembers
	debug: true,
	
	// init, something like a constructor
	init: function() {
		jsFrontend.events.init();
		jsFrontend.scroll.init();
	},
	
	// end
	_eoo: true
}	

jsFrontend.events = {
	// init, something like a constructor
	init: function() {
		jsFrontend.events.form();
		jsFrontend.events.maps();
		jsFrontend.events.people();
	},
	
	form: function() {
		$('.formName').focus(function() { if($(this).val() == 'Naam') $(this).select(); });
		$('.formEmail').focus(function() { if($(this).val() == 'Email') $(this).select(); });
		$('.formUrl').focus(function() { if($(this).val() == 'Website') $(this).select(); });
		
		$('.formButton').click(function() {
			// remove classes
			$('.formName').removeClass('error');
			$('.formEmail').removeClass('error');
			$('.formUrl').removeClass('error');
			$('.form-error').hide();

			var id = $(this).attr('id').split('-')[1];
			
			var event = $('#eventid-'+ id);
			var name = $('#name-'+ id);
			var email = $('#email-'+ id);
			var url = $('#url-'+ id);
			
			// validate
			var errors = false;
			if(event.val() == '') {
				alert('Fail');
				errors = true;
			}
			if(name.val() == 'Naam' || name.val() == '') {
				name.addClass('error');
				$('#formErrorName-'+ id).show();
				errors = true;
			}
			if(email.val() == 'Email' || email.val() == '') {
				email.addClass('error');
				$('#formErrorEmail-'+ id).show();
				errors = true;
			}
			else { 
				var regexp =  /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
				if(!regexp.test(email.val())) {
					email.addClass('error');
					$('#formErrorEmail-'+ id).show();
					errors = true;
				}
			}
			if(url.val() != 'Website' && url.val() != '') {
				var regexp = /(http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/;
				if(!regexp.test(url.val())) {
					url.addClass('error');
					$('#formErrorUrl-'+ id).show();
					errors = true;
				}
			}

			// no errors
			if(!errors) {
				$.ajax({type: 'POST', cache: false, dataType: 'json',
					data: 'eventId='+ event.val() +'&name='+ name.val() +'&email='+ email.val() +'&url='+ url.val(), 
					url: '/ajax.php?module=events&action=subscribe',
					success: function(json, textStatus) {
						if(json.code == 200) {
							var html = '<a';
							if(json.data.url != null && json.data.url != '') html += ' href="'+ json.data.url +'"';
							html += '>'+ json.data.name + '</a>';
							$('#eventPeopleWho-'+ id).append(html);
							$('#event-'+ id +' .people span').html(parseInt($('#event-'+ id +' .people span').html()) + 1);
							$('.eventForm-'+ id).slideUp();
						}
					},
					error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); }
			});
			}
			
			return false;
		});
	},
	
	maps: function() {
		$('.toggleMap').click(function() {
			var chunks = $(this).attr('rel').toString().split('-');
			var id = '#' + chunks[0] +'-'+ chunks[1];

			// hide others
			$('.eventPeople:visible').slideUp();
			$('.eventMap:visible').slideUp();
			
			if($(id).is(':visible')) { $(id).slideUp(); } 
			else {
				$(id).addClass('loading');
				$(id).slideDown(); 
				
				var map = new GMap2(document.getElementById('eventGoogleMap-'+ chunks[1]));
				var point = new GLatLng(chunks[2], chunks[3]);
				
				map.setCenter(point, 14);
				map.addOverlay(new GMarker(point));
	
				$(id).removeClass('loading');
			}
		});		
	},
	
	// people-data will be loaded or hidden
	people: function() {
		$('.togglePeople').click(function() {
			var id = '#' + $(this).attr('rel').toString();
			var eventId = id.split('-')[1];
			
			// hide others
			$('.eventPeople:visible').slideUp();
			$('.eventMap:visible').slideUp();

			if($(id).is(':visible')) { 
				$(id).slideUp(); 
				$('#eventPeopleWho-' + eventId).html('');
			} else {
				$('#event-' + eventId).addClass('loading');
				$.ajax({type: 'POST', cache: false, dataType: 'json',
						data: 'eventId='+ eventId, 
						url: '/ajax.php?module=events&action=get_people',
						success: function(json, textStatus) {
							if(json.code == 200) {
								// build html
								var html = '';
								for(var i in json.data) {
									var temp = '<a';
									if(json.data[i].url != null && json.data[i].url != '') temp += ' href="'+ json.data[i].url +'"';
									temp += '>'
									if(json.data[i].source == 'twitter') temp += '@';
									temp += json.data[i].name;
									temp += '</a> ';
									
									html += temp;
								}
	
								// set html
								if(html != '') $('#eventPeopleWho-' + eventId).html(html);
								
								// show
								$(id).slideDown();
							}
						},
						error: function(XMLHttpRequest, textStatus, errorThrown) { alert(textStatus); }
				});
				$('#event-' + eventId).removeClass('loading');
			}
		});
	},
	
	// end
	_eoo: true		
}

jsFrontend.scroll = {
	// init, something like a constructor
	init: function() {
		$('a[href*="#"]').click(function() {
			$.scrollTo($($(this).attr('href').replace('/#', '#').toString()), 800);
			return false;
		});
	},

	// end
	_eoo: true
}

$(document).ready(function() { jsFrontend.init(); });