var files_form;

String.prototype.trim = function () {
    return this.replace(/^\s*/, "").replace(/\s*$/, "");
}
$("document").ready(function(){
	//$(".page-holder").css("background-size", screen.width+"px "+screen.height+"px");
});
window.onload = function(){
	//$(".page-holder").css("background-size", screen.width+"px "+screen.height+"px");

	$.each( $(".share a"), function(i, val){
		$(val).attr("href", $(val).attr("link") );
	});

	var search = document.getElementById('search');
	var search_bnt = document.getElementById('search_bnt');
	search.onfocus = function(){if(this.value=='Введите запрос') this.value=''; this.style.color='#000000';};
	search.onblur = function(){if(this.value==''){this.value='Введите запрос'; this.style.color='#888888';};};
	search.onkeydown = function(event){
		var code = (event.charCode) ? event.charCode : event.keyCode;
		if(code == 13){
			var str = this.value;
			if(str == "Введите запрос" || str.trim() = '') return;
			var order = $('#search_form input[name=order]:checked').val();
			if(order == 'date') window.location = "/search/last/" + str;
			else window.location = "/search/occurrence/" + str;
		}
	};
	search_bnt.onclick = function(){
		/*var form = document.createElement("form");
		form.method = "POST";
		form.action = "/search/last/" + document.getElementById('search').value;
		
		var stype = $("#search_form").find(":radio[name=order]").filter(":checked").val();
		
		var orderChecked = document.createElement("input"); // type='radio' name='order' value='date' checked>
		orderChecked.type = "radio";
		orderChecked.name = "order";
		orderChecked.value = "date";
		if(stype == "date") orderChecked.checked = "checked";
		
		var orderOccurrence = document.createElement("input");
		orderOccurrence.type = "radio";
		orderOccurrence.name = "order";
		orderOccurrence.value = "occurrence";
		if(stype == "occurrence") orderOccurrence.checked = "checked";
		
		form.appendChild(orderChecked);
		form.appendChild(orderOccurrence);
		form.submit();*/
		var str = document.getElementById('search').value;
		if(str == "Введите запрос" || !str.trim()) return;
		var order = $('#search_form input[name=order]:checked').val();
		if(order == 'date') window.location = "/search/last/" + str;
		else window.location = "/search/occurrence/" + str;
	};

/*	var ddown = document.getElementById("drop_down");
	ddown.onclick = function(){
		var llist = document.getElementById("login_list");
		toggle(llist);
	}*/
	var login_btn_arr = getElementsByClass("login-btn", document);
	for(i=0; i<login_btn_arr.length; i++){
		login_btn_arr[i].onmouseover = function(){this.style.backgroundPosition = "center center"; window.status = this.alt;};
		login_btn_arr[i].onmouseout  = function(){this.style.backgroundPosition = "top center"; window.status = '';};
		login_btn_arr[i].onmousedown = function(){this.style.backgroundPosition = "bottom center"};
		login_btn_arr[i].onmouseup   = function(){this.style.backgroundPosition = "center center"};
		login_btn_arr[i].onclick	   = function(){window.location = this.alt;};
	}

	
	var files_box = document.getElementById('files_box');
	if(!files_box) return;
	var links = files_box.getElementsByTagName('A');
	for(var i=0; i<links.length; i++){
		links[i].onclick = function(){
			if(this.nextSibling.nodeName=="DIV") { this.parentNode.removeChild(this.nextSibling); return}
			ajax(
				'/get_file.php',
				'act=form&id='+this.title+'&ind='+this.rel,
				this,
				function(html, el){
					var div = document.createElement('div');
					div.innerHTML=html;
					insertAfter(el.parentNode, div, el);
				}
			);
		}
	}
}
function insertAfter(parent, node, referenceNode) {
    parent.insertBefore(node, referenceNode.nextSibling);
}
function onvote(id){
	var  polls = document.getElementById('polls');

	var items = polls.getElementsByTagName('input');
	var variants='', selected='';

	for(var i=0; i<items.length; i++){
//		if(items[i].type=='radio') variants += items[i].value+'|';
//		if(items[i].type=='checkbox') variants += items[i].value+'|';
		if(items[i].checked) selected += items[i].value+'|';
	}
	if(!selected) return;
//	variants = variants.substr(0,variants.length-1);
	selected = selected.substr(0,selected.length-1);

	polls.innerHTML = "<img src='/images/ajax-loader.gif'>";
	ajax(
		'/ajax.php',
		'act=vote&id='+id+'&selected='+selected,
		polls,
		function(html, el){
			el.innerHTML=html;
		}
	);
}
function onresult(id){
	var  polls = document.getElementById('polls');
	polls.innerHTML = "<img src='/images/ajax-loader.gif'>";
	ajax(
		'/ajax.php',
		'id='+id,
		polls,
		function(html, el){
			el.innerHTML=html;
		}
	);
}
function bincrement(el,id){
	ajax(
		'/ajax.php',
		'id='+id+'&act=banner_clicked',
		el,
		function(html, el){ return true	}
	);
	window.location = el.href;
}
function ajax(url, parameters, el, callback) {
    var mozillaFlag = false;
    var XMLHttpRequestObject = false;
    if (window.XMLHttpRequest) {
        XMLHttpRequestObject = new XMLHttpRequest();
        mozillaFlag = true;
    } else if (window.ActiveXObject) {
        XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    }               

    if(XMLHttpRequestObject) {
        var t = new Date().getTime();
        XMLHttpRequestObject.open("GET", url+"?"+parameters, true);
        XMLHttpRequestObject.onreadystatechange = function() {
            if (XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200) {
				var htmlDocument = XMLHttpRequestObject.responseText;
                callback(htmlDocument, el);
            }
        }
        XMLHttpRequestObject.send(null);
	}
}
