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

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
	try { // ie
		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]);
	} catch(e) { // 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 scale		= isPopUp && (viewportWidth < minWidth || viewportHeight < minHeight)
		viewportWidth	= '100%'
		viewportHeight	= '100%'
		var flashSrc	= 'base.swf?chio=chips' + getValidQueryString()
		var flashStr	= '<object classid="CLSID:D27CDB6E-AE6D-11cf-96B8-444553540000" id="daFlashObj"\
								codebase="http://active.macromedia.com/flash' + flashVerReq + '/cabs/swflash.cab#version=' + flashVerReq + ',0,0,0" width="' + viewportWidth  + '" height="' + viewportHeight  + '">\
								<param name="movie" value="' + flashSrc + '">\
								<param name="quality" value="high">\
								<param name="menu" value="true">\
								<param name="autostart" value="true">\
								' + ((scale) ? '' : '<param name="scale" value="noscale">') + '\
								<param name="salign" value="m">\
								<param name="bgcolor" value="#980505">\
								<embed name="mainFlash" src="' + flashSrc + '"\
									quality="high"\
									id="daFlashObj"\
									autostart="true"\
									pluginspage="http://www.macromedia.com/shockwave/download/index.cgi?P1_Prod_Version=ShockwaveFlash"\
									type="application/x-shockwave-flash" width="' + viewportWidth  + '" height="' + viewportHeight  + '"\
									' + ((scale) ? '' : 'scale="noscale"') + '\
									salign="m"></embed>\
							</object></div>'
		var containerStr = '<div id="mainFlashDiv"></div>'
		storedBodySrc = document.getElementById('daBody').innerHTML
		document.getElementById('daBody').innerHTML			= containerStr
		document.getElementById('mainFlashDiv').innerHTML	= flashStr
		flashLoaded = true
		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, "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))
	}
}
