jQuery(document).ready(function($){
/* Content BG Fix */
/*-------------------------------------------------------------------*/
	var fixBg = function(){
		var footBot = $('footer').offset().top;		
		$('#content-bg, #content-bg-inner').css('height', footBot+'px');
	}	
	$(window).bind({
		'scroll resize': function(){
			fixBg();
		}
	});
	fixBg();
	//setTimeout(fixBg, 1000);
	
/* Nivo Slider */
/*-------------------------------------------------------------------*/	
	$('#slider').nivoSlider({
		effect : 'fade',
		pauseTime: 5000	
	});
		
/* Latest Tweet */
/*-------------------------------------------------------------------*/
	var twitterUser = 'probcause';
	$.ajaxSetup({ cache: true });		
	$.getJSON('http://api.twitter.com/1/statuses/user_timeline.json?screen_name='+twitterUser+'&count=3&include_rts=true&callback=?',function(data){
		if(data.length >= 1){
			$.each(data, function(i, tweet) {			
				if(tweet.text !== undefined) {		
					var tweet_html = '<div class="tweet-text">'+tweet.text + '<\/div><p class="tweet-timestamp"><a href="http://twitter.com/'+twitterUser+'/status/'+tweet.id_str+'" target="_blank">'+H(tweet.created_at)+'</a><\/p>';		
					$('#latest-tweets').append(tweet_html);					
				}
			});
			var tallestBox = 0;
			$('.small-box .content').each(function(){
				tallestBox = Math.max(tallestBox, $(this).height());
			});
			$('.small-box .content').css('height', tallestBox+'px');
			fixBg();
		} 
		else {
			$('#latest-tweets').append('<p>We haven\'t tweeted in awhile, but feel free to <a href="http://twitter.com/'+twitterUser+'" target="_blank">view our older tweets</a>.</p>');
			var tallestBox = 0;
			$('.small-box .content').each(function(){
				tallestBox = Math.max(tallestBox, $(this).height());
			});
			$('.small-box .content').css('height', tallestBox+'px');
			fixBg();
		}
	});
	
/* Youtube Videos */
/*-------------------------------------------------------------------*/
	if($('#home-video').length >= 1){	
		$.getJSON('https://gdata.youtube.com/feeds/api/playlists/E10FCDE7CC682DC5?alt=jsonc&v=2&max-results=1&callback=?', function(data){
			//http://gdata.youtube.com/feeds/api/users/probcausetv/uploads?v=2&alt=jsonc&max-results=1&callback=?
			var total = data.data.items.length;
			for(i=0;i<total;i++){				
				var video = data.data.items[i].video;
				var title = video.title;				
				var id = video.id;
				var newVideo = '<object width="440" height="250">';
				newVideo += '<param name="movie" value="http://www.youtube.com/v/'+id+'?fs=1&autohide=1&modestbranding=1&showinfo=0&cc_load_policy=0&wmode=opaque"></param>';
				newVideo +=	'<param name="allowFullScreen" value="true"></param>';
				newVideo += '<param name="allowScriptAccess" value="always"></param>';
				newVideo += '<embed src="http://www.youtube.com/v/'+id+'?fs=1&autohide=1&modestbranding=1&showinfo=0&cc_load_policy=0&wmode=opaque" type="application/x-shockwave-flash" wmode="opaque" allowfullscreen="true" allowscriptaccess="always" width="440" height="250"></embed></object>';				
				$('#home-video').append(newVideo);
			}					
		});
	}
	
	var trim = function(stringToTrim) {
		return stringToTrim.replace(/^\s+|\s+$/g,"");
	}
	
	var convertTime = function(s){
		minutes = Math.floor(s/60);
		seconds = s%60;
		return minutes+':'+seconds;
	}
	
	if($('#youtube-videos').length >= 1){	
		//$.getJSON('http://gdata.youtube.com/feeds/api/users/probcausetv?v=2&alt=json&callback=?', function(data){
			//userTotal = data.entry.gd$feedLink[5].countHint;
			$.getJSON('https://gdata.youtube.com/feeds/api/playlists/E10FCDE7CC682DC5?alt=jsonc&v=2&max-results=50&callback=?', function(data){
				//http://gdata.youtube.com/feeds/api/users/probcausetv/uploads?v=2&alt=jsonc&max-results=50&callback=?
				//https://gdata.youtube.com/feeds/api/playlists/4DAEFAF23BB3CDD0?alt=jsonc&v=2&callback=?
				console.log(data);
			var total = data.data.items.length;
			var playlistTotal = data.data.totalItems;
			$('#youtube-videos').append('<div class="video-overview">Showing <strong>'+total+'</strong> of <strong>'+playlistTotal+'</strong> Videos | <strong><a href="http://youtube.com/probcausetv" target="_blank">ProbCause YouTube Channel</a></strong></div>');
				for(i=0;i<total;i++){				
					var video = data.data.items[i].video;
					var title = video.title;
					var id = video.id;
					var views = video.viewCount;
					var date = new Date(video.uploaded.substring(0,4), video.uploaded.substring(5,7), video.uploaded.substring(8,10));
					var date = date.getMonth()+'/'+date.getDate()+'/'+date.getFullYear();
					var duration = convertTime(video.duration);
					var description = trim(video.description);
					var newVideo = ' \
						<div class="video-left"> \
							<object width="290" height="193"> \
								<param name="movie" value="http://www.youtube.com/v/'+id+'?fs=1&autohide=1&modestbranding=1&showinfo=0&cc_load_policy=0&wmode=opaque"></param> \
								<param name="allowFullScreen" value="true"></param> \
								<param name="allowScriptAccess" value="always"></param> \
								<embed src="http://www.youtube.com/v/'+id+'?fs=1&autohide=1&modestbranding=1&showinfo=0&cc_load_policy=0&wmode=opaque" type="application/x-shockwave-flash" wmode="opaque" allowfullscreen="true" allowscriptaccess="always" width="290" height="193"></embed> \
							</object> \
						</div> \
						<div class="video-right"> \
							<h4><a href="http://www.youtube.com/watch?v='+id+'" target="_blank">'+title+'</a></h4> \
							<p class="video-meta"><strong>Date:</strong> '+date+' &nbsp;&nbsp;&nbsp;<strong>Views:</strong> '+views+' &nbsp;&nbsp;&nbsp;<strong>Duration:</strong> '+duration+'</p> \
							<div class="video-description"> \
							<pre>'+description+'</pre> \
							</div> \
						</div> \
						';	
					$('#youtube-videos').append('<div class="video">'+newVideo+'</div>');
				}	
			});
		//});			
	}
		
/* Tweet Date Formatting */
/*-------------------------------------------------------------------*/
	var K=function(){var a=navigator.userAgent;return{ie:a.match(/MSIE\s([^;]*)/)}}();var H=function(a){var b=new Date();var c=new Date(a);if(K.ie){c=Date.parse(a.replace(/( \+)/,' UTC$1'))}
	var d=b-c;var e=1000,minute=e*60,hour=minute*60,day=hour*24,week=day*7;if(isNaN(d)||d<0){return""}
	if(d<e*7){return"Posted: Right Now"}
	if(d<minute){return"Posted: "+Math.floor(d/e)+" Seconds Ago"}
	if(d<minute*2){return"Posted: About 1 Minute Ago"}
	if(d<hour){return"Posted: "+Math.floor(d/minute)+" Minutes Ago"}
	if(d<hour*2){return"Posted: About 1 Hour Ago"}
	if(d<day){return"Posted: "+Math.floor(d/hour)+" Hours Ago"}
	if(d>day&&d<day*2){return"Posted: Yesterday"}
	if(d<day*365){return"Posted: "+Math.floor(d/day)+" Days Ago"}else{return"Posted: Over a Year Ago"}};
});
