
FriendsterApp = {
	
	popup_url: '',
	popup_loading: false,
	popup_offset: 20,
	canvasBaseURL: '',
	
	getPopup: function(url, params) {
		// default params to an empty object
		if ( typeof(params) == 'undefined' ) {
			params = {};
		}
		// let the controller know we're loading the view in a popup
		params.in_popup = 1;
		
		// calculate the top position of the box
		t = $(window).scrollTop() + FriendsterApp.popup_offset;
		$('#popup-overlay .popup-box-container').css('top', t + 'px');
		
		// show popup with loading graphic
		$container = $('#popup-overlay .popup-box-content');
		$container.html($('#loading-message').html());
		$('#popup-overlay').show();
		
		// CM.popup_loading = true;
		// CM.popup_url = url;
		
		$.get(url, params, function(data) {
			$container.html(data);
			// CM.popup_loading = false;
		});
	},
	
	showPopup: function(html) {
		// calculate the top position of the box
		top = $(window).scrollTop() + FriendsterApp.popup_offset;
		$('#popup-overlay .popup-box-container').css('top', top + 'px');
		// print html		
		$container = $('#popup-overlay .popup-box-content');
		$container.html(html);
		$('#popup-overlay').show();
	},
	
	hidePopup: function() {
		$('#popup-overlay').hide();
		location.hash = '#none';
		return false;
	},
	
	showMapType: function(type) {
		// toggle map containers
		$('.map-container').hide();
		$('.' + type + '-map').show();
		// update nav
		$('.map-nav a').removeClass('active');
		$('#' + type + '-map-link').addClass('active');
	},
	
	showMessage: function(type, message, opt) {
		opt = opt || {};
		selector = opt.selector || '#messages';
		timeout = typeof(opt.timeout) != 'undefined' ? opt.timeout : 2000;
		animate = typeof(opt.animate) != 'undefined' ? opt.animate : true;
		
		if ( animate ) {
			$(selector).append('<p style="display:none" class="' + type + ' message">' + message + '</p>');
			$(selector + ' p.message').slideDown();			
		}
		else {
			$(selector).append('<p class="' + type + ' message">' + message + '</p>');
		}

		if ( timeout !== false ) {
			setTimeout(function(){
				FriendsterApp.hideMessage($(selector + ' p.message.' + type));
			}, timeout);
		}
		
	},
	
	hideMessage: function(p) {
		$(p).slideUp('normal', function(){
			$(this).remove();
		});
	},	
	
	redirect: function(url) {
		top.location.href = this.canvasBaseURL + url;
	},
	
	init: function(){
		$(document).ready(function(){
			
			// close popup links
			$('.popup-box a.close, .popup-box .cancel a').live('click', function(){
				FriendsterApp.hidePopup();
			});
			
			// map nav links
			$('.map-nav a').live('click', function(){
				FriendsterApp.showMapType($(this).attr('id').replace(/-map-link$/, ''));
			});
			
			// messages go away on click
			$('p.message').live('click', function(){
				FriendsterApp.hideMessage(this);
			})
			
			// submit form to write profile box for the logged in user
			$('#set-profile-form').submit(function(){
				$form = $(this);
				$.get($form.attr('action'), $form.serialize(), function(data){
					if ( data.success == 1 ) {
						FriendsterApp.showMessage('success', data.message);
					}
					else {
						error_msg = typeof(data.message) != 'undefined' ? data.message : 'Unknown error';
						FriendsterApp.showMessage('error', error_msg);
					}
				}, 'json');
				return false;
			});
			
			// friends page compare map links
			$('body.friends a.compare-map').click(function(){
				$a = $(this);
								
				// loading...
				$('#compare-map').html($('#loading-message').html());
				
				// load compare map via ajax
				$.get($a.attr('href'), function(data){
					$('#compare-map').html(data);
					// make sure the correct map type is visible
					FriendsterApp.showMapType($('.map-nav a.active').attr('id').replace(/-map-link$/, ''));					
				});
				
				// update nav classes
				$('#friends .friend').removeClass('active');
				$a.closest('.friend').addClass('active');		
				
				return false;
			});
			
			// friends page compare map links
			$('body.friends a.invite').click(function(){
				FriendsterApp.getPopup($(this).attr('href'));				
				return false;
			});	
			
			// install page forms
			$('body.install form').submit(function(){
				$form = $(this);
				
				// this is where we'll stick our messages
				msg_div = '#' + $form.attr('id') + ' .messages';				
				
				// don't submit the form if we're missing required fields
				if ( $form.attr('id') == 'login-form' && ($('#login_email').val() == '' || $('#login_password').val() == '') ) {
					FriendsterApp.showMessage('error', "Email and Password are required fields", {selector: msg_div});
					return false;
				}
				
				// disable submit button so it can't be clicked again during the js redirect that happens on success
				$form.find('input[type="submit"]').attr('disabled', 'disabled');
				
				// hide form items
				$form.find('div').hide();
				
				// clear any lingering form messages
				$(msg_div + ' p.message').remove();
				
				// show loading message
				$form.append($('#loading-message').html());
				
				$.post($form.attr('action'), $form.serialize(), function(data){
					// show form items
					$form.find('div').show();
					
					// hide loading message
					$form.find('.loading').hide();
					
					// it worked :)
					if ( data.success == 1 ) {
						FriendsterApp.showMessage('success', data.message, {selector: msg_div, animate: false, timeout: false});
						FriendsterApp.redirect('index');
					}
					// if failed :(
					else {
						// re-enable submit button so we can use it again
						$form.find('input[type="submit"]').attr('disabled', '');
						
						msg = typeof(data.message) != 'undefined' ? data.message : 'Unknown error';
						FriendsterApp.showMessage('error', msg, {selector: msg_div, animate: false, timeout: false});
					}
				}, 'json');
				
				return false;
			});
			
		});
	}
};