function $S(id) { return document.getElementById(id); }

function setInputFieldAction(className)
	{
	var defValue = new Array();
	
	$("."+className+"[class!="+className+" null]").focus( function () {
		if ( !defValue[$(this).attr("name")] ) { defValue[$(this).attr("name")] = $(this).attr("value"); }
		if ( ($(this).attr("value") == "") || ($(this).attr("value") == defValue[$(this).attr("name")]) ) { $(this).attr("value", ""); $(this).css("color", "#000000"); }
	} );
	
	$("."+className+"[class!="+className+" null]").blur( function () {
		if ($(this).attr("value") == "")
			{
			$(this).attr("value", defValue[$(this).attr("name")] );
			$(this).css("color", "#D3CAAD");
			}
		} );
	
	$("."+className+"[class*=null]").css("color", "#D3CAAD");
	}

$(document).ready( function () {
	$(".h-middle a").hover( function() { $(".l2main").css("display", "block"); }, function() { $(".l2main").css("display", "none"); } );
	
	setInputFieldAction("inputField");
	$(".inputField").attr("autocomplete", "off");
	
	$(".headerLink div").mouseover( function() { $(this).css("background-color", "#EEF2FB"); } ).mouseout( function() { $(this).css("background-color", "#F7F7F7"); } );
	
	refreshHeight();
	$(window).resize(function() { refreshHeight(); } );
} );

function refreshHeight()
	{
	wth = $(window).height() - 344;
	if ( $(".main-block").height() < wth ) { $(".main-block").css("height", wth+"px"); } else { $(".main-block").css("height", "auto"); }
	}


function peopleShowInfo( show, id )
	{
	$.post("/sub/ajax.people.php", { show: show, id: id } , function(data) {
		showContent( data.out );
		} , "json");
	}

function showContent( text )
	{
	closeContent();
	closeFill();
		
	showFill();
	
	posX = Math.round( ( $(window).width() / 2 ) - ( 540 / 2 ) );
	posY = Math.round( ( $(window).height() / 2 ) - ( 500 / 2 ) + $(window).scrollTop() );
	
	$("body").append("<div id='fContent'><div id='fContentIn'>"+text+"</div></div>");
	$("#fContent").css("width", 540 + 2 + "px")
				  .css("height", 500 + 2 + "px")
				  .css("position", "absolute")
				  .css("z-index", "2000")
				  .css("top", posY + "px")
				  .css("left", posX + "px")
				  .css("border", "1px solid #8F8F8D")
				  .css("background-color", "#FFFFFF")
				  .css("padding", "20px")
				  .css("padding-right", "0px");
	
	$("#fContentIn").css("overflow-y", "scroll")
					.css("height", 500 + 2 + "px")
					.css("padding-right", "20px");
	
	$("#fContent").append("<img src='/template/image/close.gif' width='15' height='15' alt='Закрыть' title='Закрыть' id='fContentClose'>");
	$("#fContentClose").css("position", "absolute")
					 .css("z-index", "2002")
					 .css("margin-left", "526px")
					 .css("margin-top", "-523px")
					 .css("cursor", "pointer")
					 .css("border", "1px solid #8F8F8D");
	if ( $.browser.msie ) { $("#fContentClose").css("margin-left", "526px"); }
	
	$("#fContentClose").click( function () { closeContent(); closeFill(); } );
	
	$("#fill").click( function () { closeContent(); closeFill(); } );
	}

function closeContent()
	{
	$("#fContent").remove();
	}

function showFill()
	{
	winX = $(window).width();
	winY = $(window).height();
	
	marY = $(window).scrollTop();
	
	$("body").append("<img src='/template/image/fill.png' id='fill'>");
	$("#fill").css("width", winX + "px").css("height", winY + "px").css("position", "absolute").css("top", marY + "px").css("left", "0px").css("z-index", "1000");
	
	$(window).scroll( function () {
		fillChange();
		} );
	
	$(window).resize(function() {
		fillChange();
		} );
	}

function fillChange()
	{
	winX = $(window).width();
	winY = $(window).height();
	$("#fill").css("width", winX + "px").css("height", winY + "px");
	
	marY = $(window).scrollTop();
	$("#fill").css("top", marY + "px");
	
	posX = Math.round( ( $(window).width() / 2 ) - ( parseInt( $("#fContent").css("width") ) / 2 ) + $(window).scrollLeft() );
	posY = Math.round( ( $(window).height() / 2 ) - ( parseInt( $("#fContent").css("height") ) / 2 ) + $(window).scrollTop() );
	
	//if ( $("#fImage") ) { $("#fImage").css("left", posX + "px").css("top", posY + "px"); }
	if ( $("#fContent") ) { $("#fContent").css("left", posX + "px").css("top", posY + "px"); }
	}

function closeFill()
	{
	$("#fill").remove();
	}

/* rimg */

function rimgInit()
	{
	rimgNow = 0;
	rimgMax = 0;
	inAnimation = false;
	rimgMaxWidth = 0;
	rimgMaxHeight = 0;
	rimgStartLeft = 0;
	
	$('#pictures img').each(function()
		{
		$(this).attr('id', 'rimg' + rimgNow);
		if ( rimgMaxWidth  < parseInt($(this).css("width"))  ) { rimgMaxWidth = parseInt($(this).css("width")); }
		//if ( rimgMaxHeight < parseInt($(this).css("height")) ) { rimgMaxHeight = parseInt($(this).css("height")); }
		rimgMaxHeight = 450;
		rimgNow++;
		} );
	
	$("#pictures > .rimgClick").click( function() {
		//rID = $(this).attr("id").replace("rimg","");
		//showImage( rID );
		rimgTurnImage("right");
		return false;
	} );
	
	$("a[href='#original']").click( function() {
		rID = $("#pictures > .rimgClick:visible").attr("id").replace("rimg","");
		showImage( rID );
		return false;
	} );
	
	rimgNow--;
	rimgMax = rimgNow;
	
	rimgSetDNone(rimgNow);
	
	$("#pictures").css("overflow", "hidden").css("width", rimgMaxWidth + "px").css("height", rimgMaxHeight + "px");
	
	/* start auto change */
	rimgAutoChange = true;
	$(".photo-rep").mouseover( function() { rimgAutoChange = false; } ).mouseout( function() { rimgAutoChange = true; } );
	setTimeout( 'rimgAuto();', 10000 );
	}

function rimgTurnImage( where )
	{
	if ( !inAnimation )
		{
		inAnimation = true;
		
		if ( where == 'right' ) { rimgNext = rimgNow - 1; if ( rimgNext < 0 ) { rimgNext = rimgMax; } } else { rimgNext = rimgNow + 1; if ( rimgNext > rimgMax ) { rimgNext = 0; } }
		
		//$('#pictures').after( rimgNext );
		
		directionNext = '-'; if ( where == 'right' ) { directionNext = ''; }
		directionNow = '';   if ( where == 'right' ) { directionNow = '-'; }
		
		$("#rimg" + rimgNext).css("display", "inline").css("left", directionNext + $("#rimg" + rimgNext).outerWidth(true) + "px" ).animate( { "left" : rimgStartLeft+"px" }, 'slow');
		$("#rimg" + rimgNow).animate( { "left" : directionNow + $("#rimg" + rimgNow).outerWidth(true) + "px" }, 'slow', function () {
			rimgNow = rimgNext;
			rimgSetDNone(rimgNow);
			inAnimation = false;
			} );
		}
	}

function rimgSetDNone(rimgNow)
	{
	$('#pictures img').each(function()
		{
		if ( $(this).attr('id') != ( 'rimg' + rimgNow ) ) { $(this).css("display", "none"); }
		} );
	}

function rimgAuto()
	{
	if (rimgAutoChange) { rimgTurnImage('right'); }
	setTimeout( 'rimgAuto();', 10000 );
	}

function showImage( rID )
	{
	$.post("/sub/ajax.image.php", { id: rID , fS : "original" } , function(data) {
		//alert(data.image.link);
		window.open(data.image.link, 'page_id','resizable=no,top='+(200)+',left='+(200)+',width='+(data.image.x + 20)+',height='+(data.image.y + 20)+',location=no,status=no,scrollbars=yes');
		} , "json");
	}

$(document).ready( function() {
	$("a[href='#map']").click( function() {
		coord = $(this).attr("rel").split(",");
		ctext = $(this).html();
		
		showContent( "<div id='YMapsID' style='width: 520px; height: 300px;'></div>" );
		$("#fContentIn").css("overflow", "hidden");
		$("#fContent").css("height", "300px");
		
		YMaps.jQuery(function () {
			// Создание экземпляра карты и его привязка к созданному контейнеру
			map = new YMaps.Map(YMaps.jQuery("#YMapsID")[0]);
			
			// Установка для карты ее центра и масштаба
			map.setCenter(new YMaps.GeoPoint(60.60, 56.83), 12);
			
			// Добавление элементов управления
			map.addControl(new YMaps.Zoom());
			map.addCursor(YMaps.Cursor.ARROW);
			map.disableDblClickZoom();
			map.enableScrollZoom();
			map.setMaxZoom(18);
			map.setMinZoom(12);
			
			placemark = new YMaps.Placemark(new YMaps.GeoPoint(coord[0],coord[1]) );
			placemark.description = ctext;
			map.addOverlay(placemark);
			map.setCenter(new YMaps.GeoPoint(coord[0],coord[1]), 14);
			} );
		
		return false;
	} );
} );
