var minimumWidth = 1000,
	lastResizeHandler = [];

/**
 * Function is called from the external interface in flash
 * It passes on the width and height of the flash element internally 
 * 
 * @param {int} width
 * @param {int} height
 */
function resizeFlashHandler(w, h) {
	lastResizeHandler = [w, h];
	var viewPortSize = getViewportDimensions(),
		container = document.getElementById('flashPlaceholder');
		
	if (w && h) {
		if (h < viewPortSize[1]) {
			container.style.height = "100%";	
		} else { 						
			container.style.height = h + "px";
		}
	}
	if (viewPortSize[0] < minimumWidth) {		
		container.style.width = minimumWidth + "px";
	} else {
		container.style.width = "100%";		
	}
	scrollHandler();
}

/**
 * Returns the width and height of the browser's viewport
 * 
 * @return {Array} [width, height]
 */
function getViewportDimensions() {
	var viewportwidth,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];
}

/**
 * Tell the flash element to scroll to a specific position
 */
function scrollHandler() {
	var scroll = getScrollXY();
	flashMovie('flashElement').flashScroll(scroll[0], scroll[1]);
}

/**
 * Event listeners
 */
window.onscroll = scrollHandler;
window.onload = 
window.onresize = function() { 
	resizeFlashHandler(lastResizeHandler[0], lastResizeHandler[1]);
}

/**
 * Returns the current scrollbar position (horizontal and vertical
 * 
 * @return {Array} [x, y]
 */
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];
}

/**
 * Returns the HTML flash object (<object> / <embed>)
 * 
 * @param {String} The HTML element ID
 * @return {Object} The flash object
 */
function flashMovie(id) {
	if (navigator.appName.indexOf("Microsoft") != -1) {
		return window[id];
	} else {
		if (document[id]) {
			return document[id];
		} else {
			return document.getElementById(id);
		}
	}
}
