// Page Specific JavaScript Document

$(function() {
/**
 * Enter description here...
 *
 * @param void
 * @access public
 */
	$(document)
		.bind('init.facebox', function() {
			$.extend($.facebox.settings, {
				opacity : .5,
				overlay      : false,
				loadingImage : '/js/jquery/plugins/facebox/loading.gif'
			});
		})
		.bind('loading.facebox', function() {
			$("#facebox .body").css('width', '465px');
			$('#facebox').css('left', $(window).width() / 2 - ($('#facebox .body').width() / 2))
		})
		.bind('beforeReveal.facebox', function() {
			// Remove footer part.
			$("#facebox .footer").remove();
		})
		.bind('afterReveal.facebox', function() {
			// Create title area.
			if (!$("#facebox h2.dialog_title").is("h2.dialog_title")) {
				var _insert = $(document.createElement('h2')).insertBefore('#facebox .content');
				var _span = $(document.createElement('span'));
				_span.text($(".dialogTitle").text());
				$(".dialogTitle").remove();
				_insert.addClass('dialog_title').append(_span);				
			}
		});
/**
 * Enter description here...
 *
 * @param void
 * @access public
 */
	$("#ConnectDialogButton[class*=facebox]").click(function(){
		var _uid = $(this).attr('uid');										
		
		$.facebox(function() {
				
			$.get(ecotti.app.webroot + 'ajax/profiles/connect', {uid: _uid}, afterReveal, 'html');

			function afterReveal(data, status) {
				$.facebox(data);
				
				
				$(".UIConnectControls_AddMessageLink").click(function(){
					$(this).hide();
					$(".UIConnectControls_Message").slideDown('fast');	
					return false;
				});
				
				$("#ConnectButton").click(function(){

					$("#ConnectForm").ajaxSubmit({
						url: ecotti.app.webroot + 'ajax/profiles/connect_send_request',
						type: 'post',
						dataType: 'json',
						beforeSubmit: function(formData, jqForm, options){
							$("#ConnectButton").attr('disabled', 'disabled');
						},
						success: function(data, status){
							$("#ConnectButton").removeAttr('disabled');
							
							$(".message").remove();
							$(".error-message").remove();
					
							if (data.errors) {
								validationErrors(data.errors);
								
							} else if (data.success) {
								
								$(".dialog_title").remove();
								$(".dialog_body").remove();
								$(".dialog_buttons").remove();
								
								// Creates stauts box
								var _insert = $(document.createElement('div')).css('display', 'none');
								
								var _statusTitle = $(document.createElement('h2'))
									.addClass('StatusTitle')
									.text(data.success.message[0]);
									
								var _statusMessage  = $(document.createElement('div'))
									.addClass('StatusMessage')
									.text(data.success.message[1]);

								_insert.addClass('UIConnectControls_Standard_Status')
									.insertBefore($("#facebox .content"))
									.append(_statusTitle).append(_statusMessage).fadeIn();

								window.setTimeout(function() {
									$(document).trigger('close.facebox');
									$(".UIConnectControls_Standard_Status").remove();
									$("#AddFriend").slideUp('fast');
								}, 2000);

							}							
						}
					});
																		  
					return false;
				});
				
			}
		});
		return false;	
	});
/**
 * Enter description here...
 *
 */
	// Semantic in-field input label. 
	$("#WallBody").inputLabel(false,{color:"#666"});

	var _loadingDiv = $("#loadingDiv");

	//$("#WallShareButton:submit").attr('disabled', 'disabled');	
	//$("#WallBody").keypress(function() {
	//	$("#WallShareButton:submit").removeAttr('disabled');	
	//});

	$("#WallShareButton").bind('click', function() {
		
		var value = $("#WallBody").val();
		var label = $("label[for=WallBody]").text();
		
		if (value != label) {
			
			$("#WallAddForm").ajaxSubmit({
				url: ecotti.app.webroot + 'ajax/walls/edit',
				type: 'post',
				dataType: 'json',
				beforeSubmit: function(formData, jqForm, options){
					_loadingDiv.show();
				},
				success: function(data, status){
					$(".message").remove();
					$(".error-message").remove();
			
					if (data.errors) {
						flashMessage(data.errors.message);
						validationErrors(data.errors);
						
					} else if (data.success) {
						flashMessage(data.success.message);
						_loadingDiv.hide();
						window.location.href = ecotti.app.here;
					}							
				}
			});
		}
		return false;
	});

	function flashMessage(message) {
		var _insert = $(document.createElement('div')).css('display', 'none');
		_insert.attr('id', 'flashMessage').addClass('message').text(message);
		_insert.insertBefore($(".posts")).fadeIn();
	}
/**
 * Ajax form validation.
 *
 */
	function validationErrors(data) {
		//flashMessage(data.message);
		$.each(data.data, function(model, errors) {
			for (fieldName in this) {
				var element = $("#" + camelize(model + '_' + fieldName));
				var _insert = $(document.createElement('div')).insertAfter(element);
				_insert.addClass('error-message').text(this[fieldName])
			}
			_loadingDiv.hide();
		});
	};

	function camelize(string) {
		var a = string.split('_'), i;
		s = [];
		for (i=0; i<a.length; i++){
			s.push(a[i].charAt(0).toUpperCase() + a[i].substring(1));
		}
		s = s.join('');
		return s;
	}
	
	// jQuery textarea auto-grow, auto-height 
	$('.UIComposer .DOMControl_autogrow', "#FeedwallComposer").autogrow({
		maxHeight: 1000, minHeight: 30, lineHeight: 15
	});

});