﻿// JScript File

$(function() {
	// homepage
	
	// homepage slider
	$.preload('#homeSlide div', {
		onFinish: playHome
	});
	$('#control1').css('cursor','pointer').click(function() { actionCall(1) });
	$('#control2').css('cursor','pointer').click(function() { actionCall(2) });
	$('#control3').css('cursor','pointer').click(function() { actionCall(3) });
	$('#control4').css('cursor','pointer').click(function() { actionCall(4) });
	$('#control5').css('cursor','pointer').click(function() { actionCall(5) });
	
	
	// news slider
	// calculate total width of list by adding all list item widths
	var wid = 0;
	$('#newsfeed ul li').each(function (i) { wid = wid + $(this).innerWidth(); });
	$('#newsfeed ul').width(wid);
	// trigger news slide
	setInterval('moveNews(' + index + ')', newsDelay);
});
// you can change these values
var delay = 5000;
var playCountMax = 2
// do not change these values
var playCount = 0;
var timeOne = 0;
var timeTwo = 0;
var timeThree = 0;
var timeFour = 0;
var timeFive = 0;
var timeNext = 0;
function playHome() {
	$('#homeSlide').css('background-image','none');
	actionCall(1);
}
function show1() {
	playCount++;
	$('#slide5').fadeOut('slow');
	$('#control5').removeClass('active');
	$('#slide1').fadeIn('slow', function() { timeOne = setTimeout("show2()",delay); });
	$('#control1').addClass('active');
}
function show2() {
	if (playCount <= playCountMax) {
		$('#slide1').fadeOut('slow');
		$('#control1').removeClass('active');
		$('#slide2').fadeIn('slow', function() { timeTwo = setTimeout("show3()",delay); });
		$('#control2').addClass('active');
	}
}
function show3() {
	if (playCount <= playCountMax) {
		$('#slide2').fadeOut('slow');
		$('#control2').removeClass('active');
		$('#slide3').fadeIn('slow', function() { timeThree = setTimeout("show4()",delay); });
		$('#control3').addClass('active');
	}
}
function show4() {
	if (playCount <= playCountMax) {
		$('#slide3').fadeOut('slow');
		$('#control3').removeClass('active');
		$('#slide4').fadeIn('slow', function() { timeFour = setTimeout("show5()",delay); });
		$('#control4').addClass('active');
	}
}
function show5() {
	if (playCount <= playCountMax) {
		$('#slide4').fadeOut('slow');
		$('#control4').removeClass('active');
		$('#slide5').fadeIn('slow', function() { timeFive = setTimeout("show1()",delay); });
		$('#control5').addClass('active');
	}
}

function actionCall(sec) {
	clearTimeout(timeNext);
	if (sec <= 3) {
		var nextSec = sec + 1;
	} else {
		var nextSec = 1;
	}
	clearTimeout(timeOne);
	clearTimeout(timeTwo);
	clearTimeout(timeThree);
	clearTimeout(timeFour);
	clearTimeout(timeFive);
	
	$('#homeSlide div').fadeOut('fast');
	$('#controls li').removeClass('active');
	$('#slide' + sec + '').fadeIn('slow');
	$('#control' + sec + '').addClass('active');
	// continuous play control, comment for no autoplay
	if (playCount <= playCountMax) {
		timeNext = setTimeout("show" + nextSec + "()",delay);
	}
}
// delay between news items scrolling
var newsDelay = 5000;
// do not change below here
var index = 0;
function moveNews(index) {
	if (index <= $('#newsfeed ul li').size()) {
		$('#newsfeed ul li:eq(' + index + ')').clone().appendTo('#newsfeed ul');
		$('#newsfeed ul li:eq(' + index + ')').hide('slow', function() { $(this).remove() });
		index++
	}
}