function redirect(url){
	window.location=url;
}
function complain(id) { window.open('/complain/'+id,'complain','width=600,height=400,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10'); }
function inbox() { window.open('http://ogl.ru/project/users/inbox.php','user','width=600,height=560,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10'); }
function message(user_id) { window.open('http://ogl.ru/project/users/inbox.php?user_id='+user_id,'user','width=600,height=560,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10'); }
function playing(id) { window.open('http://ogl.ru/project/users/playing.php?game_id='+id,'user','width=600,height=500,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10'); }
function profile() { window.open('http://ogl.ru/project/users/edit.php','user','width=600,height=500,toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10'); }
function popup(url,w,h) {window.open(url,'','width=' + w + ',height=' + h + ',toolbar=0,directories=0,menubar=0,status=0,resizable=1,location=0,scrollbars=yes,copyhistory=0,top=10,left=10');}

function addme (mode,game_id) {
	if (user_online) {
		document.location="http://ogl.ru/project/users/market/add.php?game_id="+game_id+"&mode="+mode;
	} else {
		alert("Пожалуйста, вначале зарегистрируйтесь.");
	}
}

function add2my (id) {
	if (user_online) {

		var msg = "";

		if (arguments[1]) msg = "Удалить форум из списка Мои форумы?";
		else msg = "Добавить форум в список Мои форумы?";

		if (confirm(msg)) {
			document.location="/add2my.php?forum_id="+id;
		}
	} else {
		if (confirm("Создавать список Моих форумов (для быстрого доступа) можно только после регистрации.\n\nПерейти на страницу регистрации?")) {
			redirect("http://forum.ogl.ru/redir.php?url=http://ogl.ru/project/users/register.php");
		}
	}
}

function abc_switch () {

	var d = document.getElementById('abc');
	var d2 = document.getElementById('abc2');

	var s = d.innerHTML;
	d.innerHTML = d2.innerHTML;
	d2.innerHTML = s;

}

function findPos(obj) {

	var x = 0;
	var y = 0;

	if (obj.offsetParent) {
		while (obj.offsetParent)
		{
			y += obj.offsetTop;
			x += obj.offsetLeft;
			obj = obj.offsetParent;
		}

	} else {
		if (obj.x) x += obj.x;
		if (obj.y) y += obj.y;
	}

	return (new Array(x,y));

}


function findCursorPos(event) {

	var x, y;

	if (event.pageX) x = event.pageX;
	else if (event.clientX) x = event.clientX + (document.documentElement.scrollLeft ? document.documentElement.scrollLeft : document.body.scrollLeft);

	if (event.pageY) y = event.pageY;
	else if (event.clientY) y = event.clientY + (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);

	return (new Array(x,y));

}


var mc = {

	old : null,

	s : function (id,obj) {

		var pos = findPos(obj);
		var d = document.getElementById(id);

		this.ho(d);

		d.style.display = 'block';
		d.style.left = pos[0];
		d.style.top = pos[1]+obj.offsetHeight;
	},

	ho : function (d) {

		if (this.old==d) return;

		if (this.old) this.old.style.display = 'none';

		this.old = d;

	}

}


var hint = {

	timeout : 0,
	last : false,
	text : "",
	off : false,

	show : function (event,obj,id) {

		if (hint.off || hint.text=="") return false;

		var d = document.getElementById("hint");
		if (!d) {
			d = document.createElement("DIV");
			d.id = "hint";
			document.body.appendChild(d);
		}

//		if (hint.last!=id) {
			d.innerHTML = hint.text;
//			hint.last = id;
//		}

		d.style.display = "block";

		var pos = findCursorPos(event);
		d.style.left = pos[0]-60;
		d.style.top = pos[1]+22;

		if (hint.timeout) clearTimeout(hint.timeout);
		hint.timeout = setTimeout("hint.hide()",10000);

	},

	hide : function () {

		var d = document.getElementById("hint");
		if (d) d.style.display = "none";

	}
}


var requestController = {

	progressDiv: null,
	
	inProgress: false,
	
	currentRequest: null,
	
	queue: [],
	
	logger: function(msg){
		
		alert(msg);
		
	},
			
	mode: 'queue',	
	
	getProgressDiv: function(){
	
		if (!this.progressDiv){
		
			this.progressDiv = document.getElementById('progressDiv');
		
			if (!this.progressDiv&&document&&document.body){
			
				this.progressDiv = document.createElement('DIV');	
				this.progressDiv.style.position = 'absolute';
				this.progressDiv.style.display = 'none';
				this.progressDiv.style.backgroundColor = 'darkred';
				this.progressDiv.style.color = 'white';
				this.progressDiv.style.padding = '5px';
				this.progressDiv.style.left = '90%';
				this.progressDiv.style.top = '1px';
				this.progressDiv.style.width = '9%';
				this.progressDiv.style.zIndex = 100000;

				document.body.appendChild(this.progressDiv);
			}
							
		}
		
		return this.progressDiv;
	
	},
	
	showProgress: function(message){
	
//		var d = this.getProgressDiv();		
//		d.innerHTML = message;
//		d.style.display = '';
	
	},
	
	hideProgress: function(message){
			
//		this.progressDiv.style.display = 'none';
	
	},
	
	makeGetRequest: function(url,func,random){
		
		var q = {};
		q.url = url;
		q.func = func;
		q.method = "GET";
		q.random = random;
		this.queue.push(q);	
		this.next();
	
	},
	
	makePostRequest: function(url,data,func){
		
		this.makeBasicPostRequest(url,data,func,false);
		
	},
	
	makeUrgentPostRequest: function(){
		
		this.makeBasicPostRequest(url,data,func,true);
		
	},
	
	makeBasicPostRequest: function(url,data,func,urgent){						
		
		if (data && typeof(data)!='string') {
		
			var res = "";
			
			if (typeof(data)=='array'||(typeof(data)=='object')) {
																		
				for (var i in data){
						
					if (res != "") res +="&";
						
					res += i + "=" + encodeURIComponent(data[i]);
						
				}												
				
				data = res;
				
			} else {
				
				data = false;
				
			}
			
		}
		
		var q = {};
		q.url = url;
		q.func = func;
		q.method = "POST";
		q.data = data;
		q.random = true;
		
		if (urgent) this.queue.unshift(q);	
		else this.queue.push(q);
			
		this.next();
				
	},				
	
	buildRequest: function(){
						
		//setRequestHeader("If-Modified-Since", "Tue, 1 Jan 1980 00:00:00 GMT");		
		var req = null;	
		if (window.XMLHttpRequest) {		
			req = new XMLHttpRequest();
			if (req.overrideMimeType) req.overrideMimeType('text/html');						
		} else if (window.ActiveXObject) { // IE		
			req = new ActiveXObject("Msxml2.XMLHTTP");			
		}
		
		if (!req) {
		
			this.logger("Can't create XMLHTTP request");
			return false;
		
		}
		
		return req;			
	},
	
	requestOnChange: function(req,func){
								
		if(req.readyState==4) {

			this.showProgress("...");
			
			var result = null;
			
			if (req.status==200){ // 200 means "OK"										
															 				 																					
					result = {error:false, data:req.responseText};
																																																													
			} else {
						
				this.logger("Return status is not 200 " + req.status);
				result = {error:true, errorCode:"responceResultError", errorMessage:"STATUS_CODE " + req.status};
			
			} 
			
			this.hideProgress();
					
			try {
										
				if(func) func(result);
				
			} catch(ex){
			
				this.logger("error in callback function " + ex);
			
			}	
			
			this.inProgress = false;
			
		 	this.next();												
		 	
		}
				
	},

	next: function(){
			
		if (!this.inProgress) {
		
			this.inProgress = true;
								
			this.showProgress('...');		
				
			var req = this.buildRequest();
						
			if (!req) {		
			
				alert("can't initialize XMLHttpRequest")	
				this.hideProgress();
				this.inProgress = false;				
				return false;				
			
			}	
			
			this.currentRequest = this.queue.shift();
						
			if (!this.currentRequest) {
			
				this.hideProgress();
				this.inProgress = false;
				return false;
				
			}
							 	
	 		try {
	 	
	 			if (this.currentRequest.func) {
	 				
	 				var func = this.currentRequest.func;
	 				
	 				req.onreadystatechange = function(){requestController.requestOnChange(req,func);};
	 				
	 			}	
							
				if (this.currentRequest) {
					
					this.currentRequest.url = util.urlParamReplace(this.currentRequest.url,'r',new Date().getTime());	
					
				}											
																				
				req.open(this.currentRequest.method, this.currentRequest.url, true);				
				
				if (this.currentRequest.method=='POST'){
					
					req.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");					
					req.send(this.currentRequest.data);					
					
				} else {
					
					req.send('');	
					
				}		 
																																																																																			
				return true;
			
		 	} catch(ex) {
		 		 
		 		this.logger(ex);
		 		
		 		if (this.currentRequest&&this.currentRequest.func){		 			
		 			
		 			this.currentRequest.func({error:true,errorCode:"requestError",errorMessage:ex});		 			
		 			
		 		}		 		 
		 		
		 		this.inProgress = false;		 		
		 		return false;	
		 		 			 	
		 	}
		         
		} 
	
	}			
					
}




function user (user_id,obj) {

	var res = function(r) {

		var d = document.getElementById('user');
		if (!d) return false;

		d.innerHTML = r.data;

		var a = findPos(obj);
		d.style.top = a[1]+20;
		d.style.left = a[0]+10;

		d.style.display = "block";

	};
		
	requestController.makeGetRequest('/scripts/view.php?user_id='+user_id,res,Math.random());

}

function user_medals (medal_id,user_id,obj) {

	var res = function(r) {

		var d = document.getElementById('user');
		if (!d) return false;

		d.innerHTML = r.data;

		var a = findPos(obj);
		d.style.top = a[1]+20;
		d.style.left = a[0]+10;

		d.style.display = "block";

	};
		
	requestController.makeGetRequest('/scripts/view.medals.php?medal_id='+medal_id+'&user_id='+user_id,res,Math.random());

}

function user_hide () {

	var d = document.getElementById('user');
	if (!d) return false;

	d.style.display = "none";	

}




function showhide (id) {
	var d = document.getElementById(id);
	if (!d) return false;
	d.style.display = (d.style.display=="block") ? "none" : "block";
}



var util = {
	
	urlParamReplace: function(url,param_name,param_value){
						
			url = new String(url);						
											
			if (url.indexOf("&"+param_name+"=")!=-1||url.indexOf("?"+param_name+"=")!=-1) {  
			
				var pattern = param_name + '\\=[^\\&]*';
				var replace = "";
							
				if (param_value!='') {				
						replace=param_name+"="+encodeURIComponent(param_value); 									
						url = url.replace(eval(new String('/([&?])'+pattern+'/').toString()),'$1'+replace);
				} else {
						replace=''; 			
						url = url.replace(eval(new String('/[?]'+pattern+'\\&/').toString()),'?'+replace);
						url = url.replace(eval(new String('/[&?]'+pattern+'/').toString()),replace);	
				}		
				
			} else if (param_value!='') {
							
				if (url.indexOf('?')==-1)
					url = url + "?";
				else 	
					url = url + "&";
					
				url = url + param_name + "=" +encodeURIComponent(param_value); 									
				
			}
						
			return url;					
	},

	getParentStyle : function (x,styleProp,ignore) {

		var res = this.getStyle(x,styleProp);
		if (res && res!=ignore) return res;
		if (!x.parentNode) return false;

		return this.getParentStyle(x.parentNode,styleProp,ignore);

	},

	getStyle : function(obj,styleProp) {	
				
		res = this.readStyle(obj,styleProp);
		
		if (res) return res;
						
		var styleProp2 = styleProp;
		var s;
		
		if (styleProp.indexOf("-") > -1) {

			s = styleProp.split("-");			
			
			styleProp2 = s[0].toLowerCase();
			
			for (var i = 1; i < s.length; i++){
			
				styleProp2 += s[i].substr(0,1).toUpperCase() + s[i].substring(1,s[i].length).toLowerCase();
			
			}
			
			styleProp = styleProp.toLowerCase();
		
		} else {
			
			if (styleProp.toLowerCase()!=styleProp){
						
				styleProp2 = "";		
						
				for (var i=0; i < styleProp.length; i++){
				
					if (styleProp.charCodeAt(i)<91&&styleProp.charCodeAt(i)>64)
						styleProp2 += "-";
				
					styleProp2 += styleProp.charAt(i);
				
				}				
				
				styleProp2 = styleProp2.toLowerCase();
											
			}
							
		}
				
		if (styleProp2!=styleProp) return this.readStyle(obj,styleProp2);
									
		return null;
				
	},

	readStyle : function(obj,styleProp){			
		
		if (obj){						
			if (obj.style[styleProp]) {
				return obj.style[styleProp];
			} else if (obj.currentStyle) {
				return obj.currentStyle[styleProp];
			} else if (window.getComputedStyle){
				return document.defaultView.getComputedStyle(obj,null).getPropertyValue(styleProp);
			}		
		}
				
		return null;		
	},

	getParentElementWithClass : function (obj,className) {

		obj = obj.parentNode;


		while (obj.className!=className) {
			obj = obj.parentNode;
			if (!obj) return false;
		}

		return obj;

		

	},

	centerElement : function(obj){
	
		if (obj){
		
			var s = util.scrollOffset();
								
			if (obj.offsetHeight > document.body.clientHeight) {
			
				obj.style.top = 2 + s.y;
						
			} else {
			
				obj.style.top = s.y + (document.body.clientHeight - obj.offsetHeight) / 2;
			
			} 			
			
			if (obj.offsetWidth > document.body.clientWidth) {
			
				obj.style.left = 2 + s.x;
						
			} else {
			
				obj.style.left = s.x + (document.body.clientWidth - obj.offsetWidth) / 2;
			
			}
					
		}
						
	},	
    
	scrollOffset : function() {
        	
		var a = new Object();
		a.x = 0;
		a.y = 0;
    	
		if (window.pageXOffset) {
			a.x = window.pageXOffset;
			a.y = window.pageYOffset;				
		} else if (document.body.scrollLeft >= 0) {		
			a.x = document.body.scrollLeft;
		    a.y = document.body.scrollTop;
		} 		
		
		return a;				
	}


}



var incenter = {

	duration : 5,
	data : [],
	current : -1,

	show : function () {

		incenter.current++;
		if (incenter.current>=incenter.data.length) incenter.current = 0;

		var n = incenter.current;

		var e;
		e = document.getElementById("incenterBody");
		if (e) e.style.backgroundImage = 'url('+incenter.data[n][0]+')';

		e = document.getElementById("incenterText");
		if (e) e.innerHTML = incenter.data[n][2];

		setTimeout("incenter.show()",incenter.duration*1000);

	},

	init : function () {

		var isIE = window.navigator.userAgent.indexOf("MSIE")>-1;

		var obj = document.getElementById("incenterText");
		if(isIE){
			obj.style.backgroundColor = '#000000';
			obj.style.filter="progid:DXImageTransform.Microsoft.Alpha( Opacity=75, Style=0)";
		} else {
			obj.style.backgroundImage = 'url(http://static.ogl.ru/images/alfa60-fon.png)';
		}

	},

	click : function () {

		location.href = incenter.data[incenter.current][1];

	},

	add : function (src,url,title) {

		incenter.data.push([src,url,title]);

	},

	start : function () {

		incenter.show();

	}

}



rating = {

	timeout : false,
	object_id : 0,
	object_type_id : 0,
	voted : false,

	star : function (mark) {

		if (this.voted) return;

		if (this.timeout) clearTimeout(this.timeout);

		for (var i=1; i<=5; i++) {

			var d = document.getElementById('ratingStar'+i);
			d.src = (i<=mark) ? "http://static.ogl.ru/design/v1/star2.gif" : "http://static.ogl.ru/design/v1/star.gif";;

		}

		this.status(mark);

	},

	vote : function (m) {

		var res = function(r) {

			rating.status(100);

		};

		if (!user_online) {
			alert("Привет! Тебе надо зарегистрироваться на нашем сайте, чтобы принимать участие в рейтингах.");
			return;
		}

		requestController.makePostRequest('/rpc.php', { action: "rating", object_id: this.object_id, object_type_id: this.object_type_id, mark: m}, res);
		this.voted = true;

	},

	status : function (mark) {

		var status;
		switch(mark) {
	
			case 1: status = "Ужасно"; break;
			case 2: status = "Плохо"; break;
			case 3: status = "Так себе"; break;
			case 4: status = "Хорошо"; break;
			case 5: status = "Супер"; break;
			case 100: status = "Спасибо!"; break;
			default: status = ""; break;
		}

		var d = document.getElementById('ratingStatus');
		d.innerHTML = status;

	},

	clear : function () {

		if (this.timeout) clearTimeout(this.timeout);
		this.timeout = setTimeout( "rating.star(0)", 500 );
	}

}
