var smf_formSubmitted = false;
var quote_name='';
var quote_topic_id='';
var quote_msg_id='';
var quote_date=0;


// Open a new window in a smaller popup.
function reqWin(desktopURL, alternateWidth, alternateHeight)
{
	window.open(desktopURL, 'requested_popup', 'toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=' + (alternateWidth ? alternateWidth : 480) + ',height=' + (alternateHeight ? alternateHeight : 220) + ',resizable=no');

	// Return false so the click won't follow the link ;).
	return false;
}

// Remember the current position.
function storeCaret(text)
{
	// Only bother if it will be useful.
	if (typeof(text.createTextRange) != 'undefined')
		text.caretPos = document.selection.createRange().duplicate();
}

// Replaces the currently selected text with the passed text.
function replaceText(text, textarea)
{
	// Attempt to create a text range (IE).
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text + ' ' : text;
		caretPos.select();
	}
	// Mozilla text range replace.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text + end;

		if (textarea.setSelectionRange)
		{
			textarea.focus();
			textarea.setSelectionRange(begin.length + text.length, begin.length + text.length);
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put it on the end.
	else
	{
		textarea.value += text;
		textarea.focus(textarea.value.length - 1);
	}
}

// Surrounds the selected text with text1 and text2.
function surroundText(text1, text2, textarea)
{
	// Can a text range be created?
	if (typeof(textarea.caretPos) != "undefined" && textarea.createTextRange)
	{
		var caretPos = textarea.caretPos;

		caretPos.text = caretPos.text.charAt(caretPos.text.length - 1) == ' ' ? text1 + caretPos.text + text2 + ' ' : text1 + caretPos.text + text2;
		caretPos.select();
	}
	// Mozilla text range wrap.
	else if (typeof(textarea.selectionStart) != "undefined")
	{
		var begin = textarea.value.substr(0, textarea.selectionStart);
		var selection = textarea.value.substr(textarea.selectionStart, textarea.selectionEnd - textarea.selectionStart);
		var end = textarea.value.substr(textarea.selectionEnd);
		var newCursorPos = textarea.selectionStart;
		var scrollPos = textarea.scrollTop;

		textarea.value = begin + text1 + selection + text2 + end;

		if (textarea.setSelectionRange)
		{
			if (selection.length == 0)
				textarea.setSelectionRange(newCursorPos + text1.length, newCursorPos + text1.length);
			else
				textarea.setSelectionRange(newCursorPos, newCursorPos + text1.length + selection.length + text2.length);
			textarea.focus();
		}
		textarea.scrollTop = scrollPos;
	}
	// Just put them on the end, then.
	else
	{
		textarea.value += text1 + text2;
		textarea.focus(textarea.value.length - 1);
	}
}

// Checks if the passed input's value is nothing.
function isEmptyText(theField)
{
	return $.trim(theField.value) == '';
}

// Only allow form submission ONCE.
function submitonce(theform)
{
	smf_formSubmitted = true;
}
function submitThisOnce(item)
{
	// Hateful, hateful fix for Safari 1.3 beta.
	if (navigator.userAgent.indexOf('AppleWebKit') != -1)
		return !smf_formSubmitted;

	for (var i = 0; i < item.form.length; i++)
		if (typeof(item.form[i]) != "undefined" && item.form[i].tagName.toLowerCase() == "textarea")
			item.form[i].readOnly = true;

	return !smf_formSubmitted;
}

// Set the "inside" HTML of an element.
function setInnerHTML(element, toValue)
{
	// IE has this built in...
	if (typeof(element.innerHTML) != 'undefined')
		element.innerHTML = toValue;
	else
	{
		var range = document.createRange();
		range.selectNodeContents(element);
		range.deleteContents();
		element.appendChild(range.createContextualFragment(toValue));
	}
}

// Set the "outer" HTML of an element.
function setOuterHTML(element, toValue)
{
	if (typeof(element.outerHTML) != 'undefined')
		element.outerHTML = toValue;
	else
	{
		var range = document.createRange();
		range.setStartBefore(element);
		element.parentNode.replaceChild(range.createContextualFragment(toValue), element);
	}
}

// Get the inner HTML of an element.
function getInnerHTML(element)
{
	if (typeof(element.innerHTML) != 'undefined')
		return element.innerHTML;
	else
	{
		var returnStr = '';
		for (var i = 0; i < element.childNodes.length; i++)
			returnStr += getOuterHTML(element.childNodes[i]);

		return returnStr;
	}
}

function getOuterHTML(node)
{
	if (typeof(node.outerHTML) != 'undefined')
		return node.outerHTML;

	var str = '';

	switch (node.nodeType)
	{
		// An element.
		case 1:
			str += '<' + node.nodeName;

			for (var i = 0; i < node.attributes.length; i++)
			{
				if (node.attributes[i].nodeValue != null)
					str += ' ' + node.attributes[i].nodeName + '="' + node.attributes[i].nodeValue + '"';
			}

			if (node.childNodes.length == 0 && in_array(node.nodeName.toLowerCase(), ['hr', 'input', 'img', 'link', 'meta', 'br']))
				str += ' />';
			else
				str += '>' + getInnerHTML(node) + '</' + node.nodeName + '>';
			break;

		// 2 is an attribute.

		// Just some text..
		case 3:
			str += node.nodeValue;
			break;

		// A CDATA section.
		case 4:
			str += '<![CDATA' + '[' + node.nodeValue + ']' + ']>';
			break;

		// Entity reference..
		case 5:
			str += '&' + node.nodeName + ';';
			break;

		// 6 is an actual entity, 7 is a PI.

		// Comment.
		case 8:
			str += '<!--' + node.nodeValue + '-->';
			break;
	}

	return str;
}

// Checks for variable in theArray.
function in_array(variable, theArray)
{
	return $.inArray(variable, theArray);
}

// Find a specific radio button in its group and select it.
function selectRadioByName(radioGroup, name)
{
	for (var i = 0; i < radioGroup.length; i++)
	{
		if (radioGroup[i].value == name)
			return radioGroup[i].checked = true;
	}

	return false;
}

// Invert all checkboxes at once by clicking a single checkbox.
function invertAll(headerfield, checkform, mask)
{
	for (var i = 0; i < checkform.length; i++)
	{
		if (typeof(mask) != "undefined" && checkform[i].name.substr(0, mask.length) != mask)
			continue;

		if (!checkform[i].disabled)
			checkform[i].checked = headerfield.checked;
	}
}
function emoticon(text)
{
	replaceText(text, document.postmodify.message);
}

function getMsgInfo(_name, _topic_id, _msg_id, _date) {
	quote_name=_name;
	quote_topic_id=_topic_id;
	quote_msg_id=_msg_id;
	quote_date=_date;
}

function quoteSelection() {
    var txt = '';
    if (document.getSelection) {
	txt = document.getSelection();
    }
    else if (window.getSelection) {
	txt = window.getSelection();
    }
    else if (document.selection) {
	txt = document.selection.createRange().text;
    }
    
    if (txt) {
      	// Add tags around selection
      	emoticon('[quote author='+quote_name+' link=topic='+quote_topic_id+'.msg'+quote_msg_id+'#msg'+quote_msg_id+' date='+quote_date+']' + txt+ '[/quote]\n');
      	txt = '';
      	return;
      	}else{
      	alert("Сначала надо выделить текст");
      }
}

function shrinkHeader(mode)
{
	if (is_guest == "") {
		$.get("index.php?action=jsoption;var=collapse_header;val=" + (mode ? 1 : 0) + ";sesc=" + sesc + ";" + (new Date().getTime()));
	} else {
		$.cookie("upshrink",mode ? 1 : 0,{expires : 365});
	}

	$("#upshrink").attr("src", smf_images_url + (mode ? "/upshrink2.gif" : "/upshrink.gif"));

	$("#upshrinkHeader").toggleClass("hidden");

	current_header = mode;

}

function swapOptions()
{
	document.getElementById("quickReplyExpand").src = smf_images_url + "/" + (currentSwap ? "collapse.gif" : "expand.gif");
	document.getElementById("quickReplyOptions").style.display = currentSwap ? "" : "none";

	currentSwap = !currentSwap;
}

setTimeout("fetchSession();", 60000);
function fetchSession()
{
	$.get(smf_script_url + "?action=jsoption;sesc=" +sesc +";" + (new Date().getTime()));
	setTimeout("fetchSession();", 60000);
}

function doQuote(messageid)
{
	if (currentSwap)
		window.location.href = "http://rock.ru/forum/index.php?action=post;quote=" + messageid + ";topic=22901.40;sesc=" + sesc;
	else
	{
		window.open("http://rock.ru/forum/index.php?action=quotefast;quote=" + messageid + ";sesc=" + sesc, "quote", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,top=40,left=40,width=240,height=90,resizable=no;");
		if (navigator.appName == "Microsoft Internet Explorer")
			window.location.hash = "quickreply";
		else
			window.location.hash = "#quickreply";
	}
}

var smileys = [
[
[":cry:","12.gif",":cry:"],
	[":rage:","13.gif",":rage:"],
	[":B","14.gif",":B"],
	[":roll:","15.gif",":roll:"],
	[":ooi:","5.gif",":ooi:"],
	[":wink:","16.gif",":wink:"],
	[":yes:","17.gif",":yes:"],
	[":bot:","18.gif",":bot:"],
	[":ir:","9.gif","I roll!"],
	[":z)","19.gif",":z)"],
	[":arrow:","20.gif",":arrow:"],
	[":vip:","21.gif",":vip:"],
	[":Heppy:","22.gif",":Heppy:"],
	[":think:","23.gif",":think:"],
	[":bye:","24.gif",":bye:"],
	[":roul:","25.gif",":roul:"],
	[":pst:","26.gif",":pst:"]],
	[
	[":o","27.gif",":o"],
	[":closed:","28.gif",":closed:"],
	[":cens:","29.gif",":cens:"],
	[":tani:","30.gif",":tani:"],
	[":appl:","31.gif",":appl:"],
	[":idnk:","32.gif",":idnk:"],
	[":sing:","33.gif",":sing:"],
	[":shock:","34.gif",":shock:"],
	[":res:","36.gif",":res:"],
	[":alc:","37.gif",":alc:"],
	[":lam:","38.gif",":lam:"],
	[":box:","39.gif",":box:"],
	[":tom:","40.gif",":tom:"],
	[":lol:","41.gif",":lol:"],
	[":vill:","42.gif",":vill:"]],
	[
	[":idea:","43.gif",":idea:"],
	[":E","45.gif",":E"],
	[":horns:","47.gif",":horns:"],
	[":poz:","49.gif",":poz:"],
	[":meg:","51.gif",":meg:"],
	[":dj:","52.gif",":dj:"],
	[":rul:","53.gif",":rul:"],
	[":sp:","55.gif",":sp:"],
	[":stapp:","56.gif","Storm of applause"],
	[":heart:","58.gif","Heart"],
	[":kiss:","59.gif","Kiss"]],
	[
	[":spam:","60.gif","Spam"],
	[":party:","61.gif","Party"],
	[":ser:","62.gif","Song"],
	[":eam:","63.gif","Dream"],
	[":gift:","64.gif","Gift"],
	[":adore:","65.gif","I adore"],
	[":pie:","66.gif","Pie"],
	[":egg:","67.gif","Egg"],
	[":cnrt:","68.gif","Concert"],
	[":oftop:","69.gif","Off Topic"],
	[":foo:","70.gif","Football"],
	[":mob:","71.gif","Cellular"],
	[":hoo:","72.gif","Not hooligan"],
	[":tog:","73.gif","Together"],
	[":pnk:","74.gif","Pancake"],
	[":pati:","75.gif","Party Time"]],
	[
	[":-({|=:","76.gif","I here"],
	[":haaw:","77.gif","Head about a wall"],
	[":angel:","78.gif","Angel"],
	[":kil:","79.gif","killer"],
	[":died:","80.gif","Cemetery"],
	[":cof:","81.gif","Coffee"],
	[":fruit:","82.gif","Forbidden fruit"],
	[":tease:","83.gif","To tease"],
	[":evil:","84.gif","Devil"],
	[":exc:","85.gif","Excellently"],
	[":niah:","86.gif","Not I, and he"],
	[":Head:","87.gif","Studio"],
	[":gl:","88.gif","girl"],
	[":granat:","89.gif","Pomegranate"],
	[":gans:","90.gif","Gangster"],
	[":user:","91.gif","User"]],
	[
	[":ny:","92.gif","New year"],
	[":mvol:","93.gif","Megavolt"],
	[":boat:","94.gif","In a boat"],
	[":phone:","95.gif","Phone"],
	[":cop:","96.gif","Cop"],
	[":smok:","97.gif","Smoking"],
	[":bic:","98.gif","Bicycle"],
	[":ban:","99.gif","Ban?"],
	[":bar:","100.gif",":bar:"]]];
var smileyPopupWindow;

function moreSmileys()
{
	var row, i;

	if (smileyPopupWindow)
		smileyPopupWindow.close();

	smileyPopupWindow = window.open("", "add_smileys", "toolbar=no,location=no,status=no,menubar=no,scrollbars=yes,width=480,height=220,resizable=yes");
	smileyPopupWindow.document.write('<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">\n<html>');
	smileyPopupWindow.document.write('\n\t<head>\n\t\t<title>Дополнительные смайлики</title>\n\t\t<link rel="stylesheet" type="text/css" href="http://rock.ru/forum/Themes/default/style.css" />\n\t</head>');
	smileyPopupWindow.document.write('\n\t<body style="margin: 1ex;">\n\t\t<table width="100%" cellpadding="5" cellspacing="0" border="0" class="tborder">\n\t\t\t<tr class="titlebg"><td align="left">Выбрать смайлик</td></tr>\n\t\t\t<tr class="windowbg"><td align="left">');

	for (row = 0; row < smileys.length; row++)
	{
		for (i = 0; i < smileys[row].length; i++)
		{
			smileys[row][i][2] = smileys[row][i][2].replace(/"/g, '&quot;');
			smileyPopupWindow.document.write('<a href="javascript:void(0);" onclick="window.opener.replaceText(&quot; ' + smileys[row][i][0] + '&quot;, window.opener.document.postmodify.message); window.focus(); return false;"><img src="http://rock.ru/forum/Smileys/default/' + smileys[row][i][1] + '" alt="' + smileys[row][i][2] + '" title="' + smileys[row][i][2] + '" style="padding: 4px;" border="0" /></a> ');
		}
		smileyPopupWindow.document.write("<br />");
	}

	smileyPopupWindow.document.write('</td></tr>\n\t\t\t<tr><td align="center" class="windowbg"><a href="javascript:window.close();\">Закрыть окно</a></td></tr>\n\t\t</table>\n\t</body>\n</html>');
	smileyPopupWindow.document.close();
}

yandex_partner_id = 18555;
yandex_site_bg_color = '3A3A3A';
yandex_stat_id = 1;
yandex_site_charset = 'utf-8';
yandex_ad_format = 'direct';
yandex_font_size = 1;
yandex_direct_type = 'horizontal';
yandex_direct_border_type = 'ad';
yandex_direct_limit = 4;
yandex_direct_header_bg_color = 'FBE5C0';
yandex_direct_bg_color = '3A3A3A';
yandex_direct_border_color = 'FBE5C0';
yandex_direct_title_color = 'CFCECE';
yandex_direct_url_color = 'FFC246';
yandex_direct_all_color = 'FFC246';
yandex_direct_text_color = 'CACACA';
yandex_direct_hover_color = 'FFC246';
yandex_direct_favicon = false;
