/*
 *  Kotelnikov Dmitriy(dimonnot@design.ru)
 */
var fixPNG;
var my = {
	time:[(new Date()).getHours(),(new Date()).getMinutes(),(new Date()).getSeconds(),new Date()],
	getXY:function(element){
		var x = 0;
		var y = 0;
		while(element){
			x += element.offsetLeft;
			y += element.offsetTop;
			element = element.offsetParent;
		}
		if(navigator.userAgent.indexOf("Mac") != -1 && typeof document.body.leftMargin != "undefined"){
			x += document.body.leftMargin;
			y += document.body.topMargin;
		}
		return {x:x,y:y}
	},
	menu:function(){
		var openElement = 0, isLocked = 0, isClose = 0;
		var resizer = {
			value:0,
			element:0,
			progress:function(value){
				if( !value )
					return resizer.value;
				resizer.value = value;
				if( resizer.element )
					resizer.element.css('height', value * resizer.element[0].h);
			    if( openElement ){
			        if( 1 - value <= 0 )
			            openElement.addClass('hidden');
				    openElement.css('height', 1 - value > 0 ? ((1 - value) * openElement[0].h) : '0px');
			    }
			}
		};
		var onfinished = function(newOpenElement){
			if( openElement )
				openElement.addClass('hidden');
			openElement = 0;
			if( newOpenElement ){
				openElement = newOpenElement;
				openElement.css('height', 'auto').css('overflow','visible');
			}
			isLocked = 0;
		};

		var headerMenuElement = $('#main-menu > li > .f strong');
		var firstSelected = 1;
		if( headerMenuElement.filter('.selected').length > 0 )
			firstSelected = 0;
		var headerSubMenu = $('#main-menu > li > ul');
		headerMenuElement.each(
			function(iIndex){
				var that = $(this).addClass('js');
				headerSubMenu[iIndex].h = headerSubMenu[iIndex].offsetHeight;

				if( that.hasClass('selected') || (firstSelected && iIndex == 0) )
					openElement = $(headerSubMenu[iIndex]);
				else
					$(headerSubMenu[iIndex]).addClass('hidden');

				that.click(
					function(){
						if( isLocked )
							return false;

						isClose = that.hasClass('selected');
						isLocked = 1;
						headerMenuElement.removeClass('selected');
						if( !isClose )
							that.addClass('selected');
						resizer.progress(0);

						if( !isClose ){
							resizer.element = $(headerSubMenu[iIndex]);
							resizer.element.css('height', 0).css('overflow', 'hidden').removeClass('hidden');
						}
						else
							resizer.element = 0;

						if( openElement )
							openElement.css('height', openElement[0].h).css('overflow','hidden');

						resizer.value = 0;
						$t(resizer).tween({
							progress:1,
							onComplete:onfinished,
							onCompleteParams:[!isClose ? $(headerSubMenu[iIndex]) : 0]
						});
						return false
					}
				)
			}
		)
	}
};

var people = new(function(){
	var aShowPeoples = new Array();

	this.show = function( peopleBlockID ){
		var i;
		var aImages = peoples[peopleBlockID], aNewImages;
		var iIndex = 0, bHave, aPersons;

		while( aImages.length > 0 ){
			bHave = 0;
			iIndex = Math.round(Math.random() * 1000) % aImages.length;

			aPersons = (new String(aImages[iIndex][1])).split(' ');
			for(i = 0; i < aPersons.length; i++){
				if( aShowPeoples[aPersons[i]] )
					bHave = 1
			}

			if( aImages[iIndex][1] != '' && bHave ){
				aNewImages = new Array();
				for(i = 0; i < aImages.length; i++){
					if( i != iIndex )
						aNewImages.push(aImages[i]);
				}
				aImages = aNewImages;
			}
			else{
				if( aImages[iIndex][1] != '' ){
					for(i = 0; i < aPersons.length; i++)
						aShowPeoples[aPersons[i]] = 1
				}
				$('#'+peopleBlockID).show().find('img:first').attr('src',aImages[iIndex][0]);
				break;
			}
		}
	}
});

var isPopupInit = 0, Popup = function( openPopupElements, onShowListener, onHideListener ){
	var elementOpenPopup = 0, isPopupVisible = 0, time;

	var hide = function(){
		if( (new Date()).getTime() - time < 500 )
			return;
		isPopupVisible = 0;
		elementOpenPopup = 0;
		Popup.popupElement.addClass('hidden');

		if( onHideListener )
			onHideListener();
	};

	if( !isPopupInit ){
		Popup.popupElement = $('#popup');
		Popup.isIE = /MSIE/.test(navigator.userAgent);
		Popup.popupElement.find('.content').click(
			function(){
				return false;
			}
		);
		isPopupInit = 1;
	}


	$(window).click(hide);
	$(document.body).click(hide);

	openPopupElements.mousedown(
		function(){
			time = (new Date()).getTime();
			if( isPopupVisible && elementOpenPopup == this ){
				hide();
				return false
			}

			if( onShowListener )
				onShowListener(Popup.popupElement, this);


			elementOpenPopup = this;
			if( !isPopupVisible ){
				Popup.popupElement.css('top', my.getXY(this).y + this.clientHeight - 10);
				if( Popup.isIE ){
		        	Popup.popupElement.removeClass('hidden');
				}
				else{
					Popup.popupElement.css('opacity',0).removeClass('hidden');
					$t(Popup.popupElement).tween({
						time:1.5,
						opacity:1
					});
				}
				isPopupVisible = 1;
			}
			return false;
		}
	).click(
		function(){
			return false;
		}
	)
};

var Placeholder = function( elements ){
	elements.blur(this.blur).focus(this.focus).each(this.blur);
};

Placeholder.prototype = {
	blur: function(){
		if( this.value == '' || this.value == this.title ){
			this.value = this.title;
			$(this).addClass('placeholder');
		}
	},

	focus: function(){
		if( this.value == this.title ){
			this.value = '';
			$(this).removeClass('placeholder');
		}
		return true
	}
};

$(
	function(){

		/*
		 * Плейсхолдер
		 */		
		new Placeholder($('#search'));
		new Placeholder($('#site-search'));

		var popupElements = $('#page-body .popupimage');
		if( popupElements.length ){
			new Popup(
				popupElements.next('img').css('cursor','pointer'),
				function(popupJElement, openerElement){
					var image = $(openerElement).prev();
					var width = parseInt(image.css('width').replace(/px/, '')) + 180;
					popupJElement.css('width', width).css('margin-right', '-' + Math.round(width/2) +'px').find('img').attr('src',image.attr('src'));
				}
			)
		}

		/*
 		 *  Добавляем навигацию с клавиатуры
 		 */
		$(document).keydown(
			function(e){
				if(e.ctrlKey){
					var href = false;
					switch (e.keyCode ? e.keyCode : e.which ? e.which : null){
						case 0x25:
							href = $('#prevLink').attr('href');
						break;
						case 0x27:
							href = $('#nextLink').attr('href');
						break;
        	    		case 0x26:
							href = $('#upLink').attr('href');
						break;
						case 0x28:
							href = $('#downLink').attr('href');
						break;
						case 0x24:
							href = $('#homeLink').attr('href');
						break;
					}
					if( href )
						document.location = href;
				}
			}
		);

		$('#page-body .mini').each(
			function(){
				var that = $(this).removeClass('hidden');
				var popupElement = that.find('.map');
				var hide = function(){
					popupElement.hide()
				};
				that.find('.open').mousedown(
					function(){
						popupElement.show();
						return false
					}
				);
				$(window).click(hide);
				$(document.body).click(hide);
			}
		);
	}
);
