var CB = {
	$: function(id) {
		return document.getElementById(id);
	},
	createEl: function(tag, id) {
		var el = document.createElement(tag);
		if (id != null) el.id = id;
		return el;
	},
	geom: function(el, left, top, width, height) {
		var s = el.style;
		s.position = 'absolute';
		s.left = left+'px'; s.top = top+'px'; s.width = width+'px'; s.height = height+'px';
	},
	
	banner: null,
	bannerSrc: null,
	
	doBanner: function(src) {
		if (src) CB.bannerSrc = src;
		else src = CB.bannerSrc;
		if (!src) return;
		if (!CB.banner)
			CB.banner = document.createElement('iframe');
		CB.banner.frameBorder = '0';
		CB.geomBanner();
		
		if (!CB.$('flashOuter'))
		{
			//older published pages don't have this div
			
			var div = document.createElement('div');
			div.id = 'flashOuter';
			div.style.position = 'absolute';
			div.style.top = 25;
			document.body.appendChild(div);
		}
		CB.$('flashOuter').appendChild(CB.banner);
		CB.banner.src = src;
	},
	
	geomBanner: function() {
		if (!CB.banner) return;
		CB.geom(CB.banner, (f_clientWidth() - 800)/2 + 800, 5, 160, 600);
	},
	
	showMobile: function() {
		/*var el = CB.createEl('iframe', 'mobile');
		el.style.height = '100%';
		el.style.marginLeft = 'auto';
		el.style.marginRight = 'auto';
		el.style.width = '340px';
		el.src = './xhtml';
		el.frameBorder = '0';
		document.body.appendChild(el);*/
		document.body.innerHTML =
			'<iframe style="height:100%; width:340px; float:left" frameborder="0" src="./xhtml"></iframe>'+
			'<div style="margin:10px; float:left;"><a class="icn-close" href="javascript:document.location.reload()"></a></div>';
		document.body.style.background = '#EBE6E8';
	},
	detectionSettings: {desktop:'flash',desktopNoFlash:'html5',tablet:'html5',smartphone:'xhtml'}
};

//Detect mobile device, switch to HTML5
var s='script';
document.write('<'+s+' type="text/java'+s+'" src="detect.js?t='+(new Date().getTime())+'"></'+s+'>');
function detectRedirect()
{
	var device = 'desktop';
	var ua = navigator.userAgent;
	if (/iPad|Android 3|PlayBook/.test(ua) || (/Android/.test(ua) && !/mobile/i.test(ua)))
		device = 'tablet';
	else if (/iPod|iPhone|Android|SymbianOS|BlackBerry|NetFront|Jasmine|Dolfin/i.test(ua))
		device = 'smartphone';
	else if (swfobject.hasFlashPlayerVersion('9.0.0'))
		device = 'desktop';
	else
		device = 'desktopNoFlash';
	var version = CB.detectionSettings[device];
	if (version == 'flash') return;
	
	var a = document.location.pathname.split('/'), file = a.pop(); a.push(version, file);
	document.location.replace(a.join('/'));
}

if (window.location.hostname.match(/[üöäßșțăâî]/))
{
	//flash player triggers a false security error if hostname is utf-8
	if (username)
		document.location = 'http://'+username+'.cabanova.com';
}

function resizeSWF(height)
{
	if (resizeSWF.lastH != null && height != resizeSWF.lastH)
	{
		getApp().style.height = Math.max(height + 20, f_clientHeight());
		//scrollToTop();
	}
}

resizeSWF.lastH = -1;

function startNewPage()
{
	scrollToTop();
	HtmlContent.clear();
}

function scrollToTop()
{
	if (document.body) document.body.scrollTop = 0;
	else document.documentElement.scrollTop = 0;
}

function getApp(id)
{
	if (typeof id == 'undefined')
			id = 'index';
	
	var isIE = navigator.appName.indexOf("Microsoft") != -1;
	return (isIE) ? window[id] : document[id]; 
}

window.onresize = function()
{
	if (!getApp()) return;
	getApp().setScreenHeight(f_clientHeight());
	if (parseFloat(getApp().style.height) < f_clientHeight())
		getApp().style.height = f_clientHeight() + "px";
	fixBackground();
	CB.geomBanner();
	HtmlContent.fixPos();
};


var _gaq = _gaq || [];
var GA = {
	freeID: /*gaid for free websites*/'UA-237597-18',
	paidID: /*gaid for paid websites*/'UA-237597-23',
	userID: null,
	start: function() {
		//_gaq.push(['cab._setAccount', GA.id]);
		_gaq.push(['cab._setDomainName', 'none']);
		//_gaq.push(['cab._trackPageview']);
		
		var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
		ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/u/ga.js';
		var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	},
	trackPage: function(page) {
		CB.doBanner();
		//don't track draft
		if (document.location.pathname.indexOf('/draft/') == 0) return;
		var isPaid = (!CB.banner || (window.location.hostname.indexOf('cabanova') < 0)); 
		_gaq.push(['cab._setAccount', isPaid ? GA.paidID : GA.freeID]);				
		
		
		var a = document.location.host.split('.');
		a.reverse();
		a.push(page + '.html');
		a = a.join('/');
		

		_gaq.push(['cab._trackPageview', '/' + page + '.html']);		
		if (GA.userID)
			_gaq.push(['usr._trackPageview', a]);
			
		//_gaq.push(['cab._trackEvent', 'navigate', 'default', page]);
	},
	setUser: function (id) {
		if (id)
		{
			GA.userID = id;
			_gaq.push(['usr._setAccount', GA.userID]);
			_gaq.push(['usr._setDomainName', 'none']);		
		}
	},
	
	trackEvent: function (category, action, label)
	{
		_gaq.push(['cab._trackEvent', category, action, label]);
	}
};

window.onload = function()
{
	fixBackground();
	GA.start();
};


function fixBackground()
{
	var el = CB.$('index');
	if (!el) return;
	var pos=cumulativeOffset(el);
	var ox = pos[0]-200;
	var oy = pos[1]-200;
	document.body.style.backgroundPosition = ox+'px '+oy+'px';
}


function f_clientHeight() {
	return f_filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function f_clientWidth() {
	return f_filterResults (
			window.innerWidth ? window.innerWidth : 0,
			document.documentElement ? document.documentElement.clientWidth : 0,
			document.body ? document.body.clientWidth : 0
		);
}

function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}

function getBrowserInfo()
{
	return {clientHeight:f_clientHeight(), scrollBarPos:f_scrollTop()};
}

function getLocation()
{
	return window.location.href;
}

function setLocation(value)
{
	window.location.href = value;
}

function cumulativeOffset(element)
{
	var valueT = 0, valueL = 0;
	do
	{
		valueT += element.offsetTop || 0;
		valueL += element.offsetLeft || 0;
		element = element.offsetParent;
	} while (element);
	return [valueL, valueT];
}

var HtmlContent = {
	data: {},
	holder: null,
	fbApid: '126226044067291',
	
	create: function(id, left, top, w, h, content) {
		
		var url = encodeURIComponent(document.location);
		content = content.replace('{$URL}', url).replace('{$fbapid}', HtmlContent.fbApid);
		iframe = !/^\s*<iframe.*iframe>\s*$/.test(content);
		
		var el = CB.createEl(iframe ? 'iframe' : 'div', id);
		el.frameBorder = '0';
		el.allowTransparency = '1';
		
		CB.geom(el, (f_clientWidth() - 800)/2 + left, top, w, h);

		CB.$('flashOuter').appendChild(el);

		if (iframe)
		{
			var d = el.contentWindow.document;
			d.open(); 
			d.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">' +
					'<html><head></head><body style="margin:0">' + content + '</body></html>'); 
			setTimeout('document.getElementById("' + id + '").contentWindow.document.close()', 2000);
		}
		else
			el.innerHTML = content;
		
		
		HtmlContent.data[id] = {el: el, left: left, top: top, w: w, h: h};
	},
	
	clear: function() {
		for (var id in HtmlContent.data)
			CB.$('flashOuter').removeChild(HtmlContent.data[id].el);
		HtmlContent.data = {};
	},
	
	hideAll: function(value) {
		for (var id in HtmlContent.data)
			HtmlContent.data[id].el.style.display = value ? 'none' : 'block';
	},
	
	fixPos: function()
	{
		for (var id in HtmlContent.data)
		{
			var model = HtmlContent.data[id];
			CB.geom(model.el, (f_clientWidth() - 800)/2 + model.left, model.top, model.w, model.h);
		}	
	}
	
};

var SecureDownload = {
		
		post: function(actionUrl, data) 
		{
			var frm = document.createElement('form');
			frm.setAttribute('method', 'post');
			frm.setAttribute('action', actionUrl);
			frm.setAttribute('style', 'padding:0; margin:0;');
			frm.setAttribute('target', '_blank');
			document.body.appendChild(frm);
			
			for (var key in data)
			{
				var inp = document.createElement('input');
				inp.setAttribute('type', 'hidden');
				inp.setAttribute('name', key);
				inp.setAttribute('value', data[key]);
				frm.appendChild(inp);
			}
			
			frm.submit();
			document.body.removeChild(frm);
		}
	};

//window.setInterval('getBrowserInfo()', 500);
