/* AJAX-bibliotek för Det Tysta Landet v. 0.1 */

popupsOnPage	= new Array();
allPopups 		= new Array();	


function initImage () {
	setTimeout("changeHeaderImage('PI_VadArKommunism.jpg')", 550);
}

function opacity(id, opacStart, opacEnd, millisec) { 
    //speed for each frame 
    var speed = Math.round(millisec / 100); 
    var timer = 0; 

    //determine the direction for the blending, if start and end are the same nothing happens 
    if(opacStart > opacEnd) { 
        for(i = opacStart; i >= opacEnd; i--) { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } else if(opacStart < opacEnd) { 
        for(i = opacStart; i <= opacEnd; i++) 
            { 
            setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed)); 
            timer++; 
        } 
    } 
} 


//change the opacity for different browsers 
function changeOpac(opacity, id) { 
    var object 			= document.getElementById(id).style; 
    object.opacity 		= (opacity / 100); 
    object.MozOpacity 	= (opacity / 100); 
    object.KhtmlOpacity = (opacity / 100); 
    object.filter 		= "alpha(opacity=" + opacity + ")"; 
} 

front = 1;


function changeHeaderImage (headImage) {
	
	var hImage;
		
	hImage = "img/" + headImage;
	
	if (hImage == "img/undefined" || hImage == "img/")
		hImage = "PI_VadArKommunism.jpg";
	
	//alert (hImage);
	
	initMe(hImage);
}


function initMe(prmImage){	
	
	//alert(prmImage);
	//S? variable
	window.document["top"].SetVariable("myImage", prmImage);
	//g?ill reload
	var flashMovie=getFlashMovieObject("top")
	//alert(window.document["top"].GetVariable("myImage"));
	flashMovie.GotoFrame(5);
}


function getFlashMovieObject(movieName)
	{
	  if (window.document[movieName]) 
	  {
		  return window.document[movieName];
	  }
	  if (navigator.appName.indexOf("Microsoft Internet")==-1)
	  {
		if (document.embeds && document.embeds[movieName])
		  return document.embeds[movieName]; 
	  }
	  else // if (navigator.appName.indexOf("Microsoft Internet")!=-1)
	  {
		return document.getElementById(movieName);
	  }
}


function ajaxSearch (searchString) {
	
	/* Ersätt svenska tecken eftersom dess inte hanteras 
	   önskvärt av Firefox och Safari */
	searchString = searchString.replace("å",	"*aring*");
	searchString = searchString.replace("ä",	"*auml*");
	searchString = searchString.replace("ö",	"*ouml*");
	searchString = searchString.replace("Å",	"*Aring*");
	searchString = searchString.replace("Ä",	"*Auml*");
	searchString = searchString.replace("Ö",	"*Ouml*");
	
	document.getElementById('page_container').className 		= 'page_hide';
	document.getElementById('progress_indicator').className	= 'page_show';
	
	theURL	= "generators/search.php?search=" + searchString;
	
	loadXMLDoc(theURL);
	
	if (!window.ActiveXObject) {
		setTimeout("foo()", 1000);
	}
	
	document.getElementById('progress_indicator').className 	= 'page_hide';
	document.getElementById('page_container').className 		= 'page_show';
}


function addPopupsToPage (	page, popups ) {

	popupsOnPage[page] = popups;
}


function arrayTest () {

	for (i = 1; i < 4; i ++)
	{
		try {
			//alert (popupsOnPage[i][0]);
		}
		catch(fel) {}
	}
}

				
/*****************************************/
/* Visar en sida med tillh?de popuper */
/*****************************************/								
function displayPage (	pageToDisplay, 
								pagesTotal,
								popupArray,
								allPopups)

{
	currentPage = pageToDisplay;
		
	/* Visa sidan */
	/* ********** */
	
	for (n = 1; n <= pagesTotal; n++) {
		
		pageName = "page" + n;
		
		//alert (pageName);
		
		goToPage = "goToPage" + n;
		
		if (n == pageToDisplay) {
			document.getElementById(pageName).className='show_p';
			document.getElementById(goToPage).className='page_sel';
		}
		else {
			document.getElementById(pageName).className='hide_p';
			document.getElementById(goToPage).className='page_unsel';
		}
		
		
	}
	
	/* Uppdatera sidnummer och n?a-/f??del?ar */
	/* *********************************************** */
	
	if (pageToDisplay == 1) {
		document.getElementById('prevPage').className  = 'hide_p';
	}
	else {
		document.getElementById('prevPage').className  = 'show_p';
		document.getElementById('prevPage').visibility='hidden';
		document.getElementById('prevPage').visibility='visible';
	}
	if (pageToDisplay == pagesTotal) {
		document.getElementById('nextPage').className  = 'hide_p';
	}
	else {
		document.getElementById('nextPage').className  = 'show_p';
	}
	
	// Anpassa höjden på scrollkolumnen till höjden på innehållet
	//document.getElementById('scrollColumn').Height = document.getElementById('main_page_window');
	
}



/*****************************************/
/* Visar en bild i ett galleri           */
/*****************************************/								
function displayImage (	pageToDisplay, 
								pagesTotal,
								placement,
								imageArray)

{
	currentPage = pageToDisplay;

	nextPage = "'nextPage_"  + placement + "'";
	prevPage = "'prevPage_"  + placement + "'";
	
	imageName = "img" + currentPage;
	
	document.getElementById(imageName).src = "img/GalleryImages/" + imageArray[currentPage - 1];
	
	//alert ("img/GalleryImages/" + imageArray[currentPage - 1]);		

	
	/* Visa sidan */
	/* ********** */
	
	for (n = 1; n <= pagesTotal; n++) {
		
		pageName = "page" + n;
		goToPage = "goToPage" + n;
		
		if (n == pageToDisplay) {
				
			//alert (pageName);
			
			document.getElementById(pageName).className='page_show';
			//document.getElementById(goToPage).className='page_sel';
		}
		else {
			document.getElementById(pageName).className='page_hide';
			//document.getElementById(goToPage).className='page_unsel';
		}
	}
	
	
	/* Uppdatera sidnummer och n?a-/f??del?ar */
	/* *********************************************** */
	
	/* FIXME: Det h?m?e g?tt g?p?tt snyggare s? */
	
	//alert (placement);
	
	if (pageToDisplay == 1) {
		document.getElementById('prevPage_up').className = 'page_hide';
		document.getElementById('prevPage_down').className = 'page_hide';
	}
	else {
		document.getElementById('prevPage_up').className = 'page_show';
		document.getElementById('prevPage_down').className = 'page_show';
	}
	
	if (pageToDisplay == pagesTotal) {
		document.getElementById('nextPage_up').className = 'page_hide';
		document.getElementById('nextPage_down').className = 'page_hide';
	}
	else {
		document.getElementById('nextPage_up').className = 'page_show';
		document.getElementById('nextPage_down').className = 'page_show';
	}
}


/*****************************************/
/* Visar en bild i ett galleri           */
/*****************************************/								
function displayImage2 (	pageToDisplay, 
								pagesTotal,
								placement,
								imageArray)

{
	currentPage = pageToDisplay;

	nextPage = "'nextPage_"  + placement + "'";
	prevPage = "'prevPage_"  + placement + "'";
	
	imageName = "img" + currentPage;
	
	document.getElementById(imageName).src = "img/GalleryImages/" + imageArray[currentPage - 1];
	
	//alert ("img/GalleryImages/" + imageArray[currentPage - 1]);		

	
	/* Visa sidan */
	/* ********** */
	
	for (n = 1; n <= pagesTotal; n++) {
		
		pageName = "page" + n;
		goToPage = "goToPage" + n;
		
		if (n == pageToDisplay) {
				
			//alert (pageName);
			
			document.getElementById(pageName).className='page_show';
			//document.getElementById(goToPage).className='page_sel';
		}
		else {
			document.getElementById(pageName).className='page_hide';
			//document.getElementById(goToPage).className='page_unsel';
		}
	}
	
}


function loadXMLDoc(url) 
{					
	if (window.XMLHttpRequest) {
	req = new XMLHttpRequest();
	req.open("GET", url, false);
	req.send(null);
	
	// Firefox anv?er inte onreadystatechange() f?ynkrona f??ingar
	
	// Kolla om f??ingen var OK
	if (req.status == 200) {
		document.getElementById('page_container').innerHTML 		= req.responseText;
		
		// Visa sidinneh?
		document.getElementById('progress_indicator').className 	= 'page_hide';
		document.getElementById('page_container').className 		= 'page_show';
	}
	
	// Internet Explorer > 5.0
	} else if (window.ActiveXObject) {
		req = new ActiveXObject("Microsoft.XMLHTTP");
		if (req) {
			req.onreadystatechange = processReqChange;
			req.open("GET", url, true);
			req.send();
		}
	}
	
	pageTracker._trackPageview(url);
}


function processReqChange() 
{
	// XMLHttpRequest.readyState = 4 => Redo
	if (req.readyState == 4) {
		
		// Kolla om f??ingen var OK
		if (req.status == 200) {
			document.getElementById('page_container').innerHTML 		= req.responseText;
			
			// Visa sidinneh?
			document.getElementById('progress_indicator').className 	= 'page_hide';
			document.getElementById('page_container').className 		= 'page_show';
			
		} else {
			alert("Ett fel uppstod n?sidan skulle l?s in:\n" + req.statusText);
		}
	}
	else {
	}
}


function foo() {
	document.getElementById('progress_indicator').className 		= 'page_hide';
	document.getElementById('page_container').className 			= 'page_show';
}


function adaptColumnHeights () {
	//Anpassa höjden på scrollkolumnen till höjden på innehållet
	//alert("Före: " + document.getElementById('scrollColumn').style.height);
	
	var browserName=navigator.appName; 
	
	if (browserName == "Netscape") { 
		cOffset = 1020;
		sOffset = 35;
		tOffset = 25;
	}
	else { 
		if (browserName == "Microsoft Internet Explorer")	{
		cOffset = 10;
		sOffset = 25;
		tOffset = 25;
		wOffset = 50;
		}
		else {
		cOffset = 60;
		sOffset = 25;
		tOffset = 25;
		}
	}
	
	var columnHeight 		= document.getElementById('main_content_container').offsetHeight + cOffset + "px";
	var scrollbarHeight 	= document.getElementById('main_content_container').offsetHeight + sOffset + "px";
	var trackHeight 		= document.getElementById('main_content_container').offsetHeight + tOffset + "px";
	var windowHeight 		= document.getElementById('main_content_container').offsetHeight + wOffset + "px";
	
	document.getElementById('scrollColumn').style.height 	= columnHeight;
	document.getElementById('wn1').style.height 				= windowHeight;
	document.getElementById('holder1').style.height 		= windowHeight;
	document.getElementById('scrollbar1').style.height 	= scrollbarHeight;
	document.getElementById('track1').style.height 			= scrollbarHeight;
	
	//document.getElementById('dragBar1').style.height = scrollbarHeight;
	//alert("Efter: " + document.getElementById('scrollColumn').style.height);
}

/* BEGIN: RSH */

/** A function that is called whenever the user
    presses the back or forward buttons. This
    function will be passed the newLocation,
    as well as any history data we associated
    with the location. */
function handleHistoryChange(newLocation,
                             historyData) {
  // use the history data to update our UI
  updateUI(newLocation, historyData);                           
}

/** A simple method that updates our user
    interface using the new location. */
function updateUI(newLocation,
                  historyData) {
  var output = document.getElementById("output");
  
  // simply display the location and the
  // data
  var historyMessage;
  if (historyData != null)
    historyMessage = historyData.message;
    
  var message = "New location: "
                + newLocation 
                + ", history data="
                + historyMessage;
  
  output.innerHTML = message;
  //this.location = "?" + newLocation;
  
  //alert (newLocation);
  
  if (newLocation != "" && newLocation != null && newLocation != "D") {
  	eval (newLocation);
  }
  
  //if (newLocation != "" && newLocation != null)
  //	this.location="?" + newLocation;
}

/* END: RSH */

function loadPage (theID, theHeaderImage) {
	
	try {
		document.getElementById('colouredBar').style.background = getFooterColourByHeaderImage (theHeaderImage);
	}
	catch (err) {
	}
	
	//baseURL 	= "http://www.upplysningomkommunismen.se/www.thesilentland.com/";
	//theURL 	= baseURL + "generators/main_content.php?content=" + theID;
	stuffToLoad = "content=" + theID;
	theURL 	= "generators/main_content.php?" + stuffToLoad;
		
	document.getElementById('page_container').className 		= 'page_hide';
	document.getElementById('progress_indicator').className	= 'page_show';
	
	setTimeout("loadXMLDoc('" + theURL + "')",550);
	
	if (!window.ActiveXObject) {
		setTimeout("foo()", 1000);
	}
	
	currentPage = 1;	
	
	//dhtmlHistory.add(stuffToLoad, {message: 'hello world'})
	
	if (theHeaderImage != "") {
		setTimeout("changeHeaderImage ('" + theHeaderImage + "')", 500);
	}	
}


function selectMenuItem (menuItem) {
	//Sätt variabel
	window.document["dtl_menu"].SetVariable("selectitem", menuItem);
	//Gå till reload
	var flashMovie2=getFlashMovieObject("dtl_menu");
	flashMovie2.GotoFrame(3);
}


/* ***************************************************************
	
	loadPageSel

	- Laddar inneh? (content) med 	id 		= theID 
	- ?ar toppbild med 				filnamn 	= theHeaderImage
	- V?er menyalternativ med 		id 		= theMenuItem
	
   *************************************************************** */
function loadPageSel (theID, theHeaderImage, theMenuItem) {
	loadPage (theID, theHeaderImage);	
	selectMenuItem (theMenuItem);
	
	hFunc = "loadPageSel ('" + theID + "', '" + theHeaderImage + "', '" + theMenuItem + "');";
	// hFunc = "CHOPP";
	
	loadFeatures (theID, "content");	
	// Avkommentera för att aktivera DHTML-historia
	//dhtmlHistory.add(hFunc, {message: "history"})
}


function loadGallery (theID, theHeaderImage) {
	
	setTimeout("loadFeatures (" + theID + ", 'gallery');", 550);
	
	//alert(theID);

	theURL = "generators/gallery.php?feature=" + theID;
	
	document.getElementById('page_container').className 		= 'page_hide';
	document.getElementById('progress_indicator').className	= 'page_show';
	
	//setTimeout("alert('hello')",50);
	
	setTimeout("loadXMLDoc('" + theURL + "')",550);
	
	setTimeout("changeHeaderImage ('" + theHeaderImage + "')", 500);
	
	currentPage = 1;
}


function loadGallerySel (theID, theHeaderImage, theMenuItem) {
	loadGallery (theID, theHeaderImage);
	selectMenuItem (theMenuItem);
	//alert ("selectMenuItem (" + theMenuItem + ")");
	hFunc = "loadGallerySel ('" + theID + "', '" + theHeaderImage + "', '" + theMenuItem + "');";
	
	// Avkommentera för att aktivera DHTML-historia
	//dhtmlHistory.add(hFunc, {message: hFunc})
}


function IEInnerFix (myJuice, elName) {
	var newdiv = document.createElement("div");
	newdiv.innerHTML = myJuice;
	var container = document.getElementById(elName);
	container.appendChild(newdiv);
}


function changeText (theText) {
	//document.getElementById('WEE').innerHTML = "sdlfkjsdlkskldj";
	IEInnerFix (theText, 'WEE');
	//alert ("sddlskkl");
	//var parent = WEE.parent; 
	//caption	.appendChild(WEE); 
	//parent.appendChild(caption);
	
	//window.document['WEE'].innerHTML = "sdlfkjsdlkskldj";
}


function loadXMLDoc2(url) 
{	
	if (window.XMLHttpRequest) {
	req2 = new XMLHttpRequest();
	req2.open("GET", url, false);
	req2.send(null);
	
	// Firefox använder inte onreadystatechange() för synkrona förfrågingar
	
	// Kolla om förfrågan var OK
	if (req2.status == 200) {
		document.getElementById('WEE').innerHTML = req2.responseText;
		//setTimeout("changeText(req2.responseText)",550);
		//initScrollLayers()
	}
	
	// Internet Explorer > 5.0
	} else if (window.ActiveXObject) {
		req2 = new ActiveXObject("Microsoft.XMLHTTP");
		if (req2) {
			req2.onreadystatechange = processReqChange2;
			req2.open("GET", url, true);
			req2.send();
		}
	}
}


function processReqChange2() 
{
	// only if req shows "complete"
	if (req2.readyState == 4) {
		// only if "OK"
		if (req2.status == 200) {
		// ...processing statements go here..
		//document.getElementById('lyr1').innerHTML = req2.responseText;
		//initScrollLayers()
		//document.getElementById('progress_indicator').className 			= 'page_show';
		//document.getElementById('features').className 			= 'page_show';
		document.getElementById('WEE').innerHTML = req2.responseText;
		} else {
			alert("There was a problem retrieving the XML data:\n" + req.statusText);
		}
	}
	else {
	}
}

function loadFeatures (contentID, contentType) {
	
	//document.getElementById('features').className 					= 'page_hide';
	//document.getElementById('progress_indicator').className 			= 'page_show';
	
	//setTimeout("alert('hello')",50);
	
	theURL = "generators/features.php?id=" + contentID + "&type=" + contentType;
	
	//alert (theURL);
	
	setTimeout("loadXMLDoc2('" + theURL + "')",550);
	
}


	//function loadFeatures (contentID, myType) {
	
	//document.getElementById('features').className 					= 'page_hide';
	//document.getElementById('progress_indicator').classN