//IE6 bug fixer
function jerkFooter(){
	if( ($.browser.msie && (jQuery.browser.version.substr(0,1)=="6")) ){
		$('#footer').css('bottom', '-1px').css('bottom','0');
	}
}

$(document).ready(function() {
	function hovers(element){
		var topblocks_reg1 = /_act/;
		var topblocks_reg2 = /(\.gif)/;
		var topblocks_reg3 = /_hover/;
		var topblocks_reg4 = /_click/;
		element.mouseover(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg2, '_hover$1'));
			}
		});
		element.mouseout(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg3, ''));
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg4, ''));
			}
		})
		element.mousedown(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg3, '_click'));
			}
		});
		element.mouseup(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg4, ''));
			}
		});
	}
	
	$('#right_news .pic img, #auth_column .pic img, .news_item .pic img').each(function(){
		$(this).wrap('<div class="image_place" />');
		var tmp = $(this).parent();
		//tmp.width($(this).width());
		//tmp.height($(this).height());
		tmp.css('background','url(' + $(this).attr('src') + ') 0px 0px no-repeat');
		$(this).css('visibility','hidden');
		tmp.corner("3px");
	});
	
	$('.news_inside .pic img, .author_s .pic img').each(function(){
		$(this).wrap('<div class="image_place" />');
		var tmp = $(this).parent();
		//tmp.width($(this).width());
		//tmp.height($(this).height());
		tmp.css('background','url(' + $(this).attr('src') + ') 0px 0px no-repeat');
		$(this).css('visibility','hidden');
		tmp.corner("5px");
	});
	
	$('.trading_article .pic img').each(function(){
		$(this).wrap('<div class="image_place" />');
		var tmp = $(this).parent();
		//tmp.width($(this).width());
		//tmp.height($(this).height());
		tmp.css('background','url(' + $(this).attr('src') + ') 0px 0px no-repeat');
		$(this).css('visibility','hidden');
		tmp.corner("9px");
	});
	
	$('form.generic .warn').corner('4px');
	
	$('#nowvideo .pic img.r, .photovideo .video .videos .pic img').each(function(){
		$(this).wrap('<div class="image_place" />');
		$(this).after('<img src="./images/player.gif" class="player" alt="" />');
		var tmp = $(this).parent();
		//tmp.width($(this).width());
		//tmp.height($(this).height());
		tmp.css('background','url(' + $(this).attr('src') + ') 0px 0px no-repeat');
		$(this).css('visibility','hidden');
		tmp.corner("3px");
	});
	
	$('.voteres .block').corner("5px");
	
	// Gray text becomes normal and back
	$('#rightforms .login input.text.default[value!=Логин], #rightforms .login input.password.default[value!=Пароль], #rightforms .search .text[value!=Поиск по сайту], .subscribe .final input[value!=Введите адрес электронной почты]').removeClass('default');
	$('#rightforms .login input.text.default, #rightforms .login input.password.default, #rightforms .search .text, .subscribe .final input').focus(function(){
 		$(this).removeClass('default');	
 		$(this).val('');
	});
	$('#rightforms .login input.text').blur(function(){
		if($(this).val() == ''){
			$(this).addClass('default');
			$(this).val('Логин');
		}
	});
	$('#rightforms .login input.password').blur(function(){
		if($(this).val() == ''){
			$(this).addClass('default');
			$(this).val('Пароль');
		}
	});
	$('#rightforms .search input.text').blur(function(){
		if($(this).val() == ''){
			$(this).addClass('default');
			$(this).val('Поиск по сайту');
		}
	});
	$('.subscribe .final input').blur(function(){
		if($(this).val() == ''){
			$(this).addClass('default');
			$(this).val('Введите адрес электронной почты');
		}
	});
	
	$('#hormenu td .hidden_block').each(function(){
		$(this).width($(this).parents('td').outerWidth(true) + 2);
		$(this).css('left',$(this).parents('td').position().left - 1);
	});
	
	$('#hormenu2 .right .inner2 .hidden_block').each(function() {
		$(this).width($(this).parents('.right').outerWidth(true));
	});
	$('#hormenu2 .left .inner2 .hidden_block').each(function() {
		$(this).width($(this).parents('.left').outerWidth(true));
	});
	
	
	var opened = false;	
	var clickers = $('#hormenu td a, #hormenu2 .right .inner2 > a, #hormenu2 .left .inner2 > a, .vaccity a.ajax');
	var clickerso = $('#hormenu td a.opened, #hormenu2 .right .inner2 a.opened, #hormenu2 .left .inner2 a.opened, .vaccity a.opened');
	var clickersoh = $('#hormenu td .hidden_block, #hormenu2 .right .inner2 .hidden_block, #hormenu2 .left .inner2 .hidden_block, .vaccity .hidden_block');
	var clickersoha = $('#hormenu td .hidden_block a, #hormenu2 .right .inner2 .hidden_block a, #hormenu2 .left .inner2 .hidden_block a, .vaccity .hidden_block a');
	function closerc(){
		var clickers = $('#hormenu td a, #hormenu2 .right .inner2 > a, #hormenu2 .left .inner2 > a, .vaccity a.ajax');
		var clickerso = $('#hormenu td a.opened, #hormenu2 .right .inner2 a.opened, #hormenu2 .left .inner2 a.opened, .vaccity a.opened');
		var clickersoh = $('#hormenu td .hidden_block, #hormenu2 .right .inner2 .hidden_block, #hormenu2 .left .inner2 .hidden_block, .vaccity .hidden_block');
		var clickersoha = $('#hormenu td .hidden_block a, #hormenu2 .right .inner2 .hidden_block a, #hormenu2 .left .inner2 .hidden_block a, .vaccity .hidden_block a');
		$('#closer').remove();
		clickers.parents('td').removeClass('opened');
		clickerso.removeClass('opened');
		clickersoh.hide();
		opened = false;
	}
	
	clickers.click(function(){		
		if($(this).next('.hidden_block').length){
			var clickers = $('#hormenu td a, #hormenu2 .right .inner2 > a, #hormenu2 .left .inner2 > a, .vaccity a.ajax');
			var clickerso = $('#hormenu td a.opened, #hormenu2 .right .inner2 a.opened, #hormenu2 .left .inner2 a.opened, .vaccity a.opened');
			var clickersoh = $('#hormenu td .hidden_block, #hormenu2 .right .inner2 .hidden_block, #hormenu2 .left .inner2 .hidden_block, .vaccity .hidden_block');
			var clickersoha = $('#hormenu td .hidden_block a, #hormenu2 .right .inner2 .hidden_block a, #hormenu2 .left .inner2 .hidden_block a, .vaccity .hidden_block a');
			if(opened){
				$(this).removeClass('opened');
				$(this).parents('td').removeClass('opened');
				$('#closer').remove();
				$(this).next('.hidden_block').hide();
				opened = false;
				if(clickerso.length){ // if we closed nothing..
					clickers.parents('td').removeClass('opened');
					clickerso.removeClass('opened');
					clickersoh.hide();
					opened = true;
					$(this).addClass('opened');
					$(this).parents('td').addClass('opened');
					$(this).next().show();
					$('#footer').after('<div id="closer" />');
					$('#closer').height($('#all').height()).click(closerc);
				}
			}
			else{
				opened = true;
				$(this).parents('td').addClass('opened');
				$(this).addClass('opened');
				$(this).next().show();
				$('#footer').after('<div id="closer" />');
				$('#closer').height($('#all').height()).click(closerc);
			}
		}
	});
	
	clickersoha.click(function() {
		var clickers = $('#hormenu td a, #hormenu2 .right .inner2 > a, #hormenu2 .left .inner2 > a, .vaccity a.ajax');
		var clickerso = $('#hormenu td a.opened, #hormenu2 .right .inner2 a.opened, #hormenu2 .left .inner2 a.opened, .vaccity a.opened');
		var clickersoh = $('#hormenu td .hidden_block, #hormenu2 .right .inner2 .hidden_block, #hormenu2 .left .inner2 .hidden_block, .vaccity .hidden_block');
		var clickersoha = $('#hormenu td .hidden_block a, #hormenu2 .right .inner2 .hidden_block a, #hormenu2 .left .inner2 .hidden_block a, .vaccity .hidden_block a');
		$('#closer').remove();
		clickers.parents('td').removeClass('opened');
		clickerso.removeClass('opened');
		clickersoh.hide();
		opened = false;
	});
	
	if(! ($.browser.msie && (jQuery.browser.version.substr(0,1)=="6")) ){
		$('#hormenu2 .left .main').css('position', 'relative');
		$('#hormenu2 .left .main_inner').css('position', 'absolute');
	
		//change the main div to overflow-hidden as we can use the slider now
		$('#hormenu2 .left .main').css('overflow','hidden');
		
		//calculate the height that the scrollbar handle should be
		$('#hormenu2 .left .hidden_block').show();
		var difference = $('#hormenu2 .left .main_inner').height()-$('#hormenu2 .left .main').height();//eg it's 200px longer
		$('#hormenu2 .left .hidden_block').hide();
		
		if(difference>0)//if the scrollbar is needed, set it up...
		{
		var proportion = difference / $('#hormenu2 .left .main_inner').height();//eg 200px/500px
	// 	var handleHeight = Math.round((1-proportion)*$('#left').height());//set the proportional height - round it to make sure everything adds up correctly later on
	// 	handleHeight -= handleHeight%2; //ensure the handle height is exactly divisible by two
		var handleHeight = 22;
		
		$("#hormenu2 .left .main_inner").after('<\div id="slider-wrap"><\div id="slider-vertical"><\/div><\/div>');//append the necessary divs so they're only there if needed
		$('#hormenu2 .left .hidden_block').show();
		$("#slider-wrap").height($("#hormenu2 .left .main").height());//set the height of the slider bar to that of the scroll pane
		$('#hormenu2 .left .hidden_block').hide();
		
		//set up the slider 
		$('#slider-vertical').slider({
			orientation: 'vertical',
			range: 'max',
			min: 0,
			max: 100,
			value: 100,
			slide: function(event, ui) {
				var topValue = -((100-ui.value)*difference/100);
				$('#hormenu2 .left .main_inner').css({top:topValue});//move the top up (negative value) by the percentage the slider has been moved times the difference in height
			}
		});
		
		//set the handle height and bottom margin so the middle of the handle is in line with the slider
		$(".ui-slider-handle").css({height:handleHeight,'margin-bottom':-0.5*handleHeight});
			
		var origSliderHeight = $("#slider-vertical").height();//read the original slider height
		var sliderHeight = origSliderHeight - handleHeight ;//the height through which the handle can move needs to be the original height minus the handle height
		var sliderMargin =  (origSliderHeight - sliderHeight)*0.5;//so the slider needs to have both top and bottom margins equal to half the difference
		$(".ui-slider").css({height:sliderHeight,'margin-top':sliderMargin});//set the slider height and margins
		}//end if
	}
	
	function hovers(element){
		var topblocks_reg1 = /_act/;
		var topblocks_reg2 = /(\.gif)/;
		var topblocks_reg3 = /_hover/;
		var topblocks_reg4 = /_click/;
		element.mouseover(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg2, '_hover$1'));
			}
		});
		element.mouseout(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg3, ''));
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg4, ''));
			}
		})
		element.mousedown(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg3, '_click'));
			}
		});
		element.mouseup(function(){
			if(!topblocks_reg1.test($(this).attr('src'))){
				$(this).attr('src',$(this).attr('src').replace(topblocks_reg4, ''));
			}
		});
	}
	
	if( ($.browser.msie && (jQuery.browser.version.substr(0,1)=="6")) ){
		DD_belatedPNG.fix('.ui-slider-handle');
	}
	
	//scrollpane parts
		
	var scrollPane1 = $('.gal2 .cont');
	var scrollContent1 = $('.gal2 .cont_inner');
	var scrollbarInner1 = $('.gal2 .scrollbar_inner');
	var scrollbar1 = $('.gal2 .scrollbar');
	
	function MakeScroll(scrollPane, scrollContent, scrollbarInner){
		// computing width
		var scrollContentWidth = 3000;
		scrollContent.find('.item').each(function(){
			scrollContentWidth += $(this).outerWidth(true);
		});
		
		var IHateIE6 = scrollPane.width();
		scrollPane.css('overflow: hidden');
		scrollPane.width(IHateIE6);
		
		if( ($.browser.msie && (jQuery.browser.version.substr(0,1)=="6" || jQuery.browser.version.substr(0,1)=="7")) ) scrollContentWidth += 20;
		scrollContent.width(scrollContentWidth);
		
		//build slider
		var scrollbar = scrollbarInner.slider({
			slide:function(e, ui){
				if( scrollContent.width() > scrollPane.width() ){ scrollContent.css('margin-left', Math.round( ui.value / 100 * ( scrollPane.width() - scrollContent.width() )) + 'px'); }
				else { scrollContent.css('margin-left', 0); }
			}
		});
			
		//append icon to handle
		var handleHelper = scrollbar.find('.ui-slider-handle')
			.mousedown(function(){
				scrollbar.width( handleHelper.width() );
			})
			.mouseup(function(){
				scrollbar.width( '100%' );
			})
			.append('<span class="ui-icon ui-icon-grip-dotted-vertical"><span class="inner"><span class="inner2"><span class="inner3"></span></span></span></span>')
			.wrap('<div class="ui-handle-helper-parent"></div>').parent();
			;
		//change overflow to hidden now that slider handles the scrolling
		scrollPane.css('overflow','hidden');
	}
	
	if($.browser.webkit){
		setTimeout(MakeScroll(scrollPane1, scrollContent1, scrollbarInner1),10);//safari wants a timeout
	}
	else{
		MakeScroll(scrollPane1, scrollContent1, scrollbarInner1);
	}
	
	//size scrollbar and handle proportionally to scroll distance
	function sizeScrollbar(scrollPane, scrollContent, scrollBar){
		var remainder = scrollContent.width() - scrollPane.width();
		var proportion = remainder / scrollContent.width();
		var handleSize = scrollPane.width() - (proportion * scrollPane.width());

		var handleHelper = scrollBar.find('.ui-handle-helper-parent')

		if(handleSize < 40){
			handleSize = 40;
		}
		if(handleSize >= scrollBar.width()){
			//handleSize = 0;
			//scrollBar.hide();
			handleSize = scrollBar.width();
		}
		else{
			scrollBar.show();
		}
		scrollBar.find('.ui-slider-handle').css({
			width: 0
		});
		scrollBar.find('.ui-slider-handle .ui-icon').css({
			width: handleSize
		});
		handleHelper.width('').width( scrollBar.width() - handleSize);
	}
		
	//reset slider value based on scroll content position
	function resetValue(scrollPane, scrollContent, scrollbar){
		var remainder = scrollPane.width() - scrollContent.width();
		var leftVal = scrollContent.css('margin-left') == 'auto' ? 0 : parseInt(scrollContent.css('margin-left'));
		var percentage = Math.round(leftVal / remainder * 100);
		scrollbar.slider("value", percentage);
	}
	//if the slider is 100% and window gets larger, reveal content
	function reflowContent(scrollPane, scrollContent){
		var showing = scrollContent.width() + parseInt( scrollContent.css('margin-left') );
		var gap = scrollPane.width() - showing;
		if(gap > 0){
			scrollContent.css('margin-left', parseInt( scrollContent.css('margin-left') ) + gap);
		}

		var scrollContentWidth = 0;
		scrollContent.find('.item').each(function(){
			scrollContentWidth += $(this).outerWidth(true);
		});

		if(scrollContentWidth < 900) scrollContent.css('margin-left', '100px');
		
	}
		
	//change handle position on window resize
	$(window)
		.resize(function(){
			resetValue(scrollPane1, scrollContent1, scrollbarInner1);
			sizeScrollbar(scrollPane1, scrollContent1, scrollbar1);
			reflowContent(scrollPane1, scrollContent1);
		});
		//init scrollbar size
		
		
	if($.browser.webkit){
		setTimeout(sizeScrollbar(scrollPane1, scrollContent1, scrollbar1),10);//safari wants a timeout
	}
	else{
		sizeScrollbar(scrollPane1, scrollContent1, scrollbar1);
	}
	
	$('.vac_resume_item .trlink a').click(function(){
		var tmp = $(this).parents('.vac_resume_item');
		if($(this).text() == "Раскрыть"){
			tmp.removeClass('closed');
			tmp.addClass('opened');
			$(this).text("Свернуть");
		}
		else{
			tmp.removeClass('opened');
			tmp.addClass('closed');
			$(this).text("Раскрыть");
		}
		jerkFooter();
	});

	// last added

    $('a[name=modal]').click(function(e) {
        e.preventDefault();
        var id = $(this).attr('href');
     
        var maskHeight = $(document).height();
        var maskWidth = $(window).width();
     
        $('#mask').css({'width':maskWidth,'height':maskHeight});
         
        $('#mask').fadeIn(1000);   
        $('#mask').fadeTo("slow",0.8); 
     
       var winH = $(window).height();
        var winW = $(window).width();
               
        $(id).css('top',  winH/2-$(id).height()/2);
       $(id).css('left', winW/2-$(id).width()/2);
     
       $(id).fadeIn(2000);
    });
     
   $('.window .close').click(function (e) {
        e.preventDefault();
       $('#mask, .window').hide();
    });    
     
    $('#mask').click(function () {
        $(this).hide();
        $('.window').hide();
    });         

var ie = !-[1,];
if(ie) {
$(".mcv").attr("src", "images/modal_close.gif");
}


});


