// JavaScript Document

function searchSubmit() {
	if ($('#searchstring').val() && $('#searchstring').val() != 'Поиск по сайту') {	self.location = "/search/?q="+$('#searchstring').val(); }
	else { $('#searchstring').focus(); }
}

function openLoginForm() {
	if ($('#dialog').size() == 0) {
		content = '<div id="dialog">\n'+
			'	<div class="centerer">\n'+
			'		<div class="shader">\n'+
			'			<div class="lt"><img src="/img/_.gif" /></div>\n'+
			'			<div class="rt"><img src="/img/_.gif" /></div>\n'+
			'			<div class="lb"><img src="/img/_.gif" /></div>\n'+
			'			<div class="rb"><img src="/img/_.gif" /></div>\n'+
			'			<div class="container">\n'+
			'				<div class="contents">\n'+
			'					<img src="/img/_.gif" class="closebutton">\n'+
			'					<h2>Вход для зарегистрированных пользователей</h2>\n'+
			'					<form id="loginForm" target="_top" action="/index.php?login=yes" method="post">\n'+
			' 						<input type="hidden" name="backurl" value="/index.php" />\n'+
			'						<input type="hidden" name="AUTH_FORM" value="Y" />\n'+
			'						<input type="hidden" name="TYPE" value="AUTH" />\n'+
			'						<fieldset>\n'+
			'							<label>Логин:</label>\n'+
			'							<div><input type="text" name="USER_LOGIN" /></div>\n'+
			'							<label>Пароль:</label>\n'+
			'							<div><input type="password" name="USER_PASSWORD" /></div>\n'+
			'						</fieldset>\n'+
			'						<fieldset>\n'+
			'							<input type="submit" name="Login" class="submit" value="Войти" />\n'+
			'							<div class="signin"><a href="/auth/">Регистрация</a></div>\n'+
			'						</fieldset>\n'+
			'					</form>\n'+
			'				</div>\n'+
			'			</div>\n'+
			'		</div>\n'+
			'	</div>\n'+
			'</div>\n';

		$('body').append(content);
		$('#dialog').css('visibility','hidden');
		$('#dialog').css('display','block');
		if ( $.browser.msie ) {
			if ( $.browser.version == '6.0') {
				$('#dialog .lt, #dialog .rt, #dialog .lb, #dialog .rb').each(function(index) {
					$(this).height(($('#dialog .shader').height()/2)+"px");
					$('#dialog').height($('#dialog').height());
				});
				$(window).scroll(function (index) {
					pagebody = document.getElementsByTagName('body')[0];
					$('#dialog').css('top',$('html').scrollTop());
				});
				$(window).resize(function (index) {
					$('#dialog').height($(window).height());
				});
			}
		}
		$('#dialog .closebutton').bind("click", function() {
			closeLoginForm();
		});
/* temporary strings for preview purposes - remove */
/*		$('#dialog .submit').bind("click", function() {
			closeLoginForm();
			$('#login, #login_v3').hide();
			$('#logged, #logged_v3').show();
		}); */
/* end of temporary strings */

	}
	$('#dialog').css('visibility','visible');
	if (typeof stopSliding != "undefined") {
		stopSliding();
	}
}

function closeLoginForm() {
	if ($('#dialog').size() > 0) {
		$('#dialog').css('visibility','hidden');
	}
	if (typeof startSliding != "undefined") {
		clearTimeout(SlideTimer);
		startSliding();
	}
}


/* function openSigninForm() {
	self.location = "/signin/";
}
 */
function regionSubmit() {
	self.location = self.location;
}


var ClockHours = false;
var ClockMinutes = false;
var ClientMinutes = false;

function startClock() {
	ClockHours = $("#time .hours").text()*1;
	ClockMinutes = $("#time .minutes").text()*1;
	if (ClockMinutes && ClockHours) {
		ClientTime = new Date();
		ClientMinutes = ClientTime.getMinutes();
		clockTickTock()
	}
}

function clockTickTock() {
	setClockTime()
	setTimeout('clockTickTock()', 1000);
}


function setClockTime() {
	ClientTime = new Date();
	if (ClientMinutes != ClientTime.getMinutes()) {
		setClockMinutes();
		ClientMinutes = ClientTime.getMinutes()
	}
	$("#time .blink").toggleClass("show");
}

function setClockMinutes() {
	ClockMinutes++;
	if (ClockMinutes > 59) {
		ClockMinutes = 0;
		setClockHours();
	}
//	alert(ClockMinutes.length);
	if (ClockMinutes < 10 ) { 
		$("#time .minutes").text("0"+ClockMinutes);
	}
	else {
		$("#time .minutes").text(ClockMinutes);
	}
	
}

function setClockHours() {
	ClockHours++;
	if (ClockHours > 23) ClockHours = 0;
	if (ClockHours<10) { 
		$("#time .hours").text("0"+ClockHours);
	}
	else {
		$("#time .hours").text(ClockHours);
	}
}


$(document).ready(function(){
	if ($("#time")) {
		startClock();
	}
	if ($(".photo a").length > 0) {
		$(".photo a").colorbox({initialWidth:200, initialHeight:200, opacity:0.5, overlayClose: false, title:"&nbsp;"});
	}
	
	$("#topmenu>li").mouseenter(function() {
	    $(this).addClass("over");
	    $(this).children("ul").fadeIn(100);
	}).mouseleave(function() {
	    $(this).removeClass("over");
	    $(this).children("ul").fadeOut(100);
	});

	$(".qa dt").click(function() {
		$(this).blur();
		$(this).next("dd").slideToggle("fast", function () { $("#main").css({width:'100%'}); });
		$(this).toggleClass("opened");
		return false;
	});

});

// Yandex Maps

function createPlacemark (geoPoint) {

	point = new YMaps.Style();
	point.iconStyle = new YMaps.IconStyle();
	point.iconStyle.href = "/img/map-icon.png";
	point.iconStyle.size = new YMaps.Point(20, 22);
	point.iconStyle.offset = new YMaps.Point(-10, -22);

	point.iconStyle.shadow = new YMaps.IconShadowStyle();
	point.iconStyle.shadow.href = "/img/map-icon-shadow.png";
	point.iconStyle.shadow.size = new YMaps.Point(25, 22);
	point.iconStyle.shadow.offset = new YMaps.Point(-10, -22);


	var placemark = new YMaps.Placemark(geoPoint, {style : point});
	YMaps.Events.observe(placemark, placemark.Events.Click, gotoPoint);
	return placemark;
}

function gotoPoint(e) {
	if (map.getZoom() < 8) {
		map.zoomBy(3, {smooth:true, position:e.getGeoPoint(),centering:true});
	}
	else if (map.getZoom() < 16) {
		map.zoomBy(2, {smooth:true, position:e.getGeoPoint(),centering:true});
	}
	else {
		map.zoomBy(1, {smooth:true, position:e.getGeoPoint(),centering:true});
	}
}
