// JavaScript Document

var twitter_feeds = 20;
var current_tweet = 0;

$(function() {
	getTwitterFeed();
	dropDownMenu();
	fixSubscribeBox();
	getYoutubeFeed();
	searchBox();
});

function fixSubscribeBox() {	
	$('.subscribe-wrap').find('form').attr('id', 'subscribe');
	first_text = $('.subscribe-wrap').find('div.subscribe p:first').html();	
	if(first_text == 'A confirmation message is on its way!' || first_text == 'No such email address is registered.' || first_text == 'You may manage your subscription options from your <a href="http://www.annieduke.com/wp-admin/users.php?page=s2_users">profile</a>.' || first_text == '<em>You may manage your subscription options from your <a href="#" onclick="return false;">profile</a>.</em>' ) {
		$('.subscribe-wrap').find('div .title').remove();
		$('.subscribe-wrap').find('div.subscribe p:first').addClass('confirm');
	} else {
		$('.subscribe-wrap').find('div .title').remove();
		$('.subscribe-wrap').find('div.subscribe form p:last input:last').remove();
		html_email = $('.subscribe-wrap').find('div.subscribe form p:first').html();
		input_pos = html_email.indexOf('<input');
		if(input_pos == -1) {
			input_pos = html_email.indexOf('<INPUT');
		}
		html_email = html_email.substring(input_pos);
		//$('.subscribe-wrap').find('div.subscribe form p:first').html(html_email).find('input').removeAttr('size').addClass('email');
		$('.subscribe-wrap').find('div.subscribe form p:first').html(html_email).find('input').addClass('email');
		$('.subscribe-wrap').find('div.subscribe form p:last input').addClass('submit');
		alert_message = $('.subscribe-wrap').find('div.subscribe form + p').html();
		if(alert_message) {
			$('.subscribe-wrap').find('div.subscribe form + p').remove();
			alert(alert_message);
		}
	}
}

function dropDownMenu() {
  $('.main-navigation li:has(ul)').each(function() {
		 subMenu($(this).find('a').attr('class'));
  });
}

function subMenu(menu) {
	$('.main-navigation li .' + menu).hover(function() {
		$(this).addClass(menu + '-hover');
		$(this).parent().find('ul').slideDown('fast');
	}, function() {})
	.parent().hover(function() {}, function() {
		$(this).find('ul').slideUp('fast');
		$(this).find('a').removeClass(menu + '-hover');
	});
}

function getTwitterFeed() {
	$.jGFeed("http://twitter.com/statuses/user_timeline/24340604.rss",
	function(feeds){
		// Check for errors
		if(!feeds){
			// there was an error
			return false;
		}
		
		// do whatever you want with feeds here
		
		$("#annieduke").find('.current-twitt').html('<img src="/wp-content/themes/anniedukev3/images/loader.gif" alt="" \>');
		
		var entry = feeds.entries[0];
		
		var html_print = '';
		// Add entry title
		var tweet_text = entry.title;
		tweet_text = tweet_text.substring(tweet_text.indexOf(': ') + 2);
		tweet_text = tweet_text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$1" target="_blank">$1</a>');
		tweet_text = tweet_text.replace(/(^|\s)@(\w+)/g, '$1<a href="http://www.twitter.com/$2" target="_blank">@$2</a>');
		tweet_text = tweet_text.replace(/(^|\s)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>');	
		
		html_print += '<p>' + tweet_text + '</p>';
		
		var tweet_time = prettyDate(dateFormat(new Date(entry.publishedDate), 'yyyy-mm-dd"T"HH:MM:ss"Z"'));
		
		html_print += '<p class="posted">about ' + tweet_time + '</p>';
		html_print += '<a class="next-twitt">Next</a> ';	
		html_print += '<a class="previous-twitt" id="inactive">Previous</a>';		
		
		$("#annieduke").find('.current-twitt').html(html_print);
		assignNextTweetAction();
	}, 1);
}

function assignNextTweetAction() {
	$('#annieduke').find('.next-twitt')[0].onclick = function() {
		getNextTweet();
	}
}

function assignPreviousTweetAction() {
	$('#annieduke').find('.previous-twitt')[0].onclick = function() {
		getPreviousTweet();
	}
}

function getNextTweet() {
	$.jGFeed("http://twitter.com/statuses/user_timeline/24340604.rss",
	function(feeds){
		// Check for errors
		if(!feeds){
			// there was an error
			return false;
		}
		// do whatever you want with feeds here
		
		$("#annieduke").find('.current-twitt').html('<img src="/wp-content/themes/anniedukev3/images/loader.gif" alt="" \>');
		
		current_tweet = current_tweet + 1;
		
		var entry = feeds.entries[current_tweet];
		
		var html_print = '';
		// Add entry title
		var tweet_text = entry.title;
		tweet_text = tweet_text.substring(tweet_text.indexOf(': ') + 2);
		tweet_text = tweet_text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$1" target="_blank">$1</a>');
		tweet_text = tweet_text.replace(/(^|\s)@(\w+)/g, '$1<a href="http://www.twitter.com/$2" target="_blank">@$2</a>');
		tweet_text = tweet_text.replace(/(^|\s)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>');	
		
		html_print += '<p>' + tweet_text + '</p>';
		
		var tweet_time = prettyDate(dateFormat(new Date(entry.publishedDate), 'yyyy-mm-dd"T"HH:MM:ss"Z"'));
		
		html_print += '<p class="posted">about ' + tweet_time + '</p>';
		if(current_tweet == 19) {
			html_print += '<a class="next-twitt" id="inactive">Next</a> ';
			html_print += '<a class="previous-twitt">Previous</a>';
		} else {
			html_print += '<a class="next-twitt">Next</a> ';
			html_print += '<a class="previous-twitt">Previous</a>';
		}
		$("#annieduke").find('.current-twitt').html(html_print);
		if(current_tweet < 19) {
			assignNextTweetAction();
		}
		assignPreviousTweetAction();
	}, 20);
}

function getPreviousTweet() {
	$.jGFeed("http://twitter.com/statuses/user_timeline/24340604.rss",
	function(feeds){
		// Check for errors
		if(!feeds){
			// there was an error
			return false;
		}
		// do whatever you want with feeds here
		
		$("#annieduke").find('.current-twitt').html('<img src="/wp-content/themes/anniedukev3/images/loader.gif" alt="" class="loader" \>');
		
		current_tweet = current_tweet - 1;
		
		var entry = feeds.entries[current_tweet];
		
		var html_print = '';
		// Add entry title
		var tweet_text = entry.title;
		tweet_text = tweet_text.substring(tweet_text.indexOf(': ') + 2);
		tweet_text = tweet_text.replace(/(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig, '<a href="$1" target="_blank">$1</a>');
		tweet_text = tweet_text.replace(/(^|\s)@(\w+)/g, '$1<a href="http://www.twitter.com/$2" target="_blank">@$2</a>');
		tweet_text = tweet_text.replace(/(^|\s)#(\w+)/g, '$1<a href="http://search.twitter.com/search?q=%23$2" target="_blank">#$2</a>');	
		
		html_print += '<p>' + tweet_text + '</p>';
		
		var tweet_time = prettyDate(dateFormat(new Date(entry.publishedDate), 'yyyy-mm-dd"T"HH:MM:ss"Z"'));
		
		html_print += '<p class="posted">about ' + tweet_time + '</p>';
		if(current_tweet == 0) {
			html_print += '<a class="next-twitt">Next</a> ';
			html_print += '<a class="previous-twitt" id="inactive">Previous</a>';
		} else {
			html_print += '<a class="next-twitt">Next</a> ';
			html_print += '<a class="previous-twitt">Previous</a>';
		}
		$("#annieduke").find('.current-twitt').html(html_print);
		assignNextTweetAction();
		if (current_tweet > 0) {
			assignPreviousTweetAction();
		}
	}, 20);
}

function searchBox() {
	$(".subscribe-wrap").find("#subscribe .email").val('Enter email')
	.blur(function(event) {
		if ($(this).val() == '') {
			$(this).val('Enter email');
		}
	}).focus(function(event) {
		if ($(this).val() == 'Enter email') {
			$(this).val('');
		}
	});
}

function getYoutubeFeed() {
	$.jGFeed("http://gdata.youtube.com/feeds/api/playlists/B6D5CCB85C71CDF1?orderby=published&alt=rss&amp;v=2&max-results=1",
	function(feeds){
		// Check for errors
		if(!feeds){
			// there was an error
			return false;
		}
		
		// do whatever you want with feeds here
		
		var entry = feeds.entries[0];
		var video_id = '';
		var html_print = '';
		
		video_link = entry.link;
		v_pos = video_link.indexOf('v=') + 2;
		video_id = video_link.substring(v_pos);
		video_id = video_id.split('&')[0];
		
		html_embed = '<a href="/wp-content/themes/anniedukev3/scripts/video-popup.php?videoID=' + video_id + '" rel="shadowbox;width=560;height=340;"><img src="http://i.ytimg.com/vi/' + video_id + '/0.jpg" height="130" width="187" /></a>';
		$("#annieduke").find('.video-wrap .video-thumb').html(html_embed);
		
	}, 1);
}
