/*
 * Edward Burns
 * JS Version 1.0
 * Written by Knicknack (knicknack.com)
 */

/*
 * Slider
 *
 * @function SliderNavigation
 *   Sort through the featured items in the slider
 */
var EB = function() 
{
	var previous_id = 0; // Default slider id
	var mailinglist_open = false;

	/*
	 * Controls the selection of a slider item
	 *
	 * @param id
	 *   The ID of the selected slider item
	 */
	this.SliderSelect = function (id)
	{
		var position;
		var transition = true;
		
		var slider_total = $('.slider-container').children().size();

		position = id*-245+"px";

		// Display the selected slide if 'transition' is true and not the same
		if (transition && previous_id != id) 
		{
			checkNavigation(id); // Toggle navigation items
			previous_id = id; // Saves the selected id for the next selection
			
			$('.slider-container').animate({"marginTop": position}, 375, function()
			{ 
				
			});
		}
		
		/*
		 * Toggle selected slider item
		 *
		 * @param id
		 *   The ID of the selected slider item
		 */
		function checkNavigation(id)
		{
			$('#slider_nav_'+(id+1)).addClass('selected');
			$('#slider_nav_'+(previous_id+1)).removeClass('selected');
			Cufon.refresh('.slider-nav-item .title'); 

		}


	} // end function, SliderSelect
	
	/*
	 * Controls the mailing list
	 *
	 */
	this.MailingListToggle = function ()
	{
		var position;
		
		if (mailinglist_open)  {
			position 			= 620 + "px";
			mailinglist_open 	= false;
		} else {
			position 			= 920 + "px";
			mailinglist_open 	= true;
		}
		
		$('#site #header .social-links').animate({"marginLeft": position}, 375, function()
		{ 
			if(!mailinglist_open) {
				reloadExclusiveSubmit();
			}
		});
	} // end function, MailingList
	
	this.MailingListSubmit = function (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/3457/campaign/' + cid + '&fan%5Bemail%5D='+ email + '&fan%5Breferring_url%5D=http://www.edwardburns.net&fan%5Bconfirmation_target%5D=http://edwardburns.net/confirm-download';
		
		if(email != 'Enter your e-mail address...') {
			
			$('.mailing-list .mailing-list-form .container-form').animate({ "marginTop":"+=65px" , opacity:0 }, 500);
			
			$.ajax({
				type: "POST",
				url: url,
				success: function(msg){
					
					//$('.exclusive-form').animate({ "marginTop":"-=53px" , opacity:0 }, 750);

					$('.mailing-list .mailing-list-form .confirmation').css({"display":"block"});
					$('.mailing-list .mailing-list-form .confirmation').animate({ "marginTop":"+=30px" , opacity:1 }, 500, function()
					{
						setTimeout('EB.MailingListToggle()', 2500);
						
						//reloadExclusiveSubmit();
					});
				}
			});
		}
	}
	
	function reloadExclusiveSubmit()
	{
		$('.mailing-list .mailing-list-form .confirmation').css({opacity:0}).css({"margin-top":""});
		$('.mailing-list .mailing-list-form .container-form').css({"margin-top":""}).css({opacity:1});
		$('#exclusive_form_email').val('Enter your e-mail address...');
	}
	
	var current_page 		= 0;
	var previous_page		= 0;
	var paginateFirst		= true;
	
	this.PaginateMovies = function(type) 
	{
		if(paginateFirst && current_page == 0) 
		{
			current_page	= Math.floor($('.movie-list li.selected').index() / 6);
			paginateFirst = false;
		}
		
		var page_total		= Math.ceil($('.movie-list').children().size() / 6);
		var continueLoad 	= false;
		
		if(type == "previous" && current_page > 0) {
			previous_page 	= current_page;
			current_page 	= current_page - 1;
			continueLoad = true;
		} else if (type == "next" && current_page < (page_total-1)) {
			previous_page 	= current_page;
			current_page 	= current_page + 1;
			continueLoad 	= true;
		} else if (!isNaN(type)) {
			previous_page 	= current_page;
			current_page 	= type;
			continueLoad 	= true;
		}
		
		if(continueLoad) 
		{
			$('.movie-list').animate({"marginLeft": -1 * ( (current_page) * 540 ) + "px"}, 750, 
				function() { 
	
				}
			);
		}
	}
	
} // end function, EB

/*
 * Load default objects
 */
var EB = new EB;

	function checkWidth()
	{
		if((($(window).width() > 1119) && ($(window).width() < 1282)) || (($(window).width() > 1215) && ($(window).width() < 1282))){
			$('.sidebar .ngg-gallery-thumbnail-box').addClass('small-thumb');
			$('.sidebar .ngg-galleryoverview').addClass('small2');
		} else {
			$('.sidebar .ngg-gallery-thumbnail-box').removeClass('small-thumb');
			$('.sidebar .ngg-galleryoverview').removeClass('small2');
		}
		if((($(window).width() > 1068) && ($(window).width() < 1120)) || (($(window).width() > 1215) && ($(window).width() < 1282))){
			$('.sidebar .tubepress_thumb').addClass('small-thumb');
			$('.sidebar .tubepress_thumbs').addClass('small2');
		} else {
			$('.sidebar .tubepress_thumb').removeClass('small-thumb');
			$('.sidebar .tubepress_thumbs').removeClass('small2')
		}
	} // end function, checkWidth


$(document).ready(function() 
{
   $('.sidebar .tubepress_thumb a[title]').qtip({
		content: {
			text: false // Use each elements title attribute
		},
		style: {
			classes: 'ui-tooltip-blue ui-tooltip-shadow'
		},
		position: {
			my: 'top center',
			at: 'bottom center',
			adjust: {
				y: 5
			}
		}
   });

	checkWidth();

});

$(window).resize(function() {
	checkWidth();
});






// Twitter Module

var tweets				= new Array();
var tweet_user 			= 'edward_burns';
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    = '&from=' + tweet_user +'&rpp=15';

	var url = 'http://www.twitter.com/statuses/user_timeline/' + tweet_user + '.json?callback=twitterCallback&count=20';

	$.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" style="margin-top:3px;"><div class="tweet-text">';
	tweet_html	  += tweet_text + ' <span class="time">' + tweet_time + '</span></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').append(tweet_html);
	
	$('#tweet_item_'+id).css({ opacity: 0 , "margin-top": '48px' });
	$('#tweet_item_'+id).animate({ opacity: 1, "marginTop": "-=48px" }, 750);
}

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


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




function SplashMailingListSubmit(cid)
{
	var email 	= $('#splash_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/3457/campaign/' + cid + '&fan%5Bemail%5D='+ email + '&fan%5Breferring_url%5D=http://www.edwardburns.net&fan%5Bconfirmation_target%5D=http://edwardburns.net/confirm-download';
	
	if(email != 'Enter your e-mail address...') {
		
		$('#newlyweds-splash .splash .splash-mailing-list .container-form').animate({ "marginTop":"+=25px" , opacity:0 }, 500);
		
		$.ajax({
			type: "POST",
			url: url,
			success: function(msg)
			{
				$('#newlyweds-splash .splash .splash-mailing-list .confirmation').css({"marginTop":"-25px", "display":"block"});
				$('#newlyweds-splash .splash .splash-mailing-list .confirmation').animate({ "marginTop":"+=25px" , opacity:1 }, 500, function()
				{
					$('#splash_form_email').val('Enter your e-mail address...');
					setTimeout('SplashMailingListToggle()', 2500);
				});
			}
		});
	}
}

function SplashMailingListToggle()
{
	$('#newlyweds-splash .splash .splash-mailing-list .confirmation').animate({ "marginTop":"-=25px" , opacity:0 }, 500, function()
	{
		$('#newlyweds-splash .splash .splash-mailing-list .confirmation').css({"display":"none"});
	});
	
	$('#newlyweds-splash .splash .splash-mailing-list .container-form').animate({ "marginTop":"-=25px" , opacity:1 }, 500);
}

var EB_Splash = function() 
{
	//eraseCookie('edwardburns-splash');
	var splashCK = readCookie('edwardburns-splash');
	
	/*
	 * Load the Splash
	 *
	 * @param pid
	 *   The next photo id
	 */
	this.Load = function () 
	{		
		if(!splashCK) 
		{
			$('#newlyweds-splash').css({"display":"block"});

			$('#newlyweds-splash').animate({ opacity:1 }, 500);
		}
	}
	
	this.Close = function ()
	{
		$('#newlyweds-splash').animate({ opacity:0 }, 500, function()
		{
			createCookie('edwardburns-splash',true,14);
			$(this).remove();
		});
	}
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var EB_Splash = new EB_Splash;

$(document).ready(function() 
{
	EB_Splash.Load();
	
	$('#newlyweds-splash .splash .btn-close').click(function() 
	{
		EB_Splash.Close();
	});
	
	$('#newlyweds-splash .splash-bg').click(function() 
	{
		EB_Splash.Close();
	});
});


