
var ajaxWindows = new Array();
var ajaxIframeOnload = function()
{
	var ajaxIframe = document.getElementById('ajaxIframe');
	if(!openWindow())
		return;
	if(ajaxIframe.contentWindow)
		var doc = ajaxIframe.contentWindow.document;
	else if(ajaxIframe.contentDocument)
		var doc = ajaxIframe.contentDocument;
	else
		var doc = ajaxIframe.document;
// 	if(doc.location == 'about:blank')
	if((doc.location == 'about:blank') || (doc.body.getAttribute('iframe_blank_page') == 'yes'))
		return;
	var html = doc.body.innerHTML;
	ajaxIframe.src = 'http://www.youcontent.nl//blank.htm';
	openWindow()._callback(html);
};
document.write('<iframe '+
	'frameborder="0" '+
	'style="position:absolute;width:0px;height:0px;top:0px;left:0px;overflow:hidden;border:0" '+
	'id="ajaxIframe" '+
	'name="ajaxIframe" '+
	'onload="ajaxIframeOnload();" '+
        'src="http://www.youcontent.nl//blank.htm">'+
'</iframe>');
function openWindow()
{
	var output = false;
	for(var i = ajaxWindows.length-1; i >= 0; i--)
	{
		if(ajaxWindows[i].isopen && (!output || ajaxWindows[i].window.style.zIndex > output.window.style.zIndex))
			output = ajaxWindows[i]
	}
	return output;
}

function ajaxWindow(params)
{
	var cls = this;
	// make parameters available
	this.url;
	this.content;
	this.image;
	this.video;
	this.width;
	this.height;
	this.title;
	this.fullscreen;
	this.onload;
	this.backdrop;

	// make us globally available
	this.windowId = ajaxWindows.length;
	ajaxWindows[this.windowId] = this;
	this.alive = false;
	this.isopen = false;
	this.loadingHTML;

	// define the window html
	this.winHTML = '<b class="c">';
	this.winHTML += '<b class="c1"><b></b></b>';
	this.winHTML += '<b class="c2"><b></b></b>';
	this.winHTML += '<b class="c3"></b>';
	this.winHTML += '<b class="c4"></b>';
	this.winHTML += '<b class="c5"></b></b>';
	this.winHTML += '<div id="container'+this.windowId+'" class="container">';
	this.winHTML += '  <div id="head'+this.windowId+'" class="head">';
	this.winHTML += '    <div id="headtext'+this.windowId+'" class="headtext"></div>';
	this.winHTML += '    <a class="topbutton" href="javascript:ajaxWindows['+this.windowId+'].close();"><img src="http://www.youcontent.nl/img/close_small.jpg" alt="close" /></a>';
	this.winHTML += '  </div>';
	this.winHTML += '  <div id="content'+this.windowId+'" class="content">';
	// caution: any content here will be replaced
	this.winHTML += '    <br /><br /><img src="http://www.youcontent.nl/img/loading2.gif" alt="loading..." />';
	this.winHTML += '  </div>';
	this.winHTML += '</div>';
	this.winHTML += '<b class="c">';
	this.winHTML += '<b class="c5"></b>';
	this.winHTML += '<b class="c4"></b>';
	this.winHTML += '<b class="c3"></b>';
	this.winHTML += '<b class="c2"><b></b></b>';
	this.winHTML += '<b class="c1"><b></b></b></b>';

	this.imageHTML = '<center><a href="javascript:openWindow().close();"><img src="{IMAGE_SRC}" alt="" width="{IMAGE_WIDTH}" height="{IMAGE_HEIGHT}" style="border:none;" /></a></center>';

	this.videoHTML = '<center><object width="{VIDEO_WIDTH}" height="{VIDEO_HEIGHT}" classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000">';
	this.videoHTML += '<param name="movie" value="http://www.youcontent.nl/f4player/player.swf" />';
	this.videoHTML += '<param name="quality" value="high" />';
	this.videoHTML += '<param name="menu" value="false" />';
	this.videoHTML += '<param name="allowFullScreen" value="true" />';
	this.videoHTML += '<param name="scale" value="noscale" />';
	this.videoHTML += '<param name="allowScriptAccess" value="always" />';
	this.videoHTML += '<param name="swLiveConnect" value="true" />';
	this.videoHTML += '<param name="flashVars" value="';
	this.videoHTML += 'skin=http%3A%2F%2Fwww.youcontent.nl%2Ff4player%2Fskins%2Fclassic.swf'
	this.videoHTML += '&thumbnail=http%3A%2F%2Fwww.youcontent.nl%2Ffetch.php?f={VIDEO_FILE_URLENC}%26snapshot';
	this.videoHTML += '&video=http%3A%2F%2Fwww.youcontent.nl%2Ffetch.php?f={VIDEO_FILE_URLENC}%26stream';
	this.videoHTML += '&autoplay=0';
	this.videoHTML += '"/>';
	this.videoHTML += '<!--[if !IE]><!-->';
	this.videoHTML += '<object width="{VIDEO_WIDTH}" height="{VIDEO_HEIGHT}" data="http://www.youcontent.nl/f4player/player.swf" type="application/x-shockwave-flash">';
	this.videoHTML += '<param name="quality" value="high" />';
	this.videoHTML += '<param name="menu" value="false" />';
	this.videoHTML += '<param name="allowFullScreen" value="true" />';
	this.videoHTML += '<param name="scale" value="noscale" />';
	this.videoHTML += '<param name="allowScriptAccess" value="always" />';
	this.videoHTML += '<param name="swLiveConnect" value="true" />';
	this.videoHTML += '<param name="flashVars" value="';
	this.videoHTML += 'skin=http%3A%2F%2Fwww.youcontent.nl%2Ff4player%2Fskins%2Fclassic.swf';
	this.videoHTML += '&thumbnail=http%3A%2F%2Fwww.youcontent.nl%2Ffetch.php?f={VIDEO_FILE_URLENC}%26snapshot';
	this.videoHTML += '&video=http%3A%2F%2Fwww.youcontent.nl%2Ffetch.php?f={VIDEO_FILE_URLENC}%26stream';
	this.videoHTML += '&autoplay=0';
	this.videoHTML += '"/>';
	this.videoHTML += '</object>';
	this.videoHTML += '<!--<![endif]-->';
	this.videoHTML += '</object></center>';

	/**
		ajaxWindow.setParams(paramObj)
		@access public
		@description
			sets desired parameters in the object
	**/
	this.setParams = function(paramObj)
	{
		if(paramObj['url']) this.url = paramObj['url'];
		if(paramObj['content']) this.content = paramObj['content'];
		if(paramObj['image']) this.image = paramObj['image'];
		if(paramObj['video']) this.video = paramObj['video'];
		if(paramObj['width']) this.width = paramObj['width'];
		if(paramObj['height']) this.height = paramObj['height'];
		if(paramObj['title']) this.title = paramObj['title'];
		if(paramObj['fullscreen']) this.fullscreen = paramObj['fullscreen'];
		if(paramObj['onload']) this.onload = paramObj['onload'];
	}
	// set the parameters passed on construction
	this.setParams(params);

	this._calcDims = function()
	{
		// window dimensions
		var output = {};
		if(typeof(this.width) == 'number')
			output.width = this.width;
		else if ((typeof(this.fullscreen) != 'undefined') && this.fullscreen)
			output.width = windowWidth - 40;
		else
			output.width = 640;

		if(typeof(this.height) == 'number')
			output.height = this.height;
		else if ((typeof(this.fullscreen) != 'undefined') && this.fullscreen)
			output.height = windowHeight - 40;
		else
			output.height = Math.min(windowHeight - 40, 600);

		return output;
	}

	this._callback = function(content)
	{
		var winDims = this._calcDims();
		if(typeof(this.form) == 'object')
		{
			this.form.parentNode.removeChild(this.form);
			this.form = undefined;
		}
		cls = this;
		this.window.content.tween.onfadefinish = function()
		{
			cls.window.tween.resize(winDims.width, winDims.height);
			cls.window.tween.onresizefinish = function()
			{
				cls.setContent(content);
				cls.window.content.tween.onfadefinish = function()
				{
					cls.window.head.style.visibility = 'visible';
					if(navigator.userAgent.indexOf('MSIE') != -1)
						cls.window.head.tween.setOpacity(1);
					else
						cls.window.head.tween.fade(1);
				}
				cls.window.content.tween.fade(1);
			}
		}
		if(navigator.userAgent.indexOf('MSIE') != -1)
			window.setTimeout(function(){cls.window.content.tween.onfadefinish();cls.window.content.tween.onfadefinish=undefined}, 400);
		else
			this.window.content.tween.fade(0);
	};

	this._onresize = function(e)
	{
		if((typeof(cls.backdrop) == 'object') && curCSS(cls.backdrop, 'display') != 'none')
		{
			var dispStyle = cls.backdrop.style.display;
			cls.backdrop.style.display = 'none';
			cls.backdrop.style.width = '100%';
			cls.backdrop.style.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, windowHeight)+'px';
			cls.backdrop.style.display = dispStyle;
		}
	}

	/**
		ajaxWindow.open()
		@access public
		@description
			creates, initializes, opens a window and loads the desired url inside it
	**/
	this.open = function()
	{
		// ============================ pre-checks =============================
		// either url or content must be defined otherwise we cannot do anything
		if((typeof(this.url) == 'undefined') && (typeof(this.content) == 'undefined')
		&& (typeof(this.image) == 'undefined') && (typeof(this.video) == 'undefined'))
			return;
		// ============== initialize the dark overlay and fade it ==============
		if(!this.alive)
		{
			this.backdrop = document.body.appendChild(document.createElement('DIV'));
			new tween(this.backdrop);
			this.backdrop.tween.setOpacity(0);
			this.backdrop.className = 'backdrop';
			listen(window, 'resize', this._onresize);
		}
		this.backdrop.style.display = 'block';
		this.backdrop.tween.fade(0.6, 300);
		this.backdrop.tween.onfadefinish = undefined;
		this._onresize();
		// ===== find a sufficient z-index value for window and backdrop ======
		var zIndex = 1;
		for(var i = 0; i < ajaxWindows.length; i++)
		{
			if(ajaxWindows[i] == this)
				continue;
			else if(!ajaxWindows[i].isopen)
				continue;
			if(ajaxWindows[i].window.style.zIndex >= zIndex)
				zIndex = ajaxWindows[i].window.style.zIndex+1;
		}
		this.backdrop.style.zIndex = zIndex;
		// ================ check if we were opened before ====================
		if(this.alive)
		{
			this.window.style.display = 'block';
			this.window.style.zIndex = zIndex;
			this.isopen = true;
			this.window.center();
			this.window.tween.fade(1);
			return;
		}
		// ============== not opened before, will be opened now ===============
		this.alive = true;
		this.isopen = true;
		// ====================== create window objects =======================
		this.window = document.body.appendChild(document.createElement('DIV'));
		new tween(this.window);
		this.window.className = 'ajaxWindow';
		this.window.style.display = 'block';
		this.window.style.zIndex = zIndex;
		this.window.tween.resizePower = 5;
		this.window.tween.setOpacity(0);
		this.window.windowObj = this;

		// set the contents of the window
		this.window.innerHTML = this.winHTML;
		// create pointers to various objects inside the window
		this.window.content = document.getElementById('content'+this.windowId);
		this.window.container = document.getElementById('container'+this.windowId);
		this.window.head = document.getElementById('head'+this.windowId);
		this.window.headtext = document.getElementById('headtext'+this.windowId);
		new tween(this.window.content);
		new tween(this.window.head);
		// save the html of the initial content for later use
		this.loadingHTML = this.window.content.innerHTML;
		// initially hide the window header
		this.window.head.style.visibility = 'hidden';
		this.window.head.tween.setOpacity(0);

		// ================= some event-related functions ====================
		/**
			ajaxWindow.window.center()
			@access public
			@description
				positions the window to the center of the browser
		**/

		this.window.center = function(smooth)
		{
// 			if(typeof(this) != 'object')
// 				this = this.windowObj;
			var dims = cls.window.tween.getDimensions();
			var xpos = parseInt((windowWidth - dims.width) / 2);
			var ypos = parseInt((windowHeight - dims.height) / 2);
// 			cls.window.style.position = 'absolute';
			if(smooth)
			{
				cls.window.tween.motionPower = 3;
				cls.window.tween.motion(
					scrollXpos + xpos,
					scrollYpos + ypos, 200);
			}
			else
			{
// 				cls.window.style.left = scrollXpos + xpos + 'px';
// 				cls.window.style.top  = scrollYpos  + ypos + 'px';
				cls.window.tween.setPosition(
					scrollXpos + xpos,
					scrollYpos + ypos);
			}
		}
		this.window.tween.onresize = function(w,h)
		{
			// container is 10px smaller than window
			cls.window.container.style.height = (h-10) + 'px';
			// content layer height = container - header(+padding)
			var headdim = cls.window.head.tween.getDimensions();
			cls.window.content.style.height = (h-10-headdim.height) + 'px';
			// window has been resized, center it again
			cls.window.center();
		};
		// center the window again if browser is resized

		this._onscroll = function()
		{
			if(!this.window)
				return;
			if ((typeof(this.fullscreen) != 'undefined') && this.fullscreen)
			{
				var width = windowWidth - 40;
				var height = windowHeight - 40;
				var dims = cls.window.tween.getDimensions();
				if((dims.width != width) || (dims.height != height))
				{
					this.window.tween.resizePower = 3;
					this.window.tween.resize(width, height, 200);
				}
				else
					this.window.center(1);
// 				this.window.tween.setDimensions(width,height);
// 				this.window.tween.onresizefinish = function()
// 				{
// 					this.window.center();
// 				}
			}
			else
				this.window.center(1);
		};
		listen(window, 'resize', function(){cls._onscroll()});
		listen(window, 'scroll', function(){cls._onscroll()});
		// ========================== finalize =============================
		var dims = cls.window.tween.getDimensions();
		// hack: call the resize to initialize heights
		this.window.tween.onresize(dims.width, dims.height);
		// now fade it to front
		this.window.tween.fade(1);
		// set title of the window to be displayed
		this.setTitle(this.title);

		// show "loading..." until content is loaded
		this.setContent(this.loadingHTML);
		// ====================== load up the page =========================
		if(typeof(this.url) != 'undefined')
			document.getElementById('ajaxIframe').src = this.url;
		else if(typeof(this.content) != 'undefined')
			this._callback(this.content);
		else if(typeof(this.image) != 'undefined')
		{
// 			var cls = this;
			var tmpImage = new Image();
			var loaded = function()
			{
				loaded = undefined;
				var winDims = cls._calcDims();
				var scale = Math.min(1, (winDims.width - 30) / tmpImage.width);
				var imgWidth = tmpImage.width * scale;
				var imgHeight = tmpImage.height * scale;
				cls._callback(cls.imageHTML.replace(/\{IMAGE_SRC\}/g, cls.image).replace(/\{IMAGE_WIDTH\}/g, imgWidth).replace(/\{IMAGE_HEIGHT\}/g, imgHeight));
			};
			tmpImage.onload = function(){if(loaded)loaded();};
			tmpImage.src = cls.image;
			if(tmpImage.complete)
				if(loaded)loaded();
		}
		else if(typeof(this.video) != 'undefined')
		{
			var tmpImage = new Image();
			var loaded = function()
			{
				loaded = undefined;
				var winDims = cls._calcDims();
				var scale = Math.min(1, (winDims.width - 30) / tmpImage.width);
				var imgWidth  = tmpImage.width  * scale;
				var imgHeight = tmpImage.height * scale;
				cls._callback(cls.videoHTML.replace(/\{VIDEO_FILE\}/g, cls.video).
				replace(/\{VIDEO_FILE_URLENC\}/g, escape(escape(cls.video))).
				replace(/\{VIDEO_WIDTH\}/g, imgWidth).replace(/\{VIDEO_HEIGHT\}/g, imgHeight));
			}
			tmpImage.onload = function(){if(loaded)loaded();};
			tmpImage.src = 'http://www.youcontent.nl/fetch.php?f='+escape(this.video)+'&snapshot';
			if(tmpImage.complete)
				if(loaded)loaded();
		}
	}

	/**
		ajaxWindow.resize(width,height,callback)
		@access public
		@description
			smoothly resizes the window to new dimensions, calls callback when done
	**/
	this.resize = function(width, height, callback, cls)
	{
		if(typeof(cls) != 'object')
			cls = this;
		cls.width = width;
		cls.height = height;
		cls.fullscreen = false;
		cls.window.tween.resize(width, height);
		cls.window.tween.onresizefinish = callback;
	}

	/**
		ajaxWindow.setTitle(title)
		@access public
		@description
			sets the window title to desired text
	**/
	this.setTitle = function(title, cls)
	{
		if(typeof(cls) != 'object')
			cls = this;
		if(typeof(title) == 'undefined')
			title = '';
		cls.title = title;
		cls.window.headtext.innerHTML = '';
		cls.window.headtext.appendChild(document.createTextNode(title));
	}

	/**
		ajaxWindow.load()
		@access private
		@description
			loads url in window
	**/
	this.setContent = function(html)
	{
		this.window.content.innerHTML = html;
		var root_uri = 'http://www.youcontent.nl/';
		var forms = this.window.content.getElementsByTagName('FORM');
		for(var i = 0; i < forms.length; i++)
		{
			if(forms[i].target.length)
				continue;
			if(root_uri != forms[i].action.substr(0, root_uri.length))
				continue;
			if(forms[i].action.indexOf('#') != -1)
				continue;
			eval('listen(forms['+i+'], "submit", function(){forms['+i+'].style.visibility="hidden";});');
			eval('listen(forms['+i+'], "submit", function(){forms['+i+'].style.overflow="hidden";});');
			eval('listen(forms['+i+'], "submit", function(){forms['+i+'].style.width="0px";});');
			eval('listen(forms['+i+'], "submit", function(){forms['+i+'].style.height="0px";});');
			eval('listen(forms['+i+'], "submit", function(){cls.form=forms['+i+'];});');
			eval('listen(forms['+i+'], "submit", function(){document.body.appendChild(forms['+i+']);});');
			eval('listen(forms['+i+'], "submit", function(){cls.setContent(cls.loadingHTML);});');
			forms[i].target = 'ajaxIframe';
		}
		var links = this.window.content.getElementsByTagName('A');
		for(var i = 0; i < links.length; i++)
		{
			if(links[i].target.length)
				continue;
			if(root_uri != links[i].href.substr(0, root_uri.length))
				continue;
			if(links[i].href.indexOf('#') != -1)
				continue;
			eval('listen(links['+i+'], "click", function(){cls.setContent(cls.loadingHTML);});');
			links[i].target = 'ajaxIframe';
		}
/*		var links = this.window.content.getElementsByTagName('A');
		for(var i = 0; i < links.length; i++)
		{
			if(links[i].target.lenth)
				continue;
			var url = urlreg.exec(links[i].action);
			if(!url || (url[2].substring(0, root_uri.length) != root_uri))
				continue;
			eval('listen(links['+i+'], "click", function(){links['+i+'].style.visibility="hidden";});');
			eval('listen(links['+i+'], "click", function(){links['+i+'].style.overflow="hidden";});');
			eval('listen(links['+i+'], "click", function(){links['+i+'].style.width="0px";});');
			eval('listen(links['+i+'], "click", function(){links['+i+'].style.height="0px";});');
			eval('listen(links['+i+'], "click", function(){cls.form=links['+i+'];});');
			eval('listen(links['+i+'], "click", function(){document.body.appendChild(links['+i+'];}));');
			eval('listen(forms['+i+'], "click", function(){cls.setContent(cls.loadingHTML);});');
			links[i].target = 'ajaxIframe';
		}*/
		if(typeof(this.onload) == 'function')
			this.onload.call(this);
		execJS(this.window.content);
	}
	/**
		ajaxWindow.close()
		@access public
		@description
			fades the window and backdrop to transparency and hides them
	**/
	this.close = function(callback)
	{
		// fade away the backdrop
		this.backdrop.tween.fade(0, 300);
		this.backdrop.tween.onfadefinish = function(){cls.backdrop.style.display = 'none';};
		// also fade away window
		this.window.tween.fade(0, 300);
		this.isopen = false;
		this.window.tween.onfadefinish = function()
		{
			cls.window.style.display = 'none';
			if(typeof(callback) == 'function')
				callback();
		};
	}

	this.destroy = function()
	{
		this.close(function()
		{
			if(typeof(cls.window.parentNode) == 'object')
				cls.window.parentNode.removeChild(cls.window);
			cls.backdrop.parentNode.removeChild(cls.backdrop);
			//ajaxWindows[cls.windowId] = null;
			//cls.window.tween.onfadefinish = null;
		});
/*		this.window.tween.onresizefinish = null;
		this.window.content.tween.onresizefinish = null;
		this.window.content.tween.onfadefinish = null;
		this.window = null;
		this.onload = null;*/
		this.alive = false;
	}
}
