function getPopupTop(C_Height)
{
    var ITop = 0;
    W_Height = $(window).height();
    W_Scroll = $("html").scrollTop();
    if (C_Height==0) C_Height = $("#OverContent").height();
      
    if (W_Height>C_Height) ITop = parseInt((W_Height - C_Height)/2);
    else if ((W_Height + W_Scroll - C_Height) > 0) ITop = W_Height - C_Height;
    else ITop = 10;
    if (W_Scroll>0) ITop = ITop + W_Scroll;
      
    return ITop;
}


jQuery.extend({
    setHomepage: function(url) {
    if (document.all) {
     document.body.style.behavior = 'url(#default#homepage)';
     document.body.setHomePage(url);
    }
    else if (window.sidebar) {
     if (window.netscape) {
      try {
       netscape.security.PrivilegeManager.enablePrivilege("UniversalXPConnect");
      }
      catch (e) {
       var strTemp = '';
       strTemp += "Эта функция запрещена в вашем браузере,";
       strTemp += "чтобы разрешить её, необходимо набрать в адресной строке about:config,";
       strTemp += "и поменять значение поля signed.applets.codebase_principal_support на True";
       alert(strTemp);
      }
     }
     var prefs = Components.classes['@mozilla.org/preferences-service;1']
         .getService(Components.interfaces.nsIPrefBranch);
     prefs.setCharPref('browser.startup.homepage', url);
    }
    }
});


$(document).ready(function()
{
    jQuery("a.OpenPopup").click(function() {
        // Блокируем окно полупрозрачным слоем
        if ($("#OverLay").css("display")=="none")
        {
            $("#OverLay").css("height", $("#All").height());
            $("#OverLay").show().fadeTo(500, 0.6);

            // Открываем окно с формой
            $("#" + $(this).attr("fid")).show();
            window.scrollTo(0, 0);
            $("#OverContent").show().fadeTo(500, 1);

            $(".Clock").hide();
        }
        else
        {
            fid = $(this).attr("fid");

            $("#OverContent").show().fadeTo(500, 0, function(){
                $("form", "#OverContent").hide();
                $("#" + fid).show();
                $("#OverContent").show().fadeTo(500, 1);
            });
        }
        return false;
    });

    jQuery("#PopupClose a").click(function() {
        $("#OverLay").fadeOut("slow");
        $("#OverContent").fadeOut("slow", function(){
            $("#OverLay").css("height", 1);
            $("#OverContent").css("top", 0);
            $("#PopupContent .hidden").hide();
        });
        $(".Clock").show();
        return false;
    });
	
    $("#CenterColumn .block").each(function(){
        Block_OBJ = $(this);
        if (Block_OBJ.find(".item-235").size()==2)
        {
            w1 = Block_OBJ.find(".item-235:eq(0)").height();
            w2 = Block_OBJ.find(".item-235:eq(1)").height();

            if (w1<w2)
                Block_OBJ.find(".item-235:eq(0)").css("min-height", w2 + "px");
            else
                Block_OBJ.find(".item-235:eq(1)").css("min-height", w1 + "px");
        }
    }); 
    
    $(".BB_Tabs").each(function(){
        $(this).BB_Tabs();
    });

    $("#OverLay").fadeTo(0, 0);
    $("#OverContent").fadeTo(0, 0);

    //$('.addToFavorite').jFav();
    $('#setHomePage').click(function() {
        $.setHomepage('http://bnews.kz');
    });

    $(".SubmitButton").click(function() { 
        $(this).parents("form").submit();
    });
    $(".CancelButton").click(function() { 
        $("#PopupClose a").click();
    });


    $("#Grafiki-toggle a").click(function(){
        $("#Grafiki-toggle>span").toggle(); $("#Grafiki>div").toggle();
        return false;
    });
    
    $("a.addNewItem").click(function(){
        objList = $(this).parents(".BB_ContentItem").find(".BB_List");
        $.post( "AJAX_Forms.php", {
            "CacheId": $(this).parents(".BB_ContentItem").attr("id"),
            "ItemId": $(this).parents(".BB_ContentItem").find(".BB_ListItem").size(),
            "Prefix": $(this).parents(".BB_ContentItem").find(".BB_ListItem").attr("type")
            }, function(data, textStatus){
            if (textStatus=="success") objList.append(data);
        }, "html" );
        return false;
    });
    
    $("a.removeLastItem").click(function(){
        if (1<$(this).parents(".BB_ContentItem").find(".BB_ListItem").size()) $(this).parents(".BB_ContentItem").find(".BB_ListItem:eq(" + ($(this).parents(".BB_ContentItem").find(".BB_ListItem").size()-1) + ")").remove();
        return false;
    });

    $("a.ButtonClose").click(function(){
        $(this).parents(".BB_ContentItem").remove();
        return false;
    });
    
    $(".BB_Slider").each(function(){ 
        $(this).BB_Slider();
    });
    /*
    $("#datePicker").datePicker({ inline:true,startDate: '01/01/2005',endDate: (new Date()).asString() }).bind( 'dateSelected', function(e, selectedDate ) { 
        var f = document.forms.chooseDateForm.elements;
        f.month.value = selectedDate.getMonth();
        f.year.value = selectedDate.getYear() + 1900;
        f.day.value = selectedDate.getDate();
        $("#chooseDateForm").submit();
        //$("#DateFilter").show(); 
    });
    //$('#datePicker').dpSetDisplayedMonth((new Date().getMonth()), (new Date().getYear()));

    */
    
    $(".Toggle .ToggleTop a").click(function(){
        $(this).toggleClass("Close");
        $($(this).parents(".Toggle").find("div.ToggleContent")).slideToggle(0);
        return false;
    });

    $("a.ButtonReply").click(function(){
        $("#commentform").hide();
        $(".commentform").remove();

        $(this).parents(".comment-item:eq(0)").append("<form class='padd-10-0 commentform' action='wp-comments-post.php' method='post' onsubmit='if(!document.getElementById(\"comment\").value){alert(\"Введите комментарий!\");document.getElementById(\"comment\").focus();return false;}'><textarea name='comment' id='comment' rows='5' class='item-100pr'></textarea><div class='Buttons block'><a href='JavaScript:;' title='Отправить' class='SubmitButton'><b>Отправить</b></a><a href='JavaScript:;' title='Отмена' class='CancelButton'><b>Отмена</b></a></div></form>");
        $(this).parents(".comment-item").find(".SubmitButton").click(function() { 
            $(this).parents("form").submit();
        });
        $(this).parents(".comment-item").find(".CancelButton").click(function() { 
            $(this).parents("form").remove(); $("#commentform").show();
        });
    });


    $(".send_friend").click(function(){
        $("#SendPostForm h2").html($(this).parents(".ContentBlock").find("h1").html());
    });


    /*$(".ShowTime").each(function(){
        
        TimeUTC = $(this).attr("UTC");
        $(this).jclock({
            utc: true,
            utc_offset: TimeUTC
        });
    });*/


	  
});

