// static properties for main window
var sTimerID,sTimerRunning = false;
var sHistoryChap = null,sHistoryData = "";
var sActiveChap = 'UChp0';
var sActiveData = "Intro0.php";
var sOldTitle = "";
var sBrowser = navigator.userAgent;
var sConfirmedAlert = false;
var x = new String(document.location);
var sRoot = x.replace(/Frame.php/,"");

// generate next random number
function Random(range) {
	return( 1 + Math.floor(range * Math.random()) );
}

// replace spaces and &xxx; for php arguments
function MakeArgString(string) {
	var newStr, phpStr = new String(string);
	var rExp = /\s+/;

	newStr = phpStr.replace(rExp,"%20");
	rExp = /&/;
	phpStr = newStr.replace(rExp,"%26");
	return(phpStr);
}

// assign url names to chapter objects
function GetUrlBase(newChap) {
	var chap = "Intro";

	if( newChap == 'UChp1' ) chap = "Rental";
	if( newChap == 'UChp2' ) chap = "Screenings";
	if( newChap == 'UChp3' ) chap = "Archive";
	if( newChap == 'UChp4' ) chap = "Support";
	if( newChap == 'UChp5' ) chap = "Sitemap";

	return sRoot + chap;
}

// get current frame width
function GetFrameWidth(target) {
	if(document.all) w = target.document.body.clientWidth; // m$ xPloder
	else w = target.innerWidth; // NS, Moz, Opera
	if( isNaN(w) ) w = 640; // if everything fails
	return(w);
}

// get current frame height
function GetFrameHeight(target) {
	if(document.all) h = target.document.body.clientHeight; // m$ xPloder
	else h = target.innerHeight; // NS, Moz, Opera
	if( isNaN(h) ) h = 480; // if everything fails
	return(h);
}

// select a single chapter object on change
function DoNewChapter(newChap) {
	var target = top.kFrameSelect.document;

	if( sActiveChap == newChap ) return;	
	if( target.getElementById(sActiveChap) )
		target.getElementById(sActiveChap).style.display = 'none';

	// select new paragraph objects to this chapter
	sActiveChap = newChap;
	if( target.getElementById(newChap) ) {
		target.getElementById(newChap).style.display = 'block';
	} else sActiveChap = 'UChp0';
}

// select paragraph within chapter
function DoNewParagraph(newChap,newPar) {
	var	target = GetUrlBase(newChap) + eval(newPar);
	DoNewDataFrame(newChap,target + ".php");
}

// select paragraph within chapter with possible arguments
function DoNewArgParagraph(newChap,newPar,args) {
	var	target = GetUrlBase(newChap) + newPar.toString() + ".php";
	if( args != "" ) target += "?" + args;
	DoNewDataFrame(newChap,target);
}

// switch data frame and put current in history
function DoNewDataFrame(newChap,newUrl) {
	sHistoryData = sActiveData;
	sHistoryChap = sActiveChap;
	top.kFrameData.location.replace(newUrl);
	DoNewChapter(newChap);
	sActiveData = newUrl;
	top.kFrameData.focus();
	FillStatusBar("");
}

// switch to selected language
function SelectLanguage(newLangauage) {
	var arg = "?kDOM=0";
	if(document.getElementById) arg = "";
  top.window.location.replace("/" + newLangauage + "/Frame.php" + arg);
}

// (re)create and open window for messages
function CreateMsgWindow(windURL) {
	var status = "resizable=1,scrollbars=1,location=0,status=1,width=";
	var w = GetFrameWidth(this);
	var h = GetFrameHeight(this);
	var winID;

	status += w + ",height=" + h;
	winID = window.open(windURL.toString(),"kWindMessage",status);
	winID.focus();
}

// show flash animation in data frame
function MouseClickFlash(source,caption) {
	var target = top.kFrameData;
	var newLocation = "/Projects/Flash.php?";
	var width = GetFrameWidth(target);

	// create argument array
	newLocation += "width=" + width.toString();
	newLocation += "&src=" + source;
	newLocation += "&caption=" + caption;

	// fill data frames
	TitleLoad('Animation');
	target.location.replace(newLocation);
}

// open data frame with foto from gallery
function MouseClickGallery(source,back,album,index) {
	var target = top.kFrameData;
	var newLocation = "Album.php?";

	// get maximum width/height
	var width = GetFrameWidth(target);
	width += GetFrameHeight(target) / 10000;

	// create argument array
	newLocation += "kWidth=" + width.toString();
	newLocation += "&kSource=" + source;
	newLocation += "&kId=" + index.toString();
	newLocation += "&kBack=" + back.toString();
	newLocation += "&kAlbum=" + album.toString();

	target.location.replace(newLocation);
	self.status = "";
}

// (re)open message window and fill with given URL
function MouseClickLink(newURL) {
	CreateMsgWindow("http://" + newURL);
}

// show info on selected manual
function MouseClickManual(name) {
	DoNewDataFrame('UChp1',"/uk/Manuals/" + name + ".php");
}

// show info on selected project
function MouseClickPortfolio(name) {
	DoNewDataFrame('UChp3',"/uk/Portfolio/" + name + ".php");
}

// show selected portfolio
function MouseClickPortfolioArchive(name,year) {
	DoNewDataFrame('UChp3',"/uk/Portfolio/" + name + ".php?kYear=" + year);
}

// fill data frame with a given HTML file in Project folder
function MouseClickProject(name) {
	if( name.length == 0 ) name = 'NoProject';
	DoNewDataFrame('UChp2',"/uk/Projects/" + name + ".php");
}

// open corresponding review pages
function MouseClickReview(title,filmkrant) {
	if( title == "" ) return;
	var url = "/uk/Projects/Review.php?kFKid=" + filmkrant + "&kTitle="
	var phpStr = MakeArgString(title);

	self.status = "";
	CreateMsgWindow(url + phpStr);
}

// write string in status bar for download
function MouseOverParagraph(par) {
	return FillStatusBar("Paragraph: " + par);
}

// write string in status bar for download
function MouseOverChapter(chapter) {
	var x = GetUrlBase(chapter);
	return FillStatusBar("Chapter: " + x);
}

function MouseOverFlash() {
	return FillStatusBar("Flash: Show animation");
}

// write string in status bar for fotoalbum
function MouseOverGallery(caption) {
	return top.FillStatusBar("Gallery: " + caption);
}

// write string in status bar for links
function MouseOverLink() {
	return FillStatusBar("Link: Show site");
}

// write string in status bar for portfolio
function MouseOverPortfolio() {
	return FillStatusBar("Link: Archive of screenings");
}

// write 'project' string in status bar for links
function MouseOverProject() {
	return FillStatusBar("Project: Show program");
}

// write 'review' string in status bar for search
function MouseOverReview() {
	return FillStatusBar("Link: Find article");
}

// convert to real email address
function DoMail(address) {
	location.href = "mailto:" + address.replace(/\.at\./,"@");
}

// scroll data page to top
function DoTop() {
	top.kFrameData.window.scrollTo(0,0);
}

// adjust form to current situation
function DoAdjustForm(doc) {
	doc.getElementById("kFirst").focus();

	// remove current year from menu if not in datafile
	if( doc.getElementById("kDiff").value > 0 )
		doc.getElementById("kMenu").remove(1);
}

// go back to previous paragraph in history
function DoBack() {
	var tmpChap = sActiveChap;
	var tmpData = sActiveData;

	sActiveData = sHistoryData;
	top.kFrameData.location.replace(sHistoryData);
	DoNewChapter(sHistoryChap);
	sHistoryChap = tmpChap;
	sHistoryData = tmpData;
}

// event handler when window is resized
function DoReload() {
	top.kFrameFooter.location.reload();
	top.kFrameData.location.reload();
}

// make sure only one timer is running
function SetTimer(defProc,clickCount) {
	if( sTimerRunning ) clearTimeout(sTimerID);
	sTimerID = setTimeout(defProc,clickCount);
	sTimerRunning = true;
}

// write a string in status bar of current window and clear after 5 seconds
function FillStatusBar(msgString) {
	top.status = msgString;
	SetTimer("ClearStatusBar()",5000);
	return true;
}

// clear status bar of current window
function ClearStatusBar() {
	top.status = "";
	sTimerRunning = false;
	return true;
}

// adjust title frame
function TitleLoad(newTitle) {
	if( sOldTitle != newTitle ) {
		var el = top.kFrameTitle.document.getElementById('kTabTitle');
		el.rows[0].cells[0].innerHTML = newTitle;
		sOldTitle = newTitle;
	}
}

