var dir;
var typ;
var album;
var fotograf;
var picarray = new Array();
var picnr = "";
var n;
var height;
var width;
var slideId;
// ------------
var count = 0;
var msFirst = 0; // Taktningen börjar
var msPrevious = 0; // Föregående takt
//ms = 0;
var t;
var s = 0;

function timer(t)
	{
	time = new Date;
	ms = time.getTime();
	s = Math.floor((ms - msFirst) / 1000);
	if (s <= 30) document.BPM.T.value = 30-s;
	t = setTimeout("timer()",1000);
	}

function TapForBPM() {
	time = new Date;
	ms = time.getTime();
	if ((ms - msPrevious) > 2000)
		{
		// Nollställ räknaren och börja om om man pausat mer än 2 sekunder sedan förra trycket
		count = 0;
		document.BPM.AVG.value = "Första";
		msFirst = ms;
		timer();
		}
	else {
		s = Math.floor((ms - msFirst)/1000);
		if (s <30) count++;
		}
	
  if (count > 0)
    {
    bpmAvg = count * Math.round(60 / s);
	if (s <= 30) {
		document.BPM.AVG.value = bpmAvg;
		}
	else
		clearTimeout(t);
    }
	document.BPM.HIT.value = count;
	msPrevious = ms;
	return true;
	}






function getElm(eID) { return document.getElementById(eID);	}
function show(eID) { getElm(eID).style.display='block'; }
function hide(eID) { getElm(eID).style.display='none'; }

function setOpacity(eID, opacityLevel) {
	// opacityLevel: 0 = Transparent, 100 = Fullt synligt
	var eStyle = getElm(eID).style;
	eStyle.filter = 'alpha(opacity:' + opacityLevel + ')'; // IE/Win
	eStyle.MozOpacity = (opacityLevel / 100); // Gamla Mozilla och Firefox
	eStyle.opacity = (opacityLevel / 100); // Safari 1.2, newer Firefox and Mozilla, CSS3
	
	}

// R E K L A M
// Visa klickad reklam
function showadd(id) { getElm(id).style.display="block"; getElm('blanket').style.display="block"; }

// Göm klickad reklam
function hideadd(id) { getElm(id).style.display="none";	getElm('blanket').style.display="none";	}

// AVANCERAT LÄGE (FORMULÄR ETC)
// Visa avanceradet läge
function show_advanced(id) { getElm(id).style.display="block"; getElm('show').style.display="none"; getElm('hide').style.display="block"; }

// Göm avancerat läge
function hide_advanced(id) { getElm(id).style.display="none"; getElm('hide').style.display="none"; getElm('show').style.display="block"; }

function test() {
	alert("TEST!");
	}
	
function highlight_F11(display) {
	// display = block -> Visa
	// display = none  -> Gömma
	getElm('F11').style.display=display;
	}

function pause(milliseconds) {
	var dt = new Date();
	while ((new Date()) - dt <= milliseconds) { /* Do nothing */ }
	}

function downloadImage() {
	// Ladda hem bilden och spara lokalt
	// alert (picarray[picnr].src);
	getElm("download").href="download.php?file="+picarray[picnr].total+"&name="+picarray[picnr].name;
	}
	
function imgBtnCheck() {
	var container;
	
	if (picarray[picnr-1]) {
		getElm("prev").style.display="inline";
	} else {
		getElm("prev").style.display="none";
		}
	if (picarray[picnr+1]) {
		getElm("next").style.display="inline";
	} else {
		getElm("next").style.display="none";
		}
	}

function imageFirst() {
	if (slideId) imageslideEnd();
	getElm("photo2").src=picarray[picnr].src;
	x = getElm("photo2").width/2;
	getElm("photo2").style.marginLeft = 0-x-11+"px";
	while (picarray[picnr-1]) {
		picnr--;
		}
	getElm("photo").src=picarray[picnr].src;
	x = (getElm("photo").width/2);
	getElm("photo").style.marginLeft = 0-x-11+"px";
	fadeTo("photo2","photo");
	}	

function imagePrev() {
	if (slideId) imageslideEnd();
	if (picarray[picnr-1]) {
		getElm("photo2").src=picarray[picnr].src;
		x = (getElm("photo2").width/2);
		getElm("photo2").style.marginLeft = 0-x-11+"px";
		picnr--;
		getElm("photo").src=picarray[picnr].src;
		x = (getElm("photo").width/2);
		getElm("photo").style.marginLeft = 0-x-11+"px";
		fadeTo("photo2","photo");
		}
	}

function imageNext() {
	if (picarray[picnr+1]) {
		// Ladda gamla bilden bakom
		getElm('photo2').src=picarray[picnr].src;
		picnr++;
		// Ladda ny bild i främre foto transparent
		getElm('photo').src=picarray[picnr].src;
		// Tona in ny bild
		fadeTo('photo2','photo');
		x = getElm('photo2').width/2;
		getElm('photo2').style.marginLeft = 0-x-11+'px';
		x = getElm("photo").width/2;
		getElm('photo').style.marginLeft = 0-x-11+'px';
	} else {
		if (slideId) imageslideEnd();
		}
	}

function imageLast() {
	if (slideId) imageslideEnd();
	getElm("photo2").src=picarray[picnr].src;
	x = (getElm("photo2").width/2);
	getElm("photo2").style.marginLeft = 0-x-11+"px";
	while (picarray[picnr+1]) {
		picnr++;
		}
	getElm("photo").src=picarray[picnr].src;
	x = (getElm("photo").width/2);
	getElm("photo").style.marginLeft = 0-x-11+"px";
	fadeTo("photo2","photo");
	}
	
function windowSize() {	
	if (window.innerHeight && window.innerWidth){
		height = window.innerHeight;
		width = window.innerWidth;
	} else if (document.body && document.body.offsetHeight && document.body.offsetWidth){
		height = document.body.offsetHeight;
		width = document.body.offsetWidth;
		}
	}
	
function tempSolid(elementId,normal,delay) {
	setOpacity(elementId,100);
	fadeId = setTimeout("setOpacity('"+elementId+"',"+normal+")",delay*1000);
	}
	
function fadeIn(eID) {
	var speed = 10;
	var timer = 0;
	
	setOpacity(eID,0); // 100% tranaparent
	show(eID);
	for (var i=1; i<=100; i++) {
		setTimeout("setOpacity('"+eID+"',"+i+")",timer * speed);
		timer++;
		}
	}
	
function fadeOut(eID) {
	var speed = 10;
	var timer = 0;
		
	for (var i=100; i>=1; i--) {
		setTimeout("setOpacity('"+eID+"',"+i+")", timer * speed);
		timer++;
		}
	setTimeout("hide('"+eID+"')", 200 * speed);
	}

function fadeTo(idFrom,idTo) {
	var speed = 5;
	var timer = 0;
	
	setOpacity(idTo,0); // 100% tranaparent
	setOpacity(idFrom,100); // 100% tranaparent
	show(idTo);
	show(idFrom)
	for (var i=1; i<=100; i++) {
		setTimeout("setOpacity('"+idTo+"',"+i+")",timer * speed / 1.5);
		setTimeout("setOpacity('"+idFrom+"',"+(100-i)+")",timer * speed / 1.5);
		timer++;
		}
	setTimeout("hide('"+idFrom+"')", 200 * speed / 1.5);
	}
	
function imageOpen(t,a,f,n) {
	typ = t;
	album = a;
	fotograf = f;
	picnr = n;
	
	scroll(0,0); // Rulla tillbaka sidan längst upp
	windowSize(); // Kolla fönsterstorleken
	getElm('blanket').style.display='block'; // Tona ned resten av webbplatsen
	getElm('body').style.overflow='hidden'; // Spärra möjlighet att scolla ned
	getElm('nav3').style.display='block'; // Visa navigeringsknappar
	setOpacity('photo',0); // Bildern startar transparent
	setOpacity('photo2',0); // "
	getElm('photo').src=picarray[picnr].src; // Hämta bilden
	fadeIn('photo');	
	x = (getElm('photo').width/2);	
	getElm('photo').style.marginLeft = 0-x-11+'px';
	}

function imageClose() {
	imageslideEnd();
	getElm("nav3").style.display="none";
	getElm("photo").style.display="none";
	getElm("photo2").style.display="none";
	getElm("blanket").style.display="none";
	getElm("body").style.overflow="visible";
	}

function imageFullscreen() {
	setOpacity("blanket",100);
	setOpacity("nav3",30);
	getElm("fullscreen").style.display="none";
	getElm("nofullscreen").style.display="inline";
	
	getElm("photo").style.top = 0;
	getElm("photo").style.height = "100%";
	getElm("photo").style.padding  = 0;
	getElm("photo").style.border  = 0;
	x = getElm("photo").width/2;
	getElm("photo").style.marginLeft = 0-x-11+"px";
	
	getElm("photo2").style.top = 0;
	getElm("photo2").style.height= "100%";
	getElm("photo2").style.padding  = 0;
	getElm("photo2").style.border  = 0;
	x = getElm("photo2").width/2;
	getElm("photo2").style.marginLeft = 0-x-11+"px";
	}
	
function imageNofullscreen() {
	setOpacity("blanket",90);
	getElm("fullscreen").style.display="inline";
	getElm("nofullscreen").style.display="none";
	getElm("photo").style.top = "100px";
	getElm("photo").style.height = "600px";
	getElm("photo").style.padding  = "7px";
	getElm("photo").style.border  = "4px solid black";
	x = getElm("photo").width/2;
	getElm("photo").style.marginLeft = 0-x-11+"px";
		
	getElm("photo2").style.top = "100px";
	getElm("photo2").style.height= "600px";
	getElm("photo2").style.padding  = "7px";
	getElm("photo2").style.border  = "4px solid black";
	x = getElm("photo2").width/2;
	getElm("photo2").style.marginLeft = 0-x-11+"px";
	setOpacity("nav3",100);
	}
	
function imageslideShow() {
	var delay = 4000;
	
	getElm("start").style.display="none";
	getElm("pause").style.display="inline";
	slideId = setInterval("imageNext()",delay);
	}

function imageslidePause() {
	getElm("start").style.display="inline";
	getElm("pause").style.display="none";
	if (slideId) {
		clearInterval(slideId);
		slideId = "";
		}
	}

function imageslideEnd() {
	getElm("start").style.display="inline";
	getElm("pause").style.display="none";
	if (slideId) {
		clearInterval(slideId);
		slideId = "";
		}
	}
