var d				= document
var winModifier		= 'daChio'
var minWidth		= 800 //1000
var minHeight		= 606
var orgWidth		= 0
var orgHeight		= 0

function documentWidth() {
	return (d.all) ? ((d.documentElement.clientWidth)  ? d.documentElement.clientWidth  : d.body.clientWidth) : window.innerWidth
}

function documentHeight() {
	return (d.all) ? ((d.documentElement.clientHeight) ? d.documentElement.clientHeight : d.body.clientHeight) : window.innerHeight
}

function flashVersionGet() { // part of http://jquery.lukelutman.com/plugins/flash
	if (typeof ActiveXObject != 'undefined') {
		try {
			// avoid fp6 minor version lookup issues, see: http://blog.deconcept.com/2006/01/11/getvariable-setvariable-crash-internet-explorer-flash-6/
			var axo = new ActiveXObject('ShockwaveFlash.ShockwaveFlash.6');
			try { axo.AllowScriptAccess = 'always';	}
			catch(e) { return parseInt('6,0,0')}
		} catch(e) {}
		return parseInt(new ActiveXObject('ShockwaveFlash.ShockwaveFlash').GetVariable('$version').replace(/\D+/g, ',').match(/^,?(.+),?$/)[1]);
	}
	else { // other browsers
		try {
			if (navigator.mimeTypes["application/x-shockwave-flash"].enabledPlugin)
				return parseInt((navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]).description.replace(/\D+/g, ",").match(/^,?(.+),?$/)[1]);
		} catch(e) {}
	}
	return parseInt('0,0,0');
}

function getQueryStringAsArr(loc) {
	if (loc.charAt(loc.length - 1) == '#')
		loc = loc.substr(0, loc.length - 1)
	var returnArr	= []
	var qs			= (loc.indexOf('?') != -1) ? loc.substr(loc.indexOf('?') + 1) : loc
	var attrArr		= qs.split('&')
	for(var i=0, j=attrArr.length; i<j; i++) {
		var splitArr = attrArr[i].split('=')
		returnArr[splitArr[0]] = splitArr[1]
	}
	return returnArr
}

function getValidQueryString() {
	var qsAdd	= ''
	var qsArr	= getQueryStringAsArr(window.location.href)
	if (qsArr['jumper'] != null && /^[\w\d_\/]+$/.test(qsArr['jumper']))
		qsAdd += '&jumper=' + escape(qsArr['jumper'])
	return qsAdd
}

function startChio() {
	var qsArr			= getQueryStringAsArr(window.location.href)
	var isPopUp			= (qsArr['popUp'] != null)
	var viewportWidth	= orgWidth	= documentWidth()
	var viewportHeight	= orgHeight	= documentHeight()
	var qs				= getValidQueryString()
	if (isPopUp || (viewportWidth >= minWidth && viewportHeight >= minHeight)) {
		document.getElementById('daBody').className = 'flash'
		var flashVerReq	= 8
		var scale		= isPopUp && (viewportWidth < minWidth || viewportHeight < minHeight)
		viewportWidth	= '100%'
		viewportHeight	= '100%'
		var flashSrc	= 'base.swf?chio=chips' + getValidQueryString()
		var flashvars	= {}
		var params		= {
							menu: false,
							allowfullscreen: true,
							bgcolor: '#000000'
						  }
		if (!scale)
			params.scale = 'noscale'

		var attributes	= {}
		swfobject.embedSWF(flashSrc, 'container', viewportWidth, viewportHeight, flashVerReq.toString(), 'expressInstall.swf', flashvars, params, attributes)
		return false
	} else {
		return startChioPopUp()
	}
}

function startChioPopUp() {
	var nUA			= navigator.userAgent
	var buggyIEWin	= (nUA.indexOf('MSIE') != -1 && nUA.indexOf('Windows') != -1)
	var buggyIEMac	= (nUA.indexOf('MSIE') != -1 && nUA.indexOf('Mac') != -1)
	var buggySafari	= (nUA.indexOf('afari') != -1)
	var qsAdd		= getValidQueryString()
	var offX		= 0
	var offY		= -38
	switch (true) {
		case (buggyIEMac):
			offY = -86
			break
		case (buggyIEWin):
			offX = -11
			break
	}
	wWidth		= screen.availWidth  + offX
	wHeight		= screen.availHeight + offY
	paramStr	= "dependent=no,location=no,menubar=no,resizable=yes,scrollbars=no,fullscreen=no,status=no,toolbar=no,left=0,top=0,screenX=0,screenY=0,width=" + wWidth + ",height=" + wHeight + ",outerWidth=" + wWidth
	daChioWin	= window.open("/?popUp=true&nocache=" + Math.round(Math.random()*100000) + qsAdd + window.location.hash, "chioWin", paramStr)
	if (!buggyIEMac && daChioWin != null && daChioWin.focus)
		daChioWin.focus()
	return false
}

function openWin(daPath, daW, daH, daX, daY) {
	var win = window.open(daPath, 'extWin', 'location=no,toolbar=no,status=no,scrollbars=no,left=' + daX + ',top=' + daY + ',screenX=' + daX + 'screenY=' + daY + ',width=' + daW + ',height=' + daH)
	if (win.focus)
		win.focus()
}

function openImpressum() {
		openWin('/popups/pop_chioInfo.php?jumper=impressum', 900, 740, (screen.availWidth/2 - 900/2), (screen.availHeight/2 - 740/2))
		return false
}

var gA = {
	add: function(path) {
		if (typeof pageTracker != 'undefined' && typeof gaAccount != 'undefined' && gaAccount != '') {
			if (gaAccount == 'UA-2541222-1')
				this.debug(path)
			pageTracker._trackPageview(path)
		}
		else {
			this.debug(path)
		}
	},

	debug: function(path) {
		if (typeof console == 'object')
			console.log('analytics-query: ' + ((path != '') ? path : window.location.pathname))
	}
}

function fireDebug(what) {
	if (typeof console == 'object')
		console.info(what)
}