jQuery.cookie = function (key, value, options) {

    // key and at least value given, set cookie...
    if (arguments.length > 1 && String(value) !== "[object Object]") {
        options = jQuery.extend({}, options);

        if (value === null || value === undefined) {
            options.expires = -1;
        }

        if (typeof options.expires === 'number') {
            var days = options.expires, t = options.expires = new Date();
            t.setDate(t.getDate() + days);
        }

        value = String(value);

        return (document.cookie = [
            encodeURIComponent(key), '=',
            options.raw ? value : encodeURIComponent(value),
            options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
            options.path ? '; path=' + options.path : '',
            options.domain ? '; domain=' + options.domain : '',
            options.secure ? '; secure' : ''
        ].join(''));
    }

    // key and possibly options given, get cookie...
    options = value || {};
    var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
    return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
};

var likeobj = {
		aid: null,
		
		vote: function(v) {
			$.ajax({
				type: "POST",
				url: 'http://devaka.ru/cron/like.php',
				dataType: 'json',
				data: 'a=vote&aid='+this.aid+'&v='+v,
				success: function(msg){
					if (!$.cookie('hash') && msg.hash)
						$.cookie('hash', msg.hash, {expires: 90, path: '/'});
				}
			});
		},
		
		view: function() {
			if (this.isViewed())
				return;
			
			$.ajax({
				type: "POST",
				url: 'http://devaka.ru/cron/like.php',
				dataType: 'json',
				data: 'a=view&aid='+this.aid,
				success: function(msg){
					likeobj.setViewed();
				}
			});
		},
		
		isViewed: function(){
			for(var i=0, w=this.whatViewed(); i<w.length; i++) {
				if (w[i] == this.aid)
					return true;
			}
			
			return false;
		},
		
		setViewed: function(){
			if (this.isViewed()) 
				return;
			$.cookie('viewed', this.whatViewed().concat([this.aid]), {expires: 90, path: '/'});
		},

		whatViewed: function(){
			return $.cookie('viewed') ? $.cookie('viewed').split(",") : [];
		}
};

$(document).ready(function(){
	$("ul#a-tabs a").click(function(){
		$("ul#a-tabs li").removeClass('active');
		$(this).parent().addClass('active');
		var div = $(this).attr('href');
		$("ul.tab-items.active").hide('slow').removeClass('active');
		$(div).show('slow').addClass('active');
		return false;
	});

	var lastMenuItem = $("ul#menu li.active"), hovered = false, mTo = null;

	$("ul#menu > li > a").hover(function(){
		hovered = true;
		if (mTo) clearTimeout(mTo);
		$("ul#menu > li").removeClass('active');
		$(this).parent().addClass('active');
	}, function(){
		hovered = false;
		mTo = setTimeout(function(){
			if (hovered) return;
			$("ul#menu > li").removeClass('active');
			$(lastMenuItem).addClass('active');
		}, 750);
	});

	$("ul#menu ul").hover(function(){
		hovered = true;
		if (mTo) clearTimeout(mTo);
	}, function(){
		hovered = false;
		mTo = setTimeout(function(){
			if (hovered) return;
			$("ul#menu > li").removeClass('active');
			$(lastMenuItem).addClass('active');
		}, 1200);
	});

	$("form span.i-srch").click(function(){
		$("form#search-form").submit();
	});

	setTimeout(function(){
		$("iframe").attr("allowTransparency", "true");
	}, 5000);

	
	var context = $('div.nav-cont');
	if (context.length)
	{
		$.ajax({
			url: "/cron/backlinks.html",
			cache: false,
			context: context,
			success: function(html){
				$('div.nav-cont').append(html);
			}
		});
	}
	
$('#comment-nav a').click(function(){
	var show = $(this).attr('href').replace('#c_', '');
	var aid = $(this).attr('rel');

	$('#comment-nav a').removeClass('active');
	$(this).addClass('active');
	$('#commentlist').fadeOut('fast');

	$.ajax({
		type: 'POST',
		url: '/dvk_comment_ajax',
		data: 'show='+show+'&aid='+aid,
		cache: false,
		success: function(html){
			$('#commentlist').html(html);
			$('#commentlist').fadeIn('fast');
		}

	});

	return false;
});



});


	function send_req(t, id) {
		$.ajax({
			type: 'POST',
			url: 'http://devaka.ru/cron/like.php',
			dataType: 'json',
			data: 'a=comments&t='+t+'&id='+id,
			success: function(msg){
				if (msg) alert(msg.status);
			}

		});
	}



