var curritem2=0;
var begunTimer = true;
var divItems;

$(document).ready(function() {
	if ($("#module-images a.imagebox")) {$("#module-images a.imagebox").lightBox()};
	if ($(".image-gallery a.imagebox")) {$(".image-gallery a.imagebox").lightBox()};
	
	$("div#hero-images .hero:not(div#hero-images .hero:first)").hide();
	$("div#module-news .items:not(div#module-news .items:first)").hide();
	$("ul#image-archive li:first").addClass("current");
	$("div#image-gallerys .image-gallery:not(div#image-gallerys .image-gallery:first)").hide();
	getTweets(3);
	divItems = $(".hero").size();
	myInterval2=setInterval(tickdivs, 12000);	
});

$("div#module-news ol li").click(function() {
	showPanel(this,"div#module-news ol li","div#module-news .items_"+$(this).text(), "div#module-news .items");
});
$("ul#image-archive li").click(function() {
	showPanel(this,"ul#image-archive li","div#image-gallerys #gallery-"+$(this).attr("id"), "div#image-gallerys .image-gallery");
});


function showPanel (marker, markers, showPanel, hidePanels) {
	$(hidePanels).hide();
	if ($(marker).length!==0) {
		$(markers).removeClass("current");
		$(marker).addClass("current");
	}
	$(showPanel).fadeIn('slow');
}

$(".hero .cnt ol li#bt1_1").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_2").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_3").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_4").click(function() {showHero("#hero1", 0);});

$(".hero .cnt ol li#bt2_1").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_2").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_3").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_4").click(function() {showHero("#hero2", 1);});

$(".hero .cnt ol li#bt3_1").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_2").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_3").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_4").click(function() {showHero("#hero3", 2);});

$(".hero .cnt ol li#bt4_1").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_2").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_3").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_4").click(function() {showHero("#hero4", 3);});

function tickdivs() {
    $(".hero:eq("+curritem2+")").hide();
    curritem2 = ++curritem2%divItems;
    $(".hero:eq("+curritem2+")").fadeIn("slow");
} 

function showHero (theHero,theCurrent) {
	$(".hero:eq("+curritem2+")").hide();
	$(theHero).show();
	curritem2 = theCurrent;
	clearInterval(myInterval2);
	myInterval2=setInterval(tickdivs, 12000);
}

function getTweets(amount) {
	var url = "http://search.twitter.com/search.json?callback=?&q=%23artemischallenge&rpp=" + amount;
	var results='';
	$.getJSON(url, function(data){ 
		$.each(data.results, function(i, item){
			var date_tweet = new Date(item.created_at);
			var date_now = new Date();
			var date_diff = date_now - date_tweet;
			var hours = Math.round(date_diff/(1000*60*60));
			
			var status = item.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(newurl) {
				return '<a href="'+newurl+'">'+newurl+'</a>';
			});
		
			$("#tweets ").html("");
			
			results=results + "<div class='tweet'>";
				results=results + "<a href='http://www.twitter.com/" + item.from_user + "'><img src='" + item.profile_image_url + "' alt='' height='45' width='45' /></a>";
				results=results + "<div class='cnt'>";
					results=results + "<h5><a href='http://www.twitter.com/" + item.from_user + "'>@" + item.from_user + "</a><span>" + item.created_at + "</h5>";
					results=results + "<p>" + status + "</p>";
				results=results + "</div>";
				results=results + "<div class='clear'></div>";
			results=results + "</div>";
			
    	    $("#tweets").html("" + results);
		});
	});
	if (results.length==0) {
		$("#tweets").html("");
		$("#tweets").html("<div class='item'><p style='font-size:1.3em;'>It would appear no-one has said anything for a while... Or the Twitter Feed is currently unavailable. Not to worry</p></div>");
	}
	//setTimeout("getTweets(5)", 45000);
}

function getTweets2(amount) {
	var url = "http://search.twitter.com/search.json?callback=?&q=%23AOAsolo&rpp=" + amount;
	var results='';
	$.getJSON(url, function(data){ 
		$.each(data.results, function(i, item){
			var date_tweet = new Date(item.created_at);
			var date_now = new Date();
			var date_diff = date_now - date_tweet;
			var hours = Math.round(date_diff/(1000*60*60));
			
			var status = item.text.replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(newurl) {
				return '<a href="'+newurl+'">'+newurl+'</a>';
			});
		
			$("#tweets ").html("");
			
			results=results + "<div class='tweet'>";
				results=results + "<a href='http://www.twitter.com/" + item.from_user + "'><img src='" + item.profile_image_url + "' alt='' /></a>";
				results=results + "<div class='cnt'>";
					results=results + "<h5><a href='http://www.twitter.com/" + item.from_user + "'>@" + item.from_user + "</a><span>" + item.created_at + "</h5>";
					results=results + "<p>" + status + "</p>";
				results=results + "</div>";
				results=results + "<div class='clear'></div>";
			results=results + "</div>";
			
    	    $("#tweets").html("" + results);
		});
	});
	if (results.length==0) {
		$("#tweets").html("");
		$("#tweets").html("<div class='item'><p style='font-size:1.3em;'>It would appear no-one has said anything for a while... Or the Twitter Feed is currently unavailable. Not to worry</p></div>");
	}
	//setTimeout("getTweets(5)", 45000);
}


/* DATE / TIME CALCULATIONS */
function relative_time(time_value) {
  var values = time_value.split(" ");
  time_value = values[1] + " " + values[2] + ", " + values[5] + " " + values[3];
  var parsed_date = Date.parse(time_value);
  var relative_to = (arguments.length > 1) ? arguments[1] : new Date();
  var delta = parseInt((relative_to.getTime() - parsed_date) / 1000);
  delta = delta + (relative_to.getTimezoneOffset() * 60);

  if (delta < 60) {
    return 'less than a minute ago';
  } else if(delta < 120) {
    return 'about a minute ago';
  } else if(delta < (60*60)) {
    return (parseInt(delta / 60)).toString() + ' minutes ago';
  } else if(delta < (120*60)) {
    return 'about an hour ago';
  } else if(delta < (24*60*60)) {
    return 'about ' + (parseInt(delta / 3600)).toString() + ' hours ago';
  } else if(delta < (48*60*60)) {
    return '1 day ago';
  } else {
    return (parseInt(delta / 86400)).toString() + ' days ago';
  }
}
