var UA = navigator.userAgent.toLowerCase();
var OS = detectOS();
var BRW = detectBrowser();

//alert('OS: '+OS+' - BRW: '+BRW);

/* PNG HACK */

var transparentimage = "/img/_one_pixel_image.gif";

var needhack = needhack();

function pnghack(element) {
	if (!needhack) {
		return;
	}
	var src = element.src;
	if (src.indexOf(transparentimage) != -1) {
		return;
	}
	if (src.indexOf(".png") == -1) {
		element.runtimeStyle.filter = "";
		return;
	}
	element.src = transparentimage;
	element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

function needhack() {
	var pos = navigator.userAgent.indexOf("MSIE ");
	if (pos == -1) {
		return false;
	}
	var version = navigator.userAgent.substring(pos + 5);
	return (((version.indexOf("5.5") == 0) || (version.indexOf("6") == 0)) && (navigator.platform == ("Win32")));
}

/* PNG HACK END */

function detectOS() {
	if (checkIt('linux')) return "LINUX";
	else if (checkIt('x11')) return "UNIX";
	else if (checkIt('mac')) return "MAC"
	else if (checkIt('win')) return "WIN"
	else return "UNKNOWN";		
}

function detectBrowser() {
	if (checkIt('msie')) return "IE";
	else if (checkIt('firefox')) return "FIREFOX";
	else if (checkIt('safari')) return "SAFARI";
	else if (checkIt('opera')) return "OPERA";			
	else return "UNKNOWN";		
}

function checkIt(str) {	
	return UA.indexOf(str) + 1; 
}

//
// POPUP WINDOW
// 

function popWin(url,h,w,resize) {
	if (!h) h = 434;
	if (!w) w = 554;
	if (!resize) resize = 0;
	l = (screen.width-w)/2; 
	t = (screen.height-h)/2;	
	var winId = window.open(url,"_blank","toolbar=0,scrollbars=0,location=0,status=0,menubar=0,resizable="+resize+",width="+w+",height="+h+",left="+l+",top="+t);	
	winId.focus();	
}

//
// BUTTON STATES
//

function mouseDownState(e) {
	e.className = 'button_click';
	e.style.cursor = 'pointer'; 
	e.style.cursor = 'hand';
	if (BRW == 'IE') e.style.paddingTop = '1px';
}

function mouseUpState(e) {
	e.className = 'button';	
	e.style.cursor = 'pointer'; 
	e.style.cursor = 'hand';	
	if (BRW == 'IE') e.style.paddingTop = '0px';
}

//
// FORM
//

function submitFormById(fid) {
	var frmObj = document.getElementById(fid);
	if (validateFrm(frmObj,fid)) frmObj.submit();
}

function checkAll(chkbox) {
	var mailboxTable = document.getElementById('form_list');	
	var chkboxArr = mailboxTable.getElementsByTagName("INPUT");
	var checkedStatus = chkbox.checked;
	for (var i=0; i < chkboxArr.length; i++) { 
		chkboxArr[i].checked = checkedStatus;
	}
}

function setFocusByFormId(fid) {
	var searchElement = true;
	var frm = document.getElementById(fid);
	for (var i=0; i<frm.length && searchElement; i++) {
		var eTagname = frm.elements[i].tagName.toLowerCase();
		var eType = frm.elements[i].type.toLowerCase();		
		if (eTagname == 'select' || eTagname == 'textarea' || eType == 'text') {
			frm.elements[i].focus();
			searchElement = false;
		}
	}
}

//
// INSTANT MESSAGE
//

var curChatKey;
var curUID;

function requestIM(uid,user,chatKey) {
	curUID = uid;
	curChatKey = chatKey;
	showConfirmBox();
	document.getElementById('im_request_user').innerHTML = user;
}

function acceptIM() {
	hideConfirmBox();
	acceptWaitingIM(curChatKey);
	popWin('im.php?u=' + curUID + '&chatKey=' + curChatKey);
}

function declineIM() {
	hideConfirmBox();
	declineWaitingIM(curChatKey);
}

//
// SITE DIM
//

var dimSrc;

function setDim() {		
	var dimObj = document.getElementById("dim");
	var offsetH = (BRW=="IE") ? parseInt(document.body.offsetHeight)-6 : parseInt(document.body.offsetHeight);
	var scrollH = parseInt(document.body.scrollHeight);
	dimObj.style.width = (BRW=="IE") ? "100%" : parseInt(document.body.offsetWidth)+8;
	dimObj.style.height = (offsetH > scrollH) ? offsetH : scrollH;
	posBoxes();
}

function posBoxes() {	
	var confirmBoxObj = document.getElementById("confirmDiv");
	var previewBoxObj = document.getElementById("previewDiv");
	var helpBoxObj = document.getElementById("helpDiv");
	var lConfirm = (document.body.offsetWidth/2) - (250/2);
	var lPreview = (document.body.offsetWidth/2) - (520/2);
	var lHelp = (document.body.offsetWidth/2) - (520/2);
	confirmBoxObj.style.left = (BRW=="IE") ? parseInt(lConfirm)-5 : parseInt(lConfirm)+4;
	confirmBoxObj.style.top = 180;	
	previewBoxObj.style.left = (BRW=="IE") ? parseInt(lPreview)-5 : parseInt(lPreview)+4;
	previewBoxObj.style.top = 180;		
	helpBoxObj.style.left = (BRW=="IE") ? parseInt(lHelp)-5 : parseInt(lHelp)+4;
	helpBoxObj.style.top = 180;		
	window.scrollTo(0,0);
}

function pngTransIE() {
	if (OS == "WIN" && BRW == "IE") {
		var dimObj = document.getElementById("dim");
		if (dimObj.src.indexOf("/img/_one_pixel_image.gif") == -1) dimSrc = dimObj.src;
		dimObj.src = "/img/_one_pixel_image.gif";
		dimObj.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src=\'" + dimSrc + "\',sizingMethod=\'scale\')";
	}	
}

//
// CONFIRM BOX 
//

function showConfirmBox() {	
	setDim();
	pngTransIE();		
	var dimObj = document.getElementById("dimDiv");
	var confirmBoxObj = document.getElementById("confirmDiv");	
	dimObj.style.display = "block";
	confirmBoxObj.style.display = "block";
	document.body.onresize = function() { setDim(); } 
}

function hideConfirmBox() {
	var dimObj = document.getElementById("dimDiv");
	var confirmBoxObj = document.getElementById("confirmDiv");	
	dimObj.style.display = "none";
	confirmBoxObj.style.display = "none";	
	document.body.onresize = null;
}

//
// PREVIEW BOX
//

function showPreviewBox(elm) {	
	setDim();
	pngTransIE();		
	var dimObj = document.getElementById("dimDiv");
	var previewBoxObj = document.getElementById("previewDiv");	
	dimObj.style.display = "block";
	previewBoxObj.style.display = "block";
	document.body.onresize = function() { setDim(); } 
	// Update HTML to preview
	var previewObj = document.getElementById(elm);
	var previewHTML = previewObj.value.replace(/\n/g,'<br />');
	document.getElementById("preview_box").innerHTML = previewHTML;
}

function hidePreviewBox() {
	var dimObj = document.getElementById("dimDiv");
	var previewBoxObj = document.getElementById("previewDiv");	
	dimObj.style.display = "none";
	previewBoxObj.style.display = "none";	
	document.body.onresize = null;
}


//
// HELP BOX
//

function showHelpBox() {	
	setDim();
	pngTransIE();		
	var dimObj = document.getElementById("dimDiv");
	var helpBoxObj = document.getElementById("helpDiv");	
	dimObj.style.display = "block";
	helpBoxObj.style.display = "block";
	document.body.onresize = function() { setDim(); } 
}

function hideHelpBox() {
	var dimObj = document.getElementById("dimDiv");
	var helpBoxObj = document.getElementById("helpDiv");	
	dimObj.style.display = "none";
	helpBoxObj.style.display = "none";	
	document.body.onresize = null;
}
/*
Dette er et bilelde:
<br>
<img src="http://www.yellowpixels.eu/img/content/photo/orginal/070220054142-04558bdd79b94fd400d035ed00e8b6b2.jpg">
<br><br>
...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut ...nu er billdet slut 
*/