var ajax_send = 0;

var swfVersionStr = "8.0.24";
var xiSwfUrlStr = "";
var flashvars = {};
var attributes = {};
var params = {};
params.quality = "high";
params.bgcolor = "#ffffff";
params.play = "true";
params.loop = "true";
params.wmode = "transparent";
params.allowscriptaccess = "sameDomain";
params.menu = "false";
params.devicefont = "false";
params.scale = "noscale";

$(document).ready(
	function() {
		
		MainMenuCorrect();
		
		if ($("#street_map").get(0)) swfobject.embedSWF("/tpl/map.swf", "street_map",	"966", "513",	swfVersionStr, xiSwfUrlStr,	flashvars, params, attributes);
		
		//SMILE BLOCK SHOW/HIDE
		$("div.news_comment_form_smile").click(
		 function () {
				$("div.smiles_block").toggleClass("hide");
			}
		);
		
		//ADD SMILE TO COMMENT
		$("div.smiles_block img").click(
		 function () {
				var txt = $("div.news_comment_form textarea").val();
				if (txt == "Комментарий" || txt == " ") txt = "*" + $(this).attr("rel") + "* ";
				else {
					if (txt[(txt.length - 1)] == " ") txt += "*" + $(this).attr("rel") + "* ";
					else txt += " *" + $(this).attr("rel") + "* ";
				}
				$("div.news_comment_form textarea").val(txt);
			}
		);
		
		//ADD QUOTE TO COMMENT
		$("div.news_comment_date > p").live("click",
		 function () {
				if ($("#user_quote_txt").get(0)) $("#user_quote_txt").replaceWith('<a href="#" title="Отменить цитирование" id="user_quote_txt"><div class="news_comment_form_qote"><p></p>Комментарий с цитатой от <span>' + $(this).parent().next().text() + '</span></div></a>');
				else $("div.news_comment_form").prepend('<a href="#" title="Отменить цитирование" id="user_quote_txt"><div class="news_comment_form_qote"><p></p>Комментарий с цитатой от <span>' + $(this).parent().next().text() + '</span></div></a>');
				$("#user_quote").val($(this).parent().parent().parent().get(0).id.split("_")[1]);
				$(window).scrollTop($("div.news_comment_form").offset().top);
			}
		);
		
		//COMMENT DELETE QUOTE
		$("#user_quote_txt").live("click",
		 function () {
				$(this).remove();
				$("#user_quote").val("");
				return false;
			}
		);
		
		//SWITCH QUOTE AND COMMENT
		$("div.news_comment_title > a").live("click",
		 function () {
				if (!$(this).hasClass("select")) {
					var parent = $(this).parent();
					$("a", parent).removeClass("select");
					$(this).addClass("select");
					if ($(this).attr("rel") != undefined) {
						var obj = parent.parent().find("div.news_comment_body:last");
						if (obj.html() == "") obj.html($("#comm_" + $(this).attr("rel")).find("div.news_comment_body:first").html());
					}
					parent.parent().find("div.news_comment_body").toggleClass("hide");
				}
				return false;
			}
		);
		
		//SEARCH FORM SUBMIT
		$("div.search button").click(
		 function () {
				if ($(this).prev().val() == "Поиск")	return false;
			}
		);
		
		//SHOW SEARCH DATE RANGE INPUT
		$("div.search_block input[name='tt']").change(
		 function () {
				$("input.search_date").toggleClass("hide");
			}
		);
		
		//SHOW SEARCH DATE RANGE BLOCK
		$("input.search_date").focus(
		 function () {
				$(this).after('<div class="date_range"><div class="calendar" id="calendar_start"></div><div class="calendar" id="calendar_end"></div><div class="clear"></div><button>Применить</button></div>');
				$("div.date_range").css("right", $("div.wrapper").offset().left + 5);
				sel_d = 0;
				sel_m = 0;
				sel_y = 0;
				y = cur_y;
				m = cur_m;
				date_round["calendar_end"] = undefined;
				date_round["calendar_start"] = undefined;
				$("#calendar_end").calendarWidget({month:m, year:y});
				m--;
				if (m < 0) {	m = 11;	y--;	}
				if (y < min_y) y = min_y;
				if (y > cur_y) y = cur_y;
				$("#calendar_start").calendarWidget({month:m, year:y});
			}
		);
		
		//SET SEARCH DATE RANGE
		$("div.date_range button").live("click",
		 function () {
				$("div.date_range").remove();
				var month = cur_m + 1;
				if (month < 10) month = "0" + month;
				var start = date_round["calendar_start"];
				var end = date_round["calendar_end"];
				if (end == undefined) end = cur_y + "-" + month + "-" + cur_d;
				if (start == undefined) start = "2007-01-01";
				var sec_start = Math.round(new Date(start.replace(/-/gi, "\/")) / 1000);
				var sec_end = Math.round(new Date(end.replace(/-/gi, "\/")) / 1000);
				var temp = start.split("-");
				start = temp[2] + "-" + temp[1] + "-" + temp[0];
				temp = end.split("-");
				end = temp[2] + "-" + temp[1] + "-" + temp[0];
				if (sec_start > sec_end) $("input.search_date").val(end + " - " + start);
				else $("input.search_date").val(start + " - " + end);
			}
		);
		
		//COMMENT SEND
		$("div.news_comment_form button").click(
		 function () {
				var parent_obj = $(this).parent();
				if ($("textarea", parent_obj).val() != "Комментарий" && !ajax_send) {
					ajax_send = 1;
					var user_name = "Читатель";
					if ($("input:not([type='hidden'])", parent_obj).val() != "Имя") user_name = $("input:not([type='hidden'])", parent_obj).val();
					var p_id = $("#user_quote").val();
					var from_user = "";
					if (p_id != "") from_user = $("#user_quote_txt").find("span").text();
					var news_id = $(this).get(0).id.split("_")[1];
					var url_send = "/engine/module/show_backend.php";
					if ($(this).get(0).id.split("_")[0] == "vote") url_send = "/engine/module/vote_backend.php";
					if ($(this).get(0).id.split("_")[0] == "map") url_send = "/engine/module/road_cam_backend.php";
					$.post(url_send, {
						act: "add_comment",
						news_id: news_id,
						p_id: $("#user_quote").val(),
						user_name: user_name,
						user_comment: $("textarea", parent_obj).val(),
						from_user: from_user
						}, function (reply) {
							ajax_send = 0;
							if (reply != "") {
								if (url_send == "/engine/module/road_cam_backend.php") parent_obj.after(reply);//$("div.news_comments").prepend(reply);
								else parent_obj.before(reply);
								if ($("small.comment_count").get(0)) {
									var comment_count = parseInt($("small.comment_count").html().substring(1)) + 1;
									$("small.comment_count").html("/" + comment_count);								
								}
								if (reply.search(">Антимат</strong>") == -1) {
									$("textarea", parent_obj).val("Комментарий");
									$("#user_quote").val("");
									$("#user_quote_txt").remove();
								}
								var counter_1 = "<img src='http://www.hutor.ru/cnstats/cntg.php?r="+escape(document.referrer)+'&'+Math.random()+"' width=88 height=31 border=0>";
								var counter_2 = '<a href="http://top100.rambler.ru/top100/"><img src="http://top100-images.rambler.ru/top100/banner-88x31-rambler-gray2.gif" alt="Rambler\'s Top100" border="0" height="31" width="88"></a><img src="http://counter.rambler.ru/top100.cnt?608322" style="display: none;" border="0" height="1" width="1">';
								var counter_3 = '<a href="http://www.yandex.ru/cy?base=0&amp;host=www.city-n.ru"><img src="http://www.yandex.ru/cycounter?www.city-n.ru" alt="Яндекс цитирования" border="0" height="31" width="88"></a>';
								var counter_4 = '<a href="http://www.liveinternet.ru/click" '+'target=liveinternet><img src="http://counter.yadro.ru/hit?t14.1;r'+escape(document.referrer)+((typeof(screen)=='undefined')?'':';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+';'+Math.random()+'" alt="liveinternet.ru: показано число просмотров за 24 часа, посетителей за 24 часа и за сегодн\я" '+'border=0 width=88 height=31></a>';
								d=document;var a='';a+=';r='+escape(d.referrer);js=13;
								var counter_5 = '<a href="http://top.mail.ru/jump?from=1439262"'+' target="_top"><img src="http://d6.cf.b5.a1.top.list.ru/counter'+'?id=1439262;t=210;js='+js+a+';rand='+Math.random()+'" alt="Рейтинг@Mail.ru"'+' border="0" height="31" width="88"/><\/a>';
								$("div.counters").html(counter_1 + " " + counter_2 + " " + counter_3 + " " + counter_4 + " " + counter_5);
							}
						}
					);
				}
			}
		);
		
		
		//CONFERENCE ANSWER SEND
		$("div.conference_form button").click(
		 function () {
				var parent_obj = $(this).parent();
				var user_name = $("input:first", parent_obj).val();
				var user_family = $("input:last", parent_obj).val();
				var user_question = $("textarea", parent_obj).val();
				if (user_question != "Вопрос" && user_name != "Имя" && user_family != "Фамилия" && !ajax_send) {
					ajax_send = 1;
					var conference_id = $(this).get(0).id;
					var url_send = "/engine/module/conference_backend.php";
					$.post(url_send, {
						act: "add_question",
						conference_id: conference_id,
						user_name: user_name,
						user_family: user_family,
						user_question: user_question
						}, function (reply) {
							ajax_send = 0;
							if (reply != "") {
								parent_obj.before("<div class='news_text' style='margin: 0px'><strong class='mark_red'>" + reply + "</strong></div>");
								$("textarea", parent_obj).val("Вопрос");
								$("input:first", parent_obj).val("Имя");
								$("input:last", parent_obj).val("Фамилия");
								var counter_1 = "<img src='http://www.hutor.ru/cnstats/cntg.php?r="+escape(document.referrer)+'&'+Math.random()+"' width=88 height=31 border=0>";
								var counter_2 = '<a href="http://top100.rambler.ru/top100/"><img src="http://top100-images.rambler.ru/top100/banner-88x31-rambler-gray2.gif" alt="Rambler\'s Top100" border="0" height="31" width="88"></a><img src="http://counter.rambler.ru/top100.cnt?608322" style="display: none;" border="0" height="1" width="1">';
								var counter_3 = '<a href="http://www.yandex.ru/cy?base=0&amp;host=www.city-n.ru"><img src="http://www.yandex.ru/cycounter?www.city-n.ru" alt="Яндекс цитирования" border="0" height="31" width="88"></a>';
								var counter_4 = '<a href="http://www.liveinternet.ru/click" '+'target=liveinternet><img src="http://counter.yadro.ru/hit?t14.1;r'+escape(document.referrer)+((typeof(screen)=='undefined')?'':';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+';'+Math.random()+'" alt="liveinternet.ru: показано число просмотров за 24 часа, посетителей за 24 часа и за сегодн\я" '+'border=0 width=88 height=31></a>';
								d=document;var a='';a+=';r='+escape(d.referrer);js=13;
								var counter_5 = '<a href="http://top.mail.ru/jump?from=1439262"'+' target="_top"><img src="http://d6.cf.b5.a1.top.list.ru/counter'+'?id=1439262;t=210;js='+js+a+';rand='+Math.random()+'" alt="Рейтинг@Mail.ru"'+' border="0" height="31" width="88"/><\/a>';
								$("div.counters").html(counter_1 + " " + counter_2 + " " + counter_3 + " " + counter_4 + " " + counter_5);
							}
						}
					);
				}
			}
		);
		
		//COLORBOX
		if ($("a[rel='photogallery']").get(0)) {
			$("a[rel='photogallery']").colorbox(
			{
				photo:	true,
				maxWidth: "100%",
				maxHeight: "100%",
				scalePhotos: false,
				current: "{current}/{total}"
			});
		}
		
		//INPUT ACTION
		var input_list = "input, textarea";
		$(input_list).live("focus",
			function () {
				if (this.value == this.title) this.value = "";
			}
		);
		$(input_list).live("blur",
			function () {
				if (this.value == "" || this.value == " ") this.value = this.title;
			}
		);
		
		//ADD TO FAVORITES
		$("a.add_to_favorite").click(
		 function () {
				var t,v = undefined;
				if (window.opera) t = 'Opera';
				else if (document.all) {
					t = 'IE';
					var nv = navigator.appVersion;
					var s = nv.indexOf('MSIE')+5;
					v = nv.substring(s,s+1);
				}
				else if (navigator.appName) t = 'Netscape';
				var b = {type:t,version:v};
				var url = "http://www.city-n.ru/";
				var title = 'ИА "Город Nовостей"';
				var a = this;
				if (b.type == 'IE' && 7 > b.version && b.version >= 4) window.external.AddFavorite(url,title);
				else if (b.type == 'Opera') {
					a.href = url;
					a.rel = "sidebar";
					a.title = url+','+title;
					return true;
				}
				else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
				return false;
			}
		);
		
		$("div.news_tag_view").mouseleave(
			function () {
				var counter_1 = "<img src='http://www.hutor.ru/cnstats/cntg.php?r="+escape(document.referrer)+'&'+Math.random()+"' width=88 height=31 border=0>";
				var counter_2 = '<a href="http://top100.rambler.ru/top100/"><img src="http://top100-images.rambler.ru/top100/banner-88x31-rambler-gray2.gif" alt="Rambler\'s Top100" border="0" height="31" width="88"></a><img src="http://counter.rambler.ru/top100.cnt?608322" style="display: none;" border="0" height="1" width="1">';
				var counter_3 = '<a href="http://www.yandex.ru/cy?base=0&amp;host=www.city-n.ru"><img src="http://www.yandex.ru/cycounter?www.city-n.ru" alt="Яндекс цитирования" border="0" height="31" width="88"></a>';
				var counter_4 = '<a href="http://www.liveinternet.ru/click" '+'target=liveinternet><img src="http://counter.yadro.ru/hit?t14.1;r'+escape(document.referrer)+((typeof(screen)=='undefined')?'':';s'+screen.width+'*'+screen.height+'*'+(screen.colorDepth?screen.colorDepth:screen.pixelDepth))+';'+Math.random()+'" alt="liveinternet.ru: показано число просмотров за 24 часа, посетителей за 24 часа и за сегодн\я" '+'border=0 width=88 height=31></a>';
				d=document;var a='';a+=';r='+escape(d.referrer);js=13;
				var counter_5 = '<a href="http://top.mail.ru/jump?from=1439262"'+' target="_top"><img src="http://d6.cf.b5.a1.top.list.ru/counter'+'?id=1439262;t=210;js='+js+a+';rand='+Math.random()+'" alt="Рейтинг@Mail.ru"'+' border="0" height="31" width="88"/><\/a>';
				$("div.counters").html(counter_1 + " " + counter_2 + " " + counter_3 + " " + counter_4 + " " + counter_5);
			}
		);
		
		//ON RESIZE
		$(window).resize (
			function() {
				MainMenuCorrect();
				FooterCorrect();
				if ($("div.date_range").get(0)) $("div.date_range").css("right", $("div.wrapper").offset().left + 5);
			}
		);
		
		//MENU PARTS SWITCH
		/*$("div.menu_parts > div").each(
		 function (pos) {
				$(this).click(
				 function () {
						if (!$(this).hasClass("select")) {
							$("div.menu_parts > div").removeClass("select");
							$(this).addClass("select");
							$("div.main_menu").addClass("hide");
							$("div.main_menu:eq(" + pos + ")").removeClass("hide");
							MainMenuCorrect();
						}
					}
				);
			}
		);*/
		
		FooterCorrect();
		
	 ///////////////
	}
);

function test () {
	//alert("test");
}

function MainMenuCorrect () {
	$("div.menu_line:visible").each(
		function () {
			$("p", this).css("padding", "0px");
			var padding = Math.floor(($(this).width() - $("div.menu_wrap", this).width() - 2) / ($("p", this).length * 2));
			if (parseInt(padding) > 0) $("p", this).css("padding", "0px " + padding + "px");
		}
	);
}

function FooterCorrect () {
	$("div.footer").css("position", "relative");
	$("div.wrapper").css("height", "auto");
	if ($("div.wrapper").height() < $(window).height()) {
		$("div.wrapper").height($(window).height());
		$("div.footer").css({"position":"absolute","bottom":"0px"});
	} else {
		$("div.wrapper").css("height", "auto");
		$("div.footer").css("position", "relative");
	}
}
