// JavaScript Document
/**
 * Fortum scripts
 *
 * Provides basic functionality for Flash to HTML communication and a 
 * framework for dynamic layout of the Flash application   
 */

var flashHeight = 600;
var flashWidth = 960;

// Flash hook for resizing the Flash area
function resizeFlash(value){
	flashHeight = value; 
	document.getElementById('flash_frame').style.minHeight = value + 'px';
	document.getElementById('flash_element').minHeight = value;
	updateFlashSize();  
}

// Updates the Flash area to either fit the screen or the content  
function updateFlashSize() { 
	var windowHeight = document.body.clientHeight;
	var windowWidth = document.body.clientWidth;

	if (flashHeight < windowHeight) {
		document.getElementById('flash_frame').style.height = document.documentElement.clientHeight + 'px';
		document.getElementById('flash_element').height = '100%';
	}
	else { 
		document.getElementById('flash_frame').style.height = flashHeight + 'px';
		document.getElementById('flash_element').height = flashHeight;
	}

	if (flashWidth < windowWidth) {
		document.getElementById('flash_frame').style.width = '100%';
		document.getElementById('flash_element').width = '100%';
	}
	else { 
		document.getElementById('flash_frame').style.width = flashWidth + 'px';
		document.getElementById('flash_element').width = flashWidth;
	}			

	resolveScrollPosition();
}

//			
var scrollPosition = 0;
var windowHeight = 0; 			

// Calculates the scroll position and passes the value to Flash application 
function resolveScrollPosition() {
	var currentScrollPosition = Math.max(document.documentElement.scrollTop, document.body.scrollTop);  
	var scrollDelta = (currentScrollPosition - scrollPosition); 
	var flashObject = document.getElementById("flash_element");
	var windowHeightDelta = document.body.clientHeight - windowHeight;

	if ((scrollDelta != 0 || windowHeightDelta != 0) && flashObject != null && flashObject.setScrollPosition != null) {
		scrollPosition += scrollDelta;
		windowHeight += windowHeightDelta;
		flashObject.setScrollPosition(scrollPosition, windowHeight);
	}
}

function resetScrollPosition() {
	if (document.documentElement) document.documentElement.scrollTop = 0; 
	if (document.body) document.body.scrollTop = 0; 
}


/*
//
function resolveRealPath() {
	// 
	var baseURL = '/test'; 	// <?= $_SERVER['documentRoot']; ?>
	var documentURL = window.document.location.href;			
	var subPath = ''; 
	var urlPart = ''; 			
	var splitPosition;	
	var redirected; 
	
	//documentURL = "file:///C:/Projects/greatapes/fortum_sahkoauto/bin-debug/kysysahkoautosta/#/pagevars=0,1";
	
	splitPosition = documentURL.indexOf('#'); 
	
	if (splitPosition > -1) { 
		subPath = documentURL.substr(splitPosition + 1); 
		documentURL = documentURL.substr(0, splitPosition); 
	} 
	
	while(documentURL.lastIndexOf('/') == documentURL.length - 1) { 
		documentURL = documentURL.substr(0, documentURL.length - 1); 
	} 
	
	//alert(splitPosition + ", " + subPath + ", " + documentURL); 
	
	for(splitPosition = documentURL.lastIndexOf('/'); splitPosition > -1 && documentURL.lastIndexOf(baseURL) != (documentURL.length - baseURL.length); splitPosition = documentURL.lastIndexOf('/')) {
		urlPart = documentURL.substr(splitPosition); 
		documentURL = documentURL.substr(0, splitPosition); 
		subPath = urlPart + subPath; 
		//alert(splitPosition + ", " + subPath + ", " + documentURL);
	}
	
	documentURL = documentURL + "/#" + subPath;
	
 	redirected = (documentURL != window.document.location.href);
	
	if (redirected) { 
		window.document.location = documentURL;
	}
	return redirected;
}
*/

/*
// Flash hook for tracking page views 
function trackPageView(path) {
	path = "/sahkoauto/flash/" + path;
	//alert(path);	
	snoobi.trackPageView(path, path);
	pageTracker._trackPageview(path);	
}
*/
