/**
	UNDP is the global namespace container
	@namespace
	@requires jQuery.js
*/

var UNDP = {};

/**
	@class
	@description General page initialization
*/
UNDP.general = {

	/**
		Initialize the app functions
	*/
	init: function() {
		UNDP.general.setGlobals();
		UNDP.share.init();
		$('.social .newsletter a').click( function(e) {
			e.preventDefault();
			var url = $(this).attr('href');
			UNDP.newsletter.init(url);
		});
	},
	
	/**
		Set up global functions
	*/
	setGlobals: function() {
		$('a[rel=external]').attr('target','_blank');
	}
	
};

/**
	@class
	@description Share 
*/
UNDP.share = {

	/**
		Initialize the app functions
	*/
	init: function() {
		$("#header .share-this a").click( function(e) {
			e.preventDefault();
			
			var service = $(this).parents('li:first').attr("class");
			var kickout = (service == "facebook") ? 'kick out poverty' : '#kickoutpoverty';
			var title = encodeURIComponent( "I'm helping UNDP, Zidane and Drogba stand up and "+kickout+" by 2015! Get involved at http://www.kickoutpoverty.org" );
			var href = 'http://www.kickoutpoverty.org';
					
			switch(service) {
			  	case "facebook": 
			  		win = 'http://www.facebook.com/sharer.php?u='+href+'&t='+title;
			  		break;
				case "twitter":
					win = 'http://twitter.com/home?status='+title;
					break;
					
/*
				case "delicious":
					win = 'http://delicious.com/save?v=5&noui&jump=close&url='+location.href+'&title='+document.title;
					break;
			  	case "digg":
			  		win = 'http://digg.com/submit?phase=2&url='+location.href+'&title='+document.title;
			  		break;
			  	case "myspace":
			  		win = 'http://www.myspace.com/index.cfm?fuseaction=postto&t='+document.title+'&c=CONTENTS&u='+location.href;
			  		break;
			  	case "mixx":
			  		win = 'http://www.mixx.com/submit?page_url='+location.href;
			  		break;
			  	case "stumbleupon":
			  		win = 'http://www.stumbleupon.com/submit?url='+location.href+'&title='+document.title;
			  		break;
			  	case "reddit":
			  		win = 'http://www.reddit.com/submit?url='+ location.href + '&title='+document.title;
			  		break;
*/
			  	}
			  	window.open(win,service,'toolbar=0,status=0,width=550,height=550');
		});
	}
	
};


/**
	@class
	@description Newsletter 
*/
UNDP.newsletter = {

	/**
		Initialize the newsletter signup
	*/
	init: function(url) {
	
		$.fn.colorbox({
			href:url,
			width:'366px',
			height:'362px',
			opacity:0.65,
			scrolling: false,
			iframe: true,
			onComplete:function(){
			    UNDP.signupValidation.init();
			}
		}); 
	},
	
	close: function() {
		$('#newsletter-signup fieldset').replaceWith('<h2>Thanks for joining!</h2>');
	}
	
};

/*
 *	Validation
 */
UNDP.signupValidation = {

	/**
		Initializes app validation
	*/
	init: function() {
	
		this.setValidationDefaults();
		$('#newsletter-signup').validate({
		
/*
			submitHandler: function(form) {
				//$.fn.colorbox.close();
				var url = form.action;
				var data = $(form).serialize();
				$.ajax({
					type: 'POST',
					url: url,
					data: data,
					success: function(resp) {
						$('#newsletter-signup fieldset').replaceWith('<h2>Thanks for joining!</h2>');
					}
				});	
			},
*/
		
			rules: {
				'Email': {
					required: true,
					email: true
				},
				'Country': {
					required: true
				}
			},
			
			messages: {
				'Email': {
					required: "Please enter your e-mail address",
					email: "Please enter a valid e-mail address"
				},
				'Country': {
					required: "Please choose your country"
				}
			}
			
		});
		
	},
	
	/**
		Sets default properties for the validator
	*/
	setValidationDefaults: function(){
	
		jQuery.validator.setDefaults({
			onfocusout: false,
			onkeyup: false,
			onclick: false,
			focusInvald: false,
			focusCleanup: false,
			errorPlacement: function(error, element) {
				var $g = $(element).parents('.field-group:first');
				$g.addClass('field-error');
				var $li = $('<li></li>').html( $(error).html() );
				$li.appendTo('#error-set ul');
			},
			invalidHandler: function(form, validator) {
				// reset
				$('#error-set ul').html('');
				$('form .field-error').removeClass('field-error');
				// go!
				$('#error-set').show();
			}
		});


	}
	
};




/**
	On document ready
*/
jQuery(function($) {

	UNDP.general.init();
	UNDP.signupValidation.init();
	
});



