/* 
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the 
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.
 *
 * This program is distributed in the hope that it will be useful, but 
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General
 * Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

var addEvent;
var radioOn;
var LaceObj;
if (document.addEventListener) { // Non-IE
	addEvent = function(element, type, handler) {
		element.addEventListener(type, handler, null);
	};
} else if (document.attachEvent) { // IE6+
    addEvent = function(element, type, handler) {
        element.attachEvent("on" + type, handler);
    };
} else { // Older Browsers
    addEvent = function(element, type, handler) {
		var oldHandler = element['on' + type];
		if (oldHandler === null) {
			element['on' + type] = handler;
		} else {
			element['on' + type] = function(e) { oldHandler(e); handler(e); };
		}
	};
}

// Start Lace
addEvent(window, 'load', init);

function init() {
	if ($('lacecontrols')) {
		LaceObj = new Lace();
		scrollToBottom(document.getElementById('main'), true);
	}	
}


function scrollToBottom(el, force) {
	
	var bottom = el.scrollHeight - el.clientHeight;
	
	if (el.scrollTop == arguments.callee.scrollAtBottom || force === true) {
		el.scrollTop = bottom;
		arguments.callee.scrollAtBottom = bottom;
	}
	
/*
		var objDiv = document.getElementById('main');
		objDiv.scrollTop = objDiv.scrollHeight;
*/	
}		


function $() {
	var elements = new Array();
	for (var i = 0; i < arguments.length; i++) {
		var element = arguments[i];
		if (typeof element == 'string') {
			if (document.getElementById) {
				element = document.getElementById(element);
			} else if (document.all) {
				element = document.all[element];
			}
		}
		if (arguments.length == 1) {
			return element;
		}
		elements.push(element);
	}
	return elements;
}

showRadio = function() {
	var radioItem = $('radio');
	if(radioOn) {
		radioItem.innerHTML = '';
	} else {
//		radioItem.innerHTML = '<embed type="application/x-mplayer2"	pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"	Name="MediaPlayer"	src="http://radio.perak.org:8000"	AutoStart="1"	ShowStatusBar="1"	volume="-1"	height="50"	width="210"></embed>';
		radioItem.innerHTML = '<embed type="application/x-mplayer2"	pluginspage="http://www.microsoft.com/Windows/MediaPlayer/"	Name="MediaPlayer"	src="http://75.125.106.155:8000/"	AutoStart="1"	ShowStatusBar="1"	volume="-1"	height="50"	width="210"></embed>';
	}
	radioOn = !radioOn;
}

function showmuti(item){
	if (item.checked){
		$("face").style.display = "inline";
		$("face").innerHTML = "<div><h2>表情</h2></div>";
		for(i=0; i<img.length; i++) {
			var imgTag = document.createElement("img");
			imgTag.src = img[i];
			imgTag.onclick = function() {
				var path = this.src.split("/");
				var filename = path[path.length-1];
				var txt = filename.split(".");
				AddText("[face="+txt[0]+"]");
			}
			$("face").appendChild(imgTag);
		}
	}else{
		$("face").style.display = "none";
	}
}

function popField(item, typ) {
	if(item.checked) {
		item.checked = false;
		if(typ == "IMAGE") {
			var content = prompt("请输入图片网址!!","");
			if(content.length) {
				$("text").value = "[img="+content+"]";
				LaceObj.send();
			}
		} else {
			var content = prompt("请输入讯息内容!!","");
			if(content.length) {
				$("text").value = "/html "+content;
				LaceObj.send();
			}
		}
	}
	item.checked = false;
}

function slapTo(user) {
	$("text").value = "/me 连忙向 "+user+" 张大的嘴巴里扔了一只死苍蝇～";
	LaceObj.send();
}

function talkTo(user) {
	if(user != $('owner').innerHTML) {
		if($('talkto') != null) {
			if(user.length) 
				$('talkto').style.display = "";
			else
				$('talkto').style.display = "none";
				
			$('who').innerHTML = user;
			$('receiver').value = user;
			$('text').focus();
		}
	} else {
		clearTalkto();
	}
}

function clearTalkto() {
	$('receiver').value = "";
	$('talkto').style.display = "none";
	$('text').focus();
}

function AddText(NewCode){
	$("advcheck").checked = false;
	$("face").style.display = "none";
	$("text").value += NewCode;
}

Array.prototype.linearSearchI = function(target) {
	target = target.toLowerCase();
	for (var i = 0; i < this.length; i++) {
		var name = this[i].toLowerCase();
		if (target == name)
			return true;
	}
	return false;
};

String.prototype.trim = function() { 
	return this.replace(/^\s+/g, '').replace(/\s+$/g, '');
};

function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
        begin = dc.indexOf(prefix);
        if (begin != 0) return null;
    } else {
        begin += 2;
    }
    var end = document.cookie.indexOf(";", begin);
    if (end == -1) {
        end = dc.length;
    }
    return unescape(dc.substring(begin + prefix.length, end));
}

function deleteCookie(name, path, domain) {
    if (getCookie(name)) {
        document.cookie = name + "=" +
            ((path) ? "; path=" + path : "") +
            ((domain) ? "; domain=" + domain : "") +
            "; expires=Thu, 01-Jan-70 00:00:01 GMT";
    }
}

var scrl  = "（吉隆坡）商用車輛執照局採取3項行動。";
function titlebar() {
	scrl = scrl.substring(1, scrl.length) + scrl.substring(0, 1);
	document.title = scrl;
	setTimeout("titlebar()", 300);
} 
// titlebar("（吉隆坡）商用車輛執照局採取3項行動。");
// titlebar();

var username = "bl00d";
function setusername(setuser) {
	var u = document.getElementById("username");
	u.innerHTML = setuser;
	username = setuser;
}