/*
 @ChangeDate:	$LastChangedDate: 2009-02-25 14:50:41 +0000 (Wed, 25 Feb 2009) $
 @Revision:		$Rev: 22 $
 @Author: 		$Id: add.js 130 2009-03-09 23:08:12Z philippryce $
*/

$.postJSON = function(url, data, callback) {
	$.post(url, data, callback, "json");
};


$.fn.lyrical = function()
{
	
	var self = this;
	
	return this.each(function()
    {
    	var $this = $(this);
    	var informationTimer;
    	
    	this.informationSet = false;
    	
    	var self = this;
    	
    	this.onSubmit = function (e)
    	{
    		if ($('#artist').val() == '') { msg = 'You haven\'t entered the artist\'s name.'; }
    		if ($('#track').val() == '') { msg = 'You haven\'t entered the track\'s name.'; }
    		if ($('#lyrics').val().length > 255) { msg = 'You\'ve entered a lyric that is too long.'; }
    		if (msg) {
    			$.popper( {title: 'Problem Submitting Lyric', content: msg} );
    			return false;
    		}
    		return false;
    	};
    	
    	// the JSON requester
    	this.lookup = function (e)
    	{
    		$.postJSON('/json/lyrics', {track: $('#track').val(), artist: $('#artist').val()}, function (json)
    		{
    			if (json.error) {
    				$.popper( {title: 'Problem Fetching Lyrics', content: json.error} );
	    			return;
    			}
    			self.onlookup(json);
    		});
    	}
    	// the JSON responder
    	this.onlookup = function (json)
    	{
    		var lyrics = json.lyrics;
    		if (lyrics.toLowerCase() == 'not found') {
    			$.popper( {title: 'Problem Fetching Lyrics', content: 'That lyric could not be found.'} );
    			return;
    		}
    		else {
    			$('#lyricLookUp').stop().animate({opacity: 0}, 'fast').css('visiblity', 'hidden');
    			$('.lyricSelector').css('display', 'inline-block');
    			$('.lyricSelectorHelp').css('display', 'block');
    			$('#lyrics').css('marginLeft', '21%');
    			
    			lines = lyrics.split("\n");
    			$('.lyricSelector option').remove();
    			for (i in lines) {
     				html = '<option>' + lines[i] + '</option>';
	    			$(html).appendTo('.lyricSelector');
    			}
    			
    			$('.lyricSelector').bind('change', toggleLyricLine);
    			updateCharsLeft();
    		}
    		
    	}
    	function toggleLyricLine (e) {
    		lyric = $('.lyricSelector').val();
    		updateCharsLeft()
    		$('#lyrics').val( lyric.join("\n") );
    	}
    	    	
    	function onInformation (e)
    	{
    		if (self.informationSet == true) {
    			$('#lyricLookUp').css('visibility', 'visible').stop().animate({opacity: 1}, 'slow');
    		} else {
    			$('#lyricLookUp').stop().animate({opacity: 0}, 'fast');
    		}
    	}
    	function checkInformation (e) {
    		if ($('#artist').val() == '' || $('#track').val() == '') {
    			self.informationSet = false;
    		} else {
    			self.informationSet = true;
    		}
    		if (informationTimer) {clearTimeout(informationTimer);}
    		setTimeout(onInformation, 1000);
    	}
    	function updateCharsLeft (first) {
    		count = $('#lyrics').val().length;
    		left = 255 - count;
    		if (first) {$('#lyrics').bind('keyup', updateCharsLeft);}

    		if(count == 255){
    		    $('.charsLeft').text("0 Characters Left").css("color","#D51313");
    		} else if (left < 0){
    		    $('.charsLeft').text("You're Over by " + (left*-1) + " Characters").css("color","#D51313");
    		    $('#lyricForm button:submit').attr('disabled', "disabled");
    		} else if(left <= 4){
    		    $('.charsLeft').text(left + " Characters Left").css("color","#D51313");
    		    $('#lyricForm button:submit').removeAttr('disabled');
    		} else if(left <= 10 && left > 4){
    		    $('.charsLeft').text(left + " Characters Left").css("color","#8f0d0d");
    		    $('#lyricForm button:submit').removeAttr('disabled');
    		} else if(left <= 20 && left > 10){
    		    $('.charsLeft').text(left + " Characters Left").css("color","#7d3738");
    		    $('#lyricForm button:submit').removeAttr('disabled');
    		} else {
    		    $('.charsLeft').text(left + " Characters Left").css("color","#888984");
    		    $('#lyricForm button:submit').removeAttr('disabled');
    		}
    				
    	}
    	
    	function onLoad (e) {
    		$('#lyricLookUp button.cancel').bind('click', function () { $('#lyricLookUp').animate({opacity: 0}, 'fast'); });
    		$('#lyricLookUp button.find').bind('click', function () {  self.lookup(); });
    		
    		updateCharsLeft(true);
    	}
    	
    	$(this).find('#artist').bind('keyup', checkInformation).end().find('#track').bind('keyup', checkInformation);
    	
    	$this.bind('submit', function (e) {
    		return self.onSubmit(e);
    	});
    	
    	
    	$(document).ready(onLoad);
    });
};

$(function () {
	$('form#lyricForm').lyrical();
});