$(document).ready(function(){
	
	initCommunityConnections();
	
	initGetInTheKnow();	
	initSuperFans();
});

function initSuperFans(){
	$('#superfans .left').click(superfanLeft_click);
	$('#superfans .right').click(superfanRight_click);
}

function superfanLeft_click(){
	var container = $('#superfans .photoContainer');
	var width = parseInt(container.css('width'));
	if(parseInt(container.css('left')) !== 0){
		$('#superfans .left').unbind();
		container.animate({left: '+=93px'}, function(){
			$('#superfans .left').click(superfanLeft_click);
			if(parseInt(container.css('left')) == 0){
				container.css('left', -(width-186));
			}
		});
	}
	else{
		container.css('left', parseInt(container.css('width')) -93);
	}
}
function superfanRight_click(){
	var container = $('#superfans .photoContainer');
	var width = parseInt(container.css('width'));
	var maxWidth = -parseInt(container.css('width')) + 93;
	if(parseInt(container.css('left')) !== maxWidth){
		$('#superfans .right').unbind();
		container.animate({left: '-=93px'}, function(){
			$('#superfans .right').click(superfanRight_click);
			if(parseInt(container.css('left')) == -(width-93)){
				container.css('left', '-93px');
			}
		});
	}
}


function initGetInTheKnow(){
	
	//GET IN THE KNOW
	$('#getInTheKnow input').click(function(e){
		if($(e.currentTarget).val() == 'yourname@email.com')
			$(e.currentTarget).val('');
	});
	$('#getInTheKnow input').bind('blur', function(e){
		if($(e.currentTarget).val() == '')
			$(e.currentTarget).val('yourname@email.com');
	});
	
	$('#getInTheKnow .inTheKnowBtn').click(function(e){
		if($('#getInTheKnow input').val() == '' || $('#getInTheKnow input').val() == 'yourname@email.com'){
			alert('Please enter a valid email address.');	
		}
		else{
			var email = $('#getInTheKnow input').val();	
			$.ajax({
				type: "POST",
				url: "/_commongoal/api/newsletterFacade.cfc",
				dataType: "text",
				data: {
				   method: 'submitNewsletterEntry',
				   email: email
				},
				success: newsletterSuccess
			});
		}
	});
}
function newsletterSuccess(){
	var container = $('#getInTheKnow .content .formContainer');
	container.fadeOut('fast');
	container.html('Thanks for signing up!');
	container.fadeIn('fast');
}

var photosContainer;
function initCommunityConnections(){
	//Community Connections
	
	photosContainer = $('#homePhotoVideoContainer');
	photosContainer.find('.previous').click(photosLeft_click);
	photosContainer.find('.next').click(photosRight_click);
	
	$('#homePhotoVideoContainer .multimediaTypeBtn.photos').click(function(){
		/*if(ytplayer){
			alert(homeTextVideoArrayPos);
			ytplayer.pauseVideo();
		}*/
		
		photosContainer.find('.multimediaTypeBtn').removeClass('selected');
		$(this).addClass('selected');
		photosContainer.find('.sliderContainer .video').removeClass('active');
		photosContainer.find('.sliderContainer .photos').addClass('active');
		homeTextArrayPos = 0;
		photosContainer.find('.captionContainer').html(homePhotosTextArray[homeTextArrayPos]);
		photosContainer.find('.previous').css('display', 'none');
		photosContainer.find('.next').css('display', 'block');
		photosContainer.find('.sliderContainer .photos').css('left', '0px');
		
		
		
		
		photosContainer.find('.previous').unbind();
		photosContainer.find('.next').unbind();
		photosContainer.find('.previous').click(photosLeft_click);
		photosContainer.find('.next').click(photosRight_click);
		
		$('#VideoContainer').html('');
		$('#VideoContainer').hide();
		
	});
	
	$('#homePhotoVideoContainer .multimediaTypeBtn.video').click(function(){
		photosContainer.find('.multimediaTypeBtn').removeClass('selected');
		$(this).addClass('selected');
		photosContainer.find('.sliderContainer .photos').removeClass('active');
		photosContainer.find('.sliderContainer .video').addClass('active');
		photosContainer.find('.captionContainer').html('');
		photosContainer.find('.previous').css('display', 'none');
		if(homeVideoArray.length > 1)
			photosContainer.find('.next').css('display', 'block');
		else
			photosContainer.find('.next').css('display', 'none');
		
		photosContainer.find('.previous').unbind();
		photosContainer.find('.next').unbind();
		photosContainer.find('.previous').click(videosLeft_click);
		photosContainer.find('.next').click(videosRight_click);
		
		photosContainer.find('.captionContainer').html(homeVideoTextArray[homeTextVideoArrayPos]);
		
		
	});	
	
	
	photosContainer.find('#videoSlider .result').click(function(e){playHomepageVideo(e);});
	
	//photosContainer.find('.video .result').hover(function(e){$(e.currentTarget).children('.homePlayBtn').show();}, function(e){$(e.currentTarget).children('.homePlayBtn').hide();});
}

function photosLeft_click(){
	var container = photosContainer.find('.sliderContainer .photos');
	if(parseInt(container.css('left')) !== 0){
		photosContainer.find('.left').unbind();
		container.animate({left: '+=415px'}, function(){
			photosContainer.find('.previous').click(photosLeft_click);	
			homeTextArrayPos--;
			photosContainer.find('.captionContainer').html(homePhotosTextArray[homeTextArrayPos]);
			if(homeTextArrayPos == 0){
				photosContainer.find('.previous').css('display', 'none');	
			}
			else{
				photosContainer.find('.next').css('display', 'block');		
			}
		});
		
		
	}
}

function photosRight_click(){

	var container = photosContainer.find('.sliderContainer .photos');
	
	var maxWidth = -parseInt(container.css('width')) + 415;
	if(parseInt(container.css('left')) !== maxWidth){
		photosContainer.find('.left').unbind();
		container.animate({left: '-=415px'}, function(){
			photosContainer.find('.next').click(photosRight_click);	
			homeTextArrayPos++;
			photosContainer.find('.captionContainer').html(homePhotosTextArray[homeTextArrayPos]);
			
			if(homeTextArrayPos > 0){
				photosContainer.find('.previous').css('display', 'block');	
			}
			if(parseInt(container.css('left')) == maxWidth){
				photosContainer.find('.next').css('display', 'none');	
			}
		});
		
	}
	
	
	if(homePhotosArrayPos < container.children('img').length+1 && homePhotosArrayPos <= homePhotosArray.length-1){
		container.find('.clear').remove();
		var img = new Image();
		$(img).load(function () {
			  container.append(this);
			  $(this).attr('width', '415');
			  $(this).attr('height', '233');
			  container.append('<div class="clear"></div>');
			  container.css('width', container.children('img').length * 415+'px');
		}).attr('src', '/graphics/useruploads/photos/'+homePhotosArray[homePhotosArrayPos]);

		homePhotosArrayPos++;
	}
}


function playHomepageVideo(e){
	
	var sender = $(e.currentTarget);
	
	var videoURL = sender.attr('flv');
	videoURL = videoURL.replace('http://www.youtube.com/watch?v=', '');
	
	
	var objectHTML = '<object width="415" height="233" id="ytplayer'+homeTextVideoArrayPos+'"><param name="movie" value="http://www.youtube.com/v/'+videoURL+'&hl=en_US&fs=1&autoplay=1"></param><param name="allowFullScreen" value="true"></param><param name="allowscriptaccess" value="always"></param><embed name="ytplayer'+homeTextVideoArrayPos+'" wmode="opaque" src="http://www.youtube.com/v/'+videoURL+'&hl=en_US&fs=1&autoplay=1" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="415" height="233"></embed></object>';
	
	$('#VideoContainer').html(objectHTML);
	$('#videoSlider').hide();
	$('#VideoContainer').show();
	
	
	//sender.html(objectHTML);
	//sender.unbind();
	sender.find('.thumbnail').hide();
	sender.find('.homePlayBtn').hide();

}

function videosLeft_click(){
	$('#videoSlider').find('.thumbnail').show();
	$('#videoSlider').find('.homePlayBtn').show();
	
	$('#videoSlider').show();
	var container = photosContainer.find('.sliderContainer .video .slider');
	var pos = parseInt(container.css('left'));
	if(pos !== 0){
		container.css('left', pos+415+'px');
		
		
		homeTextVideoArrayPos--;
		photosContainer.find('.captionContainer').html(homeVideoTextArray[homeTextVideoArrayPos]);
		pos = parseInt(container.css('left'));
		if(homeTextVideoArrayPos == 0){
			photosContainer.find('.previous').css('display', 'none');	
		}
		else{
			photosContainer.find('.next').css('display', 'block');		
		}
	}
	$('#VideoContainer').html('');
	$('#VideoContainer').hide();
}

function videosRight_click(){
	$('#videoSlider').find('.thumbnail').show();
	$('#videoSlider').find('.homePlayBtn').show();
	$('#videoSlider').show();
	
	var container = photosContainer.find('.sliderContainer .video .slider');
	var maxWidth = -parseInt(container.css('width')) + 415;
	
	var pos = parseInt(container.css('left'));
	
	
	if(pos !== maxWidth){
		container.css('left',pos- 415+'px');
		
		homeTextVideoArrayPos++;
		photosContainer.find('.captionContainer').html(homeVideoTextArray[homeTextVideoArrayPos]);
		pos = parseInt(container.css('left'));
		if(homeTextVideoArrayPos > 0){
			photosContainer.find('.previous').css('display', 'block');	
		}
		if(homeTextVideoArrayPos == homeVideoTextArray.length - 1 ){
			photosContainer.find('.next').css('display', 'none');	
		}
	}
	$('#VideoContainer').html('');
	$('#VideoContainer').hide();
}

