var ID = 0;
var TITLE = 1;
var FILE = 2;
var WIDTH = 3;
var HEIGHT = 4;
var VERSION = 5;
var DESCRIPTION = 6;

var STD_WIDTH = 280;
var STD_HEIGHT = 157;

var THUMB_WIDTH = 75;
var THUMB_HEIGHT = 46;

var movies = new Array();

function getFlashMovieObject(movieName)
{
  if (window.document[movieName]) 
  {
      return window.document[movieName];
  }
  if (navigator.appName.indexOf("Microsoft Internet")==-1)
  {
    if (document.embeds && document.embeds[movieName])
      return document.embeds[movieName]; 
  }
  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
  {
    return document.getElementById(movieName);
  }
}

function updateThumb(percent) {
	document.getElementById('flashThumb').style.left = percent + '%';
	//if(percent > 0)
	//document.write(percent);
}

function writeFlash(divId, movie, id, width, height, version, bgColor, autoplay) {
	//"Qelp User Interface.swf", "Qelp User Interface", "758", "470", "8", "#FFFFFF"
	var so = new SWFObject('/support/player.swf', id, width, height, version, bgColor);
	if(!autoplay)
		so.addParam('wmode', 'opaque');
	so.addParam('allowFullScreen', 'true');
	so.addParam('allowScriptAccess', 'sameDomain');
	so.addVariable('file', escape(movie));
	if(autoplay) {
		so.addVariable('autoplay', true);
		if(/\.swf$|\.flv$/i.test(movie)) {
			document.getElementById('playbutton').style.display = 'block';
			document.getElementById('stopbutton').style.display = 'none';
			updateThumb(0);
			document.getElementById('flashThumb').style.display = 'block';
		} else {
			document.getElementById('playbutton').style.display = 'none';
			document.getElementById('stopbutton').style.display = 'none';
			document.getElementById('flashThumb').style.display = 'none';
		}
	}
	so.useExpressInstall('support/expressinstall.swf');
	so.write(divId);
}

function showMovie(divId, id, type, useIndex, autoplay) {	
	var index = 0;
	
	//document.getElementById(divId).innerHTML = "";
	if (id != 0) {	
		if (useIndex) index = id;
		else {
			for (var i=0; i < movies.length; i++) {
				if (movies[i][ID] == id) {
					index = i;
					break;		
				}
			}
		}		
	}

	switch(type) {
		case 'movieBrowser':
			writeFlash(divId, movies[index][FILE], 'movieBrowser', STD_WIDTH, STD_HEIGHT, movies[index][VERSION], '#FFFFFF', true);		
			document.getElementById('flashTitle').innerHTML = movies[index][TITLE];
			document.getElementById('flashDescription').innerHTML = movies[index][DESCRIPTION];
		break;
		case 'thumb':
			writeFlash(divId, movies[index][FILE], 'thumb' + id, THUMB_WIDTH, THUMB_HEIGHT, movies[index][VERSION], '#FFFFFF');		
			break;
		default:
			writeFlash(divId, movies[index][FILE], divId, movies[index][WIDTH], movies[index][HEIGHT], movies[index][VERSION], '#FFFFFF');		
		break;	
	}

}

function showButtons(action) {
	switch(action) {
		case "play":
		document.getElementById('playbutton').style.display = 'none';
		document.getElementById('stopbutton').style.display = 'block';
		break;
		case "stop":
		document.getElementById('playbutton').style.display = 'block';
		document.getElementById('stopbutton').style.display = 'none';
		break;
		
	}	
}

function controlFlash(movieId, action) {
	var flashMovie = getFlashMovieObject(movieId);
	showButtons(action);
	switch(action) {
		case "play":
		flashMovie.GotoFrame(3);
		flashMovie.GotoFrame(1);
		break;
		case "stop":
		flashMovie.GotoFrame(3);
		flashMovie.GotoFrame(2);
		break;
		
	}	
}

function showThumbs() {
	for (var i=0; i < movies.length; i++) {
		showMovie('thumb' + movies[i][ID], i, 'thumb', true);
	}
}

var scrollTimer;
var scrollDiv;
var scrollDirection;

function startScroll(divId, direction) {
	scrollDiv = document.getElementById(divId);
	scrollDirection = direction;
	//var scrollWidth = scrollDiv.scrollWidth;
	//var currentScroll = scrollDiv.scrollLeft;
	//scrollDiv.scrollLeft += 10;	
	scrollTimer = setTimeout("scrollHorizontal()", 100);
}

function stopScroll() {
	clearTimeout(scrollTimer);
	
}
function scrollHorizontal() {
	var scrollWidth = scrollDiv.scrollWidth;
	var currentScroll = scrollDiv.scrollLeft;
	
	if (scrollDirection == "left" && currentScroll < scrollWidth) scrollDiv.scrollLeft += 10;	
	else if  (currentScroll > 0) scrollDiv.scrollLeft -= 10;
	
	scrollTimer = setTimeout("scrollHorizontal()", 100);
}

var currentGroup = 0;

function showNewsGroup(id) {
	document.getElementById("newsGroup" + currentGroup).style.display = 'none';	
	document.getElementById("newsGroup" + id).style.display = 'block';
	currentGroup = id;
}