var rota;

$(function(){
	if($("#main-question input:radio:checked").val() == 'yes') $("fieldset#additional, fieldset#additional div").show();
	else $("fieldset#additional, fieldset#additional div").hide();

	$("#main-question input").css('left', '-50%');
	$("#reps dt a").lightBox();
	$("#main-question label")
		.click(function(){		
			$("#main-question label").removeClass("selected");
			$(this).addClass("selected");
			if($(this).attr("for") == "reunion_yes"){
				$("fieldset#additional").show();
				$("fieldset#additional div:first").fadeIn("slow");
				setTimeout(function(){$("fieldset#additional div:last").fadeIn("slow");}, 1000);
			} else {
				$("fieldset#additional, fieldset#additional div").fadeOut("slow");
				$("fieldset#additional select").each(function(){ this.selectedIndex = 0; })
			}
		}
	);
	
	$("#contact-form form")
		.submit(function(){
			var returnVal = true;

			$("#result").remove();
			
			$("#contact-form form label em").each(function(){
				var labelFor = $(this).parent("label").attr("for");
												
				/*
if(!$("#content #main-question input:radio:checked").val()){
					$("#main-question h3").addClass("darkred");
					returnVal = false;
				} else $("#main-question h3").removeClass("darkred");
*/

				if(!$("#"+labelFor).val()){
					$(this).addClass("red");
					returnVal = false;
					//returnVal = ($("#content #main-question input:radio:checked").val() == "no" && ((labelFor == "season") || (labelFor == "location"))) ? true : false;
				} else $(this).removeClass("red");
				
			});
			
			if(returnVal == false){
				$("#contact-form form").prepend('<p id="result">Please fill out the <span class="red">required</span> fields.</p>');
				$("#result").hide().fadeIn("slow");
			}	
			
			return returnVal;
		}
	);
	
	if(jQuery.browser.msie && (parseInt(jQuery.browser.version)) == 6){
		$("#main-question label").mouseover(function(){ $(this).css('background-position', 'left top');})
		$("#main-question label").mouseout(function(){ $(this).css('background-position', 'right top');})
	}
	
/*
	var current = $("#saying li:visible");	
	var nextEl = $(current).next();

	$("#saying li:not(:first)").hide();
	$("#saying li span.continuation").hide();
	$("#saying li").append(' <a href="#" class="next">/ next</a>');
	$("#saying li a.next").hide().click(function(){
		$(this).parent("li").hide().next().fadeIn("slow");
		if($(this).parent("li").is(":last-child")) $("#saying li:first").fadeIn("slow");
		return false;
	});
	$("#saying a#read-more, #saying a.more").click(
		function(){
			if($(this).text() == 'less'){
				letsStart();
				$("#saying a.next").hide();
				$("#saying li span.tail").show();
				$("#saying li span.continuation").hide();
				$("#saying li").css({'text-align':'center'});														
				$("#saying").animate({height:'30px'},{duration:200});
				$("#saying a.more").text("more");
			}
			else {
				if(rota) window.clearInterval(rota);
				$("#saying li span.tail").hide();
				$("#saying li span.continuation").show();
				$("#saying a.next").fadeIn();	
				$("#saying li").css({'text-align':'left'});																			
				$("#saying").animate({height:'125px'},{duration:200});
				$("#saying a.more").text("less");
			}
			return false;
		}
	);
	$("#saying #next").hide().click(function(){
		changeSaying();
		return false;
	});
*/
	
	//letsStart();
	
	setInterval(countdown, 1000);			
	
	// Append Facebook Javascript API		
	var e = document.createElement('script');
	e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
	e.async = true;
	document.getElementById('fb-root').appendChild(e);
	
	window.fbAsyncInit = function(){FB.XFBML.parse();};
	
	$("#fb-comments a[rel]").click(function(){	
		var url = 'https://graph.facebook.com/' + ((this.rel == 'group') ? '125496654147872' : '158774077493998') + '/feed/?limit=5&callback=?';		
		getFeed(url, false);
		if(this.rel == 'event'){ getAttendance(); $("#fb-comments h3 a.right").attr('href','http://www.facebook.com/event.php?eid=158774077493998'); }
		else { $("#survey").hide(); $("#fb-comments h3 a.right").attr('href','http://www.facebook.com/group.php?gid=125496654147872'); }
		
		$("#fb-comments a[rel]").removeClass('current');
		$(this).addClass('current');
		return false;	
	});
	
	getFeed('https://graph.facebook.com/158774077493998/feed?limit=5&access_token=138957509487736|7TyxbMQGm-rmuWP7PtSgvanZKeU&callback=?');
	getAttendance();
	
	$("#load-more").click(function(){
		getFeed(this.href);
		return false;
	});
	
});

function letsStart() {
	rota = window.setInterval("changeSaying()", 6000);
}

function changeSaying(){
	current = $("#saying li:visible");	
	nextEl = $(current).next();
	$(current).hide();
	if($(current).is(":last-child")) $("#saying li:first").fadeIn("slow");
	else $(nextEl).fadeIn("slow");
}

function countdown(){
		
	targetDate = targetDate.toLocaleString();
	
	var endDate = new Date(targetDate).getTime();
	var epoch = new Date().getTime();			
	var remMSecs = (endDate - epoch);
	
	if(remMSecs <= 0){
		
		// targetDate has passed
		$("#days").text(pad(0));
		$("#hrs").text(pad(0));
		$("#mins").text(pad(0));
		$("#secs").text(pad(0));
	
	} else {
	
		var days = Math.floor((remMSecs/(24*60*60*1000)));
		remMSecs = (remMSecs-(days*24*60*60*1000));
		
		var hours = Math.floor((remMSecs/(60*60*1000)));
		remMSecs = (remMSecs-(hours*60*60*1000));
		
		var mins = Math.floor(remMSecs/(60*1000));
		remMSecs = (remMSecs-(mins*60*1000));
		
		var secs = Math.floor(remMSecs/1000);
		
		$("#days").text(pad(days));
		$("#hrs").text(pad(hours));
		$("#mins").text(pad(mins));
		$("#secs").text(pad(secs));
	
	}

}

function pad(number){			
	number = (number <= 0) ? 0 : number;				
	return (number.toString().length < 2) ? '0'+number : number;		
}

var targetDate = "Dec 28, 2010 6:00 PM";

function getFeed(feedUrl, append){

	append = (append != null) ? append : true;

	$.getJSON(feedUrl || 'http://graph.facebook.com/125496654147872/feed/?limit=5&access_token=138957509487736|7TyxbMQGm-rmuWP7PtSgvanZKeU&callback=?', function(grpFeed){
				
		if(grpFeed.data.length){
		
			var c = '';	
			for(var i=0; i < grpFeed.data.length; i++){
				var d = (grpFeed.data[i].created_time).split(/T/)[0].split(/-/);
				c += '<li><fb:profile-pic uid="' + grpFeed.data[i].from.id + '" linked="true" width="40"></fb:profile-pic> <p><span class="date">'+d[1]+'/'+d[2]+'/'+d[0]+'</span><strong><fb:name uid="'+ grpFeed.data[i].from.id+'"></fb:name>:</strong> '+ (grpFeed.data[i].message).replace(/\n/g, '<br />') +'</p></li>';
			}
			
			$("#load-more").attr('href', grpFeed.paging.next+'&callback=?');
			
			if(append) $(c).appendTo($("#fb-comments div.pad ul#fb-posts"));
			else $("#fb-comments div.pad ul#fb-posts").html(c);
			
		}
		
		if(FB) FB.XFBML.parse();

	});

}

function getAttendance(){

	var attending = notAttending = maybe = awaiting = 0;

	$.getJSON('https://graph.facebook.com/158774077493998/invited/?access_token=138957509487736|8634785daf3e13d32a014af717f2e4b9&callback=?', function(survey){
				
		if(survey.data.length){
		
			for(var i=0; i < survey.data.length; i++){			
				if(survey.data[i].rsvp_status == 'attending') attending++;
				if(survey.data[i].rsvp_status == 'declined') notAttending++;
				if(survey.data[i].rsvp_status == 'unsure') maybe++;
				if(survey.data[i].rsvp_status == 'not_replied') awaiting++;
			}
			
			c = '<li><em>'+attending+'</em> Attending</li><li><em>'+maybe+'</em> Maybe</li><li><em>'+awaiting+'</em> Awaiting Reply</li><li><em>'+notAttending+'</em> Not Attending</li>';
			
			$("#survey ul").html(c);
			
			$("#survey").show();
			
		}
		
		if(FB) FB.XFBML.parse();

	});

}
