/*
	Navigation "Drop Down"
*/

//jQuery.noConflict();

/*
jQuery('ul#nav li').mouseenter(function(){jQuery(this).find("ul").css('display','block');});
jQuery('ul#nav li').mouseleave(function(){jQuery(this).find("ul").css('display','none');});
*/

jQuery('ul#nav li a').click(function(e){

	//Disable if there is no dropdown
	if(!jQuery(this).parent('li').children('ul').length){
		return true;
	}

	e.preventDefault();

	//Switch Classes
	jQuery('ul#nav li').removeClass('active');
	jQuery('ul#nav li').removeClass('current_page_item');
	jQuery(this).parent().addClass('active');

	//Hide all the other subnavs
	jQuery('ul#nav li ul').css('display','none');

	jQuery(this).parent().find("ul").stop().css('display','block');

});
//jQuery('ul#nav li').mouseleave(function(){jQuery(this).find("ul").css('display','none');});

//Lets try and hide all the dropdowns that aren't currently active
jQuery('ul#nav li > ul').each(function(a, b){

	var parent = jQuery(b).parent();

	if(jQuery(parent).hasClass('active')){
				
		jQuery(b).css('display','block');
	}

});

/*

	Input Hover Text Replacement

*/

jQuery('input:not([type=submit]), textarea:not(#twitter_post_content)').each(function(n, el){

	if(!jQuery(this).hasClass('no_remove')){

		var val = jQuery(el).val();
		
		jQuery(el).focus(function(){
			
			if(jQuery(this).val() == val){
				jQuery(this).val('');
				jQuery(this).addClass('input_active');
			}
			
		});
	
		jQuery(el).blur(function(){
			
			if(jQuery(this).val() == ''){
				jQuery(this).val(val);
				jQuery(this).removeClass('input_active');
			}
			
		});

	}
	
});

/*

	Remove spaces from a twitter usernam

*/

jQuery('#author').keyup(function(){
	
	jQuery(this).val(jQuery(this).val().replace(' ',''));

});

/*

	Quick hack to stop the comments from submitting blank comments

*/

if(jQuery('#respond input#submit').length){

	jQuery('#respond input#submit').click(function(event){
	
		if(jQuery('#author').val() == 'Twitter Username' || jQuery('#comment').val() == 'Write a comment...'){
		
			event.preventDefault();
			return false;		
		}
	
	});
	
}

/*

	TipTip

*/

jQuery("#social ul li a").tipTip({
	defaultPosition: 'bottom',
	delay: 0
});

/*

	Twitter @anywhere

*/

twttr.anywhere(function (T) { T.hovercards(); });

/*

	Post to Twitter

*/

if(jQuery('#post_to_twitter').length){

	jQuery('#post_to_twitter').click(function(){
	
		if(jQuery('#twitter_post_content').val() != ''){
				
			window.parent.location.replace('http://twitter.com/home?status=' + jQuery('#twitter_post_content').val());
		
		}
	
	});

}

/*

	Comment color cycle
	This may be only temporary and it may get a little dirty.
	Hope you brought a rain coat.

*/

/*
if(jQuery('ol.commentlist').length){

	var comment_count = 0;

	jQuery('ol.commentlist li').each(function(a,b){
	
		if(jQuery(b).hasClass('red')){
			//do nothing
		}else{

			var colors = ['orange','blue','green','yellow'];

			jQuery(b).addClass(colors[comment_count]);
		
			comment_count = comment_count == 2 ? 0 : comment_count += 1;
			
		}
	
	});

}
*/

/*

	Textarea Counter and Limit

*/

if(jQuery('#share_on_twitter').length){

	var textarea = jQuery('#twitter_post_content');
	var tweet = textarea.val();
	var count = jQuery('#twitter_count');
	
	//Set the initial twitter count
	count.text(140 - tweet.length);

	//Do a quick check to see if the intial is too long
	if(tweet.length >= 140){
		
		count.addClass('end');		
		textarea.val(tweet.substr(0,140));
	
	}
		
	//Lets calculate the current number of remaining characters
	textarea.keyup(function(i){
	
		var tweet = textarea.val();
	
		if(tweet.length >= 140){
			
			textarea.val(tweet.substr(0,140));
			count.addClass('end');
			count.text('0');	
				
		}else{
		
			count.removeClass('end');
			count.text(140 - tweet.length);
		
		}
	
	});

}
