/*
TDWLM Windows
*/

var TdWindow = Class.create(
{		
	height: 'auto',
	width: '600px',
	duration: 0.3,
	callBack: function()
	{
		// void
	},
	
	initialize: function()
	{
		// void
	},
	
	getPageSize: function()
	{
		
		 var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		
		if (self.innerHeight) {	// all except Explorer
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth; 
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = xScroll;		
		} else {
			pageWidth = windowWidth;
		}
	
		return [pageWidth,pageHeight];
	},
	
	attach: function(element, callBack)
	{
		
		if(!$('overlay'))
		{
			this.createRequiredElements();
		}
		var el = $(element);
		el.stopObserving('click', openWindow); // make sure there's no double event attachning!
		var openWindow = this.openWindow.bind(this);
		el.observe('click', openWindow);
		if(typeof callBack != "undefined")
		{
			this.callBack = callBack;
		}
	},
	
	openWindow: function(e)
	{
		Event.stop(e);
		var a = Event.findElement(e, 'a');
		a.addClassName('opened_by_tdwindow');
		var showWindow = this.showWindow.bind(this);
		if(a.href.match(/\.(jpe?g|gif|png|tiff?)$/i) || a.href.match(/phpThumb/i))
		{
			$('tdcontent').insert('<img src="' + a.href + '" alt="" />');
			this.showWindow();
		}
		else
		{
			new Ajax.Updater($('tdcontent'), a.href, { onComplete: showWindow });
		}
	},
	
	showWindow: function()
	{
		var wh = this.getPageSize();
		var w = wh[0] + 'px';
		var h = wh[1] + 'px';
		var offset = document.viewport.getScrollOffsets();
		
		$('tdwindow').setStyle({ top: (offset[1] + 100) + 'px' });
		$('overlay').setStyle({ height: h, width: w });
		var callBack = this.callBack.bind(this);
		new Effect.Appear($('overlay'),
		{
			from: 0.0,
			to: 0.8,
			duration: this.duration,
			afterFinish: function(e)
			{
				$('tdwindow').show();
				var w = $('tdcontent').firstDescendant().getWidth();
				// alert(w);
				$('tdwindow').hide();
				$('tdwindow').setStyle({ visibility: 'visible' });
				$('tdwrapper').setStyle( { width: w + 'px' });
				
				new Effect.Appear($('tdwindow'),
				{
					duration: this.duration,
					afterFinish: callBack
				});
			}
		});
	},
	
	createRequiredElements: function()
	{
		var b = $$('body')[0];
		$(b).insert({ bottom: '<div id="overlay" style="display: none;"></div>' });
		$(b).insert({ bottom: '<div id="tdwindow" style=" display: none; visibility: hidden; margin: 0 auto; width: 100%; text-align: center; position: absolute; top: 100px; z-index: 1000; height: ' + this.height + ';"><div id="tdwrapper" style="padding: 0 30px 30px 30px; background: white; margin: 0 auto;"><div id="tdclose"><img src="images/close_window.png" alt="Sulje" /></div><div id="tdcontent"></div></div></div>' });
		// <div id="info" style="display: none; margin: 0 auto; text-align: center; width: 100%; position: absolute; top: 100px; z-index: 100; height: 500px;"><div style="height: 500px; width: 600px; margin: 0 auto; background: white;"></div>
		var closeWindow = this.closeWindow.bind(this);
		$('tdclose').observe('click', closeWindow);
	},
	
	closeWindow: function(e, callBack)
	{
		// Event.stop(e);
		$$('.opened_by_tdwindow').invoke('removeClassName', 'opened_by_tdwindow');
		if(typeof callBack != 'undefined')
		{
			var callBack = callBack.bind(this);
		}
		new Effect.Fade($('tdwindow'),
		{
			duration: this.duration,
			afterFinish: function(e)
			{
				new Effect.Fade($('overlay'),
				{
					duration: this.duration,
					afterFinish: function(e)
					{
						$('tdwindow').setStyle({ visibility: 'hidden' });
						if(typeof callBack != 'undefined')
						{
							callBack();
						}
					}
				});
			}
		});
	}
});

var ReviewStoryWindow = new TdWindow();
var CommentWindow = new TdWindow();
