var lightboximgs;
var page = 1;
function initLightbox() {
	i = 0;
	num = new Array();
	imageset = Array();
	lightboximgs = new Array();
	lista = document.getElementsByTagName("a");
	ll = lista.length;
	
	for(i = 0 ; i < ll ; i++) {
		if((lista[i].rel && lista[i].rel.match(/lightbox/)) || (lista[i].target && lista[i].target.match(/top/))) {
			if(lista[i].rel) {
				tmp = lista[i].rel.match(/lightbox\[(.*)\]/);
				namelist = tmp[1];
			} else {
				tmp = new Array(lista[i].target, false);
			}
			if(!lightboximgs[namelist]) {
				lightboximgs[namelist] = new Array();
				imageset[namelist] = new Array();
				num[namelist] = 0;
			} else {
				num[namelist] = num[namelist] + 1;
			}
			usenum = num[namelist];
			
			useI = i;
			for(j = 0 ; j < imageset[namelist].length; j++) {
				if(lista[i].href == imageset[namelist][j][0]) {
					useI = imageset[namelist][j][1];
				}
			}
			
			imageset[namelist][usenum] = new Array(lista[i].href, i);
			
			lista[useI].num = usenum;
			lista[useI].list = namelist;
			lista[useI].onclick = function () {
				doLightbox(this);
				clearScroll();
				return false;
			}
			lightboximgs[namelist][usenum] = lista[useI];
		}
	}
}
var activenum = 0, activelist;
var imageContainer, outerImageContainer, lightbox, overlay;
var imgs, theImg;
var filmwidth, filmheight;
function doLightbox(who) {
	activetype = who.rel;
	if(typeof(overlay) == "undefined") {
		createLightBox(activetype );
	}
	outerImageContainer.innerHTML = "";
	
	if(who.href.match(/flv$/)) {
		
		lightbox_nav = createElementAdd("lightbox_nav", "div", outerImageContainer);
		lightbox_nav.className = "lightbox_nav";
		close2 = createElementAdd("close2", "div", lightbox_nav);
		close2.className = "close2";
		closea = createElementAdd("closea", "a", close2);
		closea.href = "#";
		closea.onclick = function() {closeLightbox();}
		closeaimg = createElementAdd("closeaimg", "img", closea);
		closeaimg.src = "/gfx/close2.gif";
		
		createElementAdd("activeImg", "div", outerImageContainer);
		createElementAdd("prod_title", "div", outerImageContainer);
		
		if($(who).attr("filmwidth")) {
			filmwidth = $(who).attr("filmwidth") ;
		} else {
			filmwidth = 565
		}
		
		if($(who).attr("filmheight")) {
			filmheight = $(who).attr("filmheight");
		} else {
			filmheight = 423
		}
		
		outerImageContainer.style.height = filmheight + "px";
		outerImageContainer.style.width = filmwidth + "px";
		lightbox_nav.style.width = "540px";
		
		doLfilm(who.href, "", false);
		
	} else {
		ajaxFunction(who.href + "&x=1");
	}
		
	document.getElementById('headhead').style.zIndex = 1;
	
	overlay.style.display = "block";
	lightbox.style.display = "block";
}


function detectMacXFF2() {
  var userAgent = navigator.userAgent.toLowerCase();
  if (/firefox[\/\s](\d+\.\d+)/.test(userAgent)) {
    var ffversion = new Number(RegExp.$1);
    if (ffversion < 3 && userAgent.indexOf('mac') != -1) {
      return true;
    }
  }
}

var caption, numberDisplay, nextimage, newsContainer;
function createLightBox(activetype ) {
	
	overlay = createElementAdd("overlay", "div", document.body);
	h = window.outerHeight > document.body.scrollHeight ? window.outerHeight : document.body.scrollHeight;
	overlay.style.height = "100%";
    if(detectMacXFF2() != true) {
        overlay.style.opacity = ".7";
        overlay.style.filter = "alpha(opacity=70)";
    } else {
        overlay.style.background = 'url(/gfx/lbbg.png)';
        overlay.style.backgroundRepeat = 'repeat';
    }
	//overlay.style.height = "100%";
	
	lightbox = createElementAdd("lightbox", "div", document.body);
	lightbox.style.top = "70px";
	
	outerImageContainer = createElementAdd("outerImageContainer", "div", lightbox);

}

var theImg, imgop;
var imgNew;
var popUpImg;
function doLImage(ws, t, who, popupimage) {
	imgNew = ws;
	activeI = document.getElementById('activeImg')	
	prodt = document.getElementById("prod_title");
	prodt.innerHTML = t;	
	activeI.innerHTML = "<img src='/gfx/shim.gif'  id='activeImgSrc' width='300' >";
	popUpImg = popupimage;
	
	setTimeout("setImage()", 39);
	setActiveLink(who);
}
var lbActivelink;
function setActiveLink(who) {
	if(lbActivelink) {
		lbActivelink.style.fontWeight = "normal";
	}
	
	lbActivelink = who;
	who.style.fontWeight = "bold";
}

function doLfilm(ws, t, who) {
	imgNew = ws;
	var film;
	//activeI = document.getElementById("activeImg");
	prodt = document.getElementById("prod_title");
	
	if(typeof(filmheight) == "undefined") {
		filmheight = $("#activeImg").height() + 30;
	} else {
		filmheight = filmheight;
	}
	if(typeof(filmwidth) == "undefined") {
		filmwidth = $("#activeImg").width() + 20;
	} else {
		filmwidth = filmwidth;
	}
	
	
	film = "<div style=\"display:block;width:" + (filmwidth - 20) + "px;height:" + (filmheight - 30) + "px\" id=\"player\"> </div>";
	
	//alert(thisMovie);
	
	$("#activeImg").html("");
	$("#activeImg").html(film);
	prodt.innerHTML = t;	
	//activeI.innerHTML = film;	
	
	flowplayer("player" , "/flowplayer3/flowplayer.commercial-3.1.3.swf", {
		key: flowplayerKey,
		clip: { 
			url: ws,
			autoPlay: true, 
			showMenu: false,
			loop: false
		}, 
		plugins:  { 
      controls: { 
				backgroundGradient: 'none', 
        backgroundColor: 'transparent', 
        volume: false,
        mute: false,
        time : false,
        scrubber: false,
        fullscreen: true,
        buttonColor: '#222222',
        buttonOverColor: '#333333'
			}
		}
	});
	if(who) {
		setActiveLink(who);
	}
}



function lbNext() {
    var nextElem = document.getElementById('lb-' + (page + 1)) || false;
    if(nextElem) {
        page = page + 1;
        nextElem.onclick();
    }
}

function lbReset() {
    page = 1;
    var nextElem = document.getElementById('lb-1') || false;
    if(nextElem) {
        nextElem.onclick();
    }
}

function lbPrev() {
    var prevElem = document.getElementById('lb-' + (page - 1)) || false;
    if(prevElem) {
        page = page - 1;
        prevElem.onclick();
    }
}


function setImage() {
	
	strimg = "<img src='" + imgNew + "'  id='activeImgSrc' alt=''>";
	if(popUpImg) {
		activeLink = createElementAdd("activeA", "a", activeI);
		activeLink.href = "#";
		activeLink.innerHTML = strimg;
		activeLink.onclick = function() {
			window.open(popUpImg,"_blank","toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=yes, fullscreen=yes");
		}
	} else {
		activeI.innerHTML = strimg;	
	}
	
	//activeImgSrc.src = imgNew;
	theImg = document.getElementById('activeImgSrc')
	theImg.style.opacity = 0;
	theImg.style.filter = "alpha(opacity=0)";	
	showImage();
}

function showImage() {
	imgop = parseInt(theImg.style.opacity * 100);
	imgop = imgop + 10;
	if(imgop >= 100) {
		imgop = 99;
		return;
	} else {
		setTimeout("showImage()", 39);
	}
	theImg.style.opacity = imgop / 100;
	theImg.style.filter = "alpha(opacity=" + imgop + ")";
}

function hideImage() {

	imgop = parseInt(theImg.style.opacity * 100);
	if (imgop =="") {
		imgop  =99;
	}
	imgop = imgop - 10;
	if(imgop <= 0) {
		imgop = 0;
		setImage();
	} else {
		setTimeout("hideImage()", 39);
	}
	theImg.style.opacity = imgop / 100;
	theImg.style.filter = "alpha(opacity=" + imgop + ")";
}


function closeLightbox() {
	overlay.style.display = "none";
	lightbox.style.display = "none";
	outerImageContainer.innerHTML = "";
	document.getElementById('headhead').style.zIndex = 800;
}
function createElementAdd(who, what, where) {
	itm = document.createElement(what);
	itm.id = who;
	
	where.appendChild(itm);
	return itm;
}
var ajaxRequest;
function ajaxFunction(what){
	try{
		// Opera 8.0+, Firefox, Safari
		ajaxRequest = new XMLHttpRequest();
	} catch (e){
		// Internet Explorer Browsers
		try{
			ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
		} catch (e) {
			try{
				ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
			} catch (e){
				// Something went wrong
				alert("Your browser broke!");
				return false;
			}
		}
	}
	
	if(activetype == "news") {
		outerImageContainer.style.width = "550px";
		outerImageContainer.style.height = "350px";
	} else {
		outerImageContainer.style.width = "582px";
		outerImageContainer.style.height = "460px";
	}
	
    // Create a function that will receive data sent from the server
    ajaxRequest.onreadystatechange = function(){
		if(ajaxRequest.readyState == 4){
    	outerImageContainer.innerHTML = ajaxRequest.responseText;
			if(activetype && activetype.match(/maps/)) {
				initialize();
			} else if(activetype == "news") {
				// dit moet nog aangezet worden maar geeft conflict in IE
				$(function() {
					// this initialises the demo scollpanes on the page.
					$('#newspane').jScrollPane({scrollbarWidth: 10});
				});
			} else {
				theImg = document.getElementById('activeImgSrc')
				theImg.style.opacity = 0;
				theImg.style.filter = "alpha(opacity=0)";	
				showImage();
				lbReset();
			}
		}
	}
	ajaxRequest.open("GET", what, true);
	ajaxRequest.send(null); 
}
