(function()
{
	// @todo
	Common.Object.create('static Typical.SiteWrapper', {

		contentFrame : null,
		
		currentURL : null,
		
		onFrameFirstInitBind : null,
		
		contentLoaded : null,
		
		onContentLoaded : new Common.Dispatcher(),
		
		// @debug
		printException : function(e)
		{
			var x = [];
				
			for(var i in e)
				x.push(i + ': ' + e[i]);
				
			//alert(x.join("\t,"));
		},
		
		init : function()
		{
			this.contentLoaded = false;
			
			Event.observe(document, 'dom:loaded', this.load.bindAsEventListener(this));
		},
		
		browserIsSupported : function()
		{
			return Prototype.Browser.isBrowser('Gecko|WebKit|IE6|IE7|IE8|IE9') && !Prototype.Browser.isBrowser('Mobile');
		},
		
		load : function()
		{
			if(document.location.href.split('#').length == 0 || (window.parent && window.parent != window) || !this.browserIsSupported())
				this.byPass();

			else
				this.initWrapper();
		},
		
		byPass : function()
		{
			var hash = document.location.href.split('#');
			
			var url = this.getDomainURL() + (this.currentURL || this.getBrowserURL()) + (this.currentURL && hash.length > 2 ? '#' + hash[1] : '');
			
			if(url != document.location.href)
				document.location.href = url;
			
			document.location.reload(true);
		},
		
		initWrapper : function()
		{
			// Init URL.
			/*var url = this.getBrowserURL();
			
			if(url != null && url != '')
				this.currentURL = url;*/
			
			// Display loading.
			Element.addClassName(document.body, 'loading');
			
			// Init frame.
			this.contentFrame = $$('iframe.mainframe')[0];
			
			this.onFrameFirstInit = this.onFrameFirstInit.bind(this);
			
			Event.observe(this.contentFrame, 'load', this.onFrameFirstInit);
		
			setInterval(this.checkFrameDimensions.bind(this), 100);
			
			// Set location - may anti cache homepage.
			this.contentFrame.src = Config.siteAdress + (this.currentURL == '/' ? this.currentURL + '?' : this.currentURL);
			
			// @debug console.info('Init iframe src:', this.contentFrame.src);
			
			this.goTo(this.currentURL);
		},
		
		onFrameFirstInit : function()
		{
			Event.stopObserving(this.contentFrame, 'load', this.onFrameFirstInit);
			
			Event.observe(this.contentFrame, 'load', this.iframeLoad.bindAsEventListener(this));
			
			this.iframeLoad();
			
			setInterval(this.checkURL.bind(this), 1000);
		},
		
		beforeIframeUnload : function()
		{
			// Avoid white page?
			if(Prototype.Browser.isBrowser('WebKit|IE7|IE8|IE9'))
				Element.addClassName(document.body, 'loading');
			
			this.contentLoaded = false;
			
			this.checkURL();
		},
		
		iframeLoad : function()
		{
			// Handle night or day.
			try
			{
				if(this.contentFrame.contentWindow.Typical.Site.isNight())
					Element.addClassName($$('body.sitewrapper')[0], 'night');
				
				else
					Element.removeClassName($$('body.sitewrapper')[0], 'night');
			}
			catch(e)
			{
				this.printException(e);
			}
			
			// Display.
			Element.removeClassName(document.body, 'loading');
			
			// URL handling.
			Event.observe(this.contentFrame.contentWindow, 'beforeunload', this.beforeIframeUnload.bindAsEventListener(this));
			
			this.checkURL();
			
			// 
			this.contentLoaded = true;
			
			this.onContentLoaded.dispatch();
		},
		
		isContentLoaded : function()
		{
			return this.contentLoaded;
		},
		
		checkFrameDimensions : function()
		{
			this.contentFrame.height = document.viewport.getHeight();
		},
		
		checkURL : function()
		{
			try
			{				
				var url = this.getURL();
				
				if(url == this.currentURL)
					return;
			
				this.goTo(url);
			}
			catch(e)
			{
				this.printException(e);
			}
		},
		
		goTo : function(url)
		{
			if(url.split('://').length == 0)
				url = 'http://' + document.location.domain + url;
			
			this.currentURL = url;
			
			this.goToWindow(url);
			this.goToIFrame(url);
		},
		
		goToWindow : function(url)
		{
			try
			{
				// @debug console.info('goToWindow:', url);
				
				if(document.location.href != this.getDomainURL() + '#' + url)
					document.location.href = '/#' + url;
			}
			catch(e)
			{
				this.printException(e);
			}
		},
		
		goToIFrame : function(url)
		{
			try
			{
				// @debug console.info('goToIFrame:', url);
				
				// Anti cache homepage?
				if(url == '/')
					url = '/?';
					
				url = Config.siteAdress + url;
				
				if(this.contentFrame.contentWindow.location.href != url)
					this.contentFrame.contentWindow.location.href = url;
			}
			catch(e)
			{
				this.printException();
			}
		},
		
		getDomainURL : function()
		{
			var baseUrl = document.location.href.split('/');
			
			return baseUrl[0] + '/' + baseUrl[1] + '/' + baseUrl[2] + '/';
		},
		
		getURL : function()
		{
			var browserURL = this.getBrowserURL();
			
			// @debug console.info('getBrowserURL:', browserURL);
			// @debug console.info('currentURL:', this.currentURL);
			
			if(this.currentURL != browserURL)
				return browserURL;
				
			var iframeURL = this.getIFrameURL();
						
			// @debug console.info('iframeURL:', iframeURL);	
			
			if(this.currentURL != iframeURL)
				return iframeURL;

			return this.currentURL;
		},
		
		getBrowserURL : function()
		{
			var pieces = document.location.href.split('#');
			
			// @debug console.info('getBrowserURL pieces:', pieces, pieces[1] ? pieces[1].substr(0, 1) : null);
			
			if(pieces.length <= 1 || pieces[1].substr(0, 1) != '/' || pieces[1] == '/?')
				return '/';
			
			pieces.shift();
			
			var url = pieces.join('#');	
			
			return url.substr(0, 1) != '/' ? '/' + url : url;
		},
		
		getIFrameURL : function()
		{
			var pieces = this.contentFrame.contentWindow.location.href.split('/');
			
			pieces.shift();
			pieces.shift();
			pieces.shift();
			
			var url = pieces.join('/');
			
			// Filter anti-cache homepage.
			if(url == '?' || url == '/?')
				url = '/';
			
			return url.substr(0, 1) != '/' ? '/' + url : url;
		},
		
		getContent : function()
		{
			return this.contentFrame.contentWindow;
		},
		
		getContentConfig : function()
		{
			return this.contentFrame.contentWindow.Config;	
		},
		
		defferedClose : function()
		{
			this.makeContentLinksCloseWrapper();
			
			this.onContentLoaded.add(this.makeContentLinksCloseWrapper, this);
		},
		
		makeContentLinksCloseWrapper : function()
		{
			var elements = this.contentFrame.contentWindow.$$('a');
			
			for(var i = 0; i < elements.length; i++)
				elements[i].target = '_top';
		},
		
		getLinksTarget : function()
		{
			return this.contentFrame.id;	
		}
		
	});
})();
