/* Author:
Sam Thurman
www.samthurman.com
*/
var ids = "";
var page_number = 1;
var ajax_url = 'sites/all/themes/nycmedialab/scripts/ajax_handler.php';

$(document).ready(function() {
	//pick random header
	var length = $("#thoughtsContainer p").length;
	var ran = Math.floor(Math.random()*length) + 1;
	var newBg = $("#thoughtsContainer p:nth-child(" + ran + ")");
	preload(newBg);
	var navInner = $('.navInner');
	preload(navInner);


	function preload(newBg){
		//get background image of header images, preload it, fadeIn on load.
		//screws up in IE due to image caching
		var bgImage = extractUrl(newBg.css("background-image"));
		if (($.browser.msie)){
			newBg.fadeIn('slow');
		}else{
			$('<img/>').attr("src",bgImage+ "?" + new Date().getTime()).load(function() {
				newBg.fadeIn('slow');
			});
		}
	}

	function extractUrl(input)
	{
		// remove quotes and wrapping url()
		return input.replace(/"/g,"").replace(/url\(|\)$/ig, "");
	}

	//main nav hovers
	$("#mainNav span").hover(function() {
		$(this).stop().animate({
			borderTopColor: "#8F0071"
		}, {
			duration:200,
			queue: false
		}).animate({
			backgroundColor: "#8F0071"
		}, {
			duration:200,
			queue: false
		});
	},function() {
		$(this).stop().animate({
			borderTopColor: "#EEE"
		}, {
			duration:100,
			queue: false
		}).animate({
			backgroundColor: "#222222"
		}, {
			duration:100,
			queue: false
		});
	});

	//section header hovers
	$("h1.static").hover(function() {
		$(this).find('span').fadeTo({
			duration:200,
			queue: false
		},1);
	},function() {
		$(this).find('span').fadeTo({
			duration:100,
			queue: false
		},0);
	});

	//other hovers
	$(".hoverable").hover(function() {
		$(this).stop(true,true).toggleClass('hovered', 200);
	},function() {
		$(this).stop(true,true).toggleClass('hovered', 100);
	});


	//fires a function corresponding to your current section
	$(window).scroll(function() {

		var height = $(document).height();
		var scrollTop = $(window).scrollTop();

		var offsetPeople = $("#people").offset();
		var aboutBottom = offsetPeople.top-120;

		var offsetEvents = $("#events").offset();
		var peopleBottom = offsetEvents.top-120;

		var offsetMembership = $("#membership").offset();
		var eventsBottom = offsetMembership.top-120;

		var offsetContact = $("#contact").offset();
		var membershipBottom = offsetContact.top-120;

		//fix for iphone/pad lack of fixed positioning
		var deviceAgent = navigator.userAgent.toLowerCase();
		var agentID = deviceAgent.match(/(iphone|ipod|ipad)/);

		if (agentID){
			$(".fullBio").css("top",scrollTop+"px");
		};

		//make sure header is attached past 610px
		if(scrollTop>610){
			$("#mainSubHeader").attr('class', 'attached');
			$(".headerWidth").attr('class', 'headerWidth headerAttached');
		};

		//test to see where user is scrolled to - take action when appropriate
		if(scrollTop<592){
			$(".headerWidth").attr('class', 'headerWidth');
			$("#aboutHeader").attr('style', '');
			$("#mainSubHeader").attr('class', '');

			$("#aboutHeader").attr('class', 'mainHeader static');
			$("#peopleHeader").attr('class', 'mainHeader static');
			$("#membershipHeader").attr('class', 'mainHeader static');
			$("#contactHeader").attr('class', 'mainHeader static');


		} else if((scrollTop>592) && (scrollTop<610)){
			$("#mainSubHeader").attr('class', 'attached');
			$(".headerWidth").attr('class', 'headerWidth');

			if($("#aboutHeader").hasClass("headerActive")){
				$("#aboutHeader").attr("class","mainHeader animateDown").fadeTo(300,0,function(){
					$("#aboutHeader").attr("class","mainHeader static").removeAttr('style');
				});
			}

		} else if((scrollTop>610) && (scrollTop<aboutBottom)){
			if ($("#aboutHeader").hasClass("headerActive") ){
				return false
			} else {
				navControl("","#aboutHeader","#peopleHeader");
			}

		} else if((scrollTop>aboutBottom) && (scrollTop<peopleBottom)){

			if ($("#peopleHeader").hasClass("headerActive") ){
				return false
			} else {
				navControl("#aboutHeader","#peopleHeader","#eventsHeader");
			}

		} else if((scrollTop>peopleBottom) && (scrollTop<eventsBottom) ){
			$("#aboutHeader").attr('class', 'mainHeader passed');

			if ($("#eventsHeader").hasClass("headerActive") ){
				return false
			} else {
				navControl("#peopleHeader","#eventsHeader", "#membershipHeader");
			}

		} else if((scrollTop>eventsBottom) && (scrollTop<membershipBottom) ){
			$("#aboutHeader").attr('class', 'mainHeader passed');
			$("#peopleHeader").attr('class', 'mainHeader passed');

			if ($("#membershipHeader").hasClass("headerActive") ){
				return false
			} else {
				navControl("#eventsHeader","#membershipHeader", "#contactHeader");
			}

		} else if((scrollTop>membershipBottom) ){
			$("#aboutHeader").attr('class', 'mainHeader passed');
			$("#peopleHeader").attr('class', 'mainHeader passed');
			$("#eventsHeader").attr('class', 'mainHeader passed');

			navControl("#membershipHeader","#contactHeader","");
		};

	});

	//this function controls header action when you enter a new section
	function navControl(prevSectionHead,currentSectionHead,nextSectionHead){

		if ($(prevSectionHead).hasClass("headerActive") ){
			$(prevSectionHead).attr("class","mainHeader animateDown").stop(true,true).animate({
				borderTopWidth:"0"
			}, 100, function(){
				$(prevSectionHead).attr("class","mainHeader passed");
			});
		}

		if ($(prevSectionHead).hasClass("static") ){
			$(prevSectionHead).attr("class","mainHeader passed");
		}

		if (($(currentSectionHead).hasClass("static")) && (currentSectionHead == "#aboutHeader")){
			$(currentSectionHead).attr("class","mainHeader animateUp").stop(true,true).animate({
				borderTopWidth:"10"
			}, 100, function(){
				$(currentSectionHead).attr("class","mainHeader headerActive");
			});
		} else	if ($(currentSectionHead).hasClass("static") ){
			$(currentSectionHead).attr("class","mainHeader animateUp").stop(true,true).animate({
				top:70
			}, 100, function(){
				$(currentSectionHead).attr("class","mainHeader headerActive");
			});

		} else if ($(currentSectionHead).hasClass("passed") ){
			$(currentSectionHead).attr("class","mainHeader animateDown").stop(true,true).animate({
				borderTopWidth:"10"
			}, 100, function(){
				$(currentSectionHead).attr("class","mainHeader headerActive");
				$(nextSectionHead).attr("class","mainHeader animateDown").fadeTo(300,0,function(){
					$(nextSectionHead).attr("class","mainHeader static").fadeTo(300,1,function(){
						$(this).removeAttr('style');
					});
				});
			});
		}

	}//end navControl

	//initiate carousel
	jQuery(".slideshowInner").carouFredSel({
		prev: ".previous",
		next: ".next",
		width:1479,
		auto: false,
		items: {
			minimum: 4,
			width: 493,
			visible:3,
			height: 310
		},
		scroll: {
			items: 1,
			duration: 600
		}
	});

	//clear search field for People section
	$('#searchPeople').focus(function(){
		$(this).removeClass('blink');
		var content = $(this).val();
		var placeholder = $(this).attr("placeholder");
		if(content==placeholder){
			$(this).val("")
		}
	});
	$('#searchPeople').blur(function(){
		var content = $(this).val();
		var nothing = "" ;
		if(content == nothing){
			$(this).addClass('blink').val(placeholder);
		}
	});

	//clear other fields
	$('.clearField').focus(function(){
		var content = $(this).val();
		var placeholder = $(this).attr("placeholder");
		if(content==placeholder){
			$(this).val("")
		}
	});
	$('.clearField').blur(function(){
		var content = $(this).val();
		var placeholder = $(this).attr("placeholder");
		var nothing = "";
		if(content==nothing){
			$(this).val(placeholder)
		}
	});

	//smooth scroll for mainNav
	$('#mainNav a').bind('click',function(event){
		var $anchor = $(this);

		$('html, body').stop().animate({
			scrollTop: $($anchor.attr('href')).offset().top-70
		}, 1000);
		event.preventDefault();
	});

	//smooth scroll for other Navs
	$('.mainHeader').bind('click',function(event){
		var $anchor = "#"+$(this).attr("title");

		$('html, body').stop().animate({
			scrollTop: $($anchor).offset().top-70
		}, 1000);
		event.preventDefault();
	});

	//people overlay

	//load overlay
	$('a.viewBioContainer').live('click', function(){
		var myIndex = $(this).parent().index();
		$('#peopleBios').find('span:eq('+myIndex+')').fadeIn("fast",function(){
			$(this).addClass("fullBioLoaded");
		});
	});

	//unload overlay when xButton is clicked
	$('.xButton').live('click', function(){
		$(this).parent().parent().removeClass("fullBioLoaded").fadeOut('fast');
	});
	//unload overlay when background is clicked
	$('.fullBio').click(function(){
		if ($(this).hasClass('fullBioLoaded')){
			$(this).removeClass("fullBioLoaded").fadeOut('fast');
		} else {
	//nothing
	}
	});	
	
	$('.xButton2').live('click', function(){
		$(this).parent().parent().fadeOut('fast');
	});
	
	$('#frmSearch').submit(function(){
		searchProfiles($('#searchPeople').val());
		return false;
	});
	
	$('.hoverable').click(function() {
		$('.hoverable').each(function() {
			$(this).removeClass('selected');
		});
		$(this).addClass('selected');
		$('#searchPeople').val($(this).text());
		searchProfiles($(this).text());
	});	
	
	$('.viewMore').click(function(){
		viewMore();
		return false;
	});
	
	$('.frmMC').submit(function(){
		email = this.elements['email'].value;
		if (email == "Enter your email for updates"
			|| email == "") { 
			alert("You must enter an email address");
			return false;
		}
		getMailChimpLists(email);
		return false;
	});
	
	$('#subscribeEmail').live('click', function(){
		if (!$("input[name='mcGroup']:checked").val()) {
			//alert('You must select a list');
			//return false;
		}
	});
	
	searchProfiles('');
	
	//privacy policy overlay
	//load overlay
	$('a#privacyPolicyLink').click(function(){
		$(this).next().fadeIn();
	});
});//ready

function searchProfiles(search_term) {
	ids = "";
	page_number = 1;
	$.post(ajax_url
		,{
			"action":"search",
			"search_term":search_term,
			"page_number":page_number
		}
		,function(data){
			$(".viewBioContainer", data).each(function() {
				ids += $(this).attr('id') + ",";
			});			
			$('#peopleThumbs').html(data);
			getBios();
		}
		,"html");
}

function viewMore() {
	ids = "";
	page_number++;	
	$.post(ajax_url
		,{
			"action":"viewmore"
			,
			"page_number":page_number
		}
		,function(data){
			$(".viewBioContainer", data).each(function() {
				ids += $(this).attr('id') + ",";
			});						
			$('#peopleThumbs').html(data);
			getBios();
		}
		,"html");
}

function getBios() {
	$.post(ajax_url
		,{
			"action":"getbio",
			"ids":ids
		}
		,function(data){
			$('#peopleBios').html(data);
		}
		,"html");
}

function getMailChimpLists(email) {
	$.post(ajax_url
		,{
			"action":"get_mailchimp_lists"
			,
			"email":email
		}
		,function(data){			
			$('#divMailChimp').html(data);
			$('#divMailChimp').show();
		}
		,"html");	
}
