function exclusiveSubmit(cid)
{
	var email 	= $('#exclusive_form_email').val();
	var url 	= 'http://app.topspin.net/api/v1/fan/create_fan?resp_format=xml&fan%5Bsource_campaign%5D=http://app.topspin.net/api/v1/artist/3711/campaign/'+cid+'&fan%5Bemail%5D=' + email + '&fan%5Breferring_url%5D=http://transcendentman.com/&fan%5Bconfirmation_target%5D=http://transcendentman.com/confirm-download/';
	
	if(email != 'Enter your e-mail address...') {
		$.ajax({
			type: "POST",
			url: url,
			success: function(msg){
				$('.exclusive-form').animate({ "marginTop":"-=53px" , opacity:0 }, 750);
				
				$('.exclusive-thankyou').css({"display":"block"});
				$('.exclusive-thankyou').animate({ "marginTop":"-=43px" , opacity:1 }, 750);
				
				setTimeout('reloadExclusiveSubmit()', 3000);
			}
		});
	}
}

function reloadExclusiveSubmit() 
{
	$('.exclusive-thankyou').animate({ "marginTop":"+=53px" , opacity:0 }, 500, function () { $(this).css({"display":"none"}); });
	$('#exclusive_form_email').val('Enter your e-mail address...');
	
	$('.exclusive-form').animate({ "marginTop":"+=53px" , opacity:1 }, 750, function () { 
		
	});
}

function displayTrailer()
{
	$('.trailer-flash').css({"display":"block"});
	
	$('.trailer-curtain').animate({ "marginTop":"-=335px"}, 500, function () { 				
		//$(this).css({"display":"none"}); 
	});
}

var previous_about_id = 1;

function toggleAbout(id) {
	if (previous_about_id != id) 
	{
		checkNavigation(id); // Toggle navigation items
		previous_about_id = id; // Saves the selected id for the next selection
	}
	
	function checkNavigation(id)
	{
		$('#tab_'+(id)).addClass('selected');
		$('#tab_'+(previous_about_id)).removeClass('selected');
		$('#tab_content_'+(id)).removeClass('hidden');
		$('#tab_content_'+(previous_about_id)).addClass('hidden');
	}
}

// Twitter Module

var tweets				= new Array();
var tweet_user 			= '';
var tweet_rotate_cid	= 0;
var tweet_delay			= 5000;
var tweet_timer;

function loadTweets() 
{
	var twitter_api_url = 'http://search.twitter.com/search.json?q=&callback=?';
	var twitter_query    = '&ors=%23transcendentman+%23preparetoevolve&rpp=15';

	$.getJSON(
		twitter_api_url + twitter_query,
		function(data) {
			
			$.each(data.results, function(i, tweet) {

				if(tweet.text !== undefined) {
					var tweet_date = new Date(tweet.created_at);
					var date_now   = new Date();
					var date_diff  = date_now - tweet_date;
					var tweet_time = Math.round(date_diff/(1000*60*60));
					
					if(tweet_time < 1){
						var tweet_time   = Math.round(date_diff/(1000*60));
						tweet_time += ' minutes ago';
					} else { 
						tweet_time += ' hours ago'; 
					}
					
					var tweet_id	= tweet.id;
					var tweet_text 	= tweet.text.searchLinks().searchUsers();
					var tweet_user 	= tweet.from_user;
					
					tweets.push(new Array( tweet_id , tweet_text , tweet_time , tweet_user ));

				}
			});
			outputTweet(tweet_rotate_cid);
			setTimeout("rotateTweet()",tweet_delay);
			/*
			$('.tweet-loading').animate({ opacity: 0 , "marginTop": "-=50px" }, 750,
				function () {
					$(this).remove();
					
					if(tweets.length > 0) {
						outputTweet(tweet_rotate_cid);
						setTimeout("rotateTweet()",tweet_delay);
					}
				}
			);
			*/
		}
	);
	
	String.prototype.searchLinks = function() {
		return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/, function(m) {
			return m.link(m);
		});
	};
	
	String.prototype.searchUsers = function() {
		return this.replace(/(^|[^\w]+)\@([a-zA-Z0-9_]{1,15})/g, function(m, m1, m2) {
			return m1 + '@<a href="http://twitter.com/' + m2 + '" target="_blank">' + m2 + '</a>';
		});
	}; 
}

function outputTweet(id) 
{
	
	var tweet_id		= tweets[id][0]
	var tweet_text	 	= tweets[id][1];
	var tweet_time 		= tweets[id][2];
	var tweet_user		= tweets[id][3];
	
	// Create HTML for Tweet					
	var tweet_html = '<div id="tweet_item_'+id+'" class="tweet-item">';
	//tweet_html    += tweet_text + '<\/div>';
	tweet_html	  += '<div class="vertical-a"><div class="vertical-b"><div class="vertical-c"><div class="tweet-text">';
	tweet_html	  += tweet_text+'</div></div></div></div></div>';
	//tweet_html    += '<div class="tweet-time">' + tweet_time;
	//tweet_html    += ' from <a href="http://www.twitter.com/' + tweet_user + '/status/' + tweet_id + '">' + tweet_user + '</a><\/div><\/div>';
	
	
	$('.tweet-container').append(tweet_html);
	
	$('#tweet_item_'+id).css({ opacity: 0 , "margin-top": '30px' });
	$('#tweet_item_'+id).animate({ opacity: 1, "marginTop": "-=30px" }, 750);
}

function rotateTweet() 
{
	tweet_timer=setTimeout("rotateTweet()",tweet_delay);
	
	$('#tweet_item_'+tweet_rotate_cid).animate({opacity: 0, "marginTop": "-=30px"}, 750,
		function() {
			$(this).remove();
		}
	);
	
	if(tweet_rotate_cid == tweets.length-1) {
		tweet_rotate_cid = 0;
	} else {
		tweet_rotate_cid++;
	}
	
	outputTweet(tweet_rotate_cid);
}


function entsub(event,ourform) 
{
	if (event && event.which == 13) {
    	exclusiveSubmit();
	} else {
    	return true;
	}
}

window.onload = function(){
	Shadowbox.init();
};


$(document).ready(function() {
	loadTweets();
});
