/*================== Распределение css ===================*/
$(document).ready(function() {
	$('input[type=radio]').css('border', '0');
	$('input[type=button]').css('border', '0');
	$('input[type=submit]').css('border', '0');
});

function alert_instruction(){
	alert('В данный момент сервис недоступен.');
}

browserName = navigator.appName;
browserVer = parseInt(navigator.appVersion);
var agent = navigator.userAgent;
var ua = navigator.userAgent.toLowerCase();
var msie_ver = ua.charAt(ua.indexOf('msie')+5);


if (ua.indexOf('opera') != -1) {
	document.writeln("<link rel=stylesheet type=text/css href=\"/common/css/op.css\">");
}
else if ((ua.indexOf('msie') != -1) && (msie_ver == 5)) { // IE 5.5
	document.writeln("<link rel=stylesheet type=text/css href=\"/common/css/ie5_5.css\">");
}
else if ((ua.indexOf('msie') != -1) && (msie_ver == 6)) { // IE 6.0
	document.writeln("<link rel=stylesheet type=text/css href=\"/common/css/ie6.css\">");
}
else if ((ua.indexOf('msie') != -1) && (msie_ver > 5)) { // IE all
	document.writeln("<link rel=stylesheet type=text/css href=\"/common/css/ie.css\">");
}
else if ((browserVer >3) &&(browserName == "Netscape")) {
	document.writeln("<link rel=stylesheet type=text/css href=\"/common/css/nn.css\">");
}

/*================== Скрытие показ блоков ===================*/
function ToggleCodeS(id) {
	el=document.getElementById(id);
	if(el.style.display=="none")
		el.style.display="";
	else
		el.style.display="none";
	return false;
}

function card_vis(id) {
	el=document.getElementById(id);
	if (el.style.display=="none") {
		$(document).ready(function(){
				$("div."+id).removeClass('card_visible_true');
				return false;
			}
		)
		el.style.display="";
	}
	else {
		$(document).ready(function(){
				$("div."+id).addClass('card_visible_true');
				return false;
			}
		);
		el.style.display="none";
	}
}

function Comments_sub(id) {
	var id_div=id+'_div';
	var id_sp=id+'_sp';
	el=document.getElementById(id);
	el_div=document.getElementById(id_div);
	el_sp=document.getElementById(id_sp);
	if (el.style.display=="none") {
		el.style.display="";
		el_div.style.width='100%';
		el_sp.innerHTML='–';
	}
	else {
		el.style.display="none";
		el_div.style.width='';
		el_sp.innerHTML='+';
	}
	return false;
}

/*================== Выравнивание высоты блоков ===================*/
function SizeStr() {
	if ($("td.cont_bot_1 div").hasClass('news2') && $("td.cont_bot_2 div").hasClass('question_main') )
	{
		var bot_h1 = $("td.cont_bot_1 div.news2 div.div0").height();
		var bot_h2 = $("td.cont_bot_2 div.question_main div.div0").height();
		//alert(bot_h1 + ', ' + bot_h2)
		if (bot_h1 > bot_h2){
				$("td.cont_bot_2 div.question_main div.div0").css('min-height', bot_h1);}
		else
			{$("td.cont_bot_1 div.news2 div.div0").css('min-height', bot_h2);}
	}
	return false;
}

/*================== Преобразование ссылок в версии для печати ===================*/

function Print_lnk() {
	var lnk = "javascript:void(0)";
	$("#print div.cont_div_in a").attr("href", lnk);
}

/*================== Навигация ===================*/

function $id(id){
	return elem[id]||(elem[id]=(document.getElementById)?document.getElementById(id):document.all(id))
}

var elem={};

function pager(ev){
	 ev=ev||window.event;
	var kc=ev.keyCode;
	if(ev.ctrlKey){
		var lnk;
		if(kc==37){
			lnk=$id("prev")
		}
		if(kc==39){
			lnk=$id("next");
		}
		if(lnk){
			location.href=lnk.href
		}
	}
}

/*================== Форма для ошибок ===================*/
function openFeedback()	{
	var sel = "";
	sel = window.GetSelected().toString();
	$.post('/common/set_error_text.php', {'txt':sel, 'title':document.title}, function(data) {});
	$.fn.colorbox({href:"/ru/bugreport/?title="/*+Url.encode(document.title)*/, open:true, width:"500px", height:"690px", krClose:true, iframe:true});
}

function GetSelected() {
	txt = '';
    if (window.getSelection) {
        txt = window.getSelection();
    } else if (document.getSelection) {
        txt = document.getSelection();
    } else if (document.selection) {
        txt = document.selection.createRange().text;
    }
	return txt;
}

/*================== Загрузка ===================*/
var Url = {
	// public method for url encoding
	encode : function (string) {
		return escape(this._utf8_encode(string));
	},

	// public method for url decoding
	decode : function (string) {
		return this._utf8_decode(unescape(string));
	},

	// private method for UTF-8 encoding
	_utf8_encode : function (string) {
		string = string.replace(/\r\n/g,"\n");
		var utftext = "";

		for (var n = 0; n < string.length; n++) {

			var c = string.charCodeAt(n);

			if (c < 128) {
				utftext += String.fromCharCode(c);
			}
			else if((c > 127) && (c < 2048)) {
				utftext += String.fromCharCode((c >> 6) | 192);
				utftext += String.fromCharCode((c & 63) | 128);
			}
			else {
				utftext += String.fromCharCode((c >> 12) | 224);
				utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				utftext += String.fromCharCode((c & 63) | 128);
			}

		}

		return utftext;
	},

	// private method for UTF-8 decoding
	_utf8_decode : function (utftext) {
		var string = "";
		var i = 0;
		var c = c1 = c2 = 0;

		while ( i < utftext.length ) {

			c = utftext.charCodeAt(i);

			if (c < 128) {
				string += String.fromCharCode(c);
				i++;
			}
			else if((c > 191) && (c < 224)) {
				c2 = utftext.charCodeAt(i+1);
				string += String.fromCharCode(((c & 31) << 6) | (c2 & 63));
				i += 2;
			}
			else {
				c2 = utftext.charCodeAt(i+1);
				c3 = utftext.charCodeAt(i+2);
				string += String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
				i += 3;
			}
		}
		return string;
	}
}

window.onload=function(){
	document.onkeydown=pager;
	SizeStr();
	Print_lnk();

	document.onkeypress = function(event) {
		event = (event) ? event : window.event;
		if(event.keyCode == 27){
			$.fn.colorbox.close();
			}
		if ((event.keyCode == 13 || event.keyCode == 10) && event.ctrlKey == true)
		{
			openFeedback();
		}
	}
};
