var bFooterCollapsed = true
var fbObject = null;
var twObject = null;
var flObject = null;
var oFacebookFeed = null;
var oTwitterFeed = null;
var oFlickrFeed = null;
var iPage = 0;
var oFlickrImages = Array();
var i = 0;
var isLab = false;
var isWork = false;
var photosetLength = 0;
var flickrSetID = 0;
var isStarted = false;

// object to hold the feed data for easier manipulation
function feed(author,date,message,type) {
	this.author = author;
	this.date = date;
	this.message = message;
	this.type = type;
}

window.onerror = function() { return true; }
	
$(document).ready(function() { // onload
	$('#aboutSubMenu').mouseout(hideSubMenu());
	// implement resize listener
	$(window).bind('resize',function() {
		if (($('.content_boundingbox').height() + 280) > $(window).height()) {
			$('#col_left').height($('.content_boundingbox').height() + 320);
		} else {
			$('#col_left').height($(window).height());
		}
	});
	// stretch the sidebar
	$('#col_left').height($(window).height());
	
	// implement social rollovers
	$('img.social').each(function() {
		var oldSrc = $(this).attr("src");
		$(this).mouseover(function() {
			$(this).attr("src",oldSrc.replace(".png","_on.png"));	
		});
		$(this).mouseout(function() {
			$(this).attr("src",oldSrc.replace("_on.png",".png"));				
		});
	});	
	// implement footer functionality
	$('#footer_container').hide();
	setTimeout("resizeFooter()",250);
	$('#footerHead').css('cursor','pointer');
	$('#footerHead').click(function() {
		var targetHeight = $('#footer_container').height() - 60;
		if (!bFooterCollapsed) {
			$('#footer_container').animate({bottom: "-"+ targetHeight +"px"});
			bFooterCollapsed = true;
		} else {
			$('#footer_container').animate({bottom: "0px"});
			bFooterCollapsed = false;
		}
	});
	// end footer functionality	
	
	// get most recent facebook update
	try {
		fbObject = $.getJSON("https://graph.facebook.com/chemistryagency/feed?limit=1&callback=?",function(data) {
			oFacebookFeed = new feed(data.data[0].from.name,data.data[0].updated_time,data.data[0].message,data.data[0].type);
			if (data == null)
				throw(exception);
				
			$('#fbPost').html(oFacebookFeed.message);
		});
	} catch (e) {
		$('#fbPost').html("Facebook is having problems with their servers.");
	}
		
	// get most recent twitter update
	try {
		twObject = $.getJSON("http://twitter.com/status/user_timeline/visitthelab.json?count=1&callback=?",function(data) {
			oTwitterFeed = new feed(data[0].user.name,data[0].created_at, data[0].text);
			if (!isLab && !isWork)
				compareFeeds();
			if (data == null)
				throw(exception);
		});
	} catch (e) {
		$('#tweets').html("our twitter account is packed. Please try again soon.");
	}
	// get the most recent flickr update
	if (isLab || isWork) {
		fbObject = $.getJSON("http://api.flickr.com/services/rest/?method=flickr.photosets.getPhotos&api_key=a4b796e46f5f0597792553cd75a5642a&photoset_id=" + flickrSetID + "&format=json&jsoncallback=?",{ }, function(data) {
			if (data != null) {
				photosetLength = data.photoset.photo.length;
				var k = 0;
				for (k = 0; k < photosetLength; k ++) {
					//if (oFlickrFeed == null) {
						//oFlickrFeed = new feed();//data.photoset.photo.author,data.items[0].published, data.items[0].description);
					//}
					var _link = "http://www.flickr.com/photos/visitthelab/" + data.photoset.photo[k].id;
					var currIndex = k;
					$.getJSON("http://api.flickr.com/services/rest/?method=flickr.photos.getSizes&api_key=a4b796e46f5f0597792553cd75a5642a&photo_id=" + data.photoset.photo[k].id +"&format=json&jsoncallback=?", { }, function(data) {	
						var img = new Image();
						var index = "Small";
						if (isWork == true) { 
							index = "Original";
							
						}
						
						var j = 0;
						try {
							for (j = 0; j < data.sizes.size.length; j ++) {
								if (index == data.sizes.size[j].label) {
									img.src = data.sizes.size[j].source.replace('\'','');
									break;
								}
							}
							oFlickrImages.push(["http://www.flickr.com/photos/visitthelab/sets/"+ flickrSetID +"/",img]);
						} catch (e) {
						}
						if (isLab && !isStarted && currIndex == photosetLength -1) {
							initFlickr();
							setInterval("changeFlickr()",4000);	
							isStarted = true;	
						}
						if (isWork && !isStarted && currIndex == photosetLength -1) {
							initWorkFlickr();
							setInterval("changeFlickrWork()",4000);
							isStarted = true;
						}
					});
				}
			}
			try {
				compareFeeds();
			} catch (e) {
			}
		});
	}
	
	// popup lab bubble
	var bubbleFadeInTime = Math.random()*60000;
	var bubbleFadeOutTime = Math.random()*20000;
	
	setTimeout("$('#menuBubble').animate({height:'150px'})",bubbleFadeInTime);
	setTimeout("$('#menuBubble').animate({height:'150px'})",bubbleFadeOutTime + bubbleFadeInTime);
});

function resizeFooter() {
	var targetHeight = $('#footer_container').height() - 60;
	$('#footer_container').css("bottom", "-"+ targetHeight +"px");
	$('#footer_container').show();
	if (($('.content_boundingbox').height() + 280) > $(window).height()) {
		$('#col_left').height($('.content_boundingbox').height() + 320);
	} else {
		$('#col_left').height($(window).height());
	}
}

function compareFeeds() {
	// look at the posts from each feed and display 
	var fbDateObj = oFacebookFeed.date.toString().split("T"); 	// ex: 2010-09-20T14:40:19+0000
	var twDateObj = oTwitterFeed.date.toString().split(" "); 	// ex: Mon Sep 20 15:49:51 +0000 2010
	//var flDateObj = oFlickrFeed.date.toString().split("T"); 	// ex: 2010-07-09T19:53:57Z
	
	var dFacebookDate = new Date(fbDateObj[0]);
	var dTwitterDate = new Date(twDateObj[1] +' '+ twDateObj[2] +' '+ twDateObj[5] +' '+ twDateObj[3] +' '+ twDateObj[4]);
	//var dFlickrDate = new Date(flDateObj[0]);
	
	if (( dFacebookDate > dTwitterDate )) {// && ( dFacebookDate > dFlickrDate )) {
		showFeed("<strong>Chemistry</strong> has posted something on their wall");
	} else if (( dTwitterDate > dFacebookDate )) {// && ( dTwitterDate > dFlickrDate )) {
		showFeed("<strong>Chemistry</strong> has a new tweet");
	} else {
		showFeed("<strong>Chemistry</strong> has something new in the lab");
	}
	//} else if (( dFlickrDate > dFacebookDate ) && ( dFlickrDate > dTwitterDate )) {
		//showFeed("<strong>Chemistry</strong> has posted a new image to the lab");
	//}	
}

function showFeed(message) {
	$('#fbBubbleFeed').html(message);
}

function swapBricks(POS){ // menu rollovers
	if(POS == 1){
		$('#bricks').css('backgroundPosition','30px 0px');
	}
	if(POS == 2){
		$('#bricks').css('backgroundPosition','30px -35px');
	}
	if(POS == 3){
		$('#bricks').css('backgroundPosition','30px -71px');
	}
	if(POS == 4){
		$('#bricks').css('backgroundPosition','30px -108px');
		// about, show menu
		//showSubMenu();
	}
	if(POS == 5){
		$('#bricks').css('backgroundPosition','30px -142px');
	}
	if(POS == 6){
		$('#bricks').css('backgroundPosition','30px -174px');
	}
	//if (POS != 4) 
		//hideSubMenu();
}

var konami = (function(){
 var sequence = [38, 38, 40, 40, 37, 39, 37, 39, 66, 65, 13]; //konami code
 //var sequence = [52, 48, 48, 76, 66, 70, 84, 87]; 400lbftw
 var progressIndex = 0;
 var easterEgg = function(){
  //alert("KONAMI'ED!!!");
  $('#flame').css('display', 'block');

 };
 return {
  interceptKey: function(thisEvent){
   var keyCode;
   if (window.event) {
    keyCode = thisEvent.keyCode;
   }
   else {
    if (thisEvent.which) {
     keyCode = thisEvent.which;
    }
   }
   if (keyCode == sequence[progressIndex]) {
    progressIndex++;
    if (progressIndex == sequence.length) {
     this.trigger();
    }
   }
   else {
    this.reset();
   }
  },
  reset: function(){
   progressIndex = 0;
  },
  trigger: function(){
   easterEgg();
   this.reset();
  }
 }
}());


