function InstallSearch(){
	$('#search input[type=text]').focus(function(){
		if (this.value == 'поиск по сайту'){
			this.value = '';
		}
		$(this).css({color: 'black'});
	}).blur(function(){
		if (this.value == ''){
			this.value = 'поиск по сайту';
			$(this).css({color: 'gray'});
		}
	});
	$('#search img').click(function() {
		$('#search').submit();
	});
}

function InstallMenuWrapper(){
	var OnSize = function() {
		if ($(document).width() > 1200) {
			$('#menu-top').addClass('horz');
		} else {
			$('#menu-top').removeClass('horz');
		}
	}
	$(window).resize(OnSize);
	OnSize();
}

function InstallSuperSlider(reset){
	var collapsed = true;
	var sliderWidth;
	var criticalSection = false;
	var heightMin = 191, heightMax = 331, widthMax = 751;

	function cancel(){
		if ( ! criticalSection) {
			criticalSection = true;
			var slider = $('#super-slider .slider');
			var border = slider.find('.border');

			function custom() {
				CustomAnimation(function(X){
					border.css({height: heightMax - (heightMax - heightMin)*X});
					slider.css({width: widthMax - (widthMax-sliderWidth)*X});
				}, 1000, true, function(){
					slider.css({width: '100%'});
					$('#center-column').css({zIndex: 2});
					criticalSection = false;
				});
			}

			if ($.browser.msie) {
				slider.find('.hidden').hide();
				custom();
			} else {
				slider.find('.hidden').fadeOut(500, function() {
					custom();
				});
			}
			$('#savan').hide();
			collapsed = true;
		}
		return false;
	}

	if (reset) {
		$('#super-slider .slider').css({width: '100%'})
			.find('.border').css({height: 191})
			.find('.hidden').hide();
		collapsed = true;
	}
	$('#super-slider .slider').unbind('click').click(function(){
		if ( ! criticalSection) {
			var slider = $(this);
			var border = slider.find('.border');
			if (collapsed) {
				criticalSection = true;
				sliderWidth = slider.width();
				$('#super-slider h1').css('left', $('#super-slider h1').css('left'));
				$('#super-slider p').css('padding-left', $('#super-slider p').css('padding-left'));
				CustomAnimation(function(X){
					border.css({height: (heightMax - heightMin)*X + heightMin});
					slider.css({width: (widthMax-sliderWidth)*X + sliderWidth});
				}, 1000, true, function(){
					if ($.browser.msie) {
						slider.find('.hidden').show();
						criticalSection = false;
					} else {
						slider.find('.hidden').fadeIn(500, function(){
							criticalSection = false;
						});
					}
				});
				$('#savan').unbind().click(cancel).show();
				collapsed = false;
			} else {
				cancel();
			}
		}
		return false;
	});
	$('#super-slider .slider p a').unbind('click').click(function(){
		window.open(this.href, '_blank');
		return false;
	});
}

function InstallSuperMovedOkno(){
	var current = false;
	var currentFontSize = 16;
	var criticalSection = false;

	$('#moved-tabs ul a').each(function(){
		var next = $(this).attr('rel');
		var nextLeft = $('#center-column .fons.'+next);
		if ( ! nextLeft.length) {
			$(this).parent().addClass('normal-link');
		}
	});

	function cancel(){
		$('#center-column .fons.'+current).animate({left: 0}, 300, function(){
			$(this).hide();
			$('#center-column #okno').fadeOut(600, function(){
				$('#center-column, #tabbed-tabs').css({zIndex: 1});
				criticalSection = false;
			});
		});
		$('#tabbed-tabs a.'+current).animate({fontSize: currentFontSize}, 400, function(){
			$(this).removeClass('hover');
		});
		$('#savan').hide();
		current = false;
		return false;
	}

	$('#center-column .fons').each(function(){
		this._left = $(this).css('left');
		$(this).css({visibility:'visible', display:'none'});
	});
	$('#moved-tabs ul a').click(function(){
		if ( ! criticalSection) {
			criticalSection = true;
			var $this = $(this);
			$('#center-column').css({zIndex: 20});
			$('#tabbed-tabs').css({zIndex: 25});
			InstallSuperSlider(true);
			var next = $this.attr('rel');
			if (next && (next == current)) {
				return true;
			} else if (next && ! $this.parent().hasClass('normal-link')) {
				var nextLeft = $('#center-column .fons.'+next).get(0)._left;
				if (current) {
					$('#tabbed-tabs a.'+current).animate({fontSize: currentFontSize}, 400, function(){
						$(this).removeClass('hover');
					});
					$('#center-column .fons.'+current).animate({left: 0}, 300, function(){
						$(this).hide();
						$('#center-column .fons.'+next).css({left:0}).show().animate({left:nextLeft}, 300, function(){
							criticalSection = false;
						});
					});
				} else {
					if ($.browser.msie && $.browser.version == 6) {
						var height = $('#center-column').height();
						$('#center-column #okno .fog-down').css({top: height - 20});
						$('#center-column #okno .fog-up').css({top: height - 70});
						//$('#center-column #okno .background').css({height: height});
						$('#center-column #okno').height(height);
						$('#savan').height($(document).height());
					}
					$('#center-column #okno').fadeIn(600, function(){
						$('#center-column .fons.'+next).css({left:0}).show().animate({left:nextLeft}, 300, function(){
							criticalSection = false;
						});
					});
					$('#savan').unbind().click(cancel).show();
				}
				$('#center-column #okno .okno-text, #center-column #okno .okno-float').hide().filter('.'+next).show();
				currentFontSize = $this.addClass('hover').animate({fontSize: 20}, 400).css('font-size');
				current = next;
				return false;
			}
			return true;
		}
		return false;
	});

	$('#okno .close').click(cancel);
}

function InstallSuperOkno(){
	var current = false;
	var currentFontSize = 16;
	var criticalSection = false;

	function cancel(){
		$('#center-column .fons.'+current).animate({left: 0}, 300, function(){
			$(this).hide();
			$('#center-column #okno').fadeOut(600, function(){
				$('#center-column, #tabbed-tabs').css({zIndex: 2});
				criticalSection = false;
			});
		});
		$('#tabbed-tabs a.'+current).animate({fontSize: currentFontSize}, 400, function(){
			$(this).removeClass('hover');
		});
		$('#savan').hide();
		current = false;
		return false;
	}

	$('#center-column .fons').each(function(){
		this._left = $(this).css('left');
		$(this).css({visibility:'visible',display:'none'});
	});
	$('#tabbed-tabs ul a').click(function(){
		if ( ! criticalSection) {
			criticalSection = true;
			$('#center-column').css({zIndex: 20});
			$('#tabbed-tabs').css({zIndex: 25});
			InstallSuperSlider(true);
			var next = $(this).attr('rel');
			if (next && (next == current)) {
				return true;
			} else if (next) {
				var nextLeft = $('#center-column .fons.'+next);
				if ( ! nextLeft.length) {
					return true;
				}
				nextLeft = nextLeft.get(0)._left;
				if (current) {
					$('#tabbed-tabs a.'+current).animate({fontSize: currentFontSize}, 400, function(){
						$(this).removeClass('hover');
					});
					$('#center-column .fons.'+current).animate({left: 0}, 300, function(){
						$(this).hide();
						$('#center-column .fons.'+next).css({left:0}).show().animate({left:nextLeft}, 300, function(){
							criticalSection = false;
						});
					});
				} else {
					if ($.browser.msie && $.browser.version == 6) {
						var height = $('#center-column').height();
						$('#center-column #okno .fog-down').css({top: height - 20});
						$('#center-column #okno .fog-up').css({top: height - 70});
						//$('#center-column #okno .background').css({height: height});
						$('#center-column #okno').height(height);
						$('#savan').height($(document).height());
					}
					$('#center-column #okno').fadeIn(600, function(){
						$('#center-column .fons.'+next).css({left:0}).show().animate({left:nextLeft}, 300, function(){
							criticalSection = false;
						});
					});
					$('#savan').unbind().click(cancel).show();
				}
				$('#center-column #okno .okno-text, #center-column #okno .okno-float').hide().filter('.'+next).show();
				currentFontSize = $(this).addClass('hover').animate({fontSize: 20}, 400).css('font-size');
				current = next;
				return false;
			}
			return true;
		}
		return false;
	});

	$('#center-column #okno .close').click(cancel);
}

function InstallStuffOkno(){
	function cancel(){
		$('#index-content .stuff-on').hide();
		$('#index-content').css({zIndex: 2});
		$('#savan').hide();
	}

	$('#index-content .stuff-off').hover(function(){
		$(this).addClass('hover');
	}, function(){
		$(this).removeClass('hover');
	}).click(function(){
		$(this).removeClass('hover');
		$('#index-content .stuff-on').show();
		$('#index-content').css({zIndex: 100});
		$('#savan').unbind().click(cancel).show();
	});
	$('#index-content .stuff-on h2').click(function(){
		cancel();
		$('#index-content .stuff-off').addClass('hover');
	});
}
function init_submenu() {
	var menu = $('#inner-menu');
	var item_link = menu.find('.item0 h4');
	var item_wrapper = menu.find('.item0 .wrapper');
	var item_sub = menu.find('.item0 .item ins');
	var item_sub_link = menu.find('.item0 .item a.sub');
	
	var hide_wrapper = function() {
		//item_wrapper.slideUp();
		//item_wrapper.parent().removeClass('active');
		item_wrapper.each(function() {
			if($(this).parent().hasClass('active')) {
				$(this).slideUp();
				$(this).parent().removeClass('active');
			}
		});
	}
	
	item_link.click(function() {
		$this = $(this);
		if($this.parent().hasClass('active')) {
			hide_wrapper();
		}
		else {
			hide_wrapper();
			$this.parent().find('.wrapper').slideDown();
			$this.parent().find('.wrapper').show();
			$this.parent().addClass('active');
		}

	});
	item_sub_link.click(function() {
		$this = $(this);

		if($this.parent().hasClass('active1')) {
			item_sub.slideUp();
			item_sub.parent().removeClass('active1');
		}
		else {
			item_sub.slideUp();
			item_sub.parent().removeClass('active1');
			$this.parent().find('ins').slideDown();
			$this.parent().addClass('active1');
		}

	});

}
function InstallTab() {
	var inner = $('#inner');
	var inner_tabcontent = inner.find('.tabcontent');
	var inner_tabcontent_first = inner.find('.tabcontent:first-child');
	var inner_menu = $('#inner-menu');
	var inner_menu_link = $('#inner-menu ins a');
	var cnt = $.cookie('tabcounter');

	if(cnt) {
		$('#'+cnt).css('display', 'block');
		$('#inner-menu ins a[rel='+cnt+']').addClass('active');
		$.cookie('tabcounter', null,  {path: '/'});
	}
	else {
		inner_tabcontent_first.css('display', 'block');
	}

	inner_menu_link.click(function() {
		var $this = $(this);
		$.cookie('tabcounter', $this.attr('rel'), {path: '/'});
		inner_menu_link.removeClass('active');
		$this.addClass('active');
		inner_tabcontent.css('display', 'none');
		$('#'+$this.attr('rel')).css('display', 'block');
	});

}
$(function() {

	var ie6 = $.browser.msie && /MSIE (6).+Win/.test(navigator.userAgent) && !/MSIE (7|8|9).+Win/.test(navigator.userAgent);

	if( $("#site-select").length ) {
		$("#site-select").find(".link a").bind("click", function(){
			$("#site-select").toggleClass("site-select-open");
			return false;
		});

		$("#site-select").find(".close").bind("click", function(){
			$("#site-select").removeClass("site-select-open");
			return false;
		});

		$("#body").bind("click", function(){
			$("#site-select").find(".close").trigger("click");
		});

		if(ie6) {
			$("#site-select").find(".link").hover( function(){
				$("#site-select").find(".link").addClass("hover_ie");
			}, function(){
				$("#site-select").find(".link").removeClass("hover_ie")
			});
		}
	}
	InstallTab();
	init_submenu();
	InstallSearch();
	//InstallMenuWrapper();
	InstallSuperSlider();
	//InstallSuperOkno();
	InstallSuperMovedOkno();
	InstallStuffOkno();
});