var endSub = 0;
var thisSub = "";
var debugTxt = "";
var videoLoaded = false;
function parseTime(sec){
	var hours = 0;
	var min = Math.floor(sec/60);
	sec = Math.floor(sec-(min*60));
	if (min > 60) {
		hours = Math.floor(min/60);
		min = min-(hours*60)
	}
	var t = "";
	if (hours < 10)
		t = t+"0"+hours+":";
	else
		t = t+hours+":";
	if (min < 10)
		t = t+"0"+min+":";
	else
		t = t+min+":";
	if (sec < 10)
		t = t+"0"+sec;
	else
		t = t+sec;
	return t;
}

function callPause() {
    getMovieByName("controlVideo").pauseVP();
}
function callPlay() {
    getMovieByName("controlVideo").playVP();
}

function videoSeek(sec) {
	endSub = sec;
    getMovieByName("controlVideo").videoSeekTo(parseInt(sec));
}

function videoTime(seconds){
	videoLoaded = true;
	if (document.getElementById("subtitles"))
	{
		showSubtitles(seconds);
		highlightTags(seconds);
		highlightProject(seconds);
	}
}



function getMovieByName(movieName) {
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}

function showSubtitles(seconds){
		thisSub = "";
		for (s=0;s<subtitles.length;s++)
		{
			var start = parseInt(subtitles[s][1])/25;
			var end = parseInt(subtitles[s][3])/25;
			if ((parseFloat(seconds)>=start) && (parseFloat(seconds)<=end))
			{
				thisSub = subtitles[s][4].replace(/\|/g," ");
				endSub = end;
				break;
			}
		}
		document.getElementById("subtitles").innerHTML = thisSub;
}

function convertTimeToSec(time){
	var timeArr = time.split(":");
	for (ti=1;ti<timeArr.length ;ti++)
	{
		if (timeArr[ti].charAt(0) == "0") timeArr[ti] = timeArr[ti].charAt(1);
	}
	var hours = parseInt(timeArr[0]);
	var minutes = (hours*60)+parseInt(timeArr[1]);
	var seconds = minutes*60+parseInt(timeArr[2])
	return seconds;
}


function	highlightTags(seconds) {
	var highlightedTag = -1;
	for(var t=0;t<tagsData.length;t++){
		var setStyle = true;
		var thisClass = "delight";
		var start = convertTimeToSec(tagsData[t][0]);
		var end = convertTimeToSec(tagsData[t][1]);
		var tId  = tagsData[t][2];
		if ((parseInt(seconds) >= start) && (parseInt(seconds) <= end))
		{
			thisClass = "highlight";
		}
		if (document.getElementById("t"+tId))
		{
			if (thisClass == "highlight")
			{
				highlightedTag = tId;
			} else {
				if (tId == highlightedTag) setStyle = false;
			}
			if (setStyle)
			{
				var thisDiv = document.getElementById("t"+tId);
				var anchor = thisDiv.getElementsByTagName("a");
				for (a=0;a<anchor.length;a++)
				{
					anchor[a].className = thisClass;
				}
			}
		}
	}
}

function highlightProject(seconds){
	var highlightedTag = -1;
	var dTxt = "";
	for (var i=0;i<projectsData.length;i++){
		var setStyle = true;
		var thisClass = "delight";
		var start = convertTimeToSec(projectsData[i][0]);
		var end = convertTimeToSec(projectsData[i][1]);
		var pId  = projectsData[i][2];
		if ((parseInt(seconds) >= start) && (parseInt(seconds) <= end))
		{
			thisClass = "highlight";
			highlightedTag = pId;			
			 if (!checkProjectOpen(pId))
			 {
				projectPreview("visible",pId);
			 }
			 projectsData[i][3] = true;
		} else {
			projectsData[i][3] = false;
			 if (!checkProjectOpen(pId))
			 {
				 //dTxt += "hide "+pId+"<br />";
				 projectPreview("hidden",pId);
			 }
			if (pId == highlightedTag) {
				setStyle = false;
			}
		}
		
		if (setStyle)
		{
			if (document.getElementById("p"+pId))
			{
				var thisDiv = document.getElementById("p"+pId);
				var anchor = thisDiv.getElementsByTagName("a");
				for (a=0;a<anchor.length;a++)
				{
					if ((anchor[a].className.indexOf("projectLink") == -1) && (anchor[a].className.indexOf("addLink") == -1))
					{
						anchor[a].className = thisClass;
					}
				}
			}
		}
	}
	//document.getElementById("debug").innerHTML = dTxt;
}


function checkProjectOpen(pId){
	for (var pp=0;pp<projectsData.length;pp++){
		if ((projectsData[pp][2] == pId) && (projectsData[pp][3] == true))
		{
			return true;
		}
	}
	return false;
}

function projectPreview(action,projectId){
	var testCls = "hidden"
	if (action == "hidden")
	{
		testCls = "visible";
	}
	if (document.getElementById("p"+projectId))
	{
		var pCont = document.getElementById("p"+projectId);
		var projDiv = pCont.getElementsByTagName("div");
		if (projDiv[0].className.indexOf(testCls) != -1 )
		{
			var switchProject = true;
			for(var d=0;d<projDiv.length;d++){
				if (projDiv[d].className.indexOf("state") != -1 )
				{
					if (projDiv[d].innerHTML.indexOf("+") == -1)
					{
						switchProject = false;
						break;
					}
				}
			}
			if (switchProject)
			{
				for(d=0;d<projDiv.length;d++){
					if (projDiv[d].className.indexOf(testCls) != -1 )
					{
						var newCls = projDiv[d].className.replace(testCls,action);
						projDiv[d].className = newCls;
					}
				}
			}
			//document.getElementById("debug").innerHTML = action+" "+projectId+" / "+switchProject;
		}
	}		

}

function projectSeek(id){
	if (videoLoaded){
		var startTime = convertTimeToSec(projectsData[id][0]);
		videoSeek(startTime);
	}
}

function tagSeek(id){
	if (videoLoaded){
		var startTime = convertTimeToSec(tagsData[id][0]);
		videoSeek(startTime);
	}
}

function projectChangeState(state,id){
	if (document.getElementById("p"+id))
	{
		var pDiv = document.getElementById("p"+id)
		var projDiv = pDiv.getElementsByTagName("div");
		if (state=="open")
		{
			var opened = false;
			for(d=0;d<projDiv.length;d++){
				if (projDiv[d].className.indexOf("state") != -1 )
				{
					if (projDiv[d].innerHTML.indexOf("+") != -1)
					{
						opened = true;
						projDiv[d].innerHTML = "-&nbsp;";
						break;
					}
				}
			}			
			if (opened)
			{
				for(d=0;d<projDiv.length;d++){
					if (projDiv[d].className.indexOf("visible") != -1 )
					{
						var newCls = projDiv[d].className.replace("visible","hidden");
						projDiv[d].className = newCls;
					}
				}
			}
		} else {
			for(d=0;d<projDiv.length;d++){
				if (projDiv[d].className.indexOf("state") != -1 )
				{
					if (projDiv[d].innerHTML.indexOf("-") != -1)
					{
						projDiv[d].innerHTML = "+&nbsp;";
						break;
					}
				}
			}
			for(var pd=0;pd<projectsData.length;pd++){
				if (id == projectsData[pd][2])
				{
					projectsData[pd][3] = false;
				}
			}
		}
	}
}

var previousStateId = -1;

function openDetail(urlPart,projectId){
	ajaxLoad(urlPart,"projectDetail");
	projectChangeState("open",projectId);
	document.getElementById("container").className =  "w2";
	if (previousStateId != -1)
	{
		projectChangeState("close",previousStateId);
	}
	previousStateId = projectId;
}


function closeDetail(id){
	document.getElementById("projectDetail").className = "hidden";
	document.getElementById("projectDetail").innerHTML = "";
	projectChangeState("close",id);
	previousStateId = -1;
	document.getElementById("container").className =  "w1";
}


//AJAX
if (window.XMLHttpRequest) {
	var req = new XMLHttpRequest();
} else if (window.ActiveXObject) {
	var req = new ActiveXObject("Microsoft.XMLHTTP");
}

function ajaxLoad(url, target) {
document.getElementById(target).className = "visible";
document.getElementById(target).innerHTML = 'loading...';
  if (window.XMLHttpRequest) {
    req = new XMLHttpRequest();
  } else if (window.ActiveXObject) {
    req = new ActiveXObject("Microsoft.XMLHTTP");
  }
  if (req != undefined) {
    req.onreadystatechange = function() {ajaxDone(url, target);};
    req.open("GET", url, true);
    req.send("");
  }
}  

function ajaxDone(url, target) {
  if (req.readyState == 4) { // only if req is "loaded"
    if ((req.status == 200) || (req.status == 0)) { // only if "OK"
      document.getElementById(target).innerHTML = req.responseText;
    } else {
      document.getElementById(target).innerHTML=" AHAH Error:\n"+ req.status + "\n" +req.statusText;
    }
  }
}

function loadImage(path,w,h,desc){
	if (document.getElementById("imageContainer"))
	{
		var iContainer = document.getElementById("imageContainer");
		iContainer .className = "visible";
		iContainer .style.width = w+"px";
		iContainer .style.height = h+"px";
		iContainer .innerHTML = "<a href=\"javascript:closeImage();\" title=\""+desc+"\"><img src=\""+path+"\" width=\""+w+"\" height=\""+h+"\" alt=\""+desc+"\" />";
	}
}

function closeImage(){
	if (document.getElementById("imageContainer"))
	{
		var iContainer  = document.getElementById("imageContainer");
		iContainer .className = "hidden";
		iContainer .innerHTML = "";
	}
}
