function authBottom() {
	scroll(0,0);
	document.getElementById("loginBox").style.display = "block";
	document.getElementById("login_div").firstChild.focus();
}


var request = new XMLHttpRequest();
var data;
var currentSearchState = 'search';
	
	function sendRequest() {
		request.open("POST","/ajax/games_search.php",true);
		request.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		request.send(data);
	}
	
	request.onreadystatechange = function() {
		if(request.readyState == 4 && request.status == 200) {
			var games_all = document.getElementById('games_all');
			games_all.innerHTML = request.responseText;
			var searchButton = document.getElementById('search_button_unfocused');
			searchButton.id='search_button';
			searchButton.innerHTML = '';
			window.scroll(0,0);
		}
	}
	
	function search() {
		data = document.getElementById('search_input').firstChild.value;
		data = "search=" + data;
		var searchButton = document.getElementById('search_button');
		searchButton.innerHTML = '<img src="/images/ajax-loader.gif" />';
		searchButton.id = 'search_button_unfocused';
		sendRequest();
	}
	
	function clearSearchInput() {
		var input = document.getElementById('search_input').firstChild;
		input.value = '';
	}
	
	function backSearchInput() {
		var input = document.getElementById('search_input').firstChild;
		if(input.value == '') {
			input.value = 'Поиск...';
		}
	}
	
	function currentPaginationLink() {
		var pagination = document.getElementsByClassName('pagination')[0];
		var links = [];
	
		for(var i = 1; i <= pagination.childNodes.length; i = i + 2) {
			if(document.location.href == pagination.childNodes[i]) {
				var current_link = pagination.childNodes[i];
				current_link.style.background = 'lime';
				break;
			}
		}
	}
	
	function currentGamesLink() {
		var links = document.getElementsByClassName('game_link');
		var preg = /games\/\d?$/;
		if(preg.test(document.location.href) === true) {
			var index = links[0];
			index.style.color = "#fff";
			index.style.textShadow = "1px 1px 3px #000";
			index.style.fontWeight = "bold";
			currentPosition(index.innerHTML);
			return;
		}
		for(var i = 1; i< links.length; i++) {
			var current_link = links[i];
			current_href = current_link.href;
			loc = document.location.href.indexOf(current_href);
			if(loc !== -1) {
				current_link.style.color = '#fff';
				current_link.style.textShadow = "1px 1px 3px #000";
				current_link.style.fontWeight = "bold";
				currentPosition(current_link.innerHTML);
			}
		}
	}
	
	function currentPosition(cur) {
		var pos = document.getElementsByClassName('top')[0];
		pos.innerHTML  += " " + cur;
	}
	
	
	
	onload = function() {
		
		/*
		*	Admin posts
		*/
		adminMessageRequest.help = document.getElementById("post_admin_help");
		document.getElementById("message_contacts").firstChild.onblur = backMessageInput;
		document.getElementById("message_content").onblur = backMessageInput;
		document.getElementById("post_admin_roll").onclick = rollClick;
		document.getElementById("post_admin_close").onclick = closeClick;
		document.getElementById("send_message").onclick = sendClick;
		
		
		
		var searchBlock = document.getElementById("search_block");
		
		currentGamesLink();
		currentPaginationLink();
		
		//Games blocks
		gamesBlock.construct();
		
		window.onscroll = function () {
			if(scrollY >= 150 && window.searchPos == "normal") {
				searchBlock.style.position = "fixed";
				searchBlock.style.right = "-20px";
				searchBlock.style.top = "0px";
				searchBlock.style.boxShadow = "0 0 15px #000";
				searchBlock.style.padding = "5px 10px";
				searchBlock.style.border = "2px solid #fff";
				searchBlock.style.borderRadius = "10px";
				searchBlock.style.background = "#fff";
				window.searchPos = "fixed";
			}
			else if(scrollY <= 150){
				if(window.searchPos = "fixed") {
					window.searchPos = "normal";
					searchBlock.style.position = "static";
					searchBlock.style.boxShadow = "0 0 0";
					searchBlock.style.padding = "0 0";
					searchBlock.style.border = "none";
					searchBlock.style.background = "none";
				}
			}
		}
	}
	
	/*
	*		Games block init
	*/
	gamesBlock= {};
	gamesBlock.construct = function () {
		this.newGames = document.getElementsByClassName("new_game");
		this.popularGames = document.getElementsByClassName("popular_game");
		for(var i = 0; i < 9; i++) {
			this.newGames[i].info = document.getElementsByClassName("new_game_info")[i];
			this.newGames[i].onmouseover = this.actionOver;
			this.newGames[i].onmouseout = this.actionOut;
			
			this.popularGames[i].info = document.getElementsByClassName("popular_game_info")[i];
			this.popularGames[i].onmouseover = this.actionOver;
			this.popularGames[i].onmouseout = this.actionOut;
		}
	}
	gamesBlock.actionOver = function () {
		this.info.style.display = "block";
	}
	gamesBlock.actionOut = function () {
		this.info.style.display = "none";
	}
	
	var searchHtml;
	function changeVis() {
		var ext_search = document.getElementById('ext_search');
		if (ext_search.style.visibility == '') {
			ext_search.style.visibility = 'visible';
			var search_block = document.getElementById('search_block');
			var ext_search_link = document.getElementById('ext_search_link');
			ext_search_link.onclick = changeVis;
			searchHTML = search_block.innerHTML;
			search_block.innerHTML = '<div id="search_input"><input  type="text" onclick="javascript:clearInput()" value="Поиск..."/></div><div onclick="javascript:extSearch()" id="search_button"></div>';
			currentSearchState = 'extSearch';
			if(nameSearch === undefined) {
				searchValues('name');
			}
		}
		else {
			ext_search.style.visibility = '';
			var search_block = document.getElementById('search_block');
			search_block.innerHTML = searchHTML;
			currentSearchState = 'search';
		}
	}
	
	var nameSearch;
	var typeSearch;
	var developerSearch;
	var publisherSearch;
	var searchAll;
	var data;
	var next;
	
	function clearInput() {
		var input = document.getElementById('search_input').firstChild;
		input.value = '';
	}
	
	var extRequest = new XMLHttpRequest();
	
	function extSearch() {
		var searchValue = document.getElementById('search_input').firstChild.value;
		data = new Array();
		
		if(searchAll == true) {
			data = 'all='+searchValue;
		}
		else {
			if(nameSearch == true) {
				data.push('name='+searchValue);
			}
			if(typeSearch == true) {
				data.push('type='+searchValue);
			}
			if(developerSearch == true) {
				data.push('developer='+searchValue);
			}
			if(publisherSearch == true) {
				data.push('publisher='+searchValue)
			}
			if(data.length == 0) {
				return;
			}
			else {
				data.push('search=true');
				data = data.join('&');
			}
		}
		
		var searchButton = document.getElementById('search_button');
		searchButton.innerHTML = '<img src="/images/ajax-loader.gif" />';
		searchButton.id = 'search_button_unfocused';
		
		extRequest.open('POST','http://xbox360games.com.ua/ajax/extSearch.php',true);
		extRequest.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		extRequest.send(data);
	}
	
	extRequest.onreadystatechange = function() {
		if(extRequest.readyState == 4 && extRequest.status == 200) {
			var result = document.getElementById('games_all');
			result.innerHTML = extRequest.responseText;
			
			var searchButton = document.getElementById('search_button_unfocused');
			searchButton.id='search_button';
			searchButton.innerHTML = '';
			window.scroll(0,0);
		}
	}
	
	var requestNext = new XMLHttpRequest();
	var currentHTML;
	
	function sendNextRequest() {
		requestNext.open('POST','http://xbox360games.com.ua/ajax/ViewSearchResult.php',true);
		requestNext.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
		requestNext.send(data);
	}
	
	requestNext.onreadystatechange = function() {
		if(requestNext.readyState == 4 && requestNext.status == 200) {
			var nextAjax = document.getElementById('next_ajax');
			nextAjax.style.display = 'none';
			nextAjax.id = '';
			var result = document.getElementById('games_all');
			
			currentHTML = result.innerHTML;
			result.innerHTML = result.innerHTML + requestNext.responseText;
		}
	}
	
	function nextExtSearchView(lim) {
		sendNextRequest();
	}
	
	function searchValues(val) {
		switch(val) {
			case 'name':	
			var name = document.getElementById('nameSearch');
			if(nameSearch != true) {
				name.style.background = '#fff';
				name.style.color = '#000';
				name.style.border = '1px solid gray';
				name.style.borderRadius = '5px';
				name.style.paddingTop = '5px';
				name.style.paddingBottom = '5px';
					nameSearch = true;
			}
			else {
				name.style.background = '';
				name.style.color = '';
				name.style.border = '';
				name.style.borderRadius = '';
				name.style.paddingTop = '';
				name.style.paddingBottom = '';
					nameSearch = false;
			}
			break;
	
			case 'type':
			var type = document.getElementById('typeSearch');
			if(typeSearch != true) {
				type.style.background = '#fff';
				type.style.color = '#000';
				type.style.border = '1px solid gray';
				type.style.borderRadius = '5px';
				type.style.paddingTop = '5px';
				type.style.paddingBottom = '5px';
					typeSearch = true;
			}
			else {
				type.style.background = '';
				type.style.color = '';
				type.style.border = '';
				type.style.borderRadius = '';
				type.style.paddingTop = '';
				type.style.paddingBottom = '';
					typeSearch = false;
			}
			break;
			
			case 'developer':
			var developer = document.getElementById('developerSearch')
			if(developerSearch != true) {
				developer.style.background = '#fff';
				developer.style.color = '#000';
				developer.style.border = '1px solid gray';
				developer.style.borderRadius = '5px';
				developer.style.paddingTop = '5px';
				developer.style.paddingBottom = '5px';
					developerSearch = true;
			}
			else {
				developer.style.background = '';
				developer.style.color = '';
				developer.style.border = '';
				developer.style.borderRadius = '';
				developer.style.paddingTop = '';
				developer.style.paddingBottom = '';
					developerSearch = false;
			}
			break;
			
			case 'publisher':
			var publisher = document.getElementById('publisherSearch')
			if(publisherSearch != true) {
				publisher.style.background = '#fff';
				publisher.style.color = '#000';
				publisher.style.border = '1px solid gray';
				publisher.style.borderRadius = '5px';
				publisher.style.paddingTop = '5px';
				publisher.style.paddingBottom = '5px';
					publisherSearch= true
			}
			else {
				publisher.style.background = '';
				publisher.style.color = '';
				publisher.style.border = '';
				publisher.style.borderRadius = '';
				publisher.style.paddingTop = '';
				publisher.style.paddingBottom = '';
					publisherSearch= false;
			}
			break;
			
			case 'all':
			var	 all = document.getElementById('searchAll');
			if(searchAll != true) {	
				all.style.background = '#fff';
				all.style.color = '#000';
				all.style.border = '1px solid gray';
				all.style.borderRadius = '5px';
				all.style.paddingTop = '5px';
				all.style.paddingBottom = '5px';
				nameSearch = true;
				typeSearch = true;
				developerSearch = true;
				publisherSearch = true;
				searchValues("name");
				searchValues("type");
				searchValues("developer");
				searchValues("publisher");
				
				searchAll = true;
			}
			else {
				all.style.background = '';
				all.style.color = '';
				all.style.border = '';
				all.style.borderRadius = '';
				all.style.paddingTop = '';
				all.style.paddingBottom = '';
					searchAll = false;
				nameSearch = false;
				searchValues("name");
			}
			break;
		}
	}
	
	document.onkeypress = function checkKeycode(event) {
		var keycode, keyChar;
		if(!event) var event = window.event;
		if (event.keyCode) keycode = event.keyCode;
		
		if(event.keyCode == 13) {
			if(currentSearchState == 'search') {
				search();
			}
			else {
				extSearch();
			}
		}
	}
	
	/*
	*		Admin post init
	*/
	var adminMessageRequest = new XMLHttpRequest();
	adminMessageRequest.sendTry = function () {
		this.open("POST","http://xbox360games.com.ua/ajax/PostAdmin.php",true);
		this.setRequestHeader("Content-Type","application/x-www-form-urlencoded");
		this.send(this.data);
	}
	adminMessageRequest.onreadystatechange = function () {
		if(this.readyState == 4 && this.status == 200) {
			
			if(this.responseText == "OK") {
				this.help.innerHTML = "Ваше сообщение успешно доставлено";
				this.help.style.color = "lime";
				this.help.style.fontWeight = "bold";
				setTimeout("messageOk()", 1500);
				document.getElementById("post_admin_roll").innerHTML = "";
			}
			else {
				this.help.innerHTML = this.responseText;
				this.help.style.color = "red";
			}
		}
	}

	function messageOk() {
		document.getElementById("post_admin").style.display = "none";
		document.getElementById("post_admin_roll").style.display = "none";
	}

	function backMessageInput() {
		if(this.value == "") {
			if(this.id == "") {
				this.value = "Ваши контакты...";
			}
			else {
				this.innerHTML = "Ваше сообщение...";
			}
		}
	}

	function rollClick() {
		this.style.display = "none";
		document.getElementById("post_admin").style.display = "block";
	}
	function closeClick() {
		this.parentNode.style.display = "none";
		document.getElementById("post_admin_roll").style.display = "block";
	}
	function sendClick() {
		contacts = document.getElementById("message_contacts").firstChild.value;
		if(contacts == "Ваши контакты...") {
			contacts = "";
		}
		author = document.getElementById("message_author").firstChild.value;
		content = document.getElementById("message_content").value;
		
		adminMessageRequest.data = "contacts=" + contacts + "&author=" + author + "&content=" + content;
		adminMessageRequest.sendTry();
	}


	
/*
*		Auth
*/
	
var exitRequest = new XMLHttpRequest();
var authRequest = new XMLHttpRequest();
	
	
exitRequest.onreadystatechange = function () {
	if(this.readyState == 4 && this.status == 200) {
		document.location.href = document.location.href;
	}
}
function doExit() {
	exitRequest.exit();
}
exitRequest.onreadystatechange = function () {
	if(this.readyState == 4 && this.status == 200) {
		document.location.href = document.location.href;
	}
}

authRequest.onreadystatechange = function () {
	if(this.readyState == 4 && this.status == 200) {
	//		body parsing
		bodyBegin = '<div id="body">';
		bodyEnd = '</body>';
		bodyBeginPosition = this.responseText.search(bodyBegin);
		regBody = this.responseText.slice(bodyBeginPosition);
		bodyEndPosition = regBody.search(bodyEnd);
		regBody = regBody.slice(0, bodyEndPosition);
		
		var hidden = document.getElementById('hidden');
		hidden.innerHTML = regBody;
		if(document.getElementsByClassName('gensmall')[1] !== undefined) {
			var error = document.getElementsByClassName('gensmall')[1];
			var result = document.getElementById('result');
			result.innerHTML = error.innerHTML;
			if(result.childNodes[1] !== undefined) {
				if(result.childNodes[1].innerHTML == 'Администраторский раздел') {
					document.location.href = document.location.href;
				}
				else {
					alert(result.childNodes[1].innerHTML);
				}
			}
		}
		else {
			document.location.href = document.location.href;
		}
	}
}

authRequest.auth = function (data) {
	//		If not empty result - clean it
	if(document.getElementById('result').innerHTML != '') {
		document.getElementById('result').innerHTML = '';
	}
	this.open('POST','http://xbox360games.com.ua/forum/ucp.php?mode=login',true);
	this.setRequestHeader('Content-type','application/x-www-form-urlencoded');
	this.send(data);
}
