var j$ = jQuery.noConflict();

/* ready
----------------------------------------------- */
j$(function() {

	// focus
	focusGetData();

	// stv store
	//stvstoreGetData();
	stvstoreGetData("#stvstore");
	stvstoreGetData("#stussystore");
	j$("#stvstore .item").hover(storeItemRollOver, storeItemRollOut);
	j$("#stvstore .item").bind("click", storeItemOpen);
	j$("#stussystore .item").hover(storeItemRollOver, storeItemRollOut);
	j$("#stussystore .item").bind("click", storeItemOpen);
	
	// coming up
	j$("#comingup .image").hover(imageHoverRollOver, imageHoverRollOut);
	
	// the people
	peopleGetData();
	
	// banner
	j$("#banner li").hover(imageHoverRollOver, imageHoverRollOut);
	j$("#banner li a").bind("click", bannerOpen);
	
	// stv news
	j$("#stvnews .items li").eq(0).css({ borderTop:0 });
	
	// pickup banner
	pickupbanner();

});

/* stv store
----------------------------------------------- */
function storeItemOpen() {
	window.location.href = j$(".image a", this).attr("href");
	return false;
}
function storeItemRollOver() {
	j$(".info", this).stop();
	j$(".info", this).show();
	j$(".info", this).css({ opacity:0.8, top:150 });
	j$(".info", this).delay(250).animate({ top:150 - j$(".info", this).height() }, 250, "easeInOutCubic");
}
function storeItemRollOut() {
	j$(".info", this).stop();
	j$(".info", this).animate({ top:150 }, 500, "easeInOutCirc", function() {
		j$(".info", this).hide();
	});
}

// stvstore get data
/*
function stvstoreGetData() {

	if (j$("#stvstore .item").length > 0) {

		j$("#stvstore .item").hide();

		var items = new Array();
		var count = 0;
		j$("#stvstore .item").each(function() {
			items[count] = count;
			count++;
		});
		
		items = arrshuffle(items);
		
		for (var i = 0; i < 3; i++) {
			j$("#stvstore .item").eq(items[i]).show();
		}
	}
}
*/
function stvstoreGetData(store) {

	if (j$(".item", store).length > 0) {

		j$(".item", store).hide();

		var items = new Array();
		var count = 0;
		j$(".item", store).each(function() {
			items[count] = count;
			count++;
		});
		
		items = arrshuffle(items);
		
		for (var i = 0; i < 3; i++) {
			j$(".item", store).eq(items[i]).show();
		}
	}
}

/* focus
----------------------------------------------- */
var focusIntervalid;
var focuspage = 0;
var focuslist = new Array();

// get item
function focusGetData() {

	var count = 0;
	j$("#focus").find(".item").each(function() {

		focuslist[count] = new Array();
		focuslist[count]["title"]		= j$(this).find("h3").html();
		focuslist[count]["outline"]		= j$(this).find(".outline").text();
		focuslist[count]["image"]		= j$(this).find(".image").html();
		focuslist[count]["readmore"]	= j$(this).find(".readmore").html();

		count++;
	});
	
	if (count > 0) {

		var focus;
		focus = '<div class="focus_left">';
		focus += '<p class="image">image</p>';
		focus += '</div>';
		focus += '<div class="focus_right">';
		focus += '<p class="readmore">Read more</p>';
		focus += '<h3>title</h3>';
		focus += '<p class="outline">outline</p>';
		focus += '</div>';
		j$("#focus .focus").html(focus);

		j$("#focus").hover(focusRollOver, focusRollOut);
		j$("#focus .control li").bind("click", focusPageHandler);
		focusOpenHandler();
		focusIntervalid = setInterval(focusSlideHandler, 10000);
	}
}

// hover
function focusRollOver() {
	clearInterval(focusIntervalid);
}
function focusRollOut() {
	focusIntervalid = setInterval(focusSlideHandler, 10000);
}

// slide show
function focusSlideHandler() {

	focuspage++;
	
	if (focuspage > focuslist.length - 1) {
		focuspage = 0;
	}

	focusOpenHandler();
}

// paging
function focusPageHandler() {

	if (this.className == "next") {
		if (focuspage < focuslist.length - 1) {
			focuspage++;
		}
	}
	else {
	
		if (focuspage > 0) {
			focuspage--;
		}
	}

	focusOpenHandler();
}

// focus open
function focusOpenHandler() {

	j$("#focus .control li").show();

	if (focuspage == focuslist.length - 1) {
		j$("#focus .control .next").hide();
	}
	else if (focuspage == 0) {
		j$("#focus .control .back").hide();
	}

	// add
	j$("#focus .focus .readmore").html(focuslist[focuspage]["readmore"]);
	j$("#focus .focus .image").html(focuslist[focuspage]["image"]);
	j$("#focus .focus h3").html(focuslist[focuspage]["title"]);
	j$("#focus .focus .outline").html(focuslist[focuspage]["outline"]);

	// animate
	j$("#focus .focus .image img").stop();
	j$("#focus .focus .image img").css({ opacity:0 });
	j$("#focus .focus .image img").delay(500).animate({ opacity:1 }, 500, "easeInOutCirc");
	
	// hover
	j$("#focus .focus .image").hover(imageHoverRollOver, imageHoverRollOut);
}

/* people
----------------------------------------------- */
var peopleOffset;
var peopleIntervalId;
function peopleGetData() {

	// photo shuffle
	photoSuffle();

	// photo list
	j$("#thepeople .photo li").each(function() {
		var image = '<a href="' + j$("a", this).text() + '"><img src="' + j$("a", this).attr("href") + '" width="220" /></a>';
		j$("a", this).html(image);
	});
	
	// photo set
	peopleOffset = j$("#thepeople .photo").height() - j$("#thepeople .photo ul").height() - 10;
	j$("#thepeople .photo ul").css({ top:peopleOffset });
	
	// people hover
	j$("#thepeople .photo").hover(peopleRollOver, peopleRollOut);
	j$("#thepeople .photo li").hover(imageHoverRollOver, imageHoverRollOut);

	// slide show
	peopleIntervalId = setInterval(peopleSlide, 4000);
}

// slide show
function peopleSlide() {

	// animate
	j$("#thepeople .photo ul").animate({ top:peopleOffset + 190 }, 500, "easeInOutCubic", function() {
		j$("#thepeople .photo ul").prepend(j$("#thepeople .photo li:last"));
		j$("#thepeople .photo ul").css({ top:peopleOffset });
	});
}

// people hover
function peopleRollOver() {
	clearInterval(peopleIntervalId);
}
function peopleRollOut() {
	peopleIntervalId = setInterval(peopleSlide, 4000);
}

// photo shuffle
function photoSuffle() {

	var photos = new Array();
	var count = 0;

	j$("#thepeople .photo li").each(function() {
		photos[count] = j$(this).html();
		count++;
	});
	
	photos = arrshuffle(photos);
	
	j$("#thepeople .photo ul").html("");
	
	for (var i in photos) {
		if (i < 10) {
			var li = '<li>' + photos[i] + '</li>';
			j$("#thepeople .photo ul").append(li);
		}
		else {
			break;
		}
	}
}

/* bannerOpen
----------------------------------------------- */
function bannerOpen() {
	var w = window.open(j$(this).attr("href"));
	w.focus();
	return false;
}

/* array shuffle
----------------------------------------------- */
function arrshuffle(arr) {
	var i = arr.length;
	while (--i) {
		var j = Math.floor(Math.random() * (i + 1));
		if (i == j) {
			continue;
		}
		var k = arr[i];
		arr[i] = arr[j];
		arr[j] = k;
	}	
	return arr;
}


