/*var beforePrint = function() { 
    window.location.href=window.location.href+'/print/';
};
var afterPrint = function() {
    alert('afterPrint');
};

if (window.matchMedia) {
    var mediaQueryList = window.matchMedia('print');
    mediaQueryList.addListener(function(mql) {
        if (mql.matches) {
            beforePrint();
        } else {
            afterPrint();
        }
    });
}

window.onbeforeprint = beforePrint;
window.onafterprint = afterPrint;*/

$(document).ready(function(){
    $('.red_block .open_pdf a').click(function(){
        $(".ww_redbl_wrap", $(this).parents('.ww_wraper')).toggle();
        return false;
    });
    $('.ww_redbl .close').click(function(){
        $(this).parents('.ww_redbl_wrap').hide();
        return false;
    });

//<button class="prev"><<</button>
//<button class="next">>></button>

    $(".left #m_left_2").before("<div class='plate'></div>");
    $(".left #m_left_2").after('<button class="next"></button>');


//    $('.left #m_left_2').serialScroll({
//            items:'li',
//            prev:'button.prev',
//            next:'button.next',
//            step:19,
//            //offset:-230,
//            //start:1,
//            duration:400,
//            force:true,
//            stop:true,
//            lock:false,
//            cycle:false, //don't pull back once you reach the end
//            axis:'y',
//            easing:'linear',
//             //use this easing equation for a funny effect
//            jump: false //click on the images to scroll to them
//    });

//    $("#m_left_2").scrollable({
//        size: 19,
//	prev: "button.prev",
//        next :"button.next",
//        vertical: true
//    });


    $("#m_left_2 .filter").hide();

    $("#m_left_2 .not_a, #m_left_2 .a").hover(
          function () {
            $(this).find(".filter").show();
          },
          function () {
            $(this).find(".filter").hide();
          }
    );

//Супер-код для анимации прокрутки на pskov.ru/press/pressa :-)

   /* $(".reestr_nko ul.reestr li").hover(
          function () {
            $(".but_more",$(this)).show(100);
            $("<div></div>").attr({
                id: "bg_hov"
            }).css({
               // height: $(this).height(),
                left: 0,
                opacity: 0.5,
                position: "absolute",
                top: 0,
                bottom: 0,
                width: $(this).width(),
                background: '#f3f4f5',
                zIndex: 100
            }).appendTo($(this));
          },
          function () {
              $(".but_more",$(this)).hide();
           // $(this).find(".filter").hide();
          }
    );*/
          
    $(".reestr_nko ul.reestr a.but_more").click(function() {
        var id = parseInt($('.rstr_id',$(this).parents('li')).html());
        if(id!=0){
            $.ajax({
                type: "POST",
                url: "/get/reestr_card",
                data: "id=" + id,
                success: function(result){
                    $('#ww_card .cont').html(result);
                  /*  var args = new Object();
                    args = {
                        "type": "window",
                        "opacity": .4
                    };
                    cast_blanket($("#ww_card"), 400, args);*/
                    var popup = $("#ww_card");
                    position_popup(popup);
                    
                    popup.fadeIn(400)
                    $(window).bind("resize", function () {
                        position_popup(popup)
                    });
                }
            });
        }
        return false;
    });

    $("#ww_card a.close").bind("click", function (e) {
        e.preventDefault();
        remove_blanket($("#ww_card"), 0)
    });


    var divs =  $("#m_left_2 .items-container");
    var divs_length = divs.length-1;
    var curr_div = 0;
    var animate = true;

    $(".next").live("click",function(){

        if((curr_div<divs_length+1)&&animate){

           var items_top = 0;

           if($(".items").css("top").replace("px", "")!="auto"){

               items_top = Number($(".items").css("top").replace("px", ""));
           }

            var div_height = $(divs[curr_div]).height()-items_top;
            animate = false;
            $(".items").animate({top: "-"+div_height+"px"}, "slow",function(){animate = true;});

            curr_div++;

            $(".plate").replaceWith("<button class='prev'></button>");

            if(curr_div==divs_length-1){
                $(".next").replaceWith("<div class='next plate'></div>");
            }
        }

    });

    $(".prev").live("click",function(){

        if(animate){

            if(curr_div!=1){

                var items_top = 0;

               if($(".items").css("top").replace("px", "")!="auto"){

                   items_top = Number($(".items").css("top").replace("px", ""));
               }
                var div_height = $(divs[curr_div-1]).height()+items_top;
                animate = false;
                $(".items").animate({top: div_height+"px"},"slow",function(){animate = true;});

                curr_div--;

                $(".plate").replaceWith("<button class='next'></button>");

            }else if(curr_div==1){
                $(".items").animate({top:"0px"});
                $(".prev").replaceWith("<div class='plate'></div>");
                curr_div = 0;

            }
        }
    });

    /*$("#top_news dt:first a").bind("click", function (e) {
        e.preventDefault();
        show_video();
    });
    $("#top_news .image .list li:first img").bind("click", function (e) {
        e.preventDefault();
        show_video();
    });*/

    $("#ww_video .close a").bind("click", function (e) {
        e.preventDefault();
        remove_blanket($("#ww_video"), 0)
    });

    $("#ww_down_doc a.close").bind("click", function (e) {
        e.preventDefault();
        $("#ww_down_doc").hide();
        remove_blanket($("#ww_doc"), 0);
    });
    
	/*
    $("table.eg a").bind("click", function (e) {
        e.preventDefault();
        var path = $(this).attr('href');
        doc_show("http://"+ location.host+path);
    });
	*/
    


    $(document).bind("keydown", function (e) {
        if (e == null) {
            var keycode = e.keyCode
        } else {
            var keycode = e.which
        }
        if (keycode == 27) {
           $("#ww_down_doc").hide();
           remove_blanket($("#ww_doc"), 0);
        }
    });

    /*$("#ww_down_doc").hover(
          function () {$(this).css('background-position','0px -30px');},
          function () {$(this).css('background-position','0px 0px');}
    );*/


   /* $("#vlk_mun_usl a").click(function () {
        $("#vlk_mun_usl table").toggle();
        return false;
    });*/
    $(".usl_list a.usl").click(function () {
	 $("table", $(this).parent()).toggle();
        return false;
    });

    $(".two_links").bind('mouseleave', function () {
        $(this).hide();
    });


    $('.search .fict .input').click(function(){
        var document_h = $(document).height();
        var document_w = $(document).width();
        $("<div></div>").attr({
            id: "blanket"
        }).css({
            height: document_h,
            left: 0,
            position: "absolute",
            background: 'transparent',
            top: 0,
            width: document_w,
            zIndex: 100
        }).appendTo($("body"));
        $('.search .choose').show();
        $("#blanket").bind("click", function () {
            $("#blanket").remove();
            $('.search .choose').hide();
        });
        return false;
    });

    $('.search ul.tabs').each(function() {
        $(this).find('li').each(function(i) {
            $(this).click(function(){
                $(this).addClass('act').siblings().removeClass('act').parents('div.choose').find('div.sinp').removeClass('act').end().find('div.sinp:eq('+i+')').addClass('act');//.fadeIn(150);
                $('#search div.stab').removeClass('act');
                $('#search div.stab:eq('+i+')').addClass('act');//.fadeIn(150);
                if(i==1||i==2)$('.blck.perp').addClass('disact');
                else $('.blck.perp').removeClass('disact');
                if(i==1||i==2)$('.blck.date_pick').addClass('disact');
                else $('.blck.date_pick').removeClass('disact');

            });
        });
    });

    $('.search .choose .sub_arr').click(function(){
        $('form',$(this).parents('.sinp')).submit();
        return false;
    });
    
    if($("#hot .nav .r a").length){
        var intervalID1,intervalID2;
    
        $("#hot .nav a").click(function () {
            var parent = $(this).parent();
            var lst = $("#hot .lst");
            var count = $("li",lst).size();
            var i_act = $("#hot .lst li.active").index();
            var index,active;
            if(parent.hasClass("r")){
                index = (i_act<(count-1))?(i_act+1):0;
                active = $("#hot .lst li:eq("+index+")").addClass('active');   
                $("li", lst).not(active).removeClass("active");
            } else if(parent.hasClass("l")) {
                index = (i_act>0)?(i_act-1):(count-1);
                active = $("#hot .lst li:eq("+index+")").addClass('active');   
                $("li", lst).not(active).removeClass("active");
            } else if(parent.hasClass("stop")) {
                if(parent.hasClass("active")){
                    intervalID2 = setInterval(function() {
                        $("#hot .nav .r a").trigger('click');
                    }, 5000);
                    parent.removeClass("active");
                } else {
                    clearInterval(intervalID1);
                    clearInterval(intervalID2);
                    parent.addClass("active"); 
                }
            }
            return false;
        });

        intervalID1 = setInterval(function() {
            $("#hot .nav .r a").trigger('click');
        }, 5000);
    }

    $('a.yet').click(function(){
        var icon = $('span',$(this));
        var parent='';
        var type = '';
        var lim=0;
        if($(this).hasClass("ynew")) {
            parent = $('#news_list');
            type = 'news';
            lim = lim_new;
        }
        else if($(this).hasClass("ypress")) {
            parent = $('#press_list');
            type = 'release';
            lim = lim_press;
        }
        if(parent!='')get_menu(lim,type,parent,icon);
        return false;
    });

    $(".gp .hd").click(function(){
        var gp = $(this).parent();
        if(gp.hasClass('open')){
            $('.cnt', gp).slideUp(200);
            gp.removeClass('open');
        } else {
            $('.cnt', gp).slideDown(200);
            gp.addClass('open');
        }
    });
    
    $(".gp .but").click(function(){
        var wrap = $(this).parent();
        $(this).hide(100, function() {$('.link', wrap).show()} );   
    });
    
    
    $("#m_top_1 a.parent").hover(
          function () {
            var sub_menu = $('.sub_menu', $(this).parents('.box')); 
            $("#m_top_2 .menu").html('');
            $('a', sub_menu).each(function() {
                $(this).clone().appendTo($("#m_top_2 .menu")).wrap("<li></li>");
            });
            set_m_top_2();
          },
          function () {          
           /* var sub_menu = $('#m_top_2 .sub_menu'); 
            $("#m_top_2 .menu").html('');
            $('a', sub_menu).each(function() {
                $(this).clone().appendTo($("#m_top_2 .menu")).wrap("<li></li>");
            set_m_top_2();*/
    });
    
    $("#header").mouseleave(function(){
        console.log('mouseout');
        var sub_menu = $('#m_top_2 .sub_menu'); 
        $("#m_top_2 .menu").html('');
        $('a', sub_menu).each(function() {
            $(this).clone().appendTo($("#m_top_2 .menu")).wrap("<li></li>");
            set_m_top_2();
        });
    });
    
    $("#youtube_list .list a").bind("click", function (e) {
        e.preventDefault();
        var video_id = $(this).attr('href');
        var parent = $(this).parents('.li');
        youtube_show(video_id, parent);
    });
    
    $(".youtube_videos a").bind("click", function (e) {
        e.preventDefault();
        var video_id = $(this).attr('href');
        var parent = $(this).parents('.li');
        youtube_show(video_id, parent);
    });
    
    $("#ww_youtube a.close").bind("click", function (e) {
        e.preventDefault();
        $("#ww_youtube").hide();
        $("#ww_youtube .cont").html('');
        remove_blanket($("#ww_youtube"), 0);
    });
    
//clearInterval(intervalID)
    document.documentElement.onkeypress=document.documentElement.onmousemove=document.documentElement.onclick=function(){
        last=(new Date).getTime();$("body").removeClass('zastavka');
    //    last=(new Date).getTime();$("body").removeClass('pozdrav');
    }

});


function show_video() {
    var args = new Object();
    args = {
        "type": "window",
        "opacity": .4
    };
    cast_blanket($("#ww_video"), 400, args);
}

function show_two_links() {
    $(".two_links").toggle();
    return false;
}

function doc_show(path) {
    var popup = $("#ww_doc");
    $("#ww_down_doc .down").attr('href', path);
   // $("#ww_down_doc .down").attr('href', 'http://www.pskov.ru/sites/default/files/documents/2010/284_2.doc');
    $("#ww_down_doc").show();
    var document_h = $(document).height();
    var document_w = $(document).width();
    var opacity = .7;
    $("<div></div>").attr({
        id: "blanket"
    }).css({
        height: document_h,
        left: 0,
        opacity: opacity,
        position: "absolute",
        top: 0,
        width: document_w,
        zIndex: 100
    }).appendTo($("body"));
    if ($.browser.msie) {
        $("<iframe></iframe>").attr({
            id: "blanket_iframe"
        }).css({
            border: 0,
            height: document_h,
            left: 0,
            opacity: 0,
            position: "absolute",
            top: 0,
            width: document_w,
            zIndex: 99
        }).appendTo($("body"))
    }

    var doc_h = $(window).height()-100;

    popup.width(900);
    if(doc_h<400)doc_h=400;
    popup.html('<iframe src="http://docs.google.com/viewer?url='+path+'&embedded=true" style="width:900px; height:'+doc_h+'px;" frameborder="0"></iframe><div class="t_m"><div class="wrap"></div><div class="c_l iepng"></div><div class="c_r iepng"></div></div><div class="b_m"><div class="wrap"></div><div class="c_l iepng"></div><div class="c_r iepng"></div></div>');
  //  popup.html('<iframe src="http://docs.google.com/viewer?url=http://www.pskov.ru/sites/default/files/documents/2010/284_2.doc&embedded=true" style="width:900px; height:'+doc_h+'px;" frameborder="0"></iframe><div class="t_m"><div class="wrap"></div><div class="c_l iepng"></div><div class="c_r iepng"></div></div><div class="b_m"><div class="wrap"></div><div class="c_l iepng"></div><div class="c_r iepng"></div></div>');

   /* var scribd_doc = scribd.Document.getDocFromUrl(path, 'pub-3583974855730496012');
    // var scribd_doc = scribd.Document.getDoc(doc_id, access_key);
    var oniPaperReady = function(e){
    // scribd_doc.api.setPage(3);
    }

    scribd_doc.addParam( 'height', doc_h );
    scribd_doc.addParam( 'jsapi_version', 1 );
    scribd_doc.addEventListener( 'iPaperReady', oniPaperReady );
    scribd_doc.write('embedded_flash');
    scribd_doc.addParam('public', false);*/

    position_popup(popup);

   // popup.fadeIn(400, change_blanket);
    popup.show(10, change_blanket);

    $(window).bind("resize", function () {
        change_blanket();
        position_popup(popup)
    });
    $("#blanket").bind("click", function () {
        $(window).unbind("resize");
        remove_blanket(popup, 0);
        $("#ww_down_doc").hide();
    });
    return false;
}

function youtube_show(video_id, parent) {
    var popup = $("#ww_youtube");
    
        var document_h = $(document).height();
        var document_w = $(document).width();
        var opacity = .7;
        $("<div></div>").attr({
            id: "blanket"
        }).css({
            height: document_h,
            left: 0,
            opacity: opacity,
            position: "absolute",
            top: 0,
            width: document_w,
            zIndex: 100
        }).appendTo($("body"));
        if ($.browser.msie) {
            $("<iframe></iframe>").attr({
                id: "blanket_iframe"
            }).css({
                border: 0,
                height: document_h,
                left: 0,
                opacity: 0, 
                position: "absolute",
                top: 0,
                width: document_w,
                zIndex: 99
            }).appendTo($("body"))
        }

        var content = $(".cont",popup);
        content.html('<object style="height: 390px; width: 640px"><param name="movie" value="http://www.youtube.com/v/'+video_id+'?version=3&feature=player_detailpage"><param name="allowFullScreen" value="true"><param name="allowScriptAccess" value="always"><embed src="http://www.youtube.com/v/'+video_id+'?version=3&feature=player_detailpage&rel=0" type="application/x-shockwave-flash" allowfullscreen="true" allowScriptAccess="always" width="640" height="360"/></object>');

        var title = $(".title",parent).html();
        var body = $(".v_body",parent).html();
        var date = $(".date",parent).html();
        
        if(title && title!='')content.append("<div class='title'>"+title+"</div>");
        if(body && body!='')content.append("<div class='v_body'>"+body+"</div>");
        if(date && date!='')$(".date", popup).html(date);

        position_popup(popup);

    // popup.fadeIn(400, change_blanket);
        popup.show(10, change_blanket);

        $(window).bind("resize", function () {
            change_blanket();
            position_popup(popup)
        });
        $("#blanket").bind("click", function () {
            $(window).unbind("resize");
            remove_blanket(popup, 0);
        });
        return false;
}
