// "WSM v2.0" /MAIN JS/ (23.01.2009)

if (document.all) { agent = "IE"; }
if (navigator.userAgent.indexOf("Opera")!=-1 && document.getElementById) { agent = "OP"; }
if (!document.all && document.getElementById) { agent = "MO"; }

function isNull(val) { return (val == null); }
function _(e) { return document.getElementById(e)?document.getElementById(e):false; }

function addOnloadEvent(fnc)
{
	if (typeof window.addEventListener != "undefined")
	{ window.addEventListener( "load", fnc, false ); }
	else if (typeof window.attachEvent != "undefined")
	{ window.attachEvent("onload", fnc); }
	else
	{
	    if (window.onload != null)
	    {
				var oldOnload = window.onload;
				window.onload = function (e) { oldOnload(e); window[fnc](); }
		}
		else window.onload = fnc;
	}
}

// System functions

function GetObj(id)
{
	if (agent == "IE") { obj = "document.all." + id; }
	if (agent == "MO" || agent == "OP") { obj = "document.getElementById('" + id + "')"; }
	return obj;
}
function PageOffset(id)
{
	eval("obj = " + GetObj(id));
	var PLeft = 0; var PTop = 0;
	do { PLeft += obj.offsetLeft; PTop += obj.offsetTop; } while(obj = obj.offsetParent);
	return [PLeft,PTop];
}
function GetLeft(id) { res = PageOffset(id)[0]; return res; }
function GetTop(id) { res = PageOffset(id)[1]; return res; }
function GetWidth(id) { eval("res = " + GetObj(id) + ".offsetWidth"); return res; }
function GetHeight(id) { eval("res = " + GetObj(id) + ".offsetHeight"); return res; }
function ShowLayer(id,action) { obj = GetObj(id); eval(obj + ".style.display='block'"); eval(obj + ".style.visibility='" + action + "'"); }
function MoveLayer(id,left,top) { eval(GetObj(id) + ".style.left= '" + left + "px'; " + GetObj(id) + ".style.top = '" + top + "px'"); }

// Menu functions

var objsArray = new Array(); var posArray = new Array(); var subMenu = new Array(); var fromMenu; var how = 0;

function tMenu() { tm = setTimeout("hMenu()",1000); }
function cMenu() { if(self.tm) { clearTimeout(tm); } }
function hMenu() { for (i=0;i<how;i++) { ShowLayer(subMenu[i],'hidden'); } }
function sMenu(objs,pos)
{
	if ((objs != "") && (pos != ""))
	{
		var objsArray = objs.split("-")
		var posArray = pos.split("-")
		for (how=0;how<objsArray.length;how++)
		{
			fromMenu = 'a'+objsArray[how];
			subMenu[how] = 'd'+objsArray[how];
			if (how > 0) { pos = posArray[1] } else { pos = posArray[0] }
			if (pos == 0) { MoveLayer(subMenu[how],GetLeft(fromMenu),GetTop(fromMenu) + GetHeight(fromMenu)); }
			if (pos == 1) { MoveLayer(subMenu[how],GetLeft(fromMenu) - GetWidth(subMenu[how]),GetTop(fromMenu)); }
			if (pos == 2) { MoveLayer(subMenu[how],GetLeft(fromMenu),GetTop(fromMenu) - GetHeight(subMenu[how])); }
			if (pos == 3) { MoveLayer(subMenu[how],GetLeft(fromMenu) + GetWidth(fromMenu),GetTop(fromMenu)); }
			ShowLayer(subMenu[how],'visible');
		}
	}
}

// News functions

for(i=0;i<=5;i++) { eval('var v'+i+' = new Image(); v'+i+'.src = \'/imgs/vote-'+i+'.gif\';'); }
function VoteDefine(id,event)
{
	x = 0; width = 16;
	if (agent == "MO") { var vsi = _("vsi"+id); while (vsi) { x += vsi.offsetLeft; vsi = vsi.offsetParent; } x = event.pageX - x; } else { x = event.offsetX; }
	value = Math.ceil(x/width) || 1;
	return value;
}
function VoteShow(id,value) { _("vii"+id).src = "/imgs/vote-" + value + ".gif"; }
function VoteOver(id,event)
{
	value = VoteDefine(id,event);
	VoteShow(id,value);
}
function VoteClick(id,event)
{
	value = VoteDefine(id,event);
	new AJAX('/news/ajax/',{dataSend: 'operation=vote&id='+id+'&value='+value, onLoading: 'voteLoading('+id+')', onError: 'voteError('+id+')'});
}
function ShowNews(id)
{
	new AJAX('/news/ajax/',{dataSend: 'operation=show&id='+id, onLoading: 'result = true', onError: 'result = false'});
}
function LimitText(obj,max,id)
{
	str = obj.value;
	len = str.length;
	if (len > max) { obj.value = str.substring(0,max); }
	_(id).innerHTML = max - str.length;
	return true;
}

// Ajax functions

Function.prototype.bind = function(object) { var __method = this; return function() { return __method.apply(object,arguments); } }
var Class = { create: function() { return function() { this.initialize.apply(this,arguments); } } }
AJAX = Class.create();
AJAX.prototype = {
	initialize: function(url,options)
	{
		this.transport = this.getTransport();
		this.method = options.method || 'post'; this.method = this.method.toLowerCase();
		this.divOut = _(options.divOut) || null;
		this.dataSend = options.dataSend || '';
		this.jsRun = options.jsRun || null;
		this.onLoading = options.onLoading || 'msgLoading();';
		this.onError = options.onError || 'msgError();';
		this.onComplete = options.onComplete || null;
		this.execute(url);
	},
	execute: function(url)
	{
		if (this.onLoading) { eval(this.onLoading); }
		if (this.method == "get")
		{
			this.transport.open(this.method,url+"?"+this.dataSend,true);
			this.transport.onreadystatechange = this.onStateChange.bind(this);
			this.transport.send(null);
		}
		if (this.method == "post")
		{
			this.transport.open(this.method,url,true);
			this.transport.onreadystatechange = this.onStateChange.bind(this);
			this.transport.setRequestHeader("X-Requested-With","XMLHttpRequest");
			this.transport.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
			this.transport.setRequestHeader("Content-Length",this.dataSend.length);
			if (this.transport.overrideMimeType) { this.transport.setRequestHeader("Connection","Close"); }
			this.transport.send(this.dataSend);
		}
	},
	onStateChange: function()
	{
		if (this.transport.readyState == 4)
		{
			if (this.transport.status >= 200 && this.transport.status < 300)
			{
				if (this.divOut) { setTimeout(function(){this.divOut.innerHTML = this.transport.responseText;}.bind(this),10); } else { if (this.onComplete) { setTimeout(function(){this.onComplete(this.transport);}.bind(this),10); } else { eval(this.transport.responseText); } }
				if (this.jsRun) { eval(this.jsRun); }
				this.transport.onreadystatechange = function(){};
			} else { if (this.onError) { eval(this.onError); } }
		}
	},
    getTransport: function() { if (window.ActiveXObject) { return new ActiveXObject('Microsoft.XMLHTTP'); } else if (window.XMLHttpRequest) { return new XMLHttpRequest(); } else { return false; } }
};

// Other functions

function GetFORM(form) { formdata = ""; for (i=0;i<form.length;i++) { if (form.elements[i].name != "") { if (form.elements[i].type == "checkbox") { if (form.elements[i].checked) { formdata = formdata + encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value) + "&"; } } else if (form.elements[i].type == "radio") { if (form.elements[i].checked) { formdata = formdata + encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value) + "&"; } } else if (form.elements[i].type == "select-multiple") { for(j=0;j<form.elements[i].options.length;j++) { if (form.elements[i].options[j].selected) { formdata = formdata + encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].options[j].value) + "&"; } } } else { formdata = formdata + encodeURIComponent(form.elements[i].name) + "=" + encodeURIComponent(form.elements[i].value) + "&"; } } } return formdata; }
function MarkFORM(id) { _(id).className = "lost"; _(id).focus(); }
function UnMarkFORM(id) { _(id).className = "must"; }
function Captcha(img,url) { var mytime = new Date(); document.images[img].src = url + "?rnd=" + mytime.valueOf(); }


// Login functions

function Login() {
	if (/MSIE (5\.5|6|7).+Win/.test(navigator.userAgent)) { return true; }
	else
	{
		if (agent == "IE") { ScrWidth = document.body.offsetWidth; ScrHeight = screen.height - 200; }
		if (agent == "MO" || agent == "OP") { ScrWidth = window.innerWidth; ScrHeight = window.innerHeight; }
		ScrLeft = (ScrWidth - 370) / 2;
		ScrTop = (ScrHeight - 270) / 2;
		MoveLayer("login",ScrLeft,ScrTop);
		ShowLayer('login','visible');
		return false;
	}
}
function Cancel() { ShowLayer('login','hidden'); }

function parseGET(url)
{  
	if(!url || url == '') url = document.location.search;
	if(url.indexOf('?') < 0) return Array();
 
	url = url.split('?');
	url = url[1];
 
	var GET = [];
	var params = [];
	var keyval = [];
 
	if(url.indexOf('#')!=-1)    
	{    
		anchor = url.substr(url.indexOf('#')+1); 
		url = url.substr(0,url.indexOf('#'));
	}
 
	if(url.indexOf('&') > -1) params = url.split('&');
	else params[0] = url;
 
	for (i=0; i<params.length; i++)
	{
		if(params[i].indexOf('=') > -1) keyval = params[i].split('=');
		else { keyval[0] = params[i]; keyval[1] = true; }
		GET[keyval[0]]=keyval[1];
	}
 
	return (GET); 
};
 
 
var $_GET = parseGET();