var objFlash;

var files 	= new Array();
var types	= new Array();
var options	= new Array();
var thumbs 	= new Array();
var texts 	= new Array();
var stories	= new Array();
var links 	= new Array();

var videoShowNav 	= 'true';
var videoNavHeight 	= 20;

var showDebugInfo = 0;

function build() {
	if (showDebugInfo) alert('build()');

	clearTimeout(tidTryAgain);

	// hebben we een file (image of video) gevonden? zoja, laadt deze dan
	if (files[curfileidx].length > 0) {

		if (types[curfileidx].match(/(jpg|gif|png)/i)) {

			// filetype is image: laadt afbeelding
			getEl('divimg').style.display		= 'block';
			getEl('bigimg').src 				= curpath + files[curfileidx];
			getEl('flashplayer').style.display 	= 'none';

			// does image have a link attached?
			if (links[curfileidx].length > 0) {
				getEl('bigimg').style.cursor = 'pointer';
				getEl('bigimg').title = 'Click to open link';
				getEl('bigimg').onclick = function() { window.open(links[curfileidx]); };
			} else {
				getEl('bigimg').style.cursor = 'default';
				getEl('bigimg').title = '';
				getEl('bigimg').onclick = function() { };
			}

			sendEvent('mediaplayer','stop');

		} else if (types[curfileidx].match(/(flv)/i)) {

			// filetype is flv: laadt video
			getEl('divimg').style.display 		= 'none';
			getEl('flashplayer').style.display 	= 'block';
			getEl('flashplayer').style.width    = options[curfileidx][1] + 'px';
			getEl('flashplayer').style.height   = (parseInt(options[curfileidx][2])+videoNavHeight).toString() + 'px';

			//alert(getEl('mediaplayer'));


			// autoplay video?
			if (options[curfileidx][0] == '0') {
				// no autoplay: laadt video + poster-image
				//alert('load video + poster image');
				loadVideo(0, curpath + files[curfileidx], curpath + thumbs[curfileidx]);
			} else {
				// autoplay: laadt alleen video
				loadVideo(0, curpath + files[curfileidx], '');
				//loadVideo(1, 'http://www.motoko.tv/files/portfolio/00000000/CKJL-Disco-620x350.flv', '');
				//alert('go');

				// trigger autoplay
				setTimeout("sendEvent('mediaplayer','playpause')", 1000);
			}

			//loadVideo(1, curpath + files[curfileidx], curpath + thumbs[curfileidx]);
			//loadVideo(1, 'http://www.motoko.tv/files/portfolio/00000000/CKJL-Disco-620x350.flv');
			//if (options[curfileidx][0] == '1') sendEvent('mediaplayer','playpause');


		}
	}

	// plak sidetext in linker sidebar
	getEl('workdata').innerHTML	= (texts[curfileidx].length > 0 ? texts[curfileidx] : "");

	// plak bottomtext onder image/video, indien deze is gedefinieerd.
	if (stories[curfileidx].length > 0) {
		getEl('story').style.display = 'block';
		getEl('story').innerHTML = stories[curfileidx];
	} else {
		getEl('story').style.display = 'none';
	}

	getEl('adummy').focus();
}

function show(init) {
	if (showDebugInfo) alert('init()');

	setDocsize();

	//dw	= is_safari ? top.document.clientWidth : document.body.clientWidth;
	//dh	= is_safari ? top.document.clientHeight : document.body.clientHeight;

	//dw	= is_safari ? top.innerWidth : document.body.clientWidth;
	//dh	= is_safari ? top.innerHeight : document.body.clientHeight;

	//alert(top.document.clientWidth);
	getEl('main').style.left = Math.round((DBW - cw) / 2) + "px";
	getEl('main').style.top  = Math.round((DBH - ch) / 2) + "px";

	if (init) {

		// which Flash version required? zet op '10' om te testen of flash-alt-text werkt
		var vFlash = '7'; // '7' / '9.0.115' / '10'

		// initialiseer Flash video object
		objFlash = new SWFObject('mediaplayer.swf', 'mediaplayer', "100%", "100%", vFlash);
		objFlash.addParam('allowfullscreen', 'true');
		objFlash.addParam('allowscriptaccess', 'always');
		//objFlash.addVariable("width", '100%');	// posList[2].toString() 640
		//objFlash.addVariable("height", '100%');	// posList[3].toString() 480
		objFlash.addVariable('autostart', 'false');
		objFlash.addVariable('javascriptid', 'mediaplayer');
		objFlash.addVariable('enablejs', 'true');
		objFlash.addVariable('shownavigation', videoShowNav);
		objFlash.addVariable('showicons', 'true');
		objFlash.addVariable('volume', '75');
		objFlash.addVariable('screencolor', '0x000000');		// JS 1-jun-09: ZWART!
		//objFlash.addVariable('file','gfx/placeholder.jpg'); //'xylifresh.flv');
		objFlash.write('flashplayer');

		//loadVideo(1, '../files/commissioned/Superted.flv');

		// toon main-screen
		getEl('main').style.visibility	= 'visible';

		// laadt file behorende bij categorie
		loadcat(cats[curcatidx]);

	}
}

var tidTryAgain;
function loadVideo(tryAgain, fileName, imageName) {
	try {
		if (imageName.length > 0) {
			loadFile('mediaplayer',{file:fileName,image:imageName});
			//alert('ok');
		} else {
			loadFile('mediaplayer',{file:fileName});
		}
	} catch(e) {
		if (showDebugInfo) top.document.title = 'Error in loadVideo(): ' + tryAgain;
		if (tryAgain<10) {
			//alert('foutje' + tryAgain);
			clearTimeout(tidTryAgain);
			tidTryAgain = setTimeout("loadVideo("+(tryAgain+1)+", '"+fileName+"', '"+imageName+"')", 1000);
		} else {
			alert('loadVideo() says: error while starting FLV playback!');
		}
	}
}




// laadt video/audio in flashplayer
function loadFile(swf,obj) {
	//alert('loadFile()');
	thisMovie(swf).loadFile(obj);
}

// javascript -> flash event
function sendEvent(swf,typ,prm) {
	try {
		thisMovie(swf).sendEvent(typ,prm);
	} catch(e) {
	}
};

// display status van flashplayer (buffering, playing, paused)
// dit is een 'reserved' functie die automatisch wordt aangeroepen door de player, hoeft dus niet ergens opgegeven te worden
function getUpdate(typ,pr1,pr2,swf) {
	if(typ == 'state') {

		// state: 0=stopped, 1=buffering, 2=playing, 3=end-of-video
		var s = new Array("- Paused", "- Buffering", "- Playing", "");

		//top.document.title = "Video state: " + s[pr1];

		return;

		// playing (1st time)
		if (pr1 == 2 && pbUpdate == 0) {
			pbTimeTotal = 0;
			pbUpdate = 1;
			//alert('debugmode: playing / ' + swf);
			//clearInterval(pbTid);
			//pbTid = setInterval("progbarUpdate()", 100);
		}

		// stopped/paused
		if (pr1 == 0 && pbUpdate == 1) {
			pbUpdate = 0;
			//alert('debugmode: paused / ' + swf);
			//clearInterval(pbTid);
		}

		// end of video is reached: what to do?
		if (pr1 == 3 && pbUpdate == 1) {
			//alert('debugmode: end of video ('+swf+'), pbUpdate = ' + pbUpdate);
			//sendEvent('mediaplayer','playpause');
		}
	}
}



function thisMovie(swf) {
	//if(document.embeds[swf]) return document.embeds[swf];
	//if(window.document[swf]) return window.document[swf];
	//if(window[swf]) return window[swf];
	//if(document[swf]) return document[swf];
	//return null;

	if(navigator.appName.indexOf("Microsoft") != -1) {
		return window[swf];
	} else {
		return document[swf];
	}
};



function loadcat(catid) {
	// loop door cats totdat we degene hebben gevonden die we zoeken
	for(var i=0; i<cats.length; i++) {
		if (cats[i] == catid) {
			curcatidx = i;

			// show sidetext? depends on settings of category, see table.field: ct_categories.show_descr
			displ = parseInt(catshowdescr[i],10) ? "block" : "none";
			getEl('workheader').style.display = displ;
			getEl('workdata').style.display = displ;

			// load files for this category
			window.hidden.location.href = "site_getfiles.php?ct="+ctime()+"&catid="+catid;
			return;
		}
	}
	//alert("category not found: "+catid);
}

function jumpfile(dir) {
	curfileidx += dir;
	//alert("curfile: " + curfileidx + " / curcat: " + curcatidx);
	if (curfileidx < 0) {
		curfileidx = thumbs.length - 1;
		//curcatidx--;
		//if (curcatidx < 0) {
		//	curcatidx = cats.length-1;
		//}
		//alert('load prev cat');
		//loadcat(cats[curcatidx]);
		//return;
	}

	if (curfileidx >= thumbs.length) {
		curfileidx = 0;
		//curcatidx++;
		//if (curcatidx >= cats.length) {
		//	curcatidx = 0;
		//}
		//alert('load next cat');
		//loadcat(cats[curcatidx]);
		//return;
	}

	build();
	return;


	if (files[curfileidx].length > 0) {
		getEl('bigimg').src 	= curpath + files[curfileidx];
	}
	getEl('workdata').innerHTML	= (texts[curfileidx].length > 0 ? texts[curfileidx] : "")
							+ (links[curfileidx].length > 0 ? "<br><i>" + links[curfileidx] + "</i>": "")
							+ "&nbsp;";
	getEl('adummy').focus();

	if (links[curfileidx].length > 0) {
		prtcl = (links[curfileidx].substr(0,7).toLowerCase() != "http://" ? "http://" : "");
		window.open(prtcl + links[curfileidx], 'extlink', '');
	}

}

