// Namespace

theMotherhood = {};
postManager = {};

area2 = undefined;
areaVideo = undefined;
areaAudio = undefined;
areaImage = undefined;

// Global Vars

/* theMotherhood.baseUrl = "http://motherhood.dev.deeplocal.com"; */

// Class

	postManager.initDefaultText = function(){
				
			$(".defaultText").focus(function(srcc)
			{
				if ($(this).val() == $(this)[0].title)
				{
				//	$(this).removeClass("defaultText");
				//	 $(this).addClass("defaultTextActive");
					$(this).val("");
				}
			});
			
			$(".defaultText").blur(function()
			{
				if ($(this).val() == "")
				{
				//	$(this).removeClass("defaultTextActive");
				//	$(this).addClass("defaultText");
					$(this).val($(this)[0].title);
				}
			});  
		   $(".defaultText").blur();        
		
		};
	
	postManager.initMenu = function() {
			$('#menu li a').click(function(e) {
				e.preventDefault();
				this.blur();
				if ($(this.parentNode).hasClass('selected')) {
					return;
				}
				$('#menu li').removeClass('selected');
				$(this.parentNode).addClass('selected');
				var link = this;
				var postType = $(link).text().toLowerCase();
				$("#submitType").text(postType);
				$('#forms').animate({'opacity' : 0}, 250, function() {
					$('#forms').children().css('display', 'none');
					$('#forms .' + $(link).text().toLowerCase().replace(" ","")).css('display', 'block');
					$('#forms').animate({'opacity' : 1}, 250);					
					switch($(link).text().toLowerCase()){
						case "story":
							if(typeof area2 == 'undefined'){
								area2 = new nicEditor({iconsPath: '/js/nicEdit/nicEditorIcons.gif', maxHeight: 103, fullPanel : false, buttonList:['bold','italic','underline','left','center','ol','ul','link','image']}).panelInstance('text_body');
							}
						break;
						case "video":
							if(typeof areaVideo == 'undefined'){
								areaVideo = new nicEditor({iconsPath: '/js/nicEdit/nicEditorIcons.gif', maxHeight: 38, fullPanel : false, buttonList:['bold','italic','underline','left','center','ol','ul','link','image']}).panelInstance('video_comment');
							}
						break;
						case "voice":
							if(typeof areaAudio == 'undefined'){
								areaAudio = new nicEditor({iconsPath: '/js/nicEdit/nicEditorIcons.gif', maxHeight: 54, fullPanel : false, buttonList:['bold','italic','underline','left','center','ol','ul','link','image']}).panelInstance('audio_body');
							}
						break;
						case "photo":
							if(typeof areaImage == 'undefined'){
								areaImage = new nicEditor({iconsPath: '/js/nicEdit/nicEditorIcons.gif', maxHeight: 43, fullPanel : false, buttonList:['bold','italic','underline','left','center','ol','ul','link','image']}).panelInstance('image_body');
							}
						break;
						default: break;
						
					}
					
					
				});
				
			});
		};
		
	postManager.initForms = function() {
			$('#forms input, #forms textarea').focus(function(e) {
				$(this).addClass('active');
			});
			
			$('#forms input, #forms textarea').blur(function(e) {
				$(this).removeClass('active');
			});
		};
		
	postManager.submitCurrentForm = function(e) {
			e.preventDefault();
			
			var children = $('#forms').children();
			for (var i=0; i<children.length; i++) {
				if ($(children[i]).css('display') == 'block') {					
					postManager.verifyForm(children[i]);
					break;
				}
			}
		};
		
	postManager.verifyForm = function(frm){
		var facebookStr = "";
		
		
		if ($('#sendToFacebook:checked').val() !== null && typeof $('#sendToFacebook:checked').val() !="undefined") {
			facebookStr = "sendtofacebook";
		}
		
		
		var error = false;
		
	
		var contentType = $(frm).children("form").children(":input[name=formType]").val();
		
		$(frm).children("form").children(":input[name=sendToFacebook]").val(facebookStr);
		
				
		var requiredFields = new Array();
		
		$(".defaultText").each(function(){
			if($(this).val() == $(this).attr("title")){
				$(this).val("");
			}
		});
		

		
		
		switch(contentType){
		case "createAccount":	
			postManager.verifyCreateAccount();
			return;
		break;
		case "note":
		  if($("#note_body").val()==""){
			  	error = true;
 				$("#note_body").addClass("error");
 				requiredFields.push("A Note Body");
		  }
		  break;    

		case "text":
		
		$("#text_body").val(nicEditors.findEditor('text_body').getContent());
		  if($("#text_title").val()==""){
			  	error = true;
 				$("#text_title").addClass("error");
 				requiredFields.push("A Story Title");

		  }
		  if($("#text_body").val()=="" || $("#text_body").val()=="tell your story here"){
			  	error = true;
 				$("#text_body").addClass("error");
 				requiredFields.push("A Story Body");

		  }
		  break;
		
		case  "photos":
			$("#image_body").val(nicEditors.findEditor('image_body').getContent());
		
			if($("#image_title").val()==""){
			  	error = true;
 				$("#image_title").addClass("error");
 				requiredFields.push("An Image Title");

			
			}
			if($("#image").val()=="" && $("#photo_url").val()==""){
				
			  	error = true;
 				$("#photo_url").addClass("error");	
 				requiredFields.push("A Photo");

			}
		break;
		
		case "sounds" :
			$("#audio_body").val(nicEditors.findEditor('audio_body').getContent());
			
			if($("#audio_title").val()==""){
			  	error = true;
 				$("#audio_title").addClass("error");
 				 requiredFields.push("A Title for your Audio");

			}		
			
			if($("#audio_file").val()==""){
			  	error = true;
 				$("#audio_title").addClass("error");
 				 requiredFields.push("A File for your Audio");

			}		
		break;	

		case "video":
			$("#video_comment").val(nicEditors.findEditor('video_comment').getContent());
			
			if($("#video_title").val()==""){
				 requiredFields.push("A Title for your Video");
			  	error = true;
 				$("#video_title").addClass("error");
			}	
			if($("#videoURL").val()==""){
			  	error = true;
 				 requiredFields.push("An embed code for your Video");

 				$("#videoURL").addClass("error");
			}	
		break;
		
		case "links":
		if($("#link_title").val()==""){
			  	error = true;
 				$("#link_title").addClass("error");
				 requiredFields.push("A Title for your Link");

			}	
		if($("#link_url").val()==""){
			  	error = true;
 				$("#link_url").addClass("error");
				requiredFields.push("A URL for your Link");

			}	
		break;
		
		case "quotes":
			if($("#quote_body").val()==""){
			  	error = true;
 				$("#quote_body").addClass("error");
				 requiredFields.push("A Body for your Quote");

			}	
		if($("#quote_by").val()==""){
			  	error = true;
 				$("#quote_by").addClass("error");
 				 requiredFields.push("An author for your Quote");

			}		
		break;
		default:
			
}
		
			
			if(error === true){
				$(".defaultText").blur();
				var str="";
				for (var i=0; i<requiredFields.length; i++) {
					str += "<div class='requiredField'>"+requiredFields[i]+"</div>";
				}				
				var errorstr = "<div class='warningBox' style='text-align: center'>The following fields are required: "+str+"</div>"
				
				$("<div/>").addClass("flora").html(errorstr).dialog({
					buttons:{
						"Ok" : function(){ 
							$(this).dialog("close"); $(this).remove(); 
						}
					},
					modal : true
				});
			
			
			}else{
		if(contentType=="photos"){
		$("<div/>").addClass("flora").attr("id","loadingDialog").html("<div style='text-align:center; color: #48453E'>Your photo is being uploaded.<br/><img src='/images/v3/loading.gif' /></div>").dialog();
		}else if(contentType =="sounds"){
		$("<div/>").addClass("flora").attr("id","loadingDialog").html("<div style='text-align:center; color: #48453E'>Your audio is being uploaded.<br/><img src='/images/v3/loading.gif' /></div>").dialog();
		
		}
				$(frm).children("form").submit();
			
			}
			
		
		};


		postManager.verifyCreateAccount = function(){
		$(".error").remove();
		var emailAddress = $('#createEmail').val();
		var regex = /^[-_.a-z0-9]+@(([-_a-z0-9]+\.)+(ad|ae|aero|af|ag|ai|al|am|an|ao|aq|ar|arpa|as|at|au|aw|az|ba|bb|bd|be|bf|bg|bh|bi|biz|bj|bm|bn|bo|br|bs|bt|bv|bw|by|bz|ca|cc|cd|cf|cg|ch|ci|ck|cl|cm|cn|co|com|coop|cr|cs|cu|cv|cx|cy|cz|de|dj|dk|dm|do|dz|ec|edu|ee|eg|eh|er|es|et|eu|fi|fj|fk|fm|fo|fr|ga|gb|gd|ge|gf|gh|gi|gl|gm|gn|gov|gp|gq|gr|gs|gt|gu|gw|gy|hk|hm|hn|hr|ht|hu|id|ie|il|in|info|int|io|iq|ir|is|it|jm|jo|jp|ke|kg|kh|ki|km|kn|kp|kr|kw|ky|kz|la|lb|lc|li|lk|lr|ls|lt|lu|lv|ly|ma|mc|md|mg|mh|mil|mk|ml|mm|mn|mo|mp|mq|mr|ms|mt|mu|museum|mv|mw|mx|my|mz|na|name|nc|ne|net|nf|ng|ni|nl|no|np|nr|nt|nu|nz|om|org|pa|pe|pf|pg|ph|pk|pl|pm|pn|pr|pro|ps|pt|pw|py|qa|re|ro|ru|rw|sa|sb|sc|sd|se|sg|sh|si|sj|sk|sl|sm|sn|so|sr|st|su|sv|sy|sz|tc|td|tf|tg|th|tj|tk|tm|tn|to|tp|tr|tt|tv|tw|tz|ua|ug|uk|um|us|uy|uz|va|vc|ve|vg|vi|vn|vu|wf|ws|ye|yt|yu|za|zm|zw)|(([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5])\.){3}([0-9][0-9]?|[0-1][0-9][0-9]|[2][0-4][0-9]|[2][5][0-5]))$/i;
		results = regex.test(emailAddress);
		if(emailAddress != "" && emailAddress != undefined && emailAddress.length >=5 && results ){
			if($("#createUsername").val()!="" &&$("#createUsername").val()!="username"){		
				if($("#createPassword").val()!="" && $("createPassword").val()!="password" ){
					document.createAccountForm.submit();
				}else{
					$("<div/>").addClass('error').html("Please enter in a valid password").insertAfter("#createPassword");
				}
			}else{
				$("<div/>").addClass('error').html("Please enter in a valid username").insertAfter("#createUsername");
			}
		}else{
			$("<div/>").addClass('error').html("Please enter in a valid email").insertAfter("#createEmail");
			
		}
		
			$(".defaultText").blur();
		
		};
		
		postManager.showLoading = function(msg) {
			if (typeof text == "undefined") {
				msg = "loading";
			}
			
			$('#forms, #submitButton').animate({'opacity' : 0}, 250, function() {
				$('#forms, #submitButton').css({
					'display' : 'none',
					'opacity' : 1
				});
				
				$('#loading span').text(msg);
				$('#loading').css({
					'opacity' : 0,
					'display' : 'block'
				}).animate({'opacity' : 1}, 250);
			});
		};
		
		postManager.hideLoading = function() {
			$('#loading').animate({'opacity' : 0}, 250, function() {
				$('#loading').css({
					'display' : 'none',
					'opacity' : 1
				});
				
				$('#forms, #submitButton').css({
					'opacity' : 0,
					'display' : 'block'
				}).animate({'opacity' : 1}, 250);
			});
		};
	
