var mySound = {};
function str_pad ( input, pad_length, pad_string, pad_type ) {
    var half = '', pad_to_go;

    var str_pad_repeater = function(s, len) {
        var collect = '', i;

        while(collect.length < len) collect += s;
        collect = collect.substr(0,len);

        return collect;
    };

    input += '';

    if (pad_type != 'STR_PAD_LEFT' && pad_type != 'STR_PAD_RIGHT' && pad_type != 'STR_PAD_BOTH') { pad_type = 'STR_PAD_RIGHT'; }
    if ((pad_to_go = pad_length - input.length) > 0) {
        if (pad_type == 'STR_PAD_LEFT') { input = str_pad_repeater(pad_string, pad_to_go) + input; }
        else if (pad_type == 'STR_PAD_RIGHT') { input = input + str_pad_repeater(pad_string, pad_to_go); }
        else if (pad_type == 'STR_PAD_BOTH') {
            half = str_pad_repeater(pad_string, Math.ceil(pad_to_go/2));
            input = half + input + half;
            input = input.substr(0, pad_length);
        }
    }

    return input;
}

function get_date()
{
	var date=new Date();
	var number=date.getDate();
	var monthnumber=date.getMonth()+1;
	var daynumber = date.getDay();//
	var year=date.getFullYear();
	var month=str_pad(monthnumber,2,'0','STR_PAD_LEFT');
	var day=getday(daynumber);
	var hour=str_pad(date.getHours(),2,'0','STR_PAD_LEFT');
	var minute=str_pad(date.getMinutes(),2,'0','STR_PAD_LEFT');
	number=str_pad(number,2,'0','STR_PAD_LEFT');
	$('#today').html('<em><b>Сегодня:</b></em> '+day+', '+number + '.' + month + '.'+year+', '+hour+':'+minute);
	$('#today').css({"display": "block"});
}

function getday(_daynumber) {
switch (_daynumber)
	{
	case 0: return 'вск'; break;
	case 1: return 'пнд'; break;
	case 2: return 'втр'; break;
	case 3: return 'срд'; break;
	case 4: return 'чтв'; break;
	case 5: return 'птн'; break;
	case 6: return 'сбб'; break;
	}
}


$(document).ready(function(){

	$("a.ico_enter").click( function() {
		$("#bg_enter").fadeIn(500);
		return false;
	} );


	// добавлено в январе

    $("a.ico_enter, #news_slider_top a, #weather2 div.select a").focus(function(){
        $(this).blur();
    });

/*	menu_td_count = $("#menu2 td").length;
	$("#menu2 td").each(function(i){
  		if ($(this).find("ul").length) {
			if (i==(menu_td_count-1)) {
				$(this).addClass("on on_last");
			} else {
				$(this).addClass("on");
			}
		}
	}); */
   	$("#menu2 div.back").hover(function(){
  		//alert("hi!");
  		$(this).addClass("hover");
	},function(){
  		$(this).removeClass("hover");
	});

	$("#gazeta_front div.one:last").addClass('last');

	news_slider_count = $("#news_slider_top div.lenta div").length;
	delay_top = 3000;
	top_flag_top = 1;
	if (news_slider_count>1) {
		my_top=setTimeout('top_show(top_flag_top)',delay_top);
	}

	$("#news_slider_top a.fw").click( function() {
		clearTimeout(my_top);
		if (top_flag_top==news_slider_count) {
			top_flag_top = 1;
			$("#news_slider_top div.lenta").animate({ top: 0 }, "fast");
		} else {
			var top = -top_flag_top * 28;
			$("#news_slider_top div.lenta").animate({ top: top }, "normal");
			top_flag_top++;
		}
		my_top=setTimeout('top_show(top_flag_top)',delay_top);
		return false;
	} );

	$("#news_slider_top a.bw").click( function() {
		clearTimeout(my_top);
		if (top_flag_top==1) {
			top_flag_top = news_slider_count;
			var top = -(news_slider_count-1) * 28;
			$("#news_slider_top div.lenta").animate({ top: top }, "fast");
		} else {
			var top = -(top_flag_top-2) * 28;
			$("#news_slider_top div.lenta").animate({ top: top }, "normal");
			top_flag_top--;
		}
		my_top=setTimeout('top_show(top_flag_top)',delay_top);
		return false;
	} );

	$("#news_slider_top a.stop").click( function() {
		clearTimeout(my_top);
		return false;
	} );

	$("#main_news div.img div:first, #main_news div.date div:first, #main_news div.link a:first, #main_news div.one:first").show();
	$("#main_news table.bot td:first").addClass('on');

	$("#main_news table.bot a").hover(function(){
    	var td = $(this).parents("td");
		if (!td.hasClass('on')) {
			$("#main_news table.bot td.on").removeClass('on');
			td.addClass('on');
			var flag_menu = $("#main_news table.bot a").index(this);
			$("#main_news div.img div:visible, #main_news div.date div:visible, #main_news div.link a:visible, #main_news div.one:visible").hide();
			$("#main_news div.img div").eq(flag_menu).show();
			$("#main_news div.date div").eq(flag_menu).show();
			$("#main_news div.link a").eq(flag_menu).show();
			$("#main_news div.one").eq(flag_menu).show();
		}
    });

	$("#photos_lenta div.img div:first, #photos_lenta div.text div:first").show();
	flag_photos=0;
	$("#photos_lenta div.small a").hover(function(){
		 flag_photos_new = $("#photos_lenta div.small a").index(this);
		 if (flag_photos_new!=flag_photos) {
		 	$("#photos_lenta div.img div:visible, #photos_lenta div.text div:visible").hide();
			$("#photos_lenta div.img div").eq(flag_photos_new).show();
			$("#photos_lenta div.text div").eq(flag_photos_new).show();
		 	flag_photos = flag_photos_new;
		 }
    });

	$("#weather2 div.select a").click(function(){
        $(this).toggleClass("on");
        $("#weather2 ul").slideToggle();
        return false;
    });

// конец добавления


    $("div.person_list").each(function(i){
        $(this).find('div.one:last').addClass('last');
    });

    get_date();
    $("#news_slider div.img img:first, #news_slider div.comment div:first, #news_slider div.link_img a:first").show();
    $("#news_slider div.text div.one:first").addClass('first on');
    $("#popular_news div.news div:first").addClass('first');

    $("#news_slider div.text div.one").hover(function(){
        $("#news_slider div.text div.one").each(function(i){
              $(this).removeClass("on");
        });
          $(this).addClass("on");
        var flag = $("#news_slider div.text div.one").index(this);
        $("#news_slider div.img img, #news_slider div.comment div, #news_slider div.link_img a").hide();
        $("#news_slider div.img img").eq(flag).show();
        $("#news_slider div.comment div").eq(flag).show();
        $("#news_slider div.link_img a").eq(flag).show();
    });

    $("#enter_top_a").click( function() {
        $("#bg_enter").fadeIn(500);
        return false;
    } );
    $("#enter div.close a").click( function() {
        $("#bg_enter").fadeOut(500);
        return false;
    } );

    $("#weather a.link").click(function(){
        $(this).toggleClass("link_on");
        $(this).next("ul").slideToggle();
        return false;
    });

    $("#weather a.link, #top_news_control a, #gazeta_control a, #news_zone div.arch a").focus(function(){
        $(this).blur();
    });

	$("#weather div.select ul li a").click(function(){
    	$("#weather a.link").html($(this).html());
       	var idx=$("#weather div.select ul li a").index(this);
        $("div.w_cont").hide();
        $("div.w_cont").eq(idx).show();
    	$(this).parent().parent().slideToggle();
        $("#weather a.link").toggleClass("link_on");
    });

	$("#weather2 ul li a").click(function(){
//    	alert("Hi!");
//		$("#weather2 a.link").html($(this).html());
       	var idx=$("#weather2 ul li a").index(this);
        $("div.w_cont").hide();
        $("div.w_cont").eq(idx).show();
    	$(this).parent().parent().slideToggle();
		$("#weather2 div.select a").toggleClass("on");
//        $("#weather2 a.link").toggleClass("link_on");

    });




    $("#menu li.sep:last").addClass("empty");

    $("#menu a.link_on").parent("li").prev("li.sep").toggleClass("sep_left");
    $("#menu a.link_on").parent("li").next("li.sep").toggleClass("sep_right");

    $("#menu li").hover(function(){
        $(this).find("a.link").toggleClass("link_over");
        var sep_left = $(this).prev("li.sep");
        if (sep_left.is(".sep_right")) {
            sep_left.toggleClass("sep_hover");
        } else {
            sep_left.toggleClass("sep_left_hover");
        }
        var sep_right = $(this).next("li.sep");
        if (sep_right.is(".sep_left")) {
            sep_right.toggleClass("sep_hover");
        } else {
            sep_right.toggleClass("sep_right_hover");
        }
        $(this).find("ul").show();
    },function(){
        $(this).find("a.link").toggleClass("link_over");
        var sep_left = $(this).prev("li.sep");
        if (sep_left.is(".sep_right")) {
            sep_left.toggleClass("sep_hover");
        } else {
            sep_left.toggleClass("sep_left_hover");
        }
        var sep_right = $(this).next("li.sep");
        if (sep_right.is(".sep_left")) {
            sep_right.toggleClass("sep_hover");
        } else {
            sep_right.toggleClass("sep_right_hover");
        }
        $(this).find("ul").hide();
    });

    $("#top_news_control a").mouseover(function(){
        var i=$(this).text();
        $("div.top_news").hide();
        $("div.top_news").eq(i-1).show();
        $("#top_news_control a").removeClass("on");
        $(this).addClass("on");
        top_flag = $("#top_news_control a").index(this);
        return false;
    });


    var photos_html = $("#front_photos div.one div").html();
    $("#front_photos div.show_text").html(photos_html);
    $("#front_photos img.img").hover(function(){
        var index = $("#front_photos img.img").index(this);
        var photos_html = $("#front_photos div.one").eq(index).find("div").html();
        $("#front_photos div.show_text").html(photos_html);
        $("#front_photos div.arrow").css({"left" : index*105+44 + "px"});
    });

    $("div.news_plashka_content").each(function(i){
        if ($(this).prev("div").hasClass("news_plashka")) {
            $(this).find("div.one:first").show();
        }
        if ($(this).prev("div").hasClass("zp_on")) {
            $(this).show();
        }
    });

    $("div.news_plashka li a").click(function(){
        if (!$(this).parent("li").hasClass("on")) {
            $(this).parents("ul").find("li").each(function(i){
                $(this).removeClass("on");
            });
            $(this).parent("li").addClass("on");
            var index = $("div.news_plashka li a").index(this);
            $(this).parents("div.news_plashka").next("div.news_plashka_content").find("div.one").each(function(i){
                $(this).hide();
            });
            $("div.news_plashka_content div.one").eq(index).show();
        }
        return false;
    });

    var blue_bot_h = $("#blue_line_bot div.carrier:first").height();
    if ($("#blue_line_bot div.carrier").eq(1).height()>blue_bot_h) blue_bot_h = $("#blue_line_bot div.carrier").eq(1).height();
    $("#blue_line_bot div.carrier").css({"height" : blue_bot_h + "px"});

    $("#news_zone div.arch a").click(function(){
        var div = $(this).parents("div.arch");
        if (div.hasClass("arch_on")) {
            $(this).parents("#news_zone").find("div.popup").hide();
        } else {
            $(this).parents("#news_zone").find("div.popup").show();
        }
        div.toggleClass("arch_on");
        return false;
    });

    $("div.zone_plashka").click(function(){
        $(this).toggleClass("zp_on");
        $(this).next("div.news_plashka_content").slideToggle();
        return false;
    });





});

function news_show(number) {
    if (number!=top_flag) {
        clearTimeout(my_id);
        if (number==count_news) number=0;
        $('div.top_news').eq(top_flag).hide();
        $('div.top_news').eq(number).show();
        $("#top_news_control a").removeClass("on");
        $("#top_news_control a").eq(number).addClass("on");
        top_flag=number;
    }
}

// добавлено в январе

function top_show(number) {
		if (top_flag_top==news_slider_count) {
			number = 1;
			$("#news_slider_top div.lenta").animate({ top: 0 }, "fast");
		} else {
			var top = -number * 28;
			$("#news_slider_top div.lenta").animate({ top: top }, "normal");
			number++;
		}
		top_flag_top=number;
		my_top=setTimeout('top_show(top_flag_top)',delay_top);
}

// конец добавления