
String.prototype.trim = function ()
{
	var ret = this.replace (/(^\s*)|(\s*$)/g, "");
	return ret;
}

/*
function attachEvent(obj,ev,func)
{
	if (obj.attachEvent)
		obj.attachEvent(ev,func);
	else
	{
		obj.addEventListener(ev.replace(/^on/gi,''),func,false);
	}
}
*/
function LoadHotkeys()
{
	//attachEvent(document,'onkeydown',captureKey);
}

function captureKey(evt)
{
	var evt = evt || event;
	var trg = evt.target || evt.srcElement;
	var srcTag = trg.tagName;
	var keycode = evt.keyCode || evt.which;

	if (srcTag.toLowerCase() != 'textarea' && srcTag.toLowerCase() != 'input')
	{
		switch (keycode)
		{
			case 81:
				document.location.href = BlogUrl;
				break;

			case 82:
			case 114:
				document.location.href = BlogUrl + '/reader/';
			break;

			case 83:
			case 115:
				document.location.href = BlogUrl + '/stats/';
			break;

			case 84:
			case 116:
				document.location.href = BlogUrl + '/talks/';
			break;
		}
	}
}

function initPage()
{
	LoadHotkeys();
	var name = getCookie("MYWIZZBLOG_NAME");
	var email = getCookie("MYWIZZBLOG_EMAIL");
	var homepage = getCookie("MYWIZZBLOG_HOMEPAGE");
	var frms = ['comment-form','talk-form'];

	for (var fname in frms)
	{
		var oForm = document.getElementById(frms[fname]);
		if (oForm)
		{
			oForm.elements['comment-name'].value = name;
			oForm.elements['comment-email'].value = email;
			oForm.elements['comment-homepage'].value = homepage;
		}
		try
		{
			miniTracker();
		}
		catch(e){}
	}

	CodeHighlighter.init();
}


function setCookie(name,value,expiredays)
{
	var todayDate = new Date();
	todayDate.setDate(todayDate.getDate() + expiredays);
	document.cookie = name + "=" + escape(value) + "; path=/; expires=" + todayDate.toGMTString() + ";"
}

function getCookie(name)
{
	var nameOfCookie = name + "=";
	var x = 0;
	while (x <= document.cookie.length)
	{
		var y = (x+nameOfCookie.length);
		if (document.cookie.substring( x, y ) == nameOfCookie)
		{
			if ((endOfCookie=document.cookie.indexOf(";",y)) == -1)
			{
				endOfCookie = document.cookie.length;
			}
			return unescape(document.cookie.substring(y,endOfCookie));
		}

		x = document.cookie.indexOf(" ",x) + 1;
		if (x == 0)
		{
			break;
		}
	}
	return "";
}



function getContentHeight(oImg)
{
    var nHeight;
    var o = document.getElementById("blog-main");
    try
    {
        nHeight = document.defaultView.getComputedStyle(o, '').getPropertyValue("height");
    }
    catch(e)
    {
        nHeight = o.offsetHeight + "px";
    }

    return nHeight;
}


function postComment(oForm,oButton)
{
	var name = oForm.elements['comment-name'].value;
	var email = oForm.elements['comment-email'].value;
	var homepage = oForm.elements['comment-homepage'].value;
	var contents = oForm.elements['comment-contents'].value;
	if (name.trim() && contents.trim())
	{
		setCookie("MYWIZZBLOG_NAME",name,365);
		setCookie("MYWIZZBLOG_EMAIL",email,365);
		setCookie("MYWIZZBLOG_HOMEPAGE",homepage,365);
		oButton.disabled = true;
		oForm.submit();
	}
	return;
}

function postTalk(oForm)
{
	var name = oForm.elements['comment-name'].value;
	var email = oForm.elements['comment-email'].value;
	var homepage = oForm.elements['comment-homepage'].value;
	var contents = oForm.elements['comment-contents'].value;
	if (name.trim() && contents.trim())
	{
		setCookie("MYWIZZBLOG_NAME",name,365);
		setCookie("MYWIZZBLOG_EMAIL",email,365);
		setCookie("MYWIZZBLOG_HOMEPAGE",homepage,365);
		oForm.submit();
	}
	return;
}

function selectTrackbackUrl(obj)
{
	obj.select();
}


var gCommentId = null;
var gCommentForm = null;
function replyComment(obj,comment_id,type)
{
	var t = type == undefined ? 'comment' : type;

	var oForm = document.getElementById(t + '-form');
	if (gCommentForm == null)
		gCommentForm = oForm;

	if (gCommentId == comment_id)
	{
		var oCon = document.getElementById('blog-'+ t + '-form');
		if (oCon != null)
		{
			oCon.appendChild(gCommentForm);
			gCommentForm.elements['parent_id'].value = '';
			gCommentId = null;
		}
		return;
	}

	var oRe = document.getElementById('recomment-'+comment_id);
	if (oRe != null)
	{
		gCommentId = comment_id;
		oRe.className = 'blog-'+t+'-form';
		oRe.appendChild(gCommentForm);
		gCommentForm.elements['parent_id'].value = comment_id;
	}
}





function miniTracker()
{
	var ustr = navigator.userAgent;
	var info = [];
	var is_safari = ustr.indexOf("Safari") != -1;
	var is_op = ustr.indexOf('Opera') != -1;
	var is_ie = ustr.indexOf('MSIE') != -1;
	var is_ff = ustr.indexOf('Firefox') != -1;

	if (is_op)
	{
		info['ua'] = 'Opera';
		info['ver'] = new RegExp('Opera ([0-9.]+)','gi').exec(ustr)[1];
	}
	else if (is_ie)
	{
		info['ua'] = 'IE';
		info['ver'] = new RegExp('MSIE ([0-9.]+)','gi').exec(ustr)[1];
	}
	else if (is_ff)
	{
		info['ua'] = 'Firefox';
		info['ver'] = new RegExp('Firefox\/([0-9.]+)','gi').exec(ustr)[1];
	}
	else if (is_safari)
	{
		info['ua'] = 'Safari';
		info['ver'] = new RegExp('Safari\/([0-9.]+)','gi').exec(ustr)[1];
	}
	else
	{
		info['ua'] = 'Unknown';
		info['ver'] = '';
	}

	var refer			= document.referrer || '';
	var url				= document.location.href.replace(new RegExp(BlogUrl,'gi'),'');
	info['lang'] 		= navigator.userLanguage ? navigator.userLanguage : navigator.language ? navigator.language : '';
	info['resolution'] 	= screen.width && screen.height ? screen.width + 'x'+ screen.height : '';
	info['color_depth'] = screen.colorDepth ? screen.colorDepth : '';
	info['platform'] 	= navigator.platform ? navigator.platform : '';
	info['url']			= escape(url.replace(new RegExp('#.+?$','gi'),''));
	info['refer']		= escape(refer.indexOf(BlogUrl) == -1 ? refer : '');

	data = [];
	for (var i in info)
		data.push(i+'='+info[i]);

	var s = data.join("&");
	var ajax = new Ajax();
	ajax.loadText(BlogUrl + '/miniTracker.php?rnd='+Math.random(),'POST',s);
}





//------------------------------------------------------------------
//	AJAX
//	'another' simple class by wizz
//------------------------------------------------------------------
function Ajax()
{
	_ajax = this;
	this.xmlhttp = null;
	this.method = '';
	this.async = true;
	this.url = '';
	this.handler = this.nullify;
	this.init();
}

Ajax.prototype = {
	nullify:function(s){},
	init:function()
	{
		if (this.xmlhttp)
			return this.xmlhttp;

		var xmlhttp;
		if (window.ActiveXObject)
			xmlhttp = new ActiveXObject('Microsoft.XMLHTTP');
		else if (window.XMLHttpRequest)
			xmlhttp = new XMLHttpRequest();
		else
			xmlhttp = null;
		return xmlhttp;
	},

	loadText:function(url,method,data)
	{
		this.data = data;
		this.reponseType = 'text';
		this.load(url,method);
	},

	loadXML:function(url,method,data)
	{
		this.data = data;
		this.reponseType = 'xml';
		this.load(url,method);
	},

	load:function(url,method)
	{
		this.xmlhttp = this.xmlhttp || this.init();
		this.method = method == undefined ? 'GET' : method;
		this.url = url;
		window.setTimeout(this.sendRequest,10);
	},

	sendRequest:function()
	{
		if (_ajax.async)
			_ajax.xmlhttp.onreadystatechange = _ajax.onStateChange;

		_ajax.xmlhttp.open(_ajax.method,_ajax.url,_ajax.async);

		if (_ajax.method == 'POST')
			_ajax.xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
		try
		{
			_ajax.xmlhttp.overrideMimeType("text/xml");
		}
		catch(e){}
		_ajax.xmlhttp.send(_ajax.method == 'POST' ? _ajax.data : null);
	},

	bind:function(func)
	{
		_ajax.handler = func;
	},

	onStateChange:function()
	{
		if (_ajax.xmlhttp.readyState == 4)
		{
			if (_ajax.xmlhttp.status == 200)
				_ajax.handler(_ajax.responseType == 'xml' ? _ajax.xmlhttp.responseXML : _ajax.xmlhttp.responseText);
			else
				_ajax.error();
		}
	},
	error:function()
	{
	}
}



function music(path)
{
	var s = '<object classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" width="400" height="24"><param name="movie" value="/add-ons/musicbox.swf"/><param name="menu" value="false"/><param name="quality" value="high"/><param name="allowScriptAccess" value="sameDomain"/><param name="play" value="true"/><param name="wmode" value="transparent"/><param name="flashvars" value="musicList='+path+'"/><embed swLiveConnect="true" flashvars="musicList='+path+'" src="/add-ons/musicbox.swf" quality="high" bgcolor="" wmode="transparent" width="400" height="24" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
	document.write(s);
}



function showFlashObject(id,width,height,movie,flashvars,style)
{
	var s = '<object id="'+id+'" classid="clsid:D27CDB6E-AE6D-11CF-96B8-444553540000" width="'+width+'" height="'+height+'" style="'+style+'"><param name="movie" value="'+movie+'"/><param name="menu" value="false"/><param name="quality" value="high"/><param name="allowScriptAccess" value="sameDomain"/><param name="play" value="true"/><param name="wmode" value="transparent"/><param name="flashvars" value="'+flashvars+'"/><embed swLiveConnect="true" flashvars="'+flashvars+'" src="'+movie+'" quality="high" bgcolor="" wmode="transparent" width="'+width+'" height="'+height+'" name="'+id+'" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed></object>';
	document.write(s);
}