var swfObj;
var flashSiteURL="";
var flashContent;
var flashContentWrapper;
var flashBg;
var flashClose;

var flashWidth;
var flashHeight;

var oldScrollX=0;
var oldScrollY=0;
var browserWidth=0;
var browserHeight=0;

//===========================================================
function setSiteURL( url ){
	flashSiteURL = url;
}

//===========================================================
function initFlashLayer(){
	flashContentWrapper = document.getElementById("flashContentWrapper");
	flashContent = document.getElementById("flashContent");
	flashBg = document.getElementById("flashBg");
	flashClose = document.getElementById("flashClose");
	
	flashBg.onclick = hideFlash;
	
	fixMacOpacity();
}

//===========================================================
function showFlash(filename, width, height, flashVersion, opaque, fullscreen){
	if(flashContent==null) initFlashLayer();
	
	if(this["pageTracker"]!=null) pageTracker._trackPageview('/showflash/'+filename);
	
	filename = flashSiteURL + filename + "?rid="+Math.random();
	
	if(width==null)			width=990;
	if(height==null)		height=600;
	if(flashVersion==null)	flashVersion="9.0.0";
	if(opaque==null)		opaque=true;
	if(fullscreen==null)	fullscreen=false;
	
	// fullscreen doesn't work with wmode==transparent
	if( fullscreen && !opaque ) opaque = true;
	
	flashBg.style.display = "block";
	flashClose.style.display = "block";
	
	var attributes = {};
	attributes.data = filename;
	attributes.width = width;
	attributes.height = height;
	
	var params = {};
	if(!opaque && !isFFonMac())	params.wmode = "transparent";
	else						params.wmode = "gpu";
	params.allowFullScreen = fullscreen ? "true":"false";
	flashContent = swfobject.createSWF(attributes, params, "flashContent");
	
	flashWidth = width;
	flashHeight = height;
	
	oldBodyScrollX = document.body.scrollLeft;
	oldElementScrollX = document.documentElement.scrollLeft;
	
	oldBodyScrollY = document.body.scrollTop;
	oldElementScrollY = document.documentElement.scrollTop;
	
	repositionFlash();
	window.onresize = repositionFlash;
	//window.onscroll = repositionFlashBg;
}

function hideFlash(){
	flashBg.style.display = "none";
	flashClose.style.display = "none";
	
	flashContent = null;
	flashContentWrapper.innerHTML = "<div id='flashContent'></div>";
	
	window.onresize = window.onscroll = null;
}

// positioning
function repositionFlashBg(){
	flashBg.style.left = 0;
	flashBg.style.top = 0;
	
	flashBg.style.width = 0;
	flashBg.style.height = 0;
	
	flashBg.style.width = document.body.scrollWidth+"px";
	flashBg.style.height = 100 + document.body.scrollHeight+"px";
}

function repositionFlash(){
	repositionFlashBg();
	
	var left = document.body.scrollLeft + document.documentElement.scrollLeft;
	var top = document.body.scrollTop + document.documentElement.scrollTop;
	
	updateBrowserSize();
	
	var x = left+(browserWidth-flashWidth)*0.5;
	var y = top+(browserHeight-flashHeight)*0.5;
	
	flashContent.style.left = x+"px";
	flashContent.style.top = y+"px";
	
	flashClose.style.left = Number(x) + Number(flashWidth) - Number(flashClose.offsetWidth)+"px";
	flashClose.style.top = Number(y) - Number(flashClose.offsetHeight)+"px";
}

function updateBrowserSize() {
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		browserWidth = window.innerWidth;
		browserHeight = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		browserWidth = document.documentElement.clientWidth;
		browserHeight = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		browserWidth = document.body.clientWidth;
		browserHeight = document.body.clientHeight;
	}
}

function isFFonMac() {
	var userAgent = navigator.userAgent.toLowerCase();
	return (userAgent.indexOf('mac') != -1 && userAgent.indexOf('firefox')!=-1);
}

function fixMacOpacity() {
	var userAgent = navigator.userAgent.toLowerCase();
	if(isFFonMac()) {
		flashBg.style.backgroundImage = "url(/img/flashBg.png)";
		flashBg.style.backgroundRepeat = "repeat";
	}
}