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

$(document).ready(function() {

	$("div#home-news .item:even").addClass("left");
	$("div#home-news .item:odd").addClass("right");
	$("div#home-news .items:not(div#home-news .items:first)").hide();
	$("div#home-features .items:not(div#home-features .items:first)").hide();
	
	$("#home-images .imgs .imagebox:nth-child(4n)").css("margin", "0 0 7px 0");
	$("#video-wrap .imgs .imagebox:nth-child(5n)").css("margin", "0 0 17px 0");
	
	$("#home-images .imagebox").lightBox();
	$("#video-wrap .imgs .imagebox").lightBox();
	
    divItems = $(".hero").size();
	myInterval2=setInterval(tickdivs, 12000);
	
	
	$("div#leaderboardinfo .leaderboard-days:not(div#leaderboardinfo .leaderboard-days:first)").hide();
	
	$("div#leaderboards table tbody tr:even td").css("background-color", "#111");
	$("div#leaderboards table tbody tr td:nth-child(2n)").css("background-color", "#313131");
	//$("div#leaderboards table th.team_heading:odd").css("background-color", "#212121");
	//$("div#leaderboards table td.foot-total:odd").css("background-color", "#212121")
	
});


/*
$('#register-wrap .close_window').click(function(e) {
    e.preventDefault();
    //do other stuff when a click happens
    closeDOMWindow({eventType:'click'});
});
*/
//$('#register-wrap .close_window').closeDOMWindow({eventType:'click'});

$("#leaderboardheading select").change(function(){
	changeLeaderboard("#overal-leaderboard #" + $(this).val());
	$("#leaderboardheading p").text($("#select-venue option:selected").attr("title"));
});

function changeLeaderboard(leaderboardname) {
	$("#overal-leaderboard .leaderboard").hide();
	$(leaderboardname).show();
}

$("div#leaderboardinfo li").click(function() {
	$("div#leaderboardinfo .leaderboard-days").hide();
	$("div#leaderboardinfo #" + $(this).attr("class")).show();
});

$("div#leaderboardinfo ul li").click(function() {
	$("div#leaderboardinfo ul li").attr("id", "");
	$(this).attr("id", "t_current");
});

$("div#home-news .heading ol li").click(function() {
	showPanel(this,"div#home-news ol li","div#home-news .items_"+$(this).text(), "div#home-news .items");
});
$("div#home-features .heading ol li").click(function() {
	showPanel(this,"div#home-features ol li","div#home-features .items_"+$(this).text(), "div#home-features .items");
});

$("div#overal-leaderboard ul li#tab_Full").click(function() {
	$("div#overal-leaderboard ul li#tab_Event").removeClass("on");
	$("div#overal-leaderboard ul li#tab_Full").addClass("on");
	$("div#overal-leaderboard #event-board").hide();
	$("div#overal-leaderboard #season-board").show();
});
$("div#overal-leaderboard ul li#tab_Event").click(function() {
	$("div#overal-leaderboard ul li#tab_Full").removeClass("on");
	$("div#overal-leaderboard ul li#tab_Event").addClass("on");
	$("div#overal-leaderboard #season-board").hide();
	$("div#overal-leaderboard #event-board").show();
});

function showPanel (marker, markers, showPanel, hidePanels) {
	$(hidePanels).hide();
	if ($(marker).length!==0) {
		$(markers).removeClass("on");
		$(marker).addClass("on");
	}
	$(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#bt1_5").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_6").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_7").click(function() {showHero("#hero1", 0);});
$(".hero .cnt ol li#bt1_8").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#bt2_5").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_6").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_7").click(function() {showHero("#hero2", 1);});
$(".hero .cnt ol li#bt2_8").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#bt3_5").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_6").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_7").click(function() {showHero("#hero3", 2);});
$(".hero .cnt ol li#bt3_8").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);});
$(".hero .cnt ol li#bt4_5").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_6").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_7").click(function() {showHero("#hero4", 3);});
$(".hero .cnt ol li#bt4_8").click(function() {showHero("#hero4", 3);});

$(".hero .cnt ol li#bt5_1").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_2").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_3").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_4").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_5").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_6").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_7").click(function() {showHero("#hero5", 4);});
$(".hero .cnt ol li#bt5_8").click(function() {showHero("#hero5", 4);});

$(".hero .cnt ol li#bt6_1").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_2").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_3").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_4").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_5").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_6").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_7").click(function() {showHero("#hero6", 5);});
$(".hero .cnt ol li#bt6_8").click(function() {showHero("#hero6", 5);});

$(".hero .cnt ol li#bt7_1").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_2").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_3").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_4").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_5").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_6").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_7").click(function() {showHero("#hero7", 6);});
$(".hero .cnt ol li#bt7_8").click(function() {showHero("#hero7", 6);});

$(".hero .cnt ol li#bt8_1").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_2").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_3").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_4").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_5").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_6").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_7").click(function() {showHero("#hero8", 7);});
$(".hero .cnt ol li#bt8_8").click(function() {showHero("#hero8", 7);});

$(".hero .cnt ol li#bt9_1").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_2").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_3").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_4").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_5").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_6").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_7").click(function() {showHero("#hero9", 8);});
$(".hero .cnt ol li#bt9_8").click(function() {showHero("#hero9", 8);});

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=%23exss%20OR%20%22extreme%20sailing%20series%22&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>';
			});
		
			$("#twits .wrap").html("");
			
			results=results + "<div class='item'>";
				results=results + "<img src='" + item.profile_image_url + "' alt='' />";
				results=results + "<div class='title'>";
					results=results + "<h3><a href='http://www.twitter.com/" + item.from_user + "'>@" + item.from_user + "</a></h3>";
					results=results + "<p>" + moment(item.created_at).fromNow() + "</p>";
				results=results + "</div>";
				results=results + "<div class='cnt'>";
					results=results + "<p>"+ status +"</p>";
				results=results + "</div>";
			results=results + "</div>";
			
    	    $("#twits .wrap").html("" + results);
		});
	});
	if (results.length==0) {
		$("#twits .wrap").html("");
		$("#twits .wrap").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(7)", 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';
  }
}
