window.onload = initialize;

function initialize() {
	
	dhtmlHistory.initialize();	
	dhtmlHistory.addListener(historyChanged);
	//var initialLocation = location.hash;
 	//alert("initialLocation:"+initialLocation);

}


function initHistory(pForce){
	
	
	if(pForce){
		log("forced initHistory  from browser");
	}
	else{
		log("forced initHistory  from flash");
	}
	
	var hashValue = stripHashSymbol(window.location.hash);
	
	//alert("initHistory:"+hashValue);
	
	getFlashMovie().onInitHistory(hashValue);

}



function historyChanged(newLocation, historyData) {
	//alert("historyChanged:"+newLocation+",historyData:"+historyData);	
	
	updateFlashHistory(newLocation);

}



function updateFlashHistory(pHash){
	//alert("updateFlashHistory:"+pHash);
	log("updateFlashHistory:"+pHash);
	
	if(getFlashMovie() != null){
		log("external interface call on hash changed.");
		getFlashMovie().onHashChange(pHash);
	}
	
}



function setDeepLink(newLocation, historyMessage){
	log("setDeepLink-->newLocation:"+newLocation+", historyMessage:" + historyMessage);
	
	var historyData = {message:historyMessage};
  	updateWindowStatus(newLocation, historyData);
	dhtmlHistory.add(newLocation, historyData);
	
	
}



function getDeepLink(){
	return location.hash;
}





function setTitle(pTitle){
	log("setTitle:"+pTitle);
	//document.title = pTitle;
}


function getTitle(){
	log("getTitle:"+document.title);
	return document.title;
}



function updateWindowStatus(newLocation, historyData){
	log("updateWindowStatus");
	
	var historyMessage;
	if (historyData != null){
		historyMessage = historyData.message;
		//document.title = historyData.message;
	}
	else {
		historyMessage = "<i>No History Data - init history</i>";
		//document.title = "Firefly Group";
	}    
	

}





function stripHashSymbol(pHash){
	var hashValue = pHash;
	var r;
	if (hashValue === null || hashValue === undefined) {
		r = null;
	}
	else if (hashValue === "") {
		r = "";
	}
	else if (hashValue.length == 1 && hashValue.charAt(0) == "#") {
		r = "";
	}
	else if (hashValue.length > 1 && hashValue.charAt(0) == "#") {
		r = hashValue.substring(1);
	}
	else {
		r = hashValue;
	}
	return r;
}



function getFlashMovie() {

	var flashElement = null;
	
	if (navigator.appName.indexOf("Microsoft") > -1) {
		flashElement = window["mainFlash"];
	} else {
		flashElement = document["mainFlash"];
	}
	
	return flashElement;
}





/**************************
* popup browser control
**************************/

function openBrowserWindow(href, width, height, noscrollbars){
	//alert("openBrowserWindow called.");
	
	//alert("width:"+width);
	//alert("height:"+height);
	
	if(width == null){
		width = 800;
	}
	
	if(height == null){
		height = 600;
	}
	
	if(noscrollbars != false){
		_scrollbars = 0;
	}
	else{
		_scrollbars = 1;
	}
	
	
		
	try{
		//alert("here2");
		
		//popWin = window.open("portfolio/bs_viral/files/bs_viral.html","popWin","location=0,status=0,scrollbars=0,width=500,height=600");
		//popWin = window.open("portfolio/ESC_BANNER_NEWCASTLE/files/index.html","popWin2","location=0,status=0,scrollbars=0,width="+width+",height="+height);
		//popWin = window.open(href,"popWin","location=0,status=0,scrollbars=0,width="+width+",height="+height);
		
		if(window.screen){
			var xMax = screen.width;
			var yMax = screen.height;
		}
		else if(window.outerWidth){
			var xMax = window.outerWidth;
			var yMax = window.outerHeight;
		}
		else{
			var xMax = 1024;
			var yMax = 758;
		}
		
		var xOffset = (xMax - width)/2;
		var yOffset = (yMax - height)/2;
		 
		
		
		popWin = window.open(href,"popWin","location=0,status=0,scrollbars="+_scrollbars+",width="+width+",height="+height+",left="+xOffset+",top="+yOffset);
		//popWin.moveTo(xOffset,yOffset);
		popWin.window.focus();
		
		
		
	}
	catch(e){
		alert("The popup window has failed to open. Please disable all popup blocker when viewing this site.");	
	}
	
	
}



function openFullBrowserWindow(href){
	//alert("openFullBrowserWindow called.");
	
	try{
		//alert("here2");
		popWin = window.open(href,"popWin");
		//popWin.moveTo(xOffset,yOffset);
		popWin.window.focus();
		
	}
	catch(e){
		alert("The popup window has failed to open. Please disable all popup blocker when viewing this site.");	
	}
	
	
}





/********************************
* logging 
*******************************/

function log(msg) {
	//var logNode = document.getElementById("logWin");
	//var content = "<p>" + msg + "</p>" + logNode.innerHTML;
	//logNode.innerHTML = content;
}
