var uiStringExternalLink = " (External Link)";

var uiIntroSliderTimer = 0;
function uiIntroSlider() {
	$("#introduction-hover-tip").stop(true,true);
	$("#introduction-more").stop(true,true);
	
	$("#introduction-hover-tip").fadeOut("fast");
	$("#introduction-more").slideDown("fast");
	
	clearTimeout(uiIntroSliderTimer);
}

function uiFeaturedScreenshot() {

	if(!$("#flickr-featured a:visible").length) {
		$("#flickr-featured a:first").fadeIn("slow");
		setInterval('uiFeaturedScreenshot();', 6500);
		return;
	}

	var foundActive = false;
	var featuredLoopDone = false;
	$("#flickr-featured a").each(function() {
		if(foundActive == false && featuredLoopDone == false) {
			if($(this).css("display") != "none") {
				foundActive = true;
				$(this).fadeOut("slow");
			}
		} else if(foundActive == true && featuredLoopDone == false) {
			$(this).fadeIn("slow");
			featuredLoopDone = true;
		}
	});
	
	if(foundActive == true && featuredLoopDone == false) {
		$("#flickr-featured a:first").fadeIn("slow");
	} else if(foundActive == false) {
		$("#flickr-featured a").fadeOut("slow");
	}

}

function uiUpdateChatCount() {
	$.getJSON("dynamic/chatterous.php",
		function(data) {
			if(typeof(data[0]) != "undefined") {
				$("#chat-status").html("Chat<span style=\"display:none\"></span>");
				$("#chat-status span").html(data.length);
				$("#chat-status span").fadeIn("fast");
			}
		});
}

function redditSubmit($url) {
	$url = encodeURIComponent($url);
}

$(document).ready(function(){

	$("#featured-top-right").html('<a href="http://www.aureusknights.com/forums/showthread.php?t=2766" style="background-image: url(http://aureusknights.com/images/splash-top.jpg);"><span><strong>Warcraft Cataclysm</strong> Announced</span></a>');
	$("#featured-bottom-right").html('<a href="http://www.aureusknights.com/forums/forumdisplay.php?f=52" style="background-image: url(http://aureusknights.com/images/splash-bottom.jpg);"><span>Join us in <strong>Aion</strong></span></a>');
	$("#featured-left").html('<a href="http://www.aureusknights.com/forums/forumdisplay.php?f=57" style="background-image: url(http://aureusknights.com/images/splash-featured.jpg);"><span><strong>Final Fantasy XIV</strong> Beta</span></a>');

	if($("#flickr-featured").length) {
		$.ajax({
		  url: "dynamic/flickr.php",
		  cache: true,
		  success: function(html){
			$("#flickr-featured").html(html);
			if(!$.browser.msie){ // TODO: Figure out why this isn't working on IE.
				$("#flickr-featured img").each(function() {
						var middleWidth = 115 - ($(this).attr("width") / 2);
						var middleHeight = 70 - ($(this).attr("height") / 2);
						if($(this).attr("width") >= 230) { $(this).css("left", '-'+middleWidth+'px'); } else { $(this).css("width", "230px"); }
						if($(this).attr("height") >= 140) { $(this).css("top", '-'+middleHeight+'px'); } else { $(this).css("width", "140px"); }
						/*if($(this).attr("width") > 230) { $(this).css("left", '-'+(($(this).attr("width")-230)/2)+'px'); } else { $(this).css("width", "230px"); }
						if($(this).attr("height") > 140) { $(this).css("top", '-'+(($(this).attr("height")-140)/2)+'px'); } else { $(this).css("width", "140px"); }*/
					});
			}
			uiFeaturedScreenshot();
		  }
		});
	}
	
	if($("#chat-status").length) {
		uiUpdateChatCount();
	}
	
	$("#sidebar-content").html($("#sidebar-real").html());
	$("#introduction-hover-tip").html("More information...");

	$("#sidebar").hover(
		function() { },
		function() {
			$("#introduction-hover-tip").stop(true,true);
			$("#introduction-more").stop(true,true);
			
			$("#introduction-hover-tip").fadeIn("fast");
			$("#introduction-more").slideUp("fast");
		});
	
	$("div.content-cell").hover(
		function() {
			$(this).find("span.share-buttons").stop(true,true);
			$(this).find("span.share-buttons").fadeTo("fast", 1.0);
		},
		function() {
			$(this).find("span.share-buttons").stop(true,true);
			$(this).find("span.share-buttons").fadeTo("fast", 0.15);
		});
	
	$("#introduction-slider").hover(
		function() {
			uiIntroSliderTimer = setTimeout('uiIntroSlider();', 250);
		},
		function() {
			clearTimeout(uiIntroSliderTimer);
		});

	$("#subscribe a").hover(
		function() {
			$("#subscribe-tip").stop(true,true);
			if($(this).attr("title").indexOf(uiStringExternalLink) != -1) {
				$("#subscribe-tip").html($(this).attr("title").substring(0, $(this).attr("title").indexOf(uiStringExternalLink)));
			} else {
				$("#subscribe-tip").html($(this).attr("title"));
			}
			$("#subscribe-tip").fadeIn("Slow");
		},
		function() {
			$("#subscribe-tip").stop(true,true);
			$("#subscribe-tip").fadeOut("Slow");
		});

	$("#content div.content-cell").each(function() {
	
		var title = $(this).find("h3:first a");
		var social = $(this).find(".share-buttons:first");
		
		var url = encodeURIComponent($(title).attr("href"));
		title = encodeURIComponent($(title).text());
		
		var height = 12;
		
		$(social).html('<a class="external" href="http://twitter.com/home?status='+title+': '+url+'" title="Tweet this"><img src="images/twitter_16.png" height="'+height+'" alt="Tweet this" /></a> ' +
					   '<a class="external" href="http://digg.com/submit?url='+url+'&title='+title+'&bodytext=%21&topic=gaming_news&media=news" title="Digg this story"><img src="images/digg_16.png" height="'+height+'" alt="Digg!" /></a> ' +
					   '<a class="external" href="http://www.reddit.com/submit?url='+url+'&title='+title+'" title="Submit to Reddit"><img src="images/reddit_16.png" height="'+height+'" alt="Submit to Reddit"/></a>');

		
	});
	
	$('a[href^="http:"]').filter(function(index) {
		if($(this).attr("href").length < 28) return false;
		if($(this).attr("href").substring(0,28) == "http://www.aureusknights.com") return false;
		if($(this).attr("href").substring(0,19) == "http://evansims.com") return false;
		return true;
	}).addClass("external");
		
	$("a.external").each(
		function() {
			if($(this).html().indexOf("<") == -1) { $(this).attr("title", $(this).html()); }
			$(this).attr("title", $(this).attr("title") + uiStringExternalLink);
			
			$(this).click(function() {
				window.open($(this).attr('href'));
				return false;
			});
		});
		
});
