jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options = $.extend({}, options); // clone object since it's unexpected behavior if the expired property were changed
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // NOTE Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};

function fCalen(t) 
{ 
	if (t)
	{
		$("#bl-calen").html('<img src="/im/load2.gif" alt="Загрузка"/>');
		$.ajax({
			type: "GET",
			url: 'g_calenc.php?t='+t,
			dataType: "json",
			success: function (result) {
				$("#bl-calen").html(result.txt);
			}
		});
	}
}

function newsBlocks()
{
	max = $("#bl1").height();
	if (max < $("#bl2").height()) max = $("#bl2").height();
	if (max < $("#bl3").height()) max = $("#bl3").height();
	
	$("#bl1").height(max+7);
	$("#bl2").height(max);
	$("#bl3").height(max);	
}

function fCheckAll()
{
	$("input[@type=checkbox]").attr('checked',$('#allCheck').attr('checked'));
}

var timer = new Array;
function bs(id,off)
{
	if (timer[id]) clearTimeout(timer[id])
	if (off==1) timer[id] = setTimeout("bs('"+id+"',2)",100);
	else if (off==2)
	{
		$('#v'+id).removeAttr('style');
		$('#v'+id).hide();          
	}
	else
	{
		$('#v'+id).show();
		showHeight =  fGetHeight() - $('#v'+id).offset().top - $('#v'+id).height() - 5;
		$('#v'+id).hide();
		if (showHeight<0)
		{
			$('#v'+id).css('margin-top',showHeight+'px');
			$('#v'+id).css('margin-left',$('#d'+id).width()+'px');
		}
		$('#v'+id).show();
	}
}

function fGetHeight() 
{
	var windowHeight;
	var yScrollPosition = 0;

	if (window.pageYOffset)	yScrollPosition = window.pageYOffset;
	else if (document.documentElement && document.documentElement.scrollTop) yScrollPosition = document.documentElement.scrollTop;
	else if (document.body && document.body.scrollTop) yScrollPosition = document.body.scrollTop;
	
	if (self.innerHeight) windowHeight = self.innerHeight;
	else if (document.documentElement && document.documentElement.clientHeight) windowHeight = document.documentElement.clientHeight; 
	else if (document.body) windowHeight = document.body.clientHeight;	

	if(yScrollPosition) pageHeightEnd = yScrollPosition + windowHeight;
	else pageHeightEnd = windowHeight;
	
	return pageHeightEnd;
}


function getBrowserInfo() {
    var t,v = undefined;
   
    if (window.chrome) t = 'Chrome';
    else if (window.opera) t = 'Opera';
    else if (document.all) {
        t = 'IE';
        var nv = navigator.appVersion;
        var s = nv.indexOf('MSIE')+5;
        v = nv.substring(s,s+1);
    }
    else if (navigator.appName) t = 'Netscape';
   
    return {type:t,version:v};
}

function bookmark(a){
    var url = window.document.location;
    var title = window.document.title;
    var b = getBrowserInfo();
   
    if (b.type == 'IE' && 8 >= b.version && b.version >= 4) window.external.AddFavorite(url,title);
    else if (b.type == 'Opera') {
        a.href = url;
        a.rel = "sidebar";
        a.title = url+','+title;
        return true;
    }
    else if (b.type == "Netscape") window.sidebar.addPanel(title,url,"");
    else alert("Нажмите CTRL-D, чтобы добавить страницу в закладки.");
    return false;
}

$(document).ready(function(){
	$('#calend').addClass('close');
});

function calendShow() {
	if ($('#calend').hasClass('close'))
		$('#calend').removeClass('close').addClass('open');
	else if($('#calend').hasClass('open'))
		$('#calend').removeClass('open').addClass('close');
}

function banClose() {
	$('#banClose').toggleClass('active');
	$('#banLast').slideToggle('slow');
	$('#banLast').toggleClass('active');
	if ($('#banLast').hasClass('active') == true) $.cookie('banLast', 'show');
	else $.cookie('banLast', 'hide');
}
