function Check_Changes()
{
	var count_current = $("#unread_count_header").html();
	$.get("/ajax/get_unread_count", function(count_db){
		if (count_db > count_current) {
			$("#changes_indicator").slideDown();
		}
	});
}

//Cookie shizzle
jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            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
        }
        // CAUTION: 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;
    }
};
	
$(document).ready(function () {	
	var cookie_notification = unescape($.cookie("cookie_notification")).replace(/\+/gi, " ");
	if (cookie_notification != "" && cookie_notification != "null") {
		$("#cookie_notification").show();
		$("#cookie_notification").html(cookie_notification.substr(1, cookie_notification.length-1));
		
		if(cookie_notification.substr(0, 1) == "1") {
			$("#cookie_notification").removeClass("negative");
			$("#cookie_notification").addClass("positive");
		} else {
			$("#cookie_notification").removeClass("positive");
			$("#cookie_notification").addClass("negative");
		}
		$.cookie("cookie_notification", 'null')
		setTimeout(function(){
			$("#cookie_notification").slideUp();
		}, 10000);
	}
	
	setInterval ('Check_Changes()', 30000);
	$.get("/ajax/get_saved_searches", function(data){
		$(".content_saved_searches").html(data);
	});
	
	$("#nav ul li a").each(function() {
	     if(this.href == window.location) $(this).addClass("active_page");
	});
	
	//Show Tipsy tip
	$(".tipsy_tip").tipsy({
		gravity: 'nw'
	});
});

//Show quick help
	$(".quick_help").live('click',function(){
		$("#quick_help").slideToggle();
	});

//Show saved search box
	$(".add_saved_search").live('click',function(){
		$("#add_saved_search").slideDown();
	});
//Add saved search
	$("#add_query_button").live('click',function(){
		var n = $("[name=name_of_query]").val();
		var q = $("[name=query_to_be_added]").val();
		$.get("/ajax/add_search/" + n + "/" + q);
		
		$("#add_saved_search").slideUp();
		
		$.get("/ajax/get_saved_searches", function(data){
			$(".content_saved_searches").html(data);
		});
	});
//Cancel/hide 
	$("#hide_add_query").live('click',function(){		
		$("#add_saved_search").slideUp();
	});
//Show edit box
	$(".edit_saved_search").live('click',function(){
		$("[name=edit_name_of_query]").attr("value", $(this).attr("n"));
		$("[name=edit_query_to_be_saved]").attr("value", $(this).attr("what"));	
		$("[name=edit_hidden_old_name]").attr("value", $(this).attr("n"));	
		
		$("#edit_saved_search").slideDown();
	});
//Edit saved search
	$("#edit_query_button").live('click',function(){
		var n = $("[name=edit_name_of_query]").val();
		var q = $("[name=edit_query_to_be_saved]").val();
		var n_old = $("[name=edit_hidden_old_name]").val();
		$.get("/ajax/edit_saved_search/" + n_old + "/" + n + "/" + q);
		
		$("#edit_saved_search").slideUp();
		
		$.get("/ajax/get_saved_searches", function(data){
			$(".content_saved_searches").html(data);
		});
		
	});
//Cancel/hide 
	$("#hide_edit_query").live('click',function(){		
		$("#edit_saved_search").slideUp();
	});
//Delete a saved search
	$(".delete_saved_search").live('click',function(){
		var q = $(this).attr("what");
		var n = $(this).attr("n");
		$.get("/ajax/delete_saved_search/" + n + "/" + q);
		$("li.saved_search[what=" + q + "]").hide();
		
		$.get("/ajax/get_saved_searches", function(data){
			$(".content_saved_searches").html(data);
		});
	});
//Put search result in #search_results
	$(".show_search_results").live('click',function(){
		var q = $(this).attr("q");
		
		$("#search_results_special").hide();
		$("#search_results").hide();
		
		$.get("/ajax/get_search_results/" + q, function(data){
			$("#search_results").html(data);
			$("#search_results").show();
		});
	});
//Mark an entry as read
	$("a[action=mark_as_read][article_id]").live('click',function(){
		var article_id = $(this).attr("article_id");
		
		$.get("/action/mark_as_read/" + article_id);
		
		$("#content_item_" + article_id).slideUp();
		$("#item_" + article_id).slideUp();
		
		var count = $("#unread_count_header").html();
		count = count - 1;
		
		$("#unread_count_header").html(count);
		$("title").html("Read it Better (" + count + ")");
	});
//Get the content and add the sharebar
	$("a[action=show_content][article_id]").live('click',function(){
		var article_id= $(this).attr("article_id");
			
		$("a[article_id='" + article_id + "'][action=show_content]").attr("class", "content_toggle_load");
		
		$.get("/ajax/get_content/" + article_id, function(data){
			//var sharebar = '<div class="sharebar"><span class="align-left">Share on: <a href="/action/share_twitter/' + article_id + '">Twitter</a></span> <span class="align-right"><a action="hide_content" article_id="'+article_id+'">Collapse</a> | <a article_id="' + article_id + '" action="mark_as_read">Mark as read</a></span></div>';
			var sharebar = '<div class="sharebar"><span class="align-left">Share on: <a href="/action/share_twitter/' + article_id + '">Twitter</a></span> <span class="align-right"><a action="hide_content" article_id="'+article_id+'">Collapse</a></div>';
			
			$("#content_item_" + article_id).html(data + sharebar).load();
			
			$("#content_item_" + article_id).slideDown();
			
			$("a[article_id='" + article_id + "'][action=show_content]").attr("class", "content_toggle_hide");
			$("a[article_id='" + article_id + "'][action=show_content]").attr("action", "hide_content");
		});
	});
//Hide the content again
	$("a[action=hide_content][article_id]").live('click',function(){
		var article_id= $(this).attr("article_id");

		$("#content_item_" + article_id).slideUp();
		$("#content_item_" + article_id).html("");
		$("a[article_id='" + article_id + "'][action=hide_content]").attr('class', "content_toggle_show");
		
		$("a[article_id='" + article_id + "'][action=hide_content]").attr("action", "show_content");
		
		if ($("body").scrollTop() > 0) {
			$.scrollTo("a[article_id='" + article_id + "']");
		}
	});
