function hoverTr(obj, className)	{
	obj.className = className;
}

function refreshPage(pageDoc, queryString)	{
	pageDoc.location.href = pageDoc.location.href + queryString;
}

function refreshInfoPage()	{
	parent.info.document.location.href = parent.info.document.location.href;
}
function voteMember(score, memberId)	{
	if(score == '' || score < 1 || score > 10)
		return;
	var url = "vote.php?memberId=" + memberId + "&score=" + score;
	var name = "voteWin";
	var width = 200;
	var height = 142;
	this.name = 'content';
	popupWindow(url, name, width, height);
}
function voteFile_old(obj, file)	{
	var score = obj.options[obj.selectedIndex].value;
	var url = "vote.php?file=" + file + "&score=" + score;
	var name = "voteWin";
	var width = 200;
	var height = 142;
	this.name = 'mainWin';
	popupWindow(url, name, width, height);
}
function voteFile(score, file)	{
	if(score == 0 || score < 1 || score > 10)	{
		alert("Du har angivit en felaktig poäng, försök igen genom att dra i handtaget på termometern");
		return false;
	}
	var url = "vote.php?file=" + file + "&score=" + score;
	var name = "voteWin";
	var width = 200;
	var height = 142;
	this.name = 'mainWin';
	popupWindow(url, name, width, height);
}

function updateState(obj)	{
	var state = obj.options[obj.selectedIndex].value;
	var frmState = document.getElementById("frmCurrentState");
	frmState.submit();
	
}

function viewWebCam(url) {
	popupWindow(url, 'webcam', 340, 440);
}
function popupWindow(url, name, width, height) {

		// Window size
		//width = 680;
		//height = 400;
		
		if (document.all)
			var xMax = screen.width, yMax = screen.height;
		else
			if (document.layers||document.getElementById)
				var xMax = window.outerWidth, yMax = window.outerHeight;
			else {
				var xMax = 800, yMax=600;
			}

		var xOffset = (xMax - width)/2, yOffset = (yMax - height)/2;




			attr = 'directories=no,location=no,menubar=no,scrollbars=yes,status=no,resizable=yes,toolbar=no,';
			attr += 'screenX='+xOffset+',screenY='+yOffset+',top='+yOffset+',left='+xOffset;
			attr += ',width=' + width + ',height=' + height;
		
		name = window.open(url, name, attr);
}

function viewFiles(layerId)	{
	var fileDiv = document.getElementById(layerId);
	if(fileDiv.style.display != 'block') {
		fileDiv.style.display = 'block';
	}
	else {
		fileDiv.style.display = 'none';
	}
}

// Which file is the user watching
var currentFileId;

// Used in photoalbum view
function loadPhotoInfo(src, description, id, voteEnabled)	{
	
	hideVoteButtons();
	
	var thePhoto = document.getElementById("thePhoto");
	var photoDiv = document.getElementById("photoDiv");
	var viewDescription = document.getElementById("viewDescription");
	currentFileId = id;


	if(photoDiv.style.display == 'none')
	{
		photoDiv.style.display = 'block';
		viewDescription.style.display = 'block';
	}


	var spanDescription = document.createElement("div");
	spanDescription.className = 'copy_black_bold';
	spanDescription.innerHTML = description;

	viewDescription.innerHTML = description;

	thePhoto.src = src;
//	viewDescription.innerHTML = '';
//	viewDescription.appendChild(spanDescription);

	if(voteEnabled)	{
		showVoteButtons();
	}

}

function changeMessagePage(page)	{
	var form = document.forms["pageHandler"];
	form.page.value = page;
	form.submit();
}

function includeMenu()	{
	var head = document.getElementsByTagName("head")[0];
	var mTag = document.createElement("script");
	mTag.src = "lib/default/menu.js";
	head.appendChild(mTag);
	alert("Done");
	drawMenu();
}

function toggleBoxContent(img, boxId)	{
	if(document.getElementById(boxId).style.display=='none') {
		document.getElementById(boxId).style.display='block';
		img.src='lib/default/img/gfx/plus_icon_open.gif'; 
	}
	else {
		document.getElementById(boxId).style.display='none';
		img.src='lib/default/img/gfx/plus_icon_closed.gif'; 
	}
}

function changeSkin(newSkin, vc)	{
	if(newSkin == '')
		return;
	top.location.href = 'community.php?vc=' + vc + '&skin=' + newSkin;
}



var maxVote = 10;
var voteBallsOn = new Array(maxVote);
var voteBallsOff = new Array(maxVote);

function hideVoteButtons()	{
	var v = self.document.getElementById("vote_select");
	v.innerHTML = '<i>Röstning ej tillåten</i>';
}

function showVoteButtons()	{
	var v = self.document.getElementById("vote_select");
	var voteBalls = new Array(maxVote);
	if(v)	{
		v.innerHTML = '';
		for(var i=0; i < maxVote; i++)	{
			var container = document.createElement('div');
			var img = document.createElement('img');
			img.id = i + 1;
			img.src = 'lib/default/img/vote/off.gif';
			img.title = i+1;
			img.onmouseover = function() { voteDisplaySelected(this.id); };
			img.onclick = function() { if(typeof voteHandler != 'function') {alert("Det går inte att rösta på denna sida, vänligen kontakta webmaster@gemenskap.com och meddela vilken sida du står på just nu");return;} voteHandler(this.id); }
			voteBalls[i] = img;
			voteBallsOff[i] = 'lib/default/img/vote/off.gif';

			voteBallsOn[i] = 'lib/default/img/vote/' + (i+1) + '.gif';
		}
		var table = document.createElement('table');
		var tb = document.createElement('tbody');
		var tr = document.createElement('tr');

		table.setAttribute('cellPadding',0);
		table.setAttribute('cellSpacing',0);

		
		for(var i=0;i<voteBallsOn.length;i++)	{
			var td = document.createElement('td');
			td.style.padding=0;
			td.style.margin=0;
			td.style.border = 0;
			//td.nowrap = 'nowrap';
			td.appendChild(voteBalls[i]);
			tr.appendChild(td);
		}
		tb.appendChild(tr);
		table.appendChild(tb);
		v.appendChild(table);
	}

}
function voteDisplaySelected(number)	{
	for(var i = 0; i < maxVote; i++)	{
		var ball = document.getElementById(i + 1);
		ball.src = voteBallsOff[i];
	}
	for(var i = 0; i < number; i++)	{
		var ball = document.getElementById(i + 1);
		ball.src = voteBallsOn[i];		
	}
}

function voteHandler(score)	{
	if(typeof currentFileId != 'string')	{
		alert("Välj en fil att rösta på först");
		return;
	}
	voteFile(score, currentFileId);
}


var memberTabs = new Array('presentation','facts','statistics');

function createMemberTabs(selected)	{
	var html = '<div style="display:inline;"><img src="gfx/tab_start.gif" /></div>';
	var tabImage = null;
	var imgAlt = 'Klicka här för att titta närmare på detta';
	for(var i=0;i<memberTabs.length;i++)	{
		itemId = memberTabs[i];
		if(itemId == selected)	{
			tabImage = 'tab_' + itemId + '_on.gif';
			//<div style="cursor: pointer;display:inline;margin:0px;padding:0px;">
			html += '<img title="' + imgAlt + '" alt="' + imgAlt + '" src="gfx/' + tabImage + '"  border="0" onClick="displayMemberTab(\'' + itemId + '\')" id="img_member_' + itemId + '" />';
		}
		else	{
			tabImage = 'tab_' + itemId + '.gif';
			//<div style="cursor: pointer;display:inline;margin:0px;padding:0px;">
			html += '<img title="' + imgAlt + '" alt="' + imgAlt + '" src="gfx/' + tabImage + '"  border="0" onClick="displayMemberTab(\'' + itemId + '\')" id="img_member_' + itemId + '" onMouseOver="this.src=\'gfx/tab_' + itemId + '_on.gif\'" onMouseOut="this.src=\'gfx/tab_' + itemId + '.gif\'"/>';
		}
	}
	html += '<div style="display:inline;"><img src="gfx/tab_end.gif" /></div>';
	var tabContainer = document.getElementById('memberTabContainer');
	tabContainer.innerHTML = html;
}

function displayMemberTab(divId)	{
	var content;
	
	for(var i=0;i<memberTabs.length;i++)	{
		content = document.getElementById('member_' + memberTabs[i]);
		content.style.display = 'none';
	}
	createMemberTabs(divId);
	content = document.getElementById('member_' + divId);
	content.style.display = 'block';
}

function getWindowHeight() {
	var windowHeight = 0;
	if (typeof(window.innerHeight) == 'number') {
		windowHeight = window.innerHeight;
	}
	else {
		if (document.documentElement && document.documentElement.clientHeight) {
			windowHeight = document.documentElement.clientHeight;
		}
		else {
			if (document.body && document.body.clientHeight) {
				windowHeight = document.body.clientHeight;
			}
		}
	}
	return windowHeight;
}

function hoverPhotoalbumPhoto(containerId, state)
{
	var c = document.getElementById(containerId);
	if(state==true)
		c.className='photoalbum_photo_container photoalbum_photo_container_hover';
	else
		c.className='photoalbum_photo_container photoalbum_photo_container_normal';
}

function resize_iframe()
{

	var height=window.innerWidth;//Firefox
	if (document.body.clientHeight)
	{
		height=document.body.clientHeight;//IE
	}
	//resize the iframe according to the size of the
	//window (all these should be on the same line)
	document.getElementById("chat").style.height=parseInt(height-document.getElementById("chat").offsetTop-8)+"px";
	alert(document.getElementById("chat").style.height);
}

// Ajax 
var ajax_req;

// Get an XMLHttpRequest object in a portable way.
function newRequest()
{
	ajax_req = false;
	// For Safari, Firefox, and other non-MS browsers
	if (window.XMLHttpRequest) {
		try {
			ajax_req = new XMLHttpRequest();
		}
		catch (e) {
			ajax_req = false;
		}
	}
	else if (window.ActiveXObject) {
		// For Internet Explorer on Windows
		try {
			ajax_req = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e) {
			try {
				ajax_req = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch (e) {
				ajax_req = false;
			}
		}
	}
}

function switchBetweenChatAndFavourites(what)
{
	// Om det redan finns ett request, avbryt här
	if(ajax_req){
		alert("Det pågår redan en hämtning av data här, vänta lite eller ladda om sidan");
		return;
	}

	if(what=='chat' && document.getElementById('chat_full_message_container'))
	{
		alert("Du har redan den stora chatten uppe, du kan inte aktivera denna snabb-chat då");
		return;
	}

	var asynchronous = true;
	newRequest();
	if(what=='chat')
	{
		ajax_req.open("GET", "/quick_chat.php", asynchronous);
		ajax_req.onreadystatechange = switchBetweenChatAndFavourites_SetContent;
		ajax_req.send(null);
	}
	else if(what=='favourites')
	{
		ajax_req.open("GET", "/favourites.php", asynchronous);
		ajax_req.onreadystatechange = switchBetweenChatAndFavourites_SetContent;
		ajax_req.send(null);
	}
}

function switchBetweenChatAndFavourites_SetContent()
{
  if (ajax_req.readyState != 4) {
    // We're not ready yet.
    return;
  }
  if (ajax_req.status == 200) {
    if (ajax_req.responseText) {
      // The good stuff happens here!
	  document.getElementById('favourites_chat_content_area').innerHTML = ajax_req.responseText;
    }
  } else {
    // The web server gave us an error
  }
  ajax_req = null;
}
var chatMode;
function postChatMessage(message, mode)
{
	// Om det redan finns ett request, avbryt här
	if(ajax_req){
		alert("Det pågår redan en hämtning av data här, vänta lite eller ladda om sidan");
		return;
	}

	var asynchronous = false;
	newRequest();
	
	// Small or large mode?
	chatMode = mode;


	var encoded = "";
	encoded = "message=" + escape(message);
	encoded += "&mode=" + escape(mode);

	ajax_req.open("POST", "/chat.php", asynchronous);
	ajax_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	ajax_req.onreadystatechange = reloadQuickChat;
	ajax_req.send(encoded);
}
function reloadQuickChat()
{
	if (ajax_req.readyState != 4) {
		// We're not ready yet.
		return;
	}
	if (ajax_req.status == 200) {
		// The good stuff happens here!
		if(chatMode=='small')
		{
			ajax_req = null;
			switchBetweenChatAndFavourites('chat');	
			return;
		}
		else
			 document.getElementById('chat_full_message_container').src = document.getElementById('chat_full_message_container').src;
	}
	else {
		// The web server gave us an error
	}
	ajax_req = null;
}

// Mail check
function mailCheckInterval(interval)
{
	setInterval("mailCheck()", interval);
}
function mailCheck()
{
	// Om det redan finns ett request, avbryt här
	if(ajax_req){
		return;
	}

	var asynchronous = true;
	newRequest();

	var encoded = "";
	encoded = "action=mailcheck";

	ajax_req.open("POST", "/ajax_handler.php", asynchronous);
	ajax_req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');	
	ajax_req.onreadystatechange = mailCheck_handler;
	ajax_req.send(encoded);
}
function mailCheck_handler()
{
  if (ajax_req.readyState != 4) {
    // We're not ready yet.
    return;
  }
  if (ajax_req.status == 200) {
    if (ajax_req.responseText) {
      // The good stuff happens here!
	 var count = ajax_req.responseText;
	 if(count > 0)
	 {
		if(document.getElementById('messageIcon'))
		{
			var icon = document.getElementById('messageIcon');
			icon.src = '/gfx/mail_active.gif';
			icon.title = 'Du har ' + count + ' st olästa meddelanden';
		}
	 }
    }
  } else {
    // The web server gave us an error
  }
  ajax_req = null;
}

function doPostChatMessageQuick()
{
	if(document.getElementById('quick_chat_message').value=='Skriv här' || document.getElementById('quick_chat_message').value=='')
	{
		alert("Skriv ett meddelande först");
		return false;
	}
	else
	{
		postChatMessage(document.getElementById('quick_chat_message').value, 'small');
	}
	return false;
}

