var videoSwappingIntervalTimeMS = 1500;
var videoFadeOutEffectTimeMS = 0.1;
var videoFadeInEffectTimeMS = 0.4;
var videoTitleLength = 20;

var videoColNum = 12;
var videoNum = videoColNum * 3;
var thumbnailIdPrefix = "movtn-";
var thumbnailBigIdPrefix = "movbtn-";
var videoSwappingIntervalId;
var videoULStyleId = "flvBanner";
var videos = new Array();

function showWin(i){
	resetWin();
	document.getElementById('mov' + i).style.visibility = "visible";
}

function hideWin(m) {
	var e = document.getElementById('mov' + m);
	if (e != null) { e.style.visibility = "hidden"; }
}

function resetWin() {
	for (var j=0; j < videoNum; j++) {
		hideWin(j);
	}
}

function abbreviateTitle(str) {
	if (str.length > videoTitleLength) {
		return str.substring(0, videoTitleLength) + "...";
	}
	return str;
}

function toLiHtml(video, index) {
	var col = index % videoColNum + 1;
	var ret = "<li class=\"col-" + col + "\">";

	ret += "<a href=\"/video/watch/" + video.id + "\" title=\"" + abbreviateTitle(video.title) + "\">";
	ret += "<img id=\"" + thumbnailIdPrefix + index + "\" src=\"" + video.thumbnailUrl1 + "\" alt=\"" + abbreviateTitle(video.title) + "\" border=\"0\" width=\"60\" height=\"45\" \/><\/a>";


	ret += "<\/li>";
	return ret;
}

function preloadThumbnailsOfVideos() {
	for (var i = 0; i < videoList.length; i++) {
		var video = videoList[i];
		(new Image()).src = video.thumbnailUrl2;
		(new Image()).src = video.thumbnailUrl3;
	}
}

function incNumOfVideos() {
	var diff = videoNum - videoList.length;
	if (diff < 1) { return; }
	videoList = videoList.concat(videoList);
	incNumOfVideos();
}

function fillVideoArrayRandom() {
	var usedVideoIdx = new Array();
	for (var i=0; i<videoNum; i++) {
		var idx = getRandomVideoIdx(usedVideoIdx, videoList.length);
		videos[i] = videoList[idx];
	}
}

function getRandomVideoIdx(usedVideoIdx, srcLength) {
	var rnd = Math.floor(Math.random() * srcLength);
	if (isSequencial(usedVideoIdx, rnd)) {
		return getRandomVideoIdx(usedVideoIdx, srcLength);
	} else {
		usedVideoIdx.push(rnd);
		return rnd;
	}
}

function isSequencial(usedVideoIdx, rnd) {
	for (var i=0; i<usedVideoIdx.length; i++) {
		if (typeof(usedVideoIdx[i]) != "undefined") {
			if (usedVideoIdx[i] == rnd) {
				return true;
			}
		}
	}
	return false;
}

function changeThumbnail() {
	try {
		var index = Math.floor(Math.random() * videoNum);
		var img = document.getElementById(thumbnailIdPrefix + index);
		var lIndex = img.src.lastIndexOf('/');
		var thumbnailNum = img.src.substring(lIndex + 1, lIndex + 2);
		if (typeof(thumbnailNum) != "number" || ++thumbnailNum > 3) thumbnailNum = 1;

		var thumbnailUrl = img.src.substring(0, lIndex + 1) + thumbnailNum + '.jpg';
		var anim = new YAHOO.util.Anim(img, { opacity: { to: 0 } }, videoFadeOutEffectTimeMS, YAHOO.util.Easing.easeBoth);
		anim.onComplete.subscribe(function() {
			img.src = thumbnailUrl;
			var anim = new YAHOO.util.Anim(img, { opacity: { from: 0, to: 1.0 } }, videoFadeInEffectTimeMS, YAHOO.util.Easing.easeBoth);
			anim.animate();
		});
		anim.animate();
	} catch (e) {
		changeThumbnail();
	}
}

/*
function resetLIBackground() {
	var elems = document.getElementById(videoULStyleId).getElementsByTagName('li');
	for (var i = 0; i < elems.length; i++) {
		elems[i].style.background = "#000000";
	}
}
*/

function stopVideoEffect() {
	clearInterval(videoSwappingIntervalId);
}

function resumeVideoEffect() {
	videoSwappingIntervalId = setInterval(changeThumbnail, videoSwappingIntervalTimeMS);
}

function runVideoEffect() {
	resetLIBackground();
	preloadThumbnailsOfVideos();
	resumeVideoEffect();
}

/* main */
fillVideoArrayRandom();
var ulHtml = "";
for (var i = 0; i < videos.length; i++) {
	var liHtml = "";
	liHtml = toLiHtml(videos[i], i);
	ulHtml += liHtml;
}