//RESIZE CALCULATIONS
function resizeCalculations() {
	var winH = $(window).height();
	var winW = $(window).width();
	$('html, body').css( {
		'width' 		: 	winW,
		'height'		:	winH
	} );
};
resizeCalculations();
var delay = (function(){
  var timer = 0;
  return function(callback, ms){
    clearTimeout (timer);
    timer = setTimeout(callback, ms);
  };
})();
$(window).bind('resize', function () {
	delay(function(){
		resizeCalculations();
		centerPopup();
	}, 0);
} );
//START DOC READY
$(document).ready(function () {
	//TUMBLR
	$('#posts')
		.tumblr({
			url:        'http://connectednesslocus.tumblr.com',
			pagination: '#pagination',
			loading:    '#loading'
		});
	//TWITTER
	$(".tweet").tweet({
		username: "cnnctdnsslcs",
		join_text: "auto",
		avatar_size: 32,
		count: 10,
		auto_join_text_default: "",
		auto_join_text_ed: "",
		auto_join_text_ing: "",
		auto_join_text_reply: "",
		auto_join_text_url: "",
		loading_text: "L 0 4 D 1 N 6   + 3 H   + W 3 3 + 4 G 3"
	});
	//FLICKR
	$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?id=40887083@N05&format=json&jsoncallback=?", function(data) {
		var target = "#flickr"; // Name of div surrounding images.
		for (i = 0; i <= 9; i = i + 1) { // Loop through the 10 most recent, [0-9]
			var pic = data.items[i];
			var liNumber = i + 1; // Add class to each LI (1-10)
			$(target).append("<a title='" + pic.title + "' href='" + pic.link + "'><img src='" + pic.media.m + "' /></a>");
		}
		//JQUERY CYCLE
		function onAfter() {
		   $('#output').html("Current anchor: " + this.href);
		}
		$('#flickr').cycle({
			fx:     'fade',
			speed:   500,
			timeout: 1000, 
			after:   onAfter 
		});
	});
	//SHOWS
	var popupStatus = 0;
	function loadPopup(){
		if(popupStatus==0){
			$("#backgroundPopup").css({
				"opacity": "0.8"
			});
			$("#backgroundPopup").fadeIn("slow");
			$("#popupContact").fadeIn("slow");
			popupStatus = 1;
		}
	}
	function disablePopup(){
		if(popupStatus==1){
			$("#backgroundPopup").fadeOut("slow");
			$("#popupContact").fadeOut("slow");
			popupStatus = 0;
		}
	}
	function centerPopup(){
		var windowWidth = document.documentElement.clientWidth;
		var windowHeight = document.documentElement.clientHeight;
		var popupHeight = $("#popupContact").height();
		var popupWidth = $("#popupContact").width();
		$("#popupContact").css({
			"position": "absolute",
			"top": windowHeight/2-popupHeight/2,
			"left": windowWidth/2-popupWidth/2
		});
		$("#backgroundPopup").css({
			"height": windowHeight
		});
	};
	centerPopup();
	$(".shows").click(function(){
		console.log('click');
		loadPopup();
	});
	$("#popupContactClose").click(function(){
		disablePopup();
	});
	$("#backgroundPopup").click(function(){
		disablePopup();
	});
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});//END DOC READY
});


