var xmlHttp,element,action,refPage,interval;

//window.onload = loadMenus;

var currentX,currentY,followMovement=false;

document.onmousemove=MoveIt;
function MoveIt(e)
{
	if (!e) e = window.event;
	currentX=e.clientX;
	currentY=e.clientY;
	
	if(followMovement)
	{
		var infoBox = document.getElementById("infoBox");
		infoBox.style.top=f_scrollTop()+currentY-infoBox.offsetHeight-10+"px";
		
		var posRight = currentX + infoBox.clientWidth;
		var maxRight = document.getElementById("container").offsetLeft+1000-10;
	
		bo
		if(posRight > maxRight)
			infoBox.style.left=currentX-posRight+maxRight+"px";
		else
			infoBox.style.left=currentX+"px";
			
		infoBox.style.visibility="visible";
	}
}


function openWindow(url,width,height)
{
	egenskaper="toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width="+width+",height="+height+"";
	
	unik=new Date();
	unik=unik.getSeconds()+"_"+unik.getMinutes()+"_"+unik.getHours();
	window.open(url,unik,egenskaper);
}

function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	
	catch (e)
	{
		// Internet Explorer
		try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}

		catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	
	return xmlHttp;
}

function visitPage(url)
{
				
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!");	return; }	
	
	var url=url
	xmlHttp.open("GET","/"+url,true);
	xmlHttp.send(null)	
}

function getPage(url,ele)
{

	element=ele;
				
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!");	return; }	

	xmlHttp.onreadystatechange=presentPage;
	xmlHttp.open("GET","/"+url,true);
	xmlHttp.send(null)	
}

function presentPage() 
{
	if (xmlHttp.readyState==4)		
		document.getElementById(element).innerHTML=xmlHttp.responseText;
			
}

function getValue(url,func)
{
	action=func;
				
	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null) { alert ("Your browser does not support AJAX!");	return; }	


	xmlHttp.onreadystatechange=presentValue;
	xmlHttp.open("GET","/"+url,true);
	xmlHttp.send(null)	
}

function presentValue() 
{

	if (xmlHttp.readyState==4)
	{
		var test = xmlHttp.responseText;
		test = test.replace(/\\"/g,"\"");
		test = test.replace(/"/g,"\\\"");
		test = test.replace(/\\'/g,"'");
		test = test.replace(/'/g,"\\\'");
		test = test.replace(/\r/g,"");
		test = test.replace(/\n/g,"");
		setTimeout(action+"('"+test+"')",1);
	}
			
}

function f_filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}



function f_scrollTop() {
	return f_filterResults (
		window.pageYOffset ? window.pageYOffset : 0,
		document.documentElement ? document.documentElement.scrollTop : 0,
		document.body ? document.body.scrollTop : 0
	);
}




function documentHeight(returnAll)
{
	height = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		height = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		height = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		height = document.body.clientHeight;
	}
	
	if(returnAll) return height+f_scrollTop();
	return height;
}

function documentWidth() 
{
	var width = 0;
	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		width = window.innerWidth;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		width = document.documentElement.clientWidth;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		width = document.body.clientWidth;
	}
	
	return width;
}

//###############################################################################################################################################

var fixedSize = 0;
var currentAlbum,currentOffset,currentMax,currentHeight;

function prevPhoto()
{
	currentOffset--;
	if(currentOffset<0) currentOffset = currentMax-1;
	getValue("getPhoto.php?album="+currentAlbum+"&offset="+currentOffset+"&height="+currentHeight,"presentFoto");
}

function nextPhoto()
{
	currentOffset++;
	if(currentOffset>currentMax-1) currentOffset = 0;
	getValue("getPhoto.php?album="+currentAlbum+"&offset="+currentOffset+"&height="+currentHeight,"presentFoto");
}



function box(album,offset,maximum,title,id)
{

	var cover = document.getElementById("coverAll");
	var popup = document.getElementById("popup");
	
	if(!album)
	{	
		popup.style.display="none";
		if(cover.filters) cover.filters.alpha.opacity=100;
		cover.style.MozOpacity=100;
		cover.style.opacity=1;
		
		cover.onclick="";
		return;
	}

	var docHeight = documentHeight()-120;
	currentAlbum = album;
	currentOffset = offset;
	currentMax = maximum;
	currentHeight = docHeight;
	
	document.getElementById("title").innerHTML=title;

	if(cover.filters) cover.filters.alpha.opacity=50;
	cover.style.MozOpacity=50;
	cover.style.opacity=0.5;
	
	
	document.getElementById("popup").style.height=docHeight+"px";
	document.getElementById("popup_right").style.height=docHeight+"px";
	document.getElementById("popup").style.top = f_scrollTop() + 25 + "px";
	
	popup.style.display="block";
	
	getValue("getPhoto.php?id="+id+"&album="+album+"&offset="+offset+"&height="+docHeight,"presentFoto");
	
	setTimeout("test()",100);
	
	
}

function presentFoto(value) {

	var ele = document.getElementById("popup_content");

	ele.innerHTML = value;
	document.getElementById("popup_right").style.height=ele.clientHeight+"px";
	
	getValue("getComments.php?album="+currentAlbum+"&offset="+currentOffset,"updateComments");

}

var comments,comments_count;

function updateComments(value) {

	var ele = document.getElementById("popup_right");	
	ele.innerHTML = "";//<img src=\"/gfx/loading.gif\">";
	
	comments = value.split("|");
	comments_count = 1;
	
	clearTimeout(interval);
	interval = setTimeout("updateComments_startLoop()",1000);

}

function updateComments_startLoop() {
	
	if(comments[0] == 1)
	{
		var txt = "<textarea id=\"comment\" style=\"height: 23px\" onFocus=\"expand(this)\" onblur=\"unexpand(this)\">Skriv en kommentar</textarea>"+
						"<input id=\"comment_name\" type=\"text\" value=\"Ditt namn\" onfocus=\"expand(this)\" onblur=\"unexpand(this)\" style=\"margin-top: 5px\">";
	}
	else
	{
		var txt = "<strong>Inga fler inlägg tillåtna!</strong><br>Kommenteringen för detta album är stängd, därför kan inte nya inlägg skapas!";
	}
		
	document.getElementById("popup_right").innerHTML = "<div class=\"commentbox\">" + txt + "</div><div class=\"commentbottom\" id=\"sendcomment\"></div>";
		
	if(comments.length > 2)	
		setTimeout("updateComments_next()",300);

}

function updateComments_next() {

	var thisComment = comments[comments_count].split("#");

	document.getElementById("popup_right").innerHTML += "<div class=\"commentbox\">"+
				thisComment[2]+"<div class=\"commentcheat\"></div>"+
				"<div class=\"clear\"></div></div>"+
				"<div class=\"commentbottom\" id=\"sendcomment\">/"+thisComment[1]+"</div>";
				
	comments_count++;
	
	if(comments_count < comments.length-1)
		setTimeout("updateComments_next()",300);


}



function test()
{
	var cover = document.getElementById("coverAll");
	cover.style.cursor="pointer";
	cover.onclick= function() {box();}

}


function got_Box(value)
{


	var popup = document.getElementById("popup");
	popup.innerHTML += value;
	
	var width = popup.clientWidth-20;
	var maxWidth = documentWidth()*0.7;
	if(width>maxWidth)
	{
		width = maxWidth;
		popup.style.width=width+"px";
	}
	
	if(fixedSize) { width = fixedSize; popup.style.width=fixedSize+"px"; }
	
	var height = popup.clientHeight-20; 
	var maxHeight = documentHeight()-60;
	if(height>maxHeight)
	{
		height = maxHeight;
		popup.style.height=height+"px";
	}
		
	
	popup.style.top = documentHeight()/2 + f_scrollTop() - height/2 - 10 +"px";
	popup.style.left = documentWidth()/2 - width/2 - 10 + "px";
	popup.style.visibility="visible";

}


function close_Box()
{
	
	document.body.removeChild(document.getElementById("cover"));
	document.body.removeChild(document.getElementById("popup"));

}

function delay_close_Box()
{
	setTimeout("close_Box()",2000);
}

function infoText(text,width)
{

	var box = document.getElementById("infoBox");

	if(!text)
	{
		followMovement=false;
		box.style.visibility="hidden";
		return;
	}
	
	if(width) box.style.width = width + "px";
	else box.style.width = "auto";

	followMovement=true;
	box.innerHTML=text;
	
	
}

var idet;
function setLang(lang,id)
{
	idet  = id;
	getValue("incs/setLanguage.php?lang="+lang,"langWasSet");
	
}

function langWasSet()
{
	box("","ajax/clubs.php?id="+idet,920);

}

function setLanguage(lang)
{
	getValue("incs/setLanguage.php?lang="+lang,"languageWasSet");
	
}

function languageWasSet()
{

	window.location.reload();

}

function facebook()
{


	var u = document.getElementById("photo").src;
	var t = document.getElementById("title").innerHTML;
	
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=350'); 
	return false;

}

function sendQuestion(to)
{
	
	var clubname = document.getElementById("clubname");
	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var phone = document.getElementById("phone");
	var question = document.getElementById("question");
	var send = document.getElementById("sendButt");
	send.disabled=true;
	
	var fel = "";
	if(name.value.length < 3) { fel += "• Fyll i ditt namn!\n"; name.style.borderColor="red";}
	else name.style.borderColor="black";
	
	if(email.value.length < 5) { fel += "• Fyll i din epostadress!\n"; email.style.borderColor="red"; }
	else email.style.borderColor="black";
	
	if(phone.value.length < 3) { fel += "• Fyll i ditt telefonnummer!\n"; phone.style.borderColor="red"; }
	else phone.style.borderColor="black";
	
	if(question.value.length < 10) { fel += "• Du glömde skriva frågan!\n"; question.style.borderColor="red"; }
	else question.style.borderColor="black";
	
	if(fel)
	{
		alert("Följande fel inträffade:\n"+fel);
		send.disabled=false;
	}
	else
	{
		send.value="Skickar...";
		
		var subject = "Fråga från hemsidan";
		var msg = "Skickat av: "+email.value+"<br>Telefonnummer: "+phone.value+"<br><br>"+question.value.replace("\n","<br>");
		
		var url = "incs/mail.php?toName=Fotoförman"+clubname.value+"&toMail="+to+"&fromName="+name.value
					+"&fromMail="+email.value+"&subject="+subject+"&message="+msg;
		getValue(url,"sentQuestion");
	
	}

}

function sentQuestion(value)
{

	var name = document.getElementById("name");
	var email = document.getElementById("email");
	var phone = document.getElementById("phone");
	var question = document.getElementById("question");
	var send = document.getElementById("sendButt");

	if(value)
	{
		alert("Tack! Förmännen tar kontakt\nmed dig så fort som möjligt");
		send.disabled=false;
		name.value="";
		email.value="";
		phone.value="";
		question.value="";
		send.value="Kontakta mig";
	}
	else
	{
		alert("Ett fel uppstod!\nFörsök igen eller återkom senare.");
		send.disabled=false;
		send.value="Försök skicka igen";
	}

}

function expand(element) {

	if(element.id == "comment_name")
	{
	
		if(element.value == "Ditt namn" || element.value == "")
		{
			element.value="";
			element.style.color="black";
		}
	
	}
	else
	{

		element.style.height="60px";
		
		if(element.value == "Skriv en kommentar" || element.value == "")
		{
			element.value="";
			element.style.color="black";
		}
		
		var ele = document.getElementById("comment_name");
		ele.style.display="inline";
		
	
		document.getElementById("sendcomment").innerHTML="<a href=\"javascript:tryToSend()\">Skicka</a>";
		
	}

}

function unexpand(element) {

	if(element.value == "")
	{
		element.style.color="gray";
		
	

		if(element.id == "comment_name")
		{
				element.value="Ditt namn";
		}
		else
		{				
			element.value="Skriv en kommentar";
			element.style.height="23px";
				
			document.getElementById("comment_name").style.display="none";	
			document.getElementById("sendcomment").innerHTML="";
			
		}

	}

}

function tryToSend() {

	var comment = document.getElementById("comment");
	var name = document.getElementById("comment_name");
	
	if(comment.value && name.value)
	{
	
		getValue("getComments.php?album="+currentAlbum+"&offset="+currentOffset+"&name="+name.value+"&comment="+comment.value,"updateComments");
			
	}
	else
	{
		alert("Du måste fylla i båda fälten!");
	}

}

function hoverarrow(element,dir) {

	if(dir==1)
	{
		element.style.backgroundPosition="center 0px";
	}
	else
	{
		element.style.backgroundPosition="center 10px";
	}

}

var offset = 0;
function getMorePics() {

	offset += 9;

	document.getElementById("morepics").innerHTML = "<img src=\"gfx/loading_small.gif\">";
	setTimeout("getValue('getAlbums.php?offset="+offset+"','gotPics')",500);

}

function gotPics(value) {

	document.getElementById("morepics").innerHTML = "Fler album";
	document.getElementById("albumBox").innerHTML += value;

}