﻿dis = 0;

var userAgent = navigator.userAgent.toLowerCase();
jQuery.browser.chrome = /chrome/.test( userAgent );

$(document).ready(function() {
	value = $.cookie("PHPSESSID");
	$.cookie("AUID", value, { expires: 9999, path: '/' } );
	
	setTime(); 
	
	// $(".left_menu").mouseover(function() {return false;});
	$(".top_menu UL LI").mouseover(function() {
		if ($("#" + this.id).attr("class") != 'noactiv') {
			$("#" + this.id).attr("class", "activ");
		}
	});

	$(".top_menu UL LI").mouseout(function() {
		if ($("#" + this.id).attr("class") != 'noactiv') {
			$("#" + this.id).attr("class", "");
		}
	});

	$(".ulvkl LI").click(function() {
		lis = $("LI", ".ulvkl");
		for (i = 0; i < lis.length; i++) {
			id = $(lis[i]).attr("id");
			$(lis[i]).attr("class", "");
			$("#text_" + id).hide();
		}
		$("#" + this.id).attr("class", "activ");
		$("#text_" + this.id).show();
	});

	$("#last_li A").click(function() {
		dis = 1;
		$('#last_li').hide();
		$('#vkl_submenu_top').show();
		$('#submenu_top').show();
		return false;
	});

	$(".mw").click(function() {
		if (dis == 2) {
			$('#last_li').show();
			$('#vkl_submenu_top').hide();
			$('#submenu_top').hide();
			dis = 0;
			return false;
		}
	});
	$("#vkl_submenu_top A").click(function() {
		$('#last_li').show();
		$('#vkl_submenu_top').hide();
		$('#submenu_top').hide();
		return false;
	});

	/** ******************weather************************ */
	$("#ul_weather LI").click(
			function() {
				$(".weather_city").hide();
				$("#weather_" + this.id).show();
				$("#weather_div_hide").hide();
				$("#title_city").html(
						"Погода "
								+ $("#weather_" + this.id)
										.attr("title"));
			});
	/** ******************weather************************ */
	

});

function setTime() {
	name_month=new Array ("января","февраля","марта", "апреля","мая", "июня","июля","августа", "сентября","октября", "ноября","декабря");
	name_day=new Array ("Воскресенье","Понедельник","Вторник", "Среда","Четверг", "Пятница","Суббота");
	
	var currentTime = new Date();
	var month = currentTime.getMonth() + 1;
	if (month < 10){
		month = "0" + month;
	}
	var day = currentTime.getDate();
	var year = currentTime.getFullYear();
	
	var hours = currentTime.getHours();
	var minutes = currentTime.getMinutes();
	if (minutes < 10){
		minutes = "0" + minutes;
	}
	
	$("#date_time_label").html("Сегодня "+name_day[currentTime.getDay()]+",<br />"+day+"."+month+"."+year+" "+hours+":"+minutes);
}

function change_src(src, id) {
	$("#" + id).attr("src", src);
}

function change_tab(div, id) {
	lis = $("DIV", ".tab_news");
	for (i = 0; i < lis.length; i++) {
		id_activ = $(lis[i]).attr("id");
		if (id_activ != '') {
			$(lis[i]).attr("class", "");
			$("#tab_" + id_activ).hide();
		}
	}
	$("#" + id).attr("class", "activ");
	$("#tab_" + id).show();
}

function change_width_block(id, width_new) {
	if (!$.browser.msie) {
		$("#" + id).css("width",
				parseInt($(".tmp").css("width")) - width_new + "px");
	}
}

function change_text(id, text) {
	if ($("#" + id).val() == text) {
		$("#" + id).attr("value", "");
	} else if ($("#" + id).val() == '') {
		$("#" + id).attr("value", text);
	}
}

var c_pos = 0;

function c_prev(id, id2) {
	c_pos == 0 ? c_set((c_pos = (get_total_photo(id) - 1)), 0) : '';
	c_set(--c_pos, 200, id, id2);
}

function c_next(id, id2) {
	c_pos == (get_total_photo(id) - 1) ? c_set((c_pos = 0), 0) : '';
	c_set(++c_pos, 200, id, id2);
}

function c_set(pos, delay, id, id2) {
	$('#' + id).animate( {
		marginLeft : -pos * parseInt($("#" + id2).css("width"))
	}, delay);
}

function get_total_photo(id) {
	return $("DIV", $("#" + id)).length - $("P", $("#" + id)).length;
}

function insertAudioPlayer(url) {
	
	var flashvars = {
		"comment":""
		,"st":"/styles/audio.txt"
		,"file":url
	};
	
	var params = {
		wmode:"transparent"
		,allowScriptAccess:"always"
		,id:"audioplayer"
	}; 
	
	o = new swfobject.embedSWF(
		"/uppod.swf", "audioplayer"
		,"300", "34", "9.0.115.0", false, flashvars, params
	);
}

function setSearchTopCtgr(top_ctgr_id) {
	document.getElementById('search_top_ctgr_id').value = top_ctgr_id;
	document.getElementById('search_form_top').submit();
}

FONT_SIZE = 16;
function changeFont(inc) {
	FONT_SIZE += inc;
	$("#top_table").css("font-size",FONT_SIZE+"px");
}

function resetFont() {
	FONT_SIZE = 16;
	$("#top_table").css("font-size",FONT_SIZE+"px");
}

function errorDetected(e) {
	var keynum;
	var keychar;
	var enterCode = 13;

	// IE
	if(window.event) {
		keynum = e.keyCode;
	}
	
	// Netscape/Firefox/Opera
	else if(e.which) {
		keynum = e.which;
	}

	if ($.browser.chrome) {
		enterCode = 10;
	}

	keychar = String.fromCharCode(keynum);

	if(e.ctrlKey && keynum == enterCode) {
		var txt = getSelectionText();
		if (txt && txt.length) {
			txt = getContextSelectionText();
			text_error = txt[0]+"[<span id='error_text' style='color:red;'>"+txt[1]+"</span>]"+txt[2]; 
			$( "#errorDialogDetected_error_text").html(text_error);
			$( "#form_error_feedback_before").attr("value",txt[0]);
			$( "#form_error_feedback_error").attr("value",txt[1]);
			$( "#form_error_feedback_after").attr("value",txt[2]);
			$( "#form_error_feedback_url").attr("value",location);
			
			$( "#errorDialogDetected" ).dialog({
				width: 500,
				height: 340
			});
		}
	}
}

// Get user selection text on page
function getSelectionText() {
	var txt = '';
	if (txt = window.getSelection)
		txt = window.getSelection().toString();
	else
		txt = document.selection.createRange().text;
	return txt;
}

function getContextSelectionText() {
	par = window;
	if (par.getSelection) {
		selection = par.getSelection();
		if (selection.getRangeAt) {
			range = selection.getRangeAt(0);

			if (window.opera)
				prev = selection.getRangeAt(0);
			else
				prev = document.createRange();

			prev.setStartBefore(range.startContainer);
			prev.setEnd(range.startContainer, range.startOffset);
			prev = prev.toString();

			if (prev.length > 50)
				prev = prev.substring(Math.max(prev.length - 50, 0),
						prev.length).replace(/^\S{1,10}\s+/, "");

			if (window.opera)
				next = selection.getRangeAt(0);
			else
				next = document.createRange();

			next.setStart(range.endContainer, range.endOffset);
			next.setEndAfter(range.endContainer);
			next = next.toString();
			if (next.length > 100)
				next = next.toString().substring(0, 50).replace(/\s+\S{1,10}$/,	"");
		}

	} else if (par.document.selection.createRange) {
		selection = par.document.selection.createRange();

		selection.expand("word");

		prev = par.document.selection.createRange();
		prev.moveStart("character", -50);
		prev.setEndPoint('EndToStart', selection);
		prev.expand("word");
		prev = prev.text;

		next = par.document.selection.createRange();
		next.moveEnd("character", 50);
		next.setEndPoint('StartToEnd', selection);
		next.expand("word");
		next = next.text;

		selection = selection.text;
	}
//	return prev+"[<span id='error_text' style='color:red;'>"+selection+"</span>]"+next;
	return new Array(prev, selection, next );
}
