var acDiv, oLDiv, lB, aDiv, contDiv, headDiv, bodyH, viewW, viewH, x, y, fObj, path, mType;
var prevPos = 0;
var imgIsLoaded = true;

window.onresize = function(){ (fObj != undefined && imgIsLoaded) ? sizeNCenter() : centerLoader(); }

function init(){
	acDiv = document.getElementById('assetCont');
	oLDiv = document.getElementById('overlay');
	aDiv = document.getElementById('asset');
	contDiv = document.getElementById('content');
	headDiv = document.getElementById('header');
	if(typeof prettyPrint == 'function'){ prettyPrint(); }
}
addLoadEvent(init);

function showAsset($mType, $fObj){
	fObj = $fObj;
	mType = $mType;
	aOn();
	sizeNCenter();
	
	path = root+types[mType];
	
	switch(mType){
		case 0 : showIMG(); break;
		case 1 : showVID(); break;
		case 2 : showSWF(); break;
	}
}

// IMGs
function showIMG(img){
	aDiv.innerHTML = (img != undefined)
		? '<img src="'+ img +'"/>'
		: '<img src="'+ (path + iSizes[0] +'/'+ fObj.fName) +'"/>';
		
	if(img != undefined){
		imgIsLoaded = false;
		aOn();
		aDiv.style.display = 'none';
		var imgFile = new Image();
		imgFile.onload = CreateDelegate(imgFile, imgLoaded);
		imgFile.src = img;
		mType = 0;
	}else{
		sizeNCenter();
	}
	aDiv.onclick = aOff;
	aDiv.style.cursor = 'pointer';
	aDiv.title = "Cleeeek Me to Close";
}
function CreateDelegate(contextObject, delegateMethod){ return function(){ return delegateMethod.apply(contextObject, arguments); } }
function imgLoaded(){
	fObj = { w:this.width, h:this.height };
	imgIsLoaded = true;
	oLDiv.innerHTML = '';
	sizeNCenter();
}
// VIDs
function showVID(){
	var vidName = path + fObj.fName;
	var imgName = path + iSizes[0] +'/'+ fObj.fName.replace('.flv', '.jpg');
	
	path = path.split('posts/').shift() + 'js/';
	
	var flashvars = {name:vidName, imgName:imgName};
	if(fObj.userVars){ for(var i in fObj.userVars){ flashvars[i] = fObj.userVars[i]; } }
	var params = {base:'.', menu:'false', align:'tl', wmode:'opaque'};
	var attributes = {id:'assetSWF', name:'assetSWF'};
	swfobject.embedSWF(path+"vidPlayer.swf", "assetSWF", fObj.w, fObj.h, "10.0.0", path+"expressInstall.swf", flashvars, params, attributes);
}
// SWFs
function showSWF(){
	var swfFile = path + fObj.fName.replace('.swf','') + '/' + fObj.fName;
	
	path = path.split('posts/').shift() + 'js/';
	
	var flashvars = (fObj.userVars) ? fObj.userVars : {};
	var params = {base:'.', menu:'false', align:'tl', wmode:'opaque'};
	var attributes = {id:'assetSWF', name:'assetSWF'};
	swfobject.embedSWF(swfFile, "assetSWF", fObj.w, fObj.h, "10.0.0", path+"expressInstall.swf", flashvars, params, attributes);
}

// Asset On / Off
function aOn(){ 
	acDiv.style.display = 'block';
	oLDiv.style.height = '100%';
	prevPos = f_scrollTop();
	contDiv.style.display = 'none';
	headDiv.style.visibility = 'hidden';
	
	oLDiv.innerHTML = '<img id="loadBar" src="'+rootUrl+'/public/imgs/loadBar.gif"/>';
	
	lB = document.getElementById('loadBar');
	lB.style.position = 'relative';
	centerLoader();
	
	//if(fObj != undefined){ location.hash = types[mType].replace('s/', ''); }
}
function aOff(){ 
	contDiv.style.display = 'block';
	headDiv.style.visibility = 'visible';
	window.scrollTo(0, prevPos);
	
	aDiv.innerHTML = '';
	if(document.getElementById('assetSWF') == undefined){
		var newSWF = document.createElement('div');
		newSWF.id = 'assetSWF';
		aDiv.appendChild(newSWF);
	}
	aDiv.style.height = 0 + 'px';
	oLDiv.style.height = 0 + 'px';
	acDiv.style.display = 'none';
	
	aDiv.style.cursor = 'default';
	aDiv.title = '';
	aDiv.onclick = null;
	
	//if(fObj != undefined){ document.location.hash = '#'; }
}

function centerLoader(){
	//viewH = document.height;
	if(lB != undefined){
		viewH = document.body.clientHeight;
		lB.style.top = Math.round((viewH - 10)/2) + 'px';
	}
}

// Size & center the asset
function sizeNCenter(){
	bodyH = getDocHeight();
	//viewW = document.width;
	//viewH = document.height;
	viewW = document.body.clientWidth;
	viewH = document.body.clientHeight;
	
	var padding = 10;
	var addPadding = ((fObj.h + (padding*2)) >= bodyH) ? true : false;
	var oH = (addPadding) ? (fObj.h + (padding*2)) : bodyH;
	
	x = (fObj.w < viewW) ? Math.round((viewW - fObj.w)/2) + 'px' : padding + 'px';
	y = (fObj.h < viewH) ? Math.round((viewH - fObj.h)/2) + 'px' : padding + 'px';
	
	oLDiv.style.width = (fObj.w > viewW) ? (fObj.w + padding*2) + 'px' : '100%';
	oLDiv.style.height = oH + 'px';
	aDiv.style.left = x;
	aDiv.style.top = y;
	aDiv.style.width = fObj.w+'px';
	aDiv.style.height = fObj.h+'px';
	aDiv.style.display = 'block';
}

function getDocHeight() {
	var D = document;
	return Math.max(
    	Math.max(D.body.scrollHeight, D.documentElement.scrollHeight),
    	Math.max(D.body.offsetHeight, D.documentElement.offsetHeight),
    	Math.max(D.body.clientHeight, D.documentElement.clientHeight)
	);
}

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;
}
