var minimumWidth = 960;
var lastResizeHandler;
var viewPortSize;

function resizeFlashHandler(size) {
	var w, h;	
	var holder = document.getElementById('flashContainer');
	if (size && size != lastResizeHandler) {
		size = size.split(",");
		w = parseInt(size[0]);
		h = parseInt(size[1]);	
		
		if (h < viewPortSize[1]) {
			holder.style.height = "100%";	
		} else { 			
			holder.style.height = h+"px";
		}
		lastResizeHandler = size;
	}
	if (viewPortSize[0] < minimumWidth) {		
		holder.style.width = minimumWidth+"px";
	} else {
		holder.style.width = "100%";		
	}
}

function getViewportDimensions() {
	var viewportwidth;
	var viewportheight;
	
	if (typeof window.innerWidth != 'undefined') {
		viewportwidth = window.innerWidth,
		viewportheight = window.innerHeight
	} else if (typeof document.documentElement != 'undefined'
			 && typeof document.documentElement.clientWidth !=
			 'undefined' && document.documentElement.clientWidth != 0) {
		viewportwidth = document.documentElement.clientWidth,
		viewportheight = document.documentElement.clientHeight
	} else {
		viewportwidth = document.getElementsByTagName('body')[0].clientWidth,
		viewportheight = document.getElementsByTagName('body')[0].clientHeight
	}
	return [viewportwidth,viewportheight];
}

function getScrollXY() {
	var scrOfX = 0, scrOfY = 0;
	if( typeof( window.pageYOffset ) == 'number' ) {
		scrOfY = window.pageYOffset;
		scrOfX = window.pageXOffset;
	} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
		scrOfY = document.body.scrollTop;
		scrOfX = document.body.scrollLeft;
	} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
		scrOfY = document.documentElement.scrollTop;
		scrOfX = document.documentElement.scrollLeft;
	}
	return [ scrOfX, scrOfY ];
}

function flashMovie(id) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[id];
	} else {
		if (document[id]) {
			return document[id];
		} else {
			return document.getElementById(id);
		}
	}
}
window.onload = window.onresize = resize;
function resize() { 
	viewPortSize = getViewportDimensions();
	resizeFlashHandler();
};

window.onscroll = scrollHandler;

function scrollHandler() {
	//if (document.getElementById("flashContainer").style.display != "none") {
		flashMovie('flashContainerObject').scrollHandler(getScrollXY().join(','));
	//}
}



function flashScroll(dir) {
	if (dir == "top") {
		dir = "0,0";
	} else if (dir == "bottom") {
		var dim = getViewportDimensions();
		dir = "0," + (parseInt(document.getElementById('flashHolder').style.height.split("px")[0]) - dim[1]);
	}
	dir = dir.split(",");
	if (!dir[1]) { return; }
	window.scrollTo(dir[0],dir[1]);
}

function openPage(str,pre) {
	if (!pre) { pre = ''; }
	if (str.substr(str.length-5,5)  != ".html" && str.substr(str.length-1,1)  != "/") {
		str += "/";
	}
	
	if (str.substr(0,1) != "/") { 
		str = "/"+str;
	}
	if (str == "/home/") { str = "/"; }
	currentPage = str;
	window.location = pre+"#"+str;	
}

var currentPage = currentHash();

function currentHash() {
	var loc = document.location+"";
	loc = loc.split("?")[0];
	var hash = loc.split("#")[1];
	if (!hash || hash == "/") {
		hash = "/home/";	
	}
	return hash;
}

function checkNewPage() {
	var loc = document.location+"";
	var hash = loc.split("#")[1];
	var change = false;
	if (!hash || hash == "/") {
		hash = "/home/";	
	}
	if (currentPage != hash) {
		currentPage = hash;
		flashMovie("flashContainerObject").openPage(hash);
	}
}

function changeTitle(str) {
	if (str == '') {
		document.title = "kolnedra . com";
		return;	
	}
	document.title = str + " | kolnedra . com";
}

function pageTrack(page) {
	try {
		pageTracker._trackPageview(page);
	} catch (e) { }
}
