function loadMovie5(container,url,linknum,highres,noAutoplay){
	if(linknum!==false){
		link_num=linknum;
		var linkstotal = $('movie_link_container').getElementsByTagName('a').length;
		for(var i=0;i<linkstotal;i++){
			if(i%2==0){
				$('movie_item_'+i).className 			= 'movie_item row1';
			} else {
				$('movie_item_'+i).className 			= 'movie_item';
			}
			$('movie_pointer_'+i).style.display 	= 'none';
		}
		$('movie_item_'+linknum).className 			= 'movie_item active_movie';
		$('movie_pointer_'+linknum).style.display 	= 'block';
	}
	
	if(iphone){
		$(container).innerHTML = '<video src="/uploads/flvs/'+url+'" width="1280" height="720" controls="controls"></video>';
	} else {
		var oc = new SWFObject("/includes/swf/flvplayer.swf", "flvplayer", 1280, 720, "7");
		if(noAutoplay){
			oc.addVariable("autoStart","false");
		} else {
			if(linknum===false){
				oc.addVariable("bg","white");
				oc.addVariable("autoStart","false");
			}
		}
		oc.addVariable("file","/uploads/flvs/"+url);
		oc.addParam("menu","false");
		oc.addParam("wmode","transparent");
		oc.addVariable("lang",lang);
		oc.write(container);
	}
}
function stopPopupMovie(){
	$('movie2').innerHTML = $('movie2').innerHTML;
	loadMovie('movie2',$('movie2').title,false);
}
function loadMovie(container,url,linknum,highres,noAutoplay){
	if(linknum!==false){
		link_num=linknum;
		var linkstotal = $('movie_link_container').getElementsByTagName('a').length;
		for(var i=0;i<linkstotal;i++){
			if(i%2==0){
				$('movie_item_'+i).className 			= 'movie_item row1';
			} else {
				$('movie_item_'+i).className 			= 'movie_item';
			}
			$('movie_pointer_'+i).style.display 	= 'none';
		}
		$('movie_item_'+linknum).className 			= 'movie_item active_movie';
		$('movie_pointer_'+linknum).style.display 	= 'block';
	}
	
	if(iphone){
		$(container).innerHTML = '<video src="/uploads/flvs/'+url+'" width="'+(highres?640:320)+'" height="'+(highres?380:200)+'" controls="controls"></video>';
	} else {
		var oc = new SWFObject("/includes/swf/flvplayer.swf", "flvplayer", highres?640:320, highres?380:200, "7");
		if(noAutoplay){
			oc.addVariable("autoStart","false");
		} else {
			if(linknum===false){
				oc.addVariable("bg","white");
				oc.addVariable("autoStart","false");
			}
		}
		oc.addVariable("file","/uploads/flvs/"+url);
		oc.addParam("menu","false");
		oc.addParam("wmode","transparent");
		oc.addVariable("lang",lang);
		oc.write(container);
	}
}
function popUp(page,width,height,noScroll){
	var sw = screen.availWidth;	
	var sh = screen.availHeight;
	var l = (sw-width)/2;
	var t = (sh-height)/2;
	var win = window.open(page,'win_pop','width='+width+',height='+height+',left='+l+',top='+t+',resizable=yes,'+(noScroll?'scrollbars=yes,':'scrollbars=no,')+'location=no,status=no,screenX='+l+',screenY='+t+',');
	if(win) win.focus();
}
function pageWidth() {
	return screen.availWidth;
}
function pageHeight() {
	return screen.availHeight;
}
function getNode(xml,nodename){
	var node = xml.getElementsByTagName(nodename);
	if(node.length){
		return node[0];
	}
	return false;
}
var movie_project_id;

var frameWidth;
var frameHeight;
var showhigh=false;

function getWinSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  return [myWidth,myHeight];
}
var highres_index=0;
var highres_data=[];
var pid;
var pc;
function showHighres(project_id,current){
	
	$('popup').hide();
	pid=project_id;
	pc=current;
	
	new Ajax.Request('/pages/highres.xml.php?project_id='+project_id+'&current='+current,{onComplete:function(req){
		highres_data = [];

		var xml = req.responseXML;
		var nd;
		
		if(n=getNode(xml,'highres')){
			highres_data.push([n.getAttribute('width'),n.getAttribute('height'),n.firstChild.nodeValue]);
			nd=highres_data[highres_data.length-1];
		}
		if(n=getNode(xml,'medium')){
			highres_data.push([n.getAttribute('width'),n.getAttribute('height'),n.firstChild.nodeValue]);
			nd=highres_data[highres_data.length-1];
		}
		if(n=getNode(xml,'small')){
			highres_data.push([n.getAttribute('width'),n.getAttribute('height'),n.firstChild.nodeValue]);
			nd=highres_data[highres_data.length-1];
		}

		var vp = document.viewport.getDimensions();

		for(var i=0;i<highres_data.length;i++){
			if(highres_data[i][0]>(vp.width-10) || highres_data[i][1]>(vp.height-10)){
				continue;
			} else {
				nd=highres_data[i];
				break;
			}
		}
		
		var hr = $('highres_container');
		if(!hr){
			hr = Builder.node('div',{id:'highres_container'});
			hr.appendChild(Builder.node('div',{id:'img_container'}));
			document.body.appendChild(hr);
			
			var btnLeft = Builder.node('div',{id:'bprev'});
			btnLeft.update('&lt;');
			
			var btnRight = Builder.node('div',{id:'bnext'});
			btnRight.update('&gt;');
			
			hr.appendChild(btnLeft);
			hr.appendChild(btnRight);
			
			Event.observe(btnLeft,'click',onBprev);
			Event.observe(btnRight,'click',onBnext);
			
			var bclose = Builder.node('div',{id:'bclose'});
			hr.appendChild(bclose);
			
			Event.observe(bclose,'click',onCloseHighres);
		} else {
			$('highres_img').remove();
		}

		$('img_container').update(nd[2]);
		
		img = Element.extend($('img_container').childNodes[0]);
		
		var l = (vp.width/2)-(img.getWidth()/2)+'px';
		var t = (vp.height/2)-(img.getHeight()/2)+'px';

		hr.setStyle({width:nd[0]+'px',height:nd[1]+'px',left:l,top:t});

		if(Number(getNode(xml,'pager').getAttribute('next'))){
			//next
			$('bnext').setStyle({top:(img.getHeight()/2)+'px'});
			$('bnext').show();
		} else {
			$('bnext').hide();
		}
		if(current>0){
			//prev
			$('bprev').setStyle({top:(img.getHeight()/2)+'px'});
			$('bprev').show();
		} else {
			$('bprev').hide();
		}

	}});
	
}

function onCloseHighres(){
	$('highres_container').remove();
	$('popup').show();
}
function onBnext(){
	showHighres(pid,pc+1);
}
function onBprev(){
	showHighres(pid,pc-1);
}
function hideMedium(){
	hidePop('medium');
	deleteSkipButtons();
}
function hideHighres(){
	hidePop('highres');
	deleteSkipButtons();
}
function hideSmall(){
	hidePop('small');
	deleteSkipButtons();
}
function deleteSkipButtons(){
	try{
		$('bprev').remove();
		$('bnext').remove();
	}catch(e){};
}
function hidePop(type){
	var h = $(type);
	h.hide();
	
	var div = $('popup');
	if(div){
		div.setStyle({overflow:'hidden',height:org[1]+'px',width:org[0]+'px'});
	}
	div.setStyle({overflow:'visible'});
	positionPopup(true);
}
function blinkSector(id,project_id){
	$('project_td_'+project_id).style.backgroundColor = '#fff';
	if(sectoren.length>0){
		for(var i=0;i<sectoren.length;i++){
			$('sector_'+sectoren[i]).style.backgroundColor = '';
		}
	}
	$('sector_'+id).style.backgroundColor = '#fff';
}
function unblinkSector(project_id){
	$('project_td_'+project_id).style.backgroundColor = '';
	if(sectoren.length>0){
		for(var i=0;i<sectoren.length;i++){
			$('sector_'+sectoren[i]).style.backgroundColor = '';
		}
	}
}
var movie_url;
var r = false;
var org = [];
function positionPopup(init){
	var div = $('popup');
	if(div){
		var w = 725;
		var h = 425;
		
		if(!init){
			w = div.getWidth();
			h = div.getHeight();
		}

		var sw=document.viewport.getWidth();
		var sh=document.viewport.getHeight();
		var l=(sw-w)/2;
		var t=(sh-h)/2;
	
		div.setStyle({left:l+'px',top:t+'px'});
	}
	var hr = $('highres_container');
	if(hr){
		var vp = document.viewport.getDimensions();
		img = Element.extend($('img_container').childNodes[0]);
		
		var l = (vp.width/2)-(img.getWidth()/2)+'px';
		var t = (vp.height/2)-(img.getHeight()/2)+'px';

		hr.setStyle({width:img.getWidth()+'px',height:img.getHeight()+'px',left:l,top:t});
	}
}
function closePopup(){
	showhigh=false;
	try{
		$('highres_container').remove();
	} catch (e){};
	if($('portfolio')){
		$('portfolio').setStyle({visibility:'visible'});
	}
	var div = $('popup');
	var overlay = $('overlay');
	if(div){
		div.remove();
		overlay.remove();
	}
}
function viewProject(id,lang,current,section){
	var uri = '/pages/project.php?id='+id+'&lang='+lang+'&current='+current;
	if(!current){
		current = 0;
	}

	new Ajax.Request(uri,{parameters:'&highres='+Number(showhigh),onComplete:function(req){
		if(!$('overlay')){
			var overlay = document.createElement('div');
			overlay.setAttribute('id','overlay');
			overlay.setAttribute('style','position:fixed;z-index:1000000000;left:0;top:0;width:100%;height:100%;background-color:#000');
			Event.observe(overlay,'click',closePopup);
			document.body.appendChild(overlay);
			Element.setOpacity(overlay,0.5);
		}

		var div = $('popup');
		if(!div){
			div = document.createElement('div');
			div.setAttribute('id','popup');
			div.setAttribute('style','position:fixed;z-index:1000000001;border:2px solid #000;background-color:#fff');
			document.body.appendChild(div);
		}
		div.innerHTML = req.responseText;
		div.innerHTML += '<div style="background:url(/images/btnclose.png) no-repeat;width:25px;height:25px;right:-25px;top:-25px;position:absolute;cursor:pointer" title="Sluiten" onclick="closePopup()"></div>';
	
		positionPopup(true);
		
		if(!r){
			r = true;
			Event.observe(window,'resize',positionPopup);
		}
		org = [720,425];
		
		div.innerHTML.evalScripts();
		if(showhigh){
			showHighres();
			if(!$('nav_next')){
				$('bnext').hide();
			}
			if(!$('nav_prev')){
				$('bprev').hide();
			}
		}
	}});
}
function resizeWindow(iWidth, iHeight){
    var resizeRef = $('resizeReference');
    var iOuterWidth = iWidth + 10;
    var iOuterHeight = iHeight + 29;
    if (resizeRef) {
        var iPreWidth = resizeRef.offsetWidth;
        var iPreHeight = resizeRef.offsetHeight;
        window.resizeTo(iPreWidth,iPreHeight);
        var iPostWidth = resizeRef.offsetWidth;
        var iPostHeight = resizeRef.offsetHeight;
        iOuterWidth = iWidth + (iPreWidth-iPostWidth);
        iOuterHeight = iHeight + (iPreHeight-iPostHeight);
    }
    window.resizeTo(iOuterWidth, iOuterHeight);
}
function stopSliding(){
	var element;
	var movieName = 'portfolio_flash';
	if (window.document[movieName]) {
		element = window.document[movieName];
	} else {
		if (navigator.appName.indexOf("Microsoft Internet")==-1){
			if (document.embeds && document.embeds[movieName]){
				element = document.embeds[movieName];
			}
		}
		else {
			element = document.getElementById(movieName);
		}
	}
	if(element){
		try{
			element.SetVariable("stopSliding","true");
		} catch(e){};
	}
}
function setCookie(c_name,value,expiredays){
	var exdate=new Date();exdate.setDate(exdate.getDate()+expiredays);
	document.cookie=(c_name+ "=" +escape(value)+((expiredays==null) ? "" : ";expires="+exdate.toGMTString()))+";path=/;";
}
function getCookie(c_name){
	if (document.cookie.length>0){
  		c_start=document.cookie.indexOf(c_name + "=");
		if(c_start!=-1){ 
    		c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
    	if(c_end==-1) c_end=document.cookie.length;
		return unescape(document.cookie.substring(c_start,c_end));
    	} 
	  }
	return "";
}
function closeXmasCard(){
	setTimeout(function(){
		try{ document.getElementById('portfolio_flash').style.visibility = 'visible';} catch(e){};
		var div = document.getElementById('xmas');
		div.innerHTML = '';
		div.style.width = div.style.height = 0 ;
		setCookie('xmascard','1',99);
	},100);
}
var zindex=0;
var Folio = Class.create();
Folio.prototype = {
	initialize:function(company,type,lang,ytop){
		this.company = company;
		this.lang = lang;
		this.type = type;
		this.ytop = ytop;
		if(!this.ytop){
			this.ytop=205;
		}
		
		this._divs = [];
		this._div;
		this._pageSize = Math.ceil(document.viewport.getWidth()/120);
		this._index = -1;
		this._nodes = [];
		
		this._loadImages();
		
		Event.observe(window,'resize',this._doResize.bind(this));
	},
	_doResize:function(){
		this._pageSize = Math.ceil(document.viewport.getWidth()/120);
		this._drawPage(this._index);
	},
	_loadImages:function(){
		if(this.type){
			new Ajax.Request('/pages/portfolio.xml.php?section='+this.type+'&lang='+this.lang+'&bv='+this.company,{onComplete:this._onLoadComplete.bind(this)});
		} else {
			new Ajax.Request('/pages/home_'+this.company+'.xml.php?lang='+this.lang,{onComplete:this._onLoadComplete.bind(this)});
		}
	},
	_onLoadComplete:function(req){

		this._nodes = req.responseXML.getElementsByTagName('item');
		this._drawPage(0);
		this._drawButtons();
	},
	_drawButtons:function(){
		var btnLeft = Builder.node('div',{style:'font-size:18px;position:absolute;width:20px;height:20px;color:#fff;left:10px;top:'+this.ytop+'px;cursor:pointer;border:1px solid #666;text-align:center'});
		btnLeft.update('&lt;');
		
		var btnRight = Builder.node('div',{style:'font-size:18px;position:absolute;width:20px;height:20px;color:#fff;right:10px;top:'+this.ytop+'px;cursor:pointer;border:1px solid #666;text-align:center'});
		btnRight.update('&gt;');
		
		$('portfolio').appendChild(btnLeft);
		$('portfolio').appendChild(btnRight);
		
		Event.observe(btnLeft,'click',this._onPrev.bindAsEventListener(this));
		Event.observe(btnRight,'click',this._onNext.bindAsEventListener(this));
	},
	_onPrev:function(){
		if(this._index-1>=0){
			this._index--;
			this._drawPage(this._index);
		}
	},
	_onNext:function(){
		if(this._index+1<Math.ceil(this._nodes.length/this._pageSize)){
			this._index++;
			this._drawPage(this._index);
		}
	},
	_drawPage:function(index){
		
		if(this._div){
			this._div.remove();
		}
		
		this._index = index;
		
		var start 	= (index*this._pageSize);
		var end 	= ((index+1)*this._pageSize);
		var actualWidth = 0;
		
		
		//if(!this._divs[index]){
			var div = Builder.node('div',{style:'margin-left:-30px;width:100%;position:relative;margin-top:-200px;height:200px'});
			var j = 0;
			for(var i=start;i<end;i++){
				var node = this._nodes[i];
				if(!node) break;
				
				var img = Builder.node('img',{src:node.getAttribute('src'),id:'thumb_'+node.getAttribute('id'),width:100,height:100,style:'cursor:pointer;position:absolute;left:'+((j*120)+50)+'px;top:50px'});
				Element.setOpacity(img,0.3);
				div.appendChild(img);
				j++;
			}
			Event.observe(div,'mouseover',this._imgHover.bind(this));
			Event.observe(div,'mouseout',this._imgOut.bind(this));
			Event.observe(div,'click',this._imgClick.bind(this));
			this._divs[index] = div;
			actualWidth = j*120;
		//}
		this._div = this._divs[index];
		$('portfolio').appendChild(this._div);
		
		if(index==0){
			if(actualWidth<document.viewport.getWidth()){
				//alert(actualWidth);
				var left = (document.viewport.getWidth()/2) - (actualWidth/2);
				this._div.setStyle({left:left+'px'});
			}
		} else {
			this._div.setStyle({left:'0px'});
		}
	},
	_imgHover:function(e){
		var elem = Event.findElement(e,'img');
		if(elem){
			zindex++;
			
			var p = elem.positionedOffset();
			var t = p[1] - 50;
			p = p[0] - 50;
			
			elem.setStyle({zIndex:zindex});
			elem.setStyle({left:p+'px',top:t+'px'});
			Element.setOpacity(elem,1);
			elem.writeAttribute('width',200);
			elem.writeAttribute('height',200);
		}
	},
	_imgOut:function(e){
		var elem = Event.findElement(e,'img');
		if(elem){
			var p = elem.positionedOffset();
			var t = p[1] + 50;
			p = p[0] + 50;
			elem.setStyle({left:p+'px',top:t+'px'});
			
			Element.setOpacity(elem,0.3);
			elem.writeAttribute('width',100);
			elem.writeAttribute('height',100);
		}
	},
	_imgClick:function(e){
		var elem = Event.findElement(e,'img');
		if(elem){
			viewProject(elem.id.split('_')[1],lang,0);
		}
	}
};
