
$(document).ready(function(){
	// Document is ready
	vote($('.vote'));
	notrelevant($('.notrel'));
	commentSubmit($('.post_comment'));
	textareaFocus();
	chartLoad($('.content_title'));
	externalLinks();
	
	// Hide share
	$('.shareWrap').hide();
	
	// Toggle share
	$('.a_share').click(function(){
		// $('.shareWrap').hide();
		$(this).parent().parent().children('.vote_area').children('.shareWrap').show();
		return false;
	});
	
	$('.share_facebook').click(function(){	
		var link = $(this).attr("rel");
window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(link)+'&t='+encodeURIComponent('Stone Roses'),'sharer','toolbar=0,status=0,width=626,height=436');
		return false;
	});
	
	// Flashing upload messages.
	if ($('.upload_msg')) {
		$('.upload_msg').fadeOut().fadeIn().fadeOut().fadeIn();
	};	
	
	if ($('.errors')) {
		$('.errors').fadeOut().fadeIn().fadeOut().fadeIn();
	};
	
	$('.a_more').toggle(
		function(){
			$(this).text('close');
			$('.about_hide').show();
			return false;
		},
		function(){
			$(this).text('more');
			$('.about_hide').hide();
			return false;
		}
	);
});

function chartLoad(link){
	$(link).click(function(){

		if ($('#player').length > 0) {
			ytplayer.stopVideo();
		};
		
		$('.content_area').empty();
		$('.content_area').hide();
		// The id of the content to load.
		var content_id = $(this).attr('rel');

		// The div to load it into
		var targetDiv = $(this).parent().parent().parent().children('.content_area');
		
		// Now check if the targetDiv opened or not. If it isnt load the content.
		if (!targetDiv.hasClass('opened')) {
			// Remove all opened classes from divs.		
			$('div .opened').removeClass('opened');
			// Add class to show the div is opened.	
			targetDiv.addClass('opened');			
			// console.log('no classs');
			$.ajax({
			  type: "GET",
			  url: "feed_content.php?c=" + content_id,
			  success: function(html) {
				targetDiv.append(html);
				targetDiv.show();
				commentSubmit($('.post_comment'));
				textareaFocus();
			  }				
			});
		} else {
			// Has a class of open. Here we could close it and then remove the html? The close cross icon will do this to. So use a function.
			targetDiv.removeClass('opened');
			targetDiv.empty();
		}
	});
	return false;
}

function vote(item){
	$(item).click(function(){
		$(this).children().load(this.href, function(){
			flasher('.vote_message');
		});		
		return false;
	});
}

function notrelevant(item){
	$(item).click(function(){
		$(this).children().load(this.href, function(){
			flasher('.vote_message');
		});		
		return false;
	});
}

function flasher(item){
	mouseMove(item);
	$(item).fadeIn('slow').fadeOut('slow').fadeIn('slow').animate({opacity: 1.0}, 800, function(){
		$(item).fadeOut('slow');
		$(item).remove();
	});
}

function mouseMove(item){
	$().mousemove(function(e){
	   $(item).css('left', e.pageX-22);
	   $(item).css('top', e.pageY-22);
	});
}


function limitChars(textclass, limit, countchars)
{
	var text = $('.'+textclass).val(); 
	var textlength = text.length;
	if(textlength > limit){
		$('.' + countchars).html('You cannot write more then '+limit+' characters!');
		$('.'+textclass).val(text.substr(0,limit));
		return false;

	} else {
		$('.' + countchars).html('You have '+ (limit - textlength) +' characters left.');
		return true;
	}
}

function commentSubmit(btn){
	$(btn).click(function(){
		// The comments section object
		var daddy = $(this).parent().parent();		
		// Comment counter - Find the current comments object and also the new value the comments should go to.
		// var voteCounter = $(this).parent().parent().parent().children('.comment_link');
		// var comment_count = parseInt($(this).parent().children("input.comment_count").val()) + 1;
		// console.log(comment_count);

		// Got to work out why it isnt posting comments is throwing an error????
		// console.log($(this).parent()); //This is the form.
		
		var comment = $(this).parent().children("textarea.selectedTextarea").val(); 
		var content_id = $(this).parent().children("input.content_id").val();
		var memberID = $(this).parent().children("input.member_id").val();    
		var dataString = 'body='+ comment + '&content_id=' + content_id + '&memberID=' + memberID;
			if (!comment) {
				alert("You need to add a comment before submitting");
				return false;
			} else {
				$.ajax({
				  type: "POST",
				  url: "commentprocess.php",
				  data: dataString,
				  success: function(html) {
					$('.comment_form').clearForm();
					daddy.replaceWith(html);
					textareaFocus();
					commentRebind();
					externalLinks();
				  }				
				});
			}
	return false;
	});
}

function textareaFocus(){
	// Function to add selected to a textarea in focus.
	$('.commentText').focus(function(){
		$('.commentText').removeClass('selectedTextarea');
		$(this).addClass('selectedTextarea');
		$('.infodiv').removeClass('countchars');
		$(this).parent().parent().children('.infodiv').addClass('countchars');
		$('.selectedTextarea').keyup(function(){
			limitChars('selectedTextarea', 140, 'countchars');
		});
	});	
}


function externalLinks(){
	//External links.
	$('a[rel="external"]').click( function() {
		window.open( $(this).attr('href') );
		return false;
	});
}

function commentRebind(){
	commentSubmit($('.post_comment'));
}

$.fn.clearForm = function() {
  return this.each(function() {
 var type = this.type, tag = this.tagName.toLowerCase();
 if (tag == 'form')
   return $(':input',this).clearForm();
 if (type == 'text' || type == 'password' || tag == 'textarea')
   this.value = '';
 else if (type == 'checkbox' || type == 'radio')
   this.checked = false;
 else if (tag == 'select')
   this.selectedIndex = -1;
  });
};
