// General browser detection routines
var dyn = document.getElementById ? true : false;
var ie4 = document.all ? true : false;
var ns4 = document.layers ? true : false;

// Image rollover variables
var outPrefix  = '_out';
var overPrefix = '_over';
var oldImg     = '';
var oldColor   = '';

// Modify stylesheet definitions for MSIE 4 and up.
if(navigator.appName == "Microsoft Internet Explorer" && document.styleSheets){
  document.styleSheets[1].rules[11].style.border = "none";
}

// Menu rollover variables
var oldBackgroundColor = "#9AAEC6";


// Menu functions
function menuOver(obj){
  obj.style.backgroundColor = "#879DB4";
  obj.style.cursor = "hand";
}

function menuOut(obj){
  obj.style.backgroundColor = oldBackgroundColor;
  obj.style.cursor = "default";
}

// Rollover functions
function mOver(imgName){
  oldImg = document.images[imgName].src;
  suffix = oldImg.substr(0,oldImg.lastIndexOf(outPrefix));
  prefix = oldImg.substr(oldImg.lastIndexOf('.'));
  newImg = suffix + overPrefix + prefix;
  document.images[imgName].src = newImg;
}

function mOut(imgName){
  if(oldImg == '')
    return false;
  document.images[imgName].src = oldImg;
}

function fOver(obj,color){
  if(!color)
    color = "#DEE7F2";
  obj.style.backgroundColor = color;
  //obj.style.cursor = "hand";
}

function fOut(obj,color){
  if(!color)
    color = "#F2F6F9";
  obj.style.backgroundColor = color;
  //obj.style.cursor = "default";
}

// Screenshot and gallery functions
function viewScreen(area,id,img){
  if(parseInt(navigator.appVersion) < 4)
    return true;
  myURL = '/viewScreen.php?area='+area+'&id='+id+'&img='+img;
  x = 1280; xpos = (screen.width - x) / 2;
  y = 720; ypos = (screen.height - y) / 2;
  window.open(myURL,'viewScreen','top='+ypos+',left='+xpos+',width='+x+',height='+y+',toolbars=no,scrollbars=yes,status=no,resizable=no');
  return false;
}

// Forum functions

var clientPC = navigator.userAgent.toLowerCase();
var clientVer = parseInt(navigator.appVersion);

var is_ie = ((clientPC.indexOf("msie") != -1) && (clientPC.indexOf("opera") == -1));
var is_nav  = ((clientPC.indexOf('mozilla')!=-1) && (clientPC.indexOf('spoofer')==-1)
                && (clientPC.indexOf('compatible') == -1) && (clientPC.indexOf('opera')==-1)
                && (clientPC.indexOf('webtv')==-1) && (clientPC.indexOf('hotjava')==-1));

var is_win   = ((clientPC.indexOf("win")!=-1) || (clientPC.indexOf("16bit") != -1));
var is_mac    = (clientPC.indexOf("mac")!=-1);

var bbcode = new Array();
var bbtags = new Array('[b]','[/b]','[i]','[/i]','[u]','[/u]','[quote]','[/quote]','[code]','[/code]','[img]','[/img]','[url]','[/url]','[email]','[/email]','[s]','[/s]');
var imageTag = false;

var b_help = "Lihavoitu teksti: [b]teksti[/b]";
var i_help = "Kursivoi teksti: [i]teksti[/i]";
var u_help = "Alleviiva teksti: [u]teksti[/u]";
var s_help = "Spoiler-teksti: [s]teksti[/s]";
var q_help = "Lainaa teksti: [quote]teksti[/quote]";
var c_help = "Näytä koodimuodossa: [code]koodi[/code]";
var p_help = "Lisää kuva: [img]http://kuvan_osoite[/img]";
var w_help = "Lisää linkki: [url]http://osoite[/url] tai [url=http://osoite]teksti[/url]";
var a_help = "Sulje kaikki avoimet BBKoodi-tagit";

function storeCaret(text){
  if (text.createTextRange) {
    text.caretPos = document.selection.createRange().duplicate();
  }
} 

function addText(text) {
  if (document.postmodify.message.createTextRange && document.postmodify.message.caretPos) {     
    var caretPos = document.postmodify.message.caretPos;     
    caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
  }else{
    document.postmodify.message.value += text;
  } 
} 

function mozWrap(txtarea, open, close){
	var selLength = txtarea.textLength;
	var selStart = txtarea.selectionStart;
	var selEnd = txtarea.selectionEnd;
	if (selEnd == 1 || selEnd == 2){
		selEnd = selLength;
	}
	var s1 = (txtarea.value).substring(0,selStart);
	var s2 = (txtarea.value).substring(selStart, selEnd)
	var s3 = (txtarea.value).substring(selEnd, selLength);
	txtarea.value = s1 + open + s2 + close + s3;
	return;
}

function bbstyle(bbnumber, append, close) {
	var txtarea = document.postmodify.message;

	theSelection = false;
	bblast = 0;

	if(bbnumber == -1){
		while (bbcode[0]) {
			codeNum = arraypop(bbcode) - 1;
			txtarea.value += bbtags[codeNum + 1];
		}
		imageTag = false;
		txtarea.focus();
		return;
	}
	if(append == 'undefined' || append == '' || append == null){
		start = bbtags[bbnumber];
	}else{
		str = bbtags[bbnumber];
		start = str.substring(0, str.length - 1) + append + str.substring(str.length-1);
	}
	if(close == 'undefined' || close == '' || close == null){
		donotinsert = false;
	}else{
		donotinsert = true;
	}

	if ((clientVer >= 4) && is_ie && is_win){
		theSelection = document.selection.createRange().text;
		if (theSelection){
			document.selection.createRange().text = start + theSelection + bbtags[bbnumber + 1];
			txtarea.focus();
			theSelection = '';
			return;
		}
	}else if (txtarea.selectionEnd && (txtarea.selectionEnd - txtarea.selectionStart > 0)){
		mozWrap(txtarea, start, bbtags[bbnumber+1]);
		return;
	}

	for(i = 0; i < bbcode.length; i++) {
		if (bbcode[i] == bbnumber + 1) {
			bblast = i;
			donotinsert = true;
		}
	}
	if(donotinsert){
		while (bbcode[bblast]){
				codeNum = arraypop(bbcode) - 1;
				txtarea.value += bbtags[codeNum + 1];
				// document.images['style' + bbnumber].src = '/img/forum/bbcode_style' + (bbnumber) + '.gif';
				imageTag = false;
			}
			txtarea.focus();
			return;
	}else{
		if (imageTag && (bbnumber != 10)) {
			txtarea.value += bbtags[11];
			lastValue = arraypop(bbcode) - 1;
			imageTag = false;
		}
		// document.images['style' + bbnumber].src = '/img/forum/bbcode_style' + (bbnumber + 1) + '.gif';
		txtarea.value += start;
		if ((bbnumber == 10) && (imageTag == false)) imageTag = 1;
		arraypush(bbcode,bbnumber+1);
		txtarea.focus();
		return;
	}
	storeCaret(txtarea);
}

function emoticon(text) {
	var txtarea = document.postmodify.message;
	text = ' ' + text + ' ';
	if (txtarea.createTextRange && txtarea.caretPos){
		var caretPos = txtarea.caretPos;
		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		txtarea.focus();
	}else{
		txtarea.value  += text;
		txtarea.focus();
	}
}


function bbcode_bold(){
	bbstyle(0);
}
function bbcode_italicize(){
	bbstyle(2);
}
function bbcode_underline(){
	bbstyle(4);
}
function bbcode_spoil(){
	bbstyle(16);
}
function bbcode_hyperlink(){
	bbstyle(12);
}
function bbcode_email(){
	bbstyle(14);
}
function bbcode_image(){
	bbstyle(10);
}
function bbcode_code(){
	bbstyle(8);
}
function bbcode_quote(){
	bbstyle(6);
}

function bbcode_close(){
	bbstyle(-1)
}

function helpline(help){
	if(dyn){
		document.getElementById('helpline').innerHTML = eval(help + '_help');
	}
}

// Replacement for arrayname.length property
function getarraysize(thearray) {
	for (i = 0; i < thearray.length; i++) {
		if ((thearray[i] == "undefined") || (thearray[i] == "") || (thearray[i] == null)){
			return i;
		}
	}
	return thearray.length;
}

function arraypush(thearray,value) {
	thearray[ getarraysize(thearray) ] = value;
}

function arraypop(thearray) {
	thearraysize = getarraysize(thearray);
	retval = thearray[thearraysize - 1];
	delete thearray[thearraysize - 1];
	return retval;
}

function smiley(legend){
  AddTxt = legend + ' ';
  addText(AddTxt);
}

function image() {
  AddTxt="[img]URL[/img]";
  addText(AddTxt);
} 

function ftp() {
  AddTxt="[ftp][/ftp]";
  addText(AddTxt);
} 

function underline() {
  var text = "";
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  AddTxt="[u]"+text+"[/u]";
  addText(AddTxt);
} 

function emai1() {
  AddTxt="[email][/email]";
  addText(AddTxt);
} 

function bold() {
  var text = "";
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  AddTxt="[b]"+text+"[/b]";
  addText(AddTxt);
} 
function italicize() {
  var text = "";
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  AddTxt="[i]"+text+"[/i]";
  addText(AddTxt);
} 

function hyperlink() {
  var text = "";
  myURL = prompt("Osoite","http://");
  if(!myURL)
    return;
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  if(text.length <= 0)
    text = prompt("Linkin teksti","Kirjoita linkin teksti tähän");
  AddTxt="[url="+myURL+"]"+text+"[/url]";
  addText(AddTxt);
} 

function showcode() {
  var text = "";
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  AddTxt="[code]"+text+"[/code]";
  addText(AddTxt);
} 

function showcolor(color) {
  var text = "";
  if(document.postmodify.message.caretPos)
    text = document.postmodify.message.caretPos.text;
  AddTxt="[color="+color+"]"+text+"[/color]";
  addText(AddTxt);
}

function confirmDelete(delURL){
  foo = confirm('Oletko varma että haluat poistaa viestin?');
  if(foo){
    location.href = '/keskustelut/' + delURL;
  }
}

function changeForum(obj){
  if(obj.value == 0){
    return false;
  }else{
    myURL = "/keskustelut/alue?fid=" + obj.value;
    location.href = myURL;
  }
}


