var static_image = null;
var ticker_items;
var current_news;
var next_news;

$(function () { 

	$('#nav-links-container').hover(function () {
		$(this).addClass('over');
	}, 
	function () {
		$(this).removeClass('over');
	});
	
	$('div.music-content').hide();
	$('div.news-content').hide();
	$('div.shows-content').hide();
	$('div.press-content').hide();
	
	$('ul.artist-options li a').click(function () {
		if(!$(this).hasClass('active'))
		{
			$('ul.artist-options li a').removeClass('active');
			$(this).addClass('active');
			var activeLink = $(this).attr('href').substr(1);
			$('div.main-column div[class*=-content]').hide('fast');
			$('div.main-column div[class=' + activeLink + ']').slideDown('fast');
			return false;
		}
	});


	var email = /^.+@.+\..{2,3}$/;

	$('form#signup').hide();
	$('div.right-column #embed-music').before('<a href="#" id="mailing-list">sign up to receive updates &raquo;</a>');
	$('div#artist-landing-page #embed-music').before('<a href="#" id="mailing-list">sign up to receive updates &raquo;</a>');
	$('#mailing-list').css('opacity','.5');
	$('#embed-music').css('opacity','.5');
	$('#mailing-list').hover(function () {
		$(this).animate({opacity : 1}, 'fast')
	}, function () {
		$(this).animate({opacity : .5}, 'fast')
	});
	$('#embed-music').hover(function () {
		$(this).animate({opacity : 1}, 'fast')
	}, function () {
		$(this).animate({opacity : .5}, 'fast')
	});
	$('#mailing-list').click(function () {
		$('#mailing-list').slideUp();
		$('form#signup').slideDown();
		return false;
	});
			
	$('form#signup input.text').focus(function () {
		if($(this).attr('value') ==  $(this).attr('name') + ' is a required field' || $(this).attr('value') == 'invalid email address')
		{
			$(this).attr('value', '');
		}
	});
	$('form#signup').submit(function () {
		var invalidForm = false;
		$(this).find('input').each(function () {
			if($(this).hasClass('required') && (this.value == '' || $(this).attr('value') ==  $(this).attr('name') + ' is a required field'))
			{
				$(this).css({'background' : 'red', 'color' : 'white'}).attr('value', $(this).attr('name') + ' is a required field');
				invalidForm = true;
			}
			else if(!email.test(document.getElementById("email-input").value))
			{
				$('#email-input').css({'background' : 'red', 'color' : 'white'}).attr('value','invalid email address');
				invalidForm = true;
			}
		});
		if(invalidForm == true)
		{
			return false;
		}
		else
		{
			$.post("/wp-content/themes/removador/mail.php", { name: document.getElementById("name-input").value, email: document.getElementById("email-input").value },
			 function(data){
			  $('form#signup').slideUp();
			  $('#mailing-list').text('thank you!');
			  $('#mailing-list').slideDown();	
			  $('#mailing-list').css('opacity','1');
			  setTimeout(function () { 
				$('#mailing-list').html('sign up to receive updates &raquo;'); 
				$('#mailing-list').animate({opacity : .5}, 'fast');
				$('form#signup input.text').css({'background' : 'white', 'color' : 'black'}).attr('value', '');
			  }, 3000);
			});
			return false;
		}
	});
	
	$('a[rel*=video-facebox]').click(function () {
		swapVideo($(this).attr('href'));
		if(static_image == null)
		{
			static_image = document.createElement('div');
			$(static_image).hide().append('<img src="/wp-content/themes/removador/images/solutions-memorandum.jpg" alt="" />');
			$('div.main-column').append(static_image);
			$('object#flashcontent').remove();
			$(static_image).show();
			
		}
		return false;
	});
	
	var feeds = new Array('/artists/lydia-burrell/', '/artists/the-fervor/', '/artists/the-ravenna-colt/', '/artists/cortney-tidwell/', '/artists/follow-the-train/');
	var feeds_loaded = 0;
	var feed_loaders = new Array(document.createElement('div'),document.createElement('div'),document.createElement('div'),document.createElement('div'),document.createElement('div'));
	var feed_paragraphs = new Array(document.createElement('p'),document.createElement('p'),document.createElement('p'),document.createElement('p'),document.createElement('p'));
	
	loadContent();
	
	function loadContent ()
	{
		$(feed_loaders[feeds_loaded]).load(feeds[feeds_loaded] + ' .news-content', function () {
			if($(this).find('h3').html() != null)
			{
				$(feed_paragraphs[feeds_loaded]).html($(this).find('h3').html()  + ' <a href="' + feeds[feeds_loaded] + '">read more »</a>');
			}
			else
			{
				$(feed_paragraphs[feeds_loaded]).html($(this).text().substr(0, 70).replace(/^\s+(.+)$/, '$1') + '...'  + ' <a href="' + feeds[feeds_loaded] + '">read more »</a>');	
			}
			$(feed_paragraphs[feeds_loaded]).css('text-transform', 'lowercase')
			$('#news-ticker').append(feed_paragraphs[feeds_loaded]);
			feeds_loaded++;
			if(feeds_loaded < feeds.length)
			{
				loadContent();
			}
			else
			{
				ticker_items = $("#news-ticker p");
				current_news = 0;
				next_news = 1;
				swapNews();
			}
		});
	}
	
	function swapNews ()
	{
		var rotater = setInterval(function () { 
			$(ticker_items[current_news]).fadeOut(function () {
				$(ticker_items[next_news]).fadeIn('slow');
				current_news = next_news;
				next_news < ticker_items.length - 1 ? next_news++ : next_news = 0;
			});
		}, 8000);
	}
	
});

function swapVideo(video_src)
{	
	$('#video-wrapper').empty();
	$('#video-wrapper').append('<div id="video-player"></div>');
	var flashvars = {};
	flashvars.video_src = video_src;
	swfobject.embedSWF(video_src, 'video-player', '630', '360', '9.0.45', false, flashvars, { bgcolor: '#000000', menu: 'false', wmode: 'transparent', base: '/wp-content/themes/removador/flash/', allowScriptAccess: 'always' }, {id: 'video_player'});
	jQuery.facebox($('#video-wrapper'));
}
