/**
 * Copyright:	 Ing. R.J. van Dongen, 2009
 * This script is copyrighted and may not be used witohout permission.
 * Also, this script comes without ANY guarantees whatsoever.
 * ANY damage to your system or files cannot be accounted to the use of this script.
 */
var pload;
var ivpfsmode=true;
//mimic prototype
if (typeof $=='undefined') { function $(id){return (document.getElementById?document.getElementById(id):null);} };
//purge() MUST BE CALLED to avoid IE6 mem-leaks
//see http://javascript.crockford.com/memory/leak.html
function purge(d) {
    var a=d.attributes,i,l,n;
    if (a) {
        l=a.length;
        for (i=0; i<l; i+=1) {
            n = a[i].name;
            if (typeof d[n] === 'function') {
                d[n]=null;
            }
        }
    }
    a=d.childNodes;
    if (a) {
        l=a.length;
        for (i=0; i<l; i+=1) {
            purge(d.childNodes[i]);
        }
    }
};
//insert a node after a given node
function insertAfter( refNode, newNode ) {
    refNode.parentNode.insertBefore( newNode, refNode.nextSibling );
};
//initializes a div for PurePlayer usage
init360=function(id,wrap) {
	var es;
	if ($(wrap)) { es=$(wrap).style; es.position='absolute'; es.zIndex='+999'; es.display='none'; }
	else {
	    //create
		if (document.createElement) {
			var wdiv=document.createElement('div'); wdiv.id=wrap;
			es=wdiv.style; es.position='absolute'; es.zIndex='+999'; es.display='none'; es.padding='0px';es.margin='0px';
		    var el = document.createElement('div');
			es=el.style; es.textAlign='center';es.position='relative';es.background='#fff none';es.height='20px';es.padding='0px';es.margin='0px';
			var tmp=document.createElement('a');
			tmp.href='javascript:close360(\''+id+'\', \''+wrap+'\');';
			tmp.title="Sluiten";tmp.style.cursor='hand';
			var txt=document.createTextNode('[ Sluiten ]');
			tmp.appendChild(txt);
			el.appendChild(tmp);
			wdiv.appendChild(el);
		    el = document.createElement('div'); el.id=id;
			es=el.style;es.display='inline';es.position='relative';es.background='#fff none';es.padding='0px';es.margin='0px';
			wdiv.appendChild(el);
			document.body.appendChild(wdiv);
		}else alert('Browser does not support adding elements');
	}
};
//Registers a method to window.onunload to destroy the PurePlayer div
//This is to avoid IE memleaks
//wu360=function(id,wrap) { removeEvent(window,'unload',function(){close360(id,wrap);});addEvent(window,'unload',function(){close360(id,wrap);}) };
var wu360A=false;
wu360=function(id,wrap) { 
	if (wu360A==false) {
		//WARNING: Opera is inconsistent with onunload
		addEvent(window,(window.opera?'unload':'beforeunload'),function(){close360(id,wrap);})
		wu360A=true;
	}
};

//closes a wrapper div for PurePlayer
close360=function(id,wrap) { 
	if ($(id)) {
		purge($(id));
		$(id).innerHTML='';
	}
	if ($(wrap)) $(wrap).style.display = 'none';
};
//Loads an IVP file into the PurePlayer
load360=function(id,wrap,ivp,w,h,tgt) {
	//close existing to avoid mem-leaks in IE6
	close360(id,wrap);
	if (pload==ivp) {
		pload=null;
	}
	else {
		init360(id,wrap);
		//assign window.onunload (avoid memleaks IE6)
		wu360(id,wrap);
		//$(id).innerHTML='Loading...';
		pload=ivp;
		var dh=0;
		if (ivpfsmode===true) {
			w=getClientWH().w;
			h=getClientWH().h;
		}
		if ($(wrap)) { 
			if (ivpfsmode===true) {
				var es=$(wrap).style; es.display='inline'; es.border='0px none'; es.margin='0px'; es.padding='0px'; es.width=w; es.height=h;
				if (tgt) { es.top=getScrollWH().y; es.left=getScrollWH().x; } 
			}
			else {
				var es=$(wrap).style; es.width=w; es.height=h; es.display='inline'; es.margin='0px'; es.padding='0px'; es.cssFloat='none'; es.clear='all'; es.border='0px none';
				if (tgt) { es.top=getAbsPos(tgt).y; es.left=getAbsPos(tgt).x; } 
			}
		}
		dh=getElWH($(id).parentNode.firstChild).h;
		var es=$(id).style; es.width=w; es.height=h-dh; es.margin='0px'; es.padding='0px'; es.border='0px solid #006600';
		var so = new SWFObject("PurePlayer.swf", "360", w, h-dh, "7", "#ffffff");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("allowFullScreen", "true");
		so.addParam("fullscreen", "true");
		so.addParam("allowScriptAccess", "sameDomain");
		so.addParam("flashVars", "panorama="+ivp);
		so.write(id);
	}
};
//get the absolute screen position of an element
getAbsPos = function(el) {
	var SL=0, ST=0;
	var is_div = /$div^/i.test(el.tagName);
	if (is_div && el.scrollLeft) { SL = el.scrollLeft; }
	if (is_div && el.scrollTop) { ST = el.scrollTop; }
	var pos = { x: el.offsetLeft - SL, y: el.offsetTop - ST };
	if (el.offsetParent){
		var tmp = getAbsPos(el.offsetParent);
		pos.x += tmp.x;
		pos.y += tmp.y;
	}
	return pos;
};
//adds an event
addEvent = function(el, event, func) {
	if (el.attachEvent){ el.attachEvent("on"+event, func); } //IE
	else if (el.addEventListener){ el.addEventListener(event, func, true); } //W3C
	else { el["on"+event] = func; } //?
};
//removes an event
removeEvent = function(el, event, func) {
	if (el.detachEvent){ el.detachEvent("on"+event, func); } //IE
	else if (el.removeEventListener){ el.removeEventListener(event, func, true); } //W3C
	else { el["on"+event] = null; } //?
};
function getElWH(el) {
	el=(typeof(el)=='string')?$(el):el;
	var h=el.clientHeight || el.offsetHeight || el.style.height;
	var w=el.clientWidth || el.offsetWidth || el.style.width;
	var p={};p.w=(w?parseInt(w):0);p.h=(h?parseInt(h):0);return p;
};
function getClientWH(){
	var p={};
	if(typeof(window.innerWidth)=='number') {
		//Non-IE
		p.w = window.innerWidth;
		p.h = window.innerHeight;
	} else if( document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight) ) {
		//IE 6+ in 'standards compliant mode'
		p.w = document.documentElement.clientWidth;
		p.h = document.documentElement.clientHeight;
	} else if( document.body && (document.body.clientWidth || document.body.clientHeight) ) {
		//IE 4 compatible
		p.w = document.body.clientWidth;
		p.h = document.body.clientHeight;
	}
	return p;
};
function getScrollWH(){
	var x = window.pageXOffset || document.body.scrollLeft || document.documentElement.scrollLeft;
	var y = window.pageYOffset || document.body.scrollTop || document.documentElement.scrollTop;
	var p={};p.x=(x?parseInt(x):0);p.y=(y?parseInt(y):0);return p;
};
