/**********************************************************
 * Photo Album by Torstein H�nsi
 ***********************************************************/

var raameFoer = StrTHBorder1();
var raameEtter = StrTHBorder2();
var visning; // tildelt i toppen av contents
var sidaErFerdiglasta = false;
var startbilete = false;
var erIAutovisning = false;
var PicVar = 0;
var klarTilNesteSerie = false;
var AutoID;
var HovudArray = new Array;

//-----------------------------------------------------------------------------
// Object Fix 
function Fix(iVal, idVal, DatVal, PicVal, ThumbVal, TitVal, TxtVal, EngVal, FotografVal, RaameVal, width, height) { 
   	this.iVal = iVal
   	this.idVal = idVal
   	this.DatVal = DatVal 
   	this.PicVal = PicVal 
   	this.ThumbVal = ThumbVal 
   	this.TitVal = TitVal 
   	this.TxtVal = TxtVal 
   	this.EngVal = EngVal
	this.FotografVal = FotografVal
	this.RaameVal = RaameVal
	this.width = width;
	this.height = height;
} 

// Object PanoFix 
function PanoFix(idVal, DatVal, PicVal, TitVal, TxtVal) { 
   	this.idVal = idVal
   	this.DatVal = DatVal 
   	this.PicVal = PicVal 
   	this.TitVal = TitVal 
   	this.TxtVal = TxtVal
} 

last = PicVar - 1;
current = -1;
sistVist = -1;

var sjekkIDpaa = false;

//-----------------------------------------------------------------------------
function Forrige()	{
	if (visning != "panorama")	{
		if (erIAutovisning)	{
			AutoStopp();
		}
		ForrigeBilete();
	}
}
//-----------------------------------------------------------------------------	
function Neste() {
	if (visning != "panorama")	{
		if (erIAutovisning)	{
			AutoStopp();
		}
		NesteBilete();
	}
}
//-----------------------------------------------------------------------------
function Tom()	{}

//-----------------------------------------------------------------------------
/* Stoppar slideshow f�r visning */
function KlikkBilete(i) {
	AutoStopp();
	VisBilete(i);
}

//-----------------------------------------------------------------------------
/* Sjekkar at sida er klar til � ta imot bilete. Viss ei anna side er oppe
i hovudramma, g�r VisBilete og sjekkBiletvisning i loop heilt til main.php er
lasta og sjekkIDpaa er sett til true */
function VisBilete(nyttbilete)	{
	if (sidaErFerdiglasta)	{
		VisBilete2(nyttbilete);
		if (sjekkIDpaa)	{
			clearTimeout(sjekkID);
			sjekkIDpaa = false;
		}
		if (erIAutovisning)	{
			document.getElementById("Auto").innerHTML = '<a href="javascript:AutoStopp();" onfocus="this.blur()"><b>'+ tStopp +'</b></a>';
		}
	}
	else {
		/*if (!sjekkIDpaa)	{
			TilHovudsida();	
		}*/
		sjekkBiletvisning(nyttbilete);
	}
}
//-----------------------------------------------------------------------------
function TilHovudsida() {
	document.getElementById("intro").className = "";
	document.getElementById("biletfelt").className = "skjult";
	
	document.getElementById("imgBilete").src = "../hovudsida/tom.gif";
	document.getElementById("imgBilete").width = 1;
	document.getElementById("imgBilete").height = 1;
}

//-----------------------------------------------------------------------------
/* Private. Fungerer i lag med ovanst�ande. */
function sjekkBiletvisning(nyttbilete)	{
	sjekkIDpaa = true;
	sjekkID = setTimeout("VisBilete(" + nyttbilete + ")", 50);
}


//-----------------------------------------------------------------------------
//preloadar bilete
function VisBilete2(nyttbilete)	{
	src = Filsti(nyttbilete);
	document.getElementById("NesteBilete").innerHTML = 
		'<img width="1" height="1" src="' + src + '" onload="SkrivBilete(' + nyttbilete + ');" />';
	document.getElementById("Tekst").innerHTML = 
		'&nbsp;' + tLastarNedBiletet + ' "' + HovudArray[nyttbilete].TitVal + '"<span id="prikkprikk"></span>';
	status = tLastarNedBiletet + ' "' + HovudArray[nyttbilete].TitVal + '"...'
	RoterPrikk();
}

//-----------------------------------------------------------------------------
// Fungerer i samarbeid med f�reg�ande. Viser prikkar som indikerer lasting.
var prikkrotasjon;
function RoterPrikk() {
	if (document.getElementById("prikkprikk")) {
		var el = document.getElementById("prikkprikk");
		if (el.innerHTML == ".........................") el.innerHTML = ".";
		else el.innerHTML += ".";
		prikkrotasjon = setTimeout("RoterPrikk()", 200);
	}
}

//-----------------------------------------------------------------------------
// Returnerer filstien ut fr� ein id i HovudArray
function Filsti(id) {
	if (!HovudArray[id]) return "";
	if (biletstorleik != "l")	{
		return "../images/" + HovudArray[id].PicVal;
	}
	else	{
		arr = HovudArray[id].PicVal.split("/");
		return "halvbilete.php?mappe=" + arr[0] + "&filnamn=" + arr[1];
	}
}

//-----------------------------------------------------------------------------
/* Viser biletet og all tekst som h�yrer til */
function SkrivBilete(newpic) {
	
	current = newpic;
	PicNr = current + 1;
	  
	if (sistVist != -1)	GjenopprettTittel(sistVist);
	
	clearTimeout(prikkrotasjon);
	status = ""; // statusbar
	
	// Filstiar
	current < (PicVar - 1) ? NesteFil = Filsti(current + 1): NesteFil = "";
	src = Filsti(current);
		
	// Skifta sj�lve biletet
	BlendTrans(src);
	
	
	Preload(NesteFil);
	if (erIAutovisning) GaaTilNeste(); 
	
	// R�me eller ikkje r�me
	var biletraame = document.getElementById("biletraame");
	var tekstOgDato = document.getElementById("tekst-og-dato");
	
	if (HovudArray[current].RaameVal == "ja" && !erLitenSkjerm)	{
		biletraame.className = "biletraame-stor";
		tekstOgDato.className = "tekst-og-dato-stor";
	} else if (HovudArray[current].RaameVal == "ja")	{
		biletraame.className = "biletraame-liti";
		tekstOgDato.className = "tekst-og-dato-liten";
	} else {
		biletraame.className = "";
	}
	
	SkrivTekst();
	setTimeout("SkrivTekst()", 100);//B�gg i 5.0?
	document.getElementById("Dato").innerHTML = HovudArray[current].DatVal;
	
	SkrivTittel(current, "aktiv");
	
	RegistrerBilete(current);
	
	sistVist = current;
	setTimeout("skroll(current)", 600); // tilpass til varigheit av BlendTrans under
	
}
//-------------------------------------------------------------------------------
/* Gradvis tilpassing av h�gde, breidde og gjennomsiktigheit */
function BlendTrans(src) {
	steg = 10;
	varigheit = 500;
	
	var intro = document.getElementById("intro");
	var biletfelt = document.getElementById("biletfelt");
	
	if (intro.className == "") intro.className = "skjult";
	if (intro.className == "skjult") biletfelt.className = "";
	
	var aBilete = document.getElementById("aBilete");
	var imgBilete = document.getElementById("imgBilete")
	
	aBilete.style.backgroundImage = "url("+ imgBilete.src + ")";
	imgBilete.style.filter = "alpha(opacity=0)";
	imgBilete.style.MozOpacity = 0;
	imgBilete.src = src;
	
	width = imgBilete.width;
	height = imgBilete.height;
	nyWidth = HovudArray[current].width;
	nyHeight = HovudArray[current].height;
	if (biletstorleik == "l") {
		nyWidth = nyWidth / 2;
		nyHeight = nyHeight / 2;
	}
	
	intervallWidth = (nyWidth - width) / steg;
	intervallHeight = (nyHeight - height) / steg;
	
	for (i = 1; i < steg; i++) {
		opacity = i * (1 / steg);
		width += intervallWidth;
		height += intervallHeight;
		setTimeout("ApplyBlendTrans("+ opacity +", "+ width +", "+ height +")", Math.round(i * (varigheit / steg)));
	}
	// Landa p� eksakt tal:
	setTimeout("ApplyBlendTrans(1, "+ nyWidth +", "+ nyHeight +")", varigheit);
}
//-------------------------------------------------------------------------------
/* Fungerer i lag med f�reg�ande */
function ApplyBlendTrans(opacity, width, height) {
	ieOpacity = opacity * 100;
	// Bug i FF, jfr: http://www.dynamicdrive.com/forums/showthread.php?p=3066
	mozOpacity = (opacity == 1) ? 0.99 : opacity;
	
	var imgBilete = document.getElementById("imgBilete")
	
	imgBilete.style.filter = "alpha(opacity="+ ieOpacity +")";
	imgBilete.style.MozOpacity = mozOpacity;
	
	imgBilete.width = width;
	imgBilete.height = height;
	
	if (opacity == 1) {
		document.getElementById("aBilete").style.backgroundImage = "none";
	}
}
//-------------------------------------------------------------------------------
/* Gjenopprettar farge og symbol i titlane til venstre */
function GjenopprettTittel(nummer)	{
	SkrivTittel(nummer,  "");
}

//-----------------------------------------------------------------------------
function SkrivTekst()	{
	flagg = "";
	// engelsk flagg
	if (HovudArray[current].EngVal != "")	{
		flagg = '<a class="ikon" href="javascript:Tom();" onMouseOver="SkrivEngelskTekst();" onMouseOut="SkrivTekst();">'
		+ '<img src="gfx/engelsk.jpg" alt="" /></a>';
	}
	// tekstfelt
	s = '<span class="nummer">' + HovudArray[current].iVal + '</span>'
		+ '<a class="ikon" href="javascript:infopopup(' + HovudArray[current].idVal + ');" onFocus="this.blur()">'
		+ '	<img src="gfx/infopopup.gif" width="15" height="12" title="'+ tMeirOmBiletet +'" alt="" /></a>'
		+ '<a class="ikon" href="javascript:kartpopup(\'bilete\', ' + HovudArray[current].idVal + ');" onFocus="this.blur()">'
		+ '<img src="../kart/kartpopup.gif" width="15" height="12" title="'+ tKart +'" alt="" /></a>'
		+ flagg
		+ '<span id="TekstLinje"> ' + HovudArray[current].TxtVal + '</span>';
	document.getElementById("Tekst").innerHTML = s;
	
	// rediger direkte
	document.getElementById("redigerDirekte").innerHTML = '<a href="javascript:OpnaRedigerDirekte(\'bilete\',' + HovudArray[current].idVal + ');" onFocus="this.blur()"><font size=1 color=#333333>o</font></a>';
}

//-----------------------------------------------------------------------------
function SkrivEngelskTekst()	{
	document.getElementById("TekstLinje").innerHTML =
	HovudArray[current].EngVal;
}
	
//-----------------------------------------------------------------------------
function RegistrerBilete(jsNummer)	{
	//setTimeout("registrering.location.href = 'registrering.php?id=' + HovudArray[" + jsNummer + "].idVal", 100); //Timeout forhindrar endelaus lasting. B�gg?
	var reg = new Image;
	reg.src = 'registrering.php?id=' + HovudArray[jsNummer].idVal;

}
	
//-----------------------------------------------------------------------------
function ForrigeBilete() {
	if (PicVar == 0) return; // ingen bilete 
  	last = current;
  	current--;
  	if (current <= 0) VisBilete(0);
  	else if (current < PicVar) VisBilete(current);
}
  
  
//-----------------------------------------------------------------------------
function NesteBilete() {
  	last = current;
  	current++;
 	// siste bilete totalt
	if (current>=PicVar)	{ 
		TilHovudsida();
  		GjenopprettTittel(last);
  		current = -1;
  		SkrollTilY(0);
  	}
    else	{
  		i = HovudArray[current].iVal;
		// siste bilete i ein serie
  		if (i == "1" && current != 0 && klarTilNesteSerie == false)	{
			current = last;
			GjenopprettTittel(last);
  			TilHovudsida();
  			AutoStopp();
  			klarTilNesteSerie = true;
  		}
		// vanleg visning
	  	else	{
			if (current<PicVar)	{
	  			VisBilete(current); 
	  			klarTilNesteSerie = false; 
			}  
		}
  	}
}

 
//-----------------------------------------------------------------------------
/*
function stegvisFlytting()	{
	parent.scrolltext.moveBy(parent.scrolltext.x,scrolleSteg);
	parent.scrollbox.moveTo(0,(parent.scrolltext.y-parent.scrollMarginTop)/parent.scrollFactor + parent.scrollArrowHeight);

	if (parent.scrollbox.y > parent.scrollBarHeight)	{
	parent.scrollbox.moveTo(0,(parent.scrollBarHeight))
	parent.scrolltext.moveTo(parent.scrolltext.x,parent.scrollFactor*(parent.scrollbox.y-parent.scrollArrowHeight)+parent.scrollMarginTop);
	}	
}
*/
//-----------------------------------------------------------------------------
/* Vert kalla opp fr� blant anna SkrivBilete for � posisjonera vindauga rett */
function skroll(current){
	var aktiv = document.getElementById("tbl" + current);
	yPosisjon = (aktiv.offsetTop);
	if (visning == "titlar") 	broek = 2;
	else 						broek = 3;
	nyPosisjon = yPosisjon - (scrollHeight/broek);
		
	if (yPosisjon < scrollHeight/broek) //toppen av lista
	{	
		dw_scrollObj.scrollTo("wn", 0, 0, 1000);		
	}	
	
	else 	{ //vanleg flytting
		dw_scrollObj.scrollTo("wn", 0, nyPosisjon, 1000);		
	}
}

//-----------------------------------------------------------------------------
function SkrollTilY(y)	{
	dw_scrollObj.scrollTo("wn", 0, y, 1000);
}
	
//-----------------------------------------------------------------------------
function SkrivTittel(i, className)	{
	if (document.getElementById("tr" + i)) document.getElementById("tr" + i).className = className;
}


  

//-----------------------------------------------------------------------------
var arImages=new Array();
function Preload() {
 var temp = Preload.arguments; 
 for(x=0; x < temp.length; x++) {
  arImages[x]=new Image();
  arImages[x].src=Preload.arguments[x];
 }
}

//-----------------------------------------------------------------------------
function AutoStart(i)	{
	erIAutovisning = true;
	if (i || i == 0) {VisBilete(i);}
	else	{
	parent.document.getElementById("Auto").innerHTML = '<a href="javascript:AutoStopp();" onfocus="this.blur()">'+ tStopp +'</a>';
	NesteBilete();}
	
}


//-----------------------------------------------------------------------------
function GaaTilNeste()	{
	AutoID = setTimeout("NesteBilete()", 5000);
	}

//-----------------------------------------------------------------------------
function AutoStopp()	{
	if (erIAutovisning)	{
	clearTimeout(AutoID);
	erIAutovisning = false;
	document.getElementById("Auto").innerHTML = '<a href="javascript:AutoStart();" onfocus="this.blur()">'+ tAuto +'</a>';
}
}

//-----------------------------------------------------------------------------
function Musehjul()  {
	var trinn = 200;
	if (event.wheelDelta >= 120) {
		dw_scrollObj.scrollBy("wn", 0, trinn, 0);
	}
	else if (event.wheelDelta <= -120) {
		dw_scrollObj.scrollBy("wn", 0, -trinn, 0);
	}
}
//-----------------------------------------------------------------------------
var biletstorleik = "m";

function Storleik(stleik)	{
	if (visning == "panorama") return;
	if (stleik == "l")	{
		biletstorleik = "l";
		document.getElementById("aStorleikL").style.background = "url(gfx/storleik_l.gif)";
		document.getElementById("aStorleikM").style.background = "none";
		if (current >= 0)	{
			AutoStopp();
			BlendTrans(document.getElementById("imgBilete").src);
			}
		}
	if (stleik == "m")	{
		biletstorleik = "m";
		document.getElementById("aStorleikL").style.background = "none";
		document.getElementById("aStorleikM").style.background = "url(gfx/storleik_m.gif)";
		if (current >= 0)	{
			AutoStopp();
			VisBilete(current);
			}
		}
	}

n = (document.layers) ? 1:0
ie = (document.all) ? 1:0

//-----------------------------------------------------------------------------
// Fr� http://www.din.or.jp/~hagi3/JavaScript/JSTips/Mozilla/Samples/KeyEvent.htm
function Key(e){
	if (!e) e = window.event;
    tast =  e.keyCode;
	var func;
	
  	switch (tast) {
	  	case 34: // Page Down
		case 39: // Pil h�gre
		case 40: // Pil ned
			func = 'Neste';
			break;
	  	case 33: // Page Up
		case 37: // Pil venstre
		case 38: // Pil opp
			func = 'Forrige';
			break;
  	}
    if (func) {
		window[func]();
		
		if (e.preventDefault) e.preventDefault();
    	else e.returnValue = false;
	}
}
document.onkeydown = Key;


//-----------------------------------------------------------------------------
// Amazing Frameless Popup Window - Version I
// (C) 2000 www.CodeLifter.com
var windowW=screen.width // wide
var windowH=screen.height // high
var windowX = 0 // from left
var windowY = 0 // from top
var autoclose = true
var s = "width="+windowW+",height="+windowH;
var beIE = document.all?true:false

function Fullskjerm(){
  if (beIE){
    NFW = window.open("","popFrameless","fullscreen,"+s)     
    NFW.blur()
    window.focus()       
    NFW.resizeTo(windowW,windowH)
    NFW.moveTo(windowX,windowY)
    var frameString=""+
"<html>"+
"<head>"+
"<title>Vikjavev.no - Fotoalbum</title>"+
"</head>"+
"<frameset rows='*,0' framespacing=0 border=0 frameborder=0>"+
"<frame name='top' src='fullskjerm.php?bilete=" + current + "' scrolling=no>"+
"<frame name='bottom' src='about:blank' scrolling='no'>"+
"</frameset>"+
"</html>"
    NFW.document.open();
    NFW.document.write(frameString)
    NFW.document.close()
  } else {
    NFW=window.open("fullskjerm.php?bilete=" + current,"popFrameless","scrollbars=no,"+s)
    NFW.blur()
    window.focus() 
    NFW.resizeTo(windowW,windowH)
    NFW.moveTo(windowX,windowY)
  }   
  NFW.focus()   
  if (autoclose){
    window.onunload = function(){NFW.close()}
  }
}

//-----------------------------------------------------------------------------
function InitFullskjerm()	{
	if (current != -1)	{
		if (HovudArray[current].idVal < 2700)	{
			alert ("I have not published large images before Jan 24th 04");	
		} else Fullskjerm();
	} else Fullskjerm();
}
	
//-----------------------------------------------------------------------------
/* This code is from Dynamic Web Coding at www.dyn-web.com
  Copyright 2001-4 by Sharon Paine 
  See Terms of Use at www.dyn-web.com/bus/terms.html
  regarding conditions under which you may use this code.
  This notice must be retained in the code as is! */

function initScrollLayer() {
	var wndo = new dw_scrollObj('wn', 'innhald');
  	wndo.setUpScrollbar("dragBar", "track", "v", 1, 1);
  	dw_scrollObj.GeckoTableBugFix('wn');  
}
//-----------------------------------------------------------------------------
/* Fyrer i hytt og ver i IE
function ReSeis() {
	alert("sjekk");
	location.reload();
}
*/

//-------------------------------------------------------------------------------
function VisPanorama(id) {
	document.getElementById("intro").className = "skjult";
	document.getElementById("biletfelt").className = "";
	
	document.getElementById("Tekst").innerHTML = PanoArray[id].TxtVal;
	document.getElementById("Dato").innerHTML = PanoArray[id].DatVal;
	document.getElementById("biletraame").className = "biletraame-stor";
	document.getElementById("tdBilete").innerHTML = "<applet id='aplPano' code='uPixScreen.class'"
	+ "archive='../panorama/uPixScreen.jar' "
	+ "width='600' height='218'>"          
    + "      <param name='url' value='../panorama/"+ PanoArray[id].PicVal +"'>"          
    + "      <param name='autoPan' value='yes'>"          
    + "      <param name='startYaw' value='0.0'>"          
    + "      <param name='startPitch' value='0.0'>"          
    + "      <param name='startFov' value='60.0'>"          
    + "      <param name='partialStitch' value='no'>"          
    + "      <param name='horFov' value='360'>"          
    + "      <param name='autoPanSpeed' value=30>"          
    + "</applet>";
	
	
}
//-----------------------------------------------------------------------------
/* Set css-reglar som er avhengige av storleiken p� klient-vindauga. Dette m� kallast opp fr� inne i body-en */
var scrollHeight;
var erLitenSkjerm;
function Storleiksavhengig(klientH, klientW) {
	erLitenSkjerm = (klientW < 900) ? 1 : 0;
	scrollHeight = klientH - 98;
	var bileteHeight = (erLitenSkjerm) ? 415 : 580;
	var hovudinnhaldMarginTop = 2 * ((klientH / 5) - 120); // empirisk
	if (hovudinnhaldMarginTop < 0) hovudinnhaldMarginTop = 0;
	var hovudinnhaldHeight = klientH - hovudinnhaldMarginTop - 60;
    
	document.write (
	"<style type='text/css'>"
	+"	div#wn	{"
	+"		height:  " + (klientH - 98) + "px;"
	+"		clip:rect(0px, 130px, " + (klientH - 98) + "px, 0px);"
	+"	}"
	+"	div#scrollbar { "
	+"		height:" + (klientH - 98) + "px "
	+"	}"
	+"	div#track { "
	+"		height:" + (klientH - 134) + "px; "
	+"	}"
	+"	#venstresida #venstrelist { "
	+"		height:" + (klientH - 96) + "px; "
	+"	}"
	+"	#venstresida #hoegrelist { "
	+"		height:" + (klientH - 96) + "px; "
	+"	}"
	+"	#venstresida #panelnede { "
	+"		top:" + (klientH - 18) + "px; "
	+"	}"
	+"	#hovudfelt #Bilete { "
	+"		height:" + bileteHeight + "px; "
	+"	}"
	+"	#hovudfelt #intro,"
	+"	#hovudfelt #biletfelt {"
	+"		_height:" + hovudinnhaldHeight + "px;"
	+"		min-height:" + hovudinnhaldHeight + "px;"
	+"		margin-top:" + hovudinnhaldMarginTop + "px;"
	+	"}"
	+"</style>"
	);
}
//-----------------------------------------------------------------------------
/*function click(e) {
	if (!e) e = window.event;
	if (e.button==2 || e.button==3) {
		alert(tKopiAlert);
	}
}

document.onmousedown = click;*/

//-----------------------------------------------------------------------------
function SkrivF11()	{
	var f11haldar;
	if (f11haldar = document.getElementById("f11haldar"))	{
		if (klientH < 545)	{
			f11haldar.className = "f11haldar";
		}
	}
}
//-----------------------------------------------------------------------------
// Initiering
function init() {
	initScrollLayer();
	sidaErFerdiglasta = true;
	SkrivF11();
	if (document.getElementById("aNeste")) {
		document.getElementById("aNeste").focus(); 
	}
	if (startbilete !== false) {
		VisBilete(startbilete);
	}
	document.getElementById("innhald").onmousewheel = Musehjul;
}
//-----------------------------------------------------------------------------
// Rediger direkte
function OpnaRedigerDirekte(tabell, id){
    targeturl = "../admin2/dbEdit/popup.php?tableName=" + tabell + "&uniqueId=" + id + "&action=edit"
    popupVindauga = window.open(targeturl,"","scrollbars=no,resizable,height=600,width=390,left = 100,top = 50")
}
