jQuery.noConflict();

//Start all JQuery event assignments
 jQuery(document).ready(function($){
		  
	// UTILITY FUNCTIONS ********************************************************** 
	
	// If the nav item "community" is active, and just been clicked
	// then the "community home" subnav needs to be set
	
	// Checks to see if "community" is the selected tab
	// if so, it checks to see if the content tabs have no selected state
	// if they don't then that means that "community" is selected, and that the 
	// first tab should get a selected state
	
	if($('#primary_menu ul li#current a').text() == 'Network') {
		if(!$('#contenttabs ul li').hasClass('active')){
			$("#contenttabs ul li").eq(0).addClass("active");
		}
	}
	
	//set up the jqModal
	$('#dialog').jqm();

	
	 // due to how the tabs are added to the inbox, a special 
	 // class assignment is required for the cCol to position it
	 
	 if ($('#inbox_body').html() != null) {
	 $('#cCol').addClass('cCol_top_adjust'); }
	 
	 if ($('#writeMessageForm').html() != null) $('#cCol').addClass('cCol_top_adjust');
	 
	 if ($('#no_message').html() != null) $('#cCol').addClass('cCol_top_adjust');
	 
	 /* add the label hint text */
	 $('label.hint').labelOver('over');
	 
	 /* append the "beta" to the "network" link in the primary nav */
	 $("li.item11").append("<div id='beta'><img src='/community/templates/bms/images/icon_beta.gif' alt=' ' /></div>");
	 
	 /* add class "last_item" to "inbox" so it doesn't wrap */
	 $("#login_greeting ul.menu li:last").addClass('last_item');
	 
	 
/**
*	@name							Elastic
*	@descripton						Elastic is Jquery plugin that grow and shrink your textareas automaticliy
*	@version						1.6
*	@requires						Jquery 1.2.6+
*	@author							Jan Jarfalk jan.jarfalk@unwrongest.com
*/

(function($){ 
	$.fn.extend({  
		elastic: function() {
			
			//	We will create a div clone of the textarea
			//	by copying these attributes from the textarea to the div.
			var mimics = [
				'paddingTop',
				'paddingRight',
				'paddingBottom',
				'paddingLeft',
				'fontSize',
				'lineHeight',
				'fontFamily',
				'width',
				'fontWeight'];
			
			return this.each( function() {
				
				// Elastic only works on textareas
				if ( this.type != 'textarea' ) {
					return false;
				}
				
				var $textarea	=	$(this),
					$twin		=	$('<div />').css({'position': 'absolute','display':'none'}),
					lineHeight	=	parseInt($textarea.css('lineHeight'),10) || parseInt($textarea.css('fontSize'),'10'),
					minheight	=	parseInt($textarea.css('height'),10) || lineHeight*3,
					maxheight	=	parseInt($textarea.css('max-height'),10) || Number.MAX_VALUE,
					goalheight	=	0,
					i 			=	0;
				
				
				// Append the twin to the DOM
				// We are going to meassure the height of this, not the textarea.
				$twin.appendTo($textarea.parent());
				
				// Copy the essential styles (mimics) from the textarea to the twin
				var i = mimics.length;
				while(i--){
					$twin.css(mimics[i].toString(),$textarea.css(mimics[i].toString()));
				}
				
				// Sets a given height and overflow state on the textarea
				function setHeightAndOverflow(height, overflow){
					curratedHeight = Math.floor(parseInt(height,10));
					if($textarea.height() != curratedHeight){
						$textarea.css({'height': curratedHeight + 'px','overflow':overflow});
						
					}
				}
				
				
				// This function will update the height of the textarea if necessary 
				function update() {
					
					// Get curated content from the textarea.
					var textareaContent = $textarea.val().replace(/<|>/g, ' ').replace(/\n/g, '<br />').replace(/&/g,"&amp;");
					var twinContent = $twin.html();
					
					if(textareaContent+'&nbsp;' != twinContent){
					
						// Add an extra white space so new rows are added when you are at the end of a row.
						$twin.html(textareaContent+'&nbsp;');
						
						// Change textarea height if twin plus the height of one line differs more than 3 pixel from textarea height
						if(Math.abs($twin.height()+lineHeight - $textarea.height()) > 3){
							
							var goalheight = $twin.height()+lineHeight;
							if(goalheight >= maxheight) {
								setHeightAndOverflow(maxheight,'auto');
							} else if(goalheight <= minheight) {
								setHeightAndOverflow(minheight,'hidden');
							} else {
								setHeightAndOverflow(goalheight,'hidden');
							}
							
						}
						
					}
					
				}
				
				// Only run update when the textarea has focus
				$textarea.css({'overflow':'hidden'}).bind('focus',function() {
					self.periodicalUpdater = window.setInterval(function() {
						update();
					}, 50);
				}).bind('blur', function() {
					clearInterval(self.periodicalUpdater);
				});
				
				// Run update once when elastic is initialized
				update();
				
			});
			
        } 
    }); 
})(jQuery);

	// edit the status, toggle the edit with the read only controls / display
	$('#status_edit_trigger').click(function(){
		status_toggle();
		return false;
	});
	
	// assign the function to the status update button
	$('a#status_cancel') 
	.livequery(function(event) { 
		$(this).click(function(){
			status_toggle();
			return false;
		});
	});
	
	// 150 character max for resume details
	$('textarea.textarea150') 
	.livequery(function(event) { 
		$(this).dodosTextCounter(150, {counterDisplayElement: "span",counterDisplayClass: "counter_display"});
	});	
	
	
	
	/**
* DodosTextCounter - jQuery plugin for text limit for input or textarea. The counter shows how many chars are remaining.
*						It will also disable the user from entering more characters after s/he reaches the maximum
* Written by Ying Zhang aka Dodo
* http://pure-essence.net/2008/05/30/dodos-text-counterdodos-text-counter/
*/

jQuery.fn.dodosTextCounter = function(max, options) {
	// if the counter display doesn't exist, the script will attempt to create it
	options = $.extend({
		counterDisplayElement: "span",					// tag for the counter display
		counterDisplayClass: "dodosTextCounterDisplay",	// class for the counter display
		addLineBreak: true								// whether to add <br /> after the input element before the counter display
	}, options);

	$(this).each(function(i) {
		updateCounter(this, max, options, i);
		$(this).keyup(function() {
			updateCounter(this, max, options, i);
			return this;
		});
	});
	return this;
};

function updateCounter(input, max, options, index) {
	var currentLength = 0;
	var val = $(input).val();
	if(val) {
		currentLength = val.length;
	}
	if(currentLength > max) {
		$(input).val(val.substring(0, max));
	} else {
		var charLeft = max - currentLength;
		var counterDisplay = options.counterDisplayElement + "." + options.counterDisplayClass + ":eq("+index+")";
		var createNew = $(counterDisplay).length == 0;
		if(createNew) {
			var element = document.createElement(options.counterDisplayElement);
			if(options.counterDisplayElement == 'input') {
				$(element).val(charLeft.toString());
			} else {
				$(element).html(charLeft.toString());
			}
			$(element).addClass(options.counterDisplayClass).insertAfter($(input));
			if(options.addLineBreak) {
				$(input).after("<br />");
			}
		} else {
			if(options.counterDisplayElement == 'input') {
				$(counterDisplay).val(charLeft.toString());
			} else {
				$(counterDisplay).html(charLeft.toString());
			}
		}
		
	}
}
	
	save_status = function() {
	jax.call('community','home,ajaxUpdate', jQuery('#userStatus').val()); 
	set_status(); 
	status_toggle(); 
	return false;	
}

set_status = function() {
	if($('#userStatus').val() != '') {
		$('#status_content').text($('#userStatus').val());
		$('#shout_timestamp').show();
	} else {
		$('#status_content').text('Open Mic: Tell the Network what you\'re up to!');
		$('#shout_timestamp').hide();
	}
	
}

status_toggle = function() {
	$('.status_readonly').toggle();
	$('.status_edit').toggle();
	if($('#status_counter')) $('#status_counter').html('150');
	if($('textarea#userStatus')) $('textarea#userStatus').val('').elastic().focus();;
}

});
 
 /* label over */
jQuery.fn.labelOver = function(overClass) {
	return this.each(function(){
		var label = jQuery(this);
		var f = label.attr('for');
		if (f) {
			var input = jQuery('#' + f);
			
			this.hide = function() {
			  label.css({ textIndent: -10000 })
			}
			
			this.show = function() {
			  if (input.val() == '') label.css({ textIndent: 0 })
			}

			// handlers
			input.focus(this.hide);
			input.blur(this.show);
		  	label.addClass(overClass).click(function(){ input.focus() });
			
			if (input.val() != '') this.hide(); 
		}
	})
}

