/**
 * @author @davkell
 * @link http://spotify.twiteye.com/scripts/app.js
 * @copyright Ambient Age, 2009 (http://www.ambientage.com)
 */


var aa = {};
 	
$(document).ready(function(){
	
	$('noscript').remove();
	
	if (aa.spotify.status !== null && aa.spotify.status == 'err') {
		// No playlists found...sucks.
		alert(aa.spotify.msg + ' Sorry!!');
	}
	else {
		// we've got some playlists to play with. Woop!
		var playListCount = aa.spotify.length;
		var listBlock = '';
		var rowCount = 0;

		for (var z = 0; z < playListCount; z++) {
			
			var link = aa.spotify[z].links;				// where's the playlist?
			var lengthen = (aa.spotify[z].isShortLink != 'isShort') ? 'isLong' : 'isShort';	// need a longurlplease? 
			
			if(rowCount == 0){	// break into rows of 3
				listBlock += '<div class="display-row">';
			}
			rowCount++; 
			
			listBlock += '<div class="list-block"><div class="mug"><a href="' + aa.spotify[z].permalink + '" title="' + aa.spotify[z].from_user + '"><img src="' + aa.spotify[z].profile_pic + '" alt="Mugshot" /></a></div>' +
								'<div class="tweet">' + aa.spotify[z].tweet + '</div>' +
								'<div id="' + aa.spotify[z].id + '" class="link-block"><a rel="external" class="' + lengthen + '" href="' + link + '">' + link + '</a>';
			if(lengthen == 'isShort'){
				listBlock += '    <a href="#" class="expand-link">Expand</a>';
			}		
			
			listBlock += '</div>';		// .link-block 
			
			/*
			 * Tags:
			 * Soon....soon....
				 if(typeof(aa.spotify[z].tags) != 'undefined'){
					listBlock += '<div class="tags">';
					for(var i = 0; i < aa.spotify[z].tags.length; i++){
						listBlock += aa.spotify[z].tags[i];
					}
					listBlock += '</div>';
				} */
			
			listBlock += '</div>';		// .list-block
			if(rowCount == 3){
				listBlock += '</div>';	// .display-row
				rowCount = 0;
			}
		}
		$('#home-display').livequery(function(){
			$(this).append(listBlock);
		
			$('#home-display').cycle({ 
					fx:      'scrollDown', 
				  	timeout: 0,
					next: 	 '#next-button' 
				});
		});
		
	}
	
	/**
	 * Option to expand short links
	 * 
	 * Hearty compliments to great work by longurlplease: 
	 * @link http://www.longurlplease.com/ 
	 */
	$('a.expand-link').livequery('click', function(){
		
		$block = $(this).parent('div.link-block').attr('id');
		$(this).hide();
		$('#'+$block).longurlplease({lengthenShortUrl: 'smart'});
		return false;
	});
	
	$('#next-button').mouseover(function(){
		$(this).css({cursor: 'pointer'});
	});
	
	
	/**
	 * Open playlist urls in new window
	 */
	$('a[rel="external"]').livequery('click', function(){
		$(this).attr('target', '_blank');
	
		/* 
		 * This is for something that isn't ready just yet...
		 *
			$.post(
				'/user/play/',
				{p: $(this).parent('div.link-block').attr('id')},
				function(data){},
				'json'
		); */
	});  
	
	/**
	 * fiddle with the feedback form
	 */
	$('#name').val('   @name');
	$('#feedback').val('    [my great idea for improving things]');
	$('input[type="text"]').addClass('funkyText')
		.focus(function(){
			$(this).val('');
			$(this).removeClass('funkyText').addClass('funkyTextFocus');
	}).blur(function(){
		$(this).removeClass('funkyTextFocus').addClass('funkyText');
	});
	
	$('form').submit(function(){
		$('.error').remove();
		if($('#name').val().indexOf('@name') != -1 || $('#name').val()== ''){
			$('form').after('<div class="error">You forgot your name!</div>');
			return false;
		}else{
			if($('#feedback').val().indexOf('[my great idea for improving things]') != -1 || $('#feedback').val() == ''){
				$('form').after('<div class="error">You forgot your suggestion!</div>');
				return false;
			};
		}
		return true;	
	});
});
