/* Images should be placed in /images folder and named "logo0.jpg", etc., and alt index should match up to image index  */
var image_alts = ["Urban Decay", "Duralee", "REACH NYC", "University of Pennsylvania", "America Scores", "BizBash", "The Camelot Group", "The Oak Room", "Expeditionary Learning Schools", "IZOD", "IZOD Center", "Genetic Disease Foundation", "The Road to Early Detection", "Playmates Toys", "Trump", "Castanea Partners", "Cigna", "Hanover Direct", "Erno Laszlo", "Fox On Demand", "Czech Republic", "Mel Robbins", "US Department of State", "VME", "Geneva Watch Group", "Nazem &amp; Company", "Teenage Mutant Ninja Turtles", "Totsy", "Prodea Systems", "Baldwin Formals", "Luxury Marketing Council", "Niermann Weeks"];
var count = 0;	// counts used logos
var logo_total = 15;	// total # of logos we want to display
var used_images = new Array(logo_total);

/* Height used for setting column heights */
var max_height = 0;

$(document).ready(function() {
	$(".twitter-feed").getTwitter({
		userName: "strikeinteractv",
		numTweets: 15,
		loaderText: "Loading tweets...",
		slideIn: false,
		slideDuration: 0,
		showHeading: false,
		showProfileLink: false,
		showTimestamp: true
	});
	
	$("#logo-slider").easySlider({
		controlsShow: false,
		vertical: true,
		speed: 700,
		auto: true,
		pause: 1000,
		continuous: false, 
		numeric: false
	});
	
	listClientLogos();
	
	$('.client-logos ul').bxSlider({
		displaySlideQty: 5,
	  moveSlideQty: 5, 
	  controls: true,
	  infiniteLoop: false,
	  hideControlOnEnd: false
	  //infiniteLoop: true,
	  //auto: true,
	  //pause: 0,
	  //easing: 'linear',
	  //speed: 1000
	});
	
	// customize slider controls
	$('a.bx-prev').attr("title", "Previous").html('<span></span>');
	$('a.bx-next').attr("title", "Next").html('<span></span>');
	

//	showRandomClients();
	initLatestNews();
	setFooterColumnHeights();
});

function listClientLogos() {
	for(var i = 0; i < image_alts.length; i++) {
		$(".client-logos ul").append('<li><img src="images/client' + i + '.gif" alt="' + image_alts[i] + '" /></li>');
	}
}

/* Display random client logos */
function showRandomClients() {
	$(".client-logos ul").hide();
	
	for(var i = 0; i < logo_total; i++) {
		getRandomLogo();
	}
	
	$(".client-logos ul").animate({opacity: "show"}, 1000);
}

function getRandomLogo(){
	do {
		var rand = getRandomImage();
	} while(used_images[rand])
	
	count++;
	used_images[rand] = true;
	
	$(".client-logos ul").append('<li><img src="images/client' + rand + '.gif" alt="' + image_alts[rand] + '" /></li>');
}

function getRandomImage() {
	return Math.floor(Math.random() * image_alts.length);
}

/* Equal height footer columns */
function setFooterColumnHeights() {
	// Twitter
	$(".twitter ul").height("auto");
	$(".twitter article").height("auto");	
	max_height = $(".twitter ul").height();

	// Article heights
	$(".latest-news article").each(function() {
		if($(this).height() > max_height) {
			max_height = $(this).height();
		}
	});
	
	// set both container heights
	$(".twitter article").animate({ height: max_height }, 500);
	$(".latest-news-articles").animate({ height: max_height }, 500);
}

function initLatestNews() {
	var article_count = 0;
	var current_article = 0;
	
	// add page # link for each article
	$(".latest-news article").each(function() {
		$(this).hide();
		$(this).attr("id", "article"+article_count);
		$(".latest-news nav ul").append("<li><a href='#' id='link_to_article"+article_count+"'>"+(article_count+1)+"</a></li>");
		article_count++;
	});
	
	// add prev & next buttons
	if(article_count > 1) {
		$(".latest-news nav ul").prepend("<li><a href='#' id='previous-article'>&laquo;</a></li>");
		$(".latest-news nav ul").append("<li><a href='#' id='next-article'>&raquo;</a></li>");
	}
	
	// init news buttons
	$(".latest-news nav a").click(function() {
		var sel_id = $(this).attr("id").replace("link_to_article", "");
		showArticle(sel_id);
		return false;
	});
	
	$("#previous-article").click(function() {
		var sel_id = $(".latest-news article:visible").attr("id").replace("article", "");
		sel_id == 0 ? sel_id = article_count-1 : sel_id--;
		showArticle(sel_id);
		return false;
	});
	
	$("#next-article").click(function() {
		var sel_id = $(".latest-news article:visible").attr("id").replace("article", "");
		sel_id == article_count-1 ? sel_id = 0 : sel_id++;
		showArticle(sel_id);
		return false;
	});
	
	// select first article
	$(".latest-news nav a#link_to_article0").click();
}

function showArticle(article_id) {
	var article_to_show = $(".latest-news #article"+article_id);
	
	$(".latest-news nav a").removeClass("selected");
	
	if($(".latest-news article:visible").length > 0) {
		$(".latest-news article:visible").animate({ opacity: "hide"}, 300, function() {
			article_to_show.animate({opacity: "show"}, 300);
			$(".latest-news nav #link_to_article"+article_id).addClass("selected");
		});
	} else {
		article_to_show.animate({opacity: "show"}, 300);
		$(".latest-news nav #link_to_article"+article_id).addClass("selected");
	}
}
