chapters = new Array();
duration = new Array();
description = new Array();
link = new Array();
link_title = new Array();
formlink = new Array();
formlink_title = new Array();
var message = "";
var messageTitle = "";

CALLBACK_CSTART = "cstart";
CALLBACK_CEND   = "cend";

START_CLIP = getCookie("chapter");
if(START_CLIP == "")
    START_CLIP = "1";
var play_clip = START_CLIP;
setCookie("chapter","1");

var browser;
var version = parseFloat(navigator.appVersion);
if ((version >= 4.06) && (navigator.appName.match(/netscape/i) != null) && (navigator.javaEnabled())) browser="ns";
if ((version >= 4) && (navigator.appName.match(/microsoft/i) != null) && (navigator.javaEnabled())) browser="ie";

function chchapter(number){
    play_clip = number;
    setTimeout("chchapter2("+number+")",300);
    if(browser == "ie"){
        if(document.VideoApplet.getStatus() != 3)
        document.VideoApplet.pressPauseButton();
    }
}

function chchapter2(number){
    setCookie("chapter",number);
	setCookie("reload","true");
    location.reload();
}

function getCookie(keyname){
    tmp = document.cookie + ";";
    index1 = tmp.indexOf(keyname,0);
    if(index1 != -1){
        tmp = tmp.substring(index1,tmp.length);
        index2 = tmp.indexOf("=",0)+1;
        index3 = tmp.indexOf(";",index2);
        return(unescape(tmp.substring(index2,index3)));
    }
    return("");
}
function setCookie(keyname,val){
    tmp = keyname + "=" + escape(val) + ";";
    tmp += "expires=Fri,31-Dec-2050 23:59:59;";
    document.cookie = tmp;
}

function msover(clipnumber){
	
	if(visibleChapterArea == "false")
		return;
	
    elem = null;
    if(browser == "ie"){
        elem = document.all.item("chapter",cn2en(clipnumber));
    }else{
        elem = document.getElementsByName("chapter").item(cn2en(clipnumber));
    }
    elem.style.color = "#ffffff";
    elem.style.background = "#bbbbbb";
    elem.style.border = "3px outset #bbbbbb";

}

function msout(clipnumber){
	
	if(visibleChapterArea == "false")
		return;
	
    if(browser == "ie")
        elem = document.all.item("chapter",cn2en(clipnumber));
    else
        elem = document.getElementsByName("chapter").item(cn2en(clipnumber));
//    if(play_clip != clipnumber){
//        elem.style.background = "#666666";
//        elem.style.border = "3px outset #666666";
        elem.style.background = "#888888";
        elem.style.border = "3px outset #888888";
//    }
}

function getClipNumberFromID(clipID){
    clips = CLIP.split(",");
    idx = 0;
    for(;idx < clips.length; idx++){
        if(clipID == clips[idx])
            break;
    }
    return idx;
}

function cstart(){
    clipid = document.VideoApplet.getClipID();
    num = getClipNumberFromID(clipid);//0,1,2,3
    play_clip = num+1;
    msover(num+1);//clipnumber
    //setClipTitle(num+1);//clipnumber
}

function cend(){
	if(document.VideoApplet.getStatus() != 0){
	    play_clip++;
    	msout(play_clip-1);
    }
}

function cn2en(clipnumber){
    clipnumber--;
    col = parseInt(chapters.length / 2);
    if ( (chapters.length % 2) != 0 ) {
        col++ 
    }
    row = 2;
    x = parseInt(clipnumber / col);
    y = parseInt(clipnumber-x*col);
    return (row * y + x);
}


function setClipTitle(clipnumber)
{ 	
    changeTitle(chapters[clipnumber-1]);
    changeDescription(description[clipnumber-1]);
}

function changeTitle(title)
{
	if(displayTitle == "true"){
		var title_area = getDivFromName("title_area");
		//if(title_area != null)
			writeDivHTML(title_area,true,true,title);
	}
}

function changeDescription(description)
{
	var text_area = getDivFromName("text_area");
	//if(text_area != null)
	    writeDivHTML(text_area,true,true,description);
}

// _mac : true = macintosh, false = other os
_mac=navigator.userAgent.indexOf('Mac')!=-1;
// _ie512 : true = MSIE 5.12(mac), false = others
_ie512=navigator.userAgent.indexOf('MSIE 5.12')!=-1;
// _dom : kind of DOM.
//        IE4 = 1, IE5+ = 2, NN4 = 3, NN6+ = 4, others = 0
_dom = document.all?(document.getElementById?2:1)
                   :(document.getElementById?4:(document.layers?3:0));
_createLayerNo = 0;                    // layer no.
function writeDivHTML(div,op,cl,s){
  if(_dom==4){
    if(op){ while(div.hasChildNodes()) div.removeChild(div.lastChild); }
    var range=document.createRange();
    range.selectNodeContents(div);
    range.collapse(true);
    var cf=range.createContextualFragment(s);
    div.appendChild(cf);
    return;
  }
  if(_dom==2 || _dom==1){
    if(op)   div.innerHTML='';
    if(_mac&&!_ie512) div.innerHTML+=s;
    else     div.insertAdjacentHTML('BeforeEnd',s);
    return;
  }
  if(_dom==3){
    div.document.open('text/html','replace');
    div.document.write(s);
    if(cl) div.document.close();
 //  document.layers["subarea"].document.open();
   //document.layers["subarea"].document.write(s);
   //document.layers["subarea"].document.close();
    return;
  }
}

function getDivFromName(nm){
  if(_dom==4 || _dom==2) return document.getElementById(nm);
  if(_dom==1)            return document.all(nm);
  if(_dom==3){
    var s='';
    for(var i=1; i<arguments.length; i++) s+='document.layers.'+arguments[i]+'.';
    return eval(s+'document.layers.'+nm);
  }
  return null;
}


function writeChapters(){
	if(visibleChapterArea == "false")
		return;
    len = parseInt(chapters.length / 2);
    if ( (chapters.length % 2) != 0 ) {
        len++ 
    }

	document.write('<table><TR><td colspan = 6><hr noshade size="1"></td></tr>');
    idx = 0;
    for( ; idx < len ;  ){
        document.write('<TR>');
        //document.write('<TD><TABLE name="chapter" id="chapter" class="chapter" onmouseover="msover(',idx+1,')" onmouseout="msout(',idx+1,')" onclick="chchapter(',idx+1,')"><TR>');
        document.write('<TD><TABLE name="chapter" id="chapter" class="chapter" onclick="chchapter(',idx+1,')"><TR>');
        document.write('<TD class="num">',idx + 1 ,')</TD>');
        document.write('<TD class="btext"><a href="javascript:chchapter(',idx+1,')" class="">',chapters[idx],'</a></TD>');
        document.write('<TD class="small">',duration[idx],'</TD>');
        document.write('</TR></TABLE></TD>');

        if( ( idx + len ) < chapters.length ) {
            //document.write('<TD><TABLE name="chapter" id="chapter" class="chapter" onmouseover="msover(',(idx+len)+1,')" onmouseout="msout(',(idx+len)+1,')" onclick="chchapter(',idx+1 + len,')"><TR>');
            document.write('<TD><TABLE name="chapter" id="chapter" class="chapter" onclick="chchapter(',idx+1 + len,')"><TR>');
            document.write('<TD class="num">',idx + 1 + len ,')</TD>');
            document.write('<TD class="btext"><a href="javascript:chchapter(',idx+1+len,')" class="">',chapters[idx+len],'</a></TD>');
            document.write('<TD class="small">',duration[idx+len],'</TD>');
            document.write('</TR></TABLE></TD>');
        }else{
            document.write('<TD></TD><TD></TD><TD></TD>');
        }
        document.write('</TR>');
        idx++;
    }
    document.write('<TR><td colspan = 6><hr noshade size="1"></td></tr></table>');
 	
 	r = getCookie("reload");
 	if(r == "true"){
    	setClipTitle(play_clip);
    	msover(play_clip);
    }
    setCookie("reload","false");
}

function writeLinks(){
        for( i=0; i < link.length; i++) {
            document.write('<a href="',link[i],'" target="_blank" class="relation"><img src="/portal/pageCreator/images/arrow.gif" border="0">',link_title[i],'</a><br>');
        }
}

function writeFormLinks(){
	if(visibleFormlinkArea == "false")
		return;
	document.write('<table><TR><TD ALIGN="CENTER">');
	for( i=0; i < formlink.length; i++) {
		document.write('<a href="',formlink[i],'" target="_blank" class="formlink">',formlink_title[i],'</a>@');
	}
	document.write('</TD></TR></TABLE>');
}

function calcTotalMinutes(){
	hour = 0;
	min = 0;
	sec = 0;
	for(i=0;i < duration.length; i++){
		time = duration[i].split(":");
		//alert(time[1]);
		min += parseInt(time[0],10);
		//time_sec = duration[i].substr(time.length+1,duration[i].length);
		sec += parseInt(time[1],10);
	}
    min += parseInt(sec/60);
   	hour = parseInt(min/60);
   	min = min%60;
    sec = sec%60;
    if(hour < 10)
    	hour = "0" + hour;
    if(hour != 0)
    	hour += ":";
    else
    	hour = "";
    if(min < 10)
    	min = "0" + min;
    if(sec < 10)
    	sec = "0" + sec;
    return hour+min+":"+sec;
}

function writeMessage(){
    	//changeDescription(message);
	    document.write(message);
}

function writeTitle(){
	if(displayTitle == "true"){
		document.write(messageTitle);
	}
}
