

function adjustCookie()
{
	var str = '';
	for(var i in hidden)
	{
		if(hidden[i] == true)
		{
			str += i+"|";
		}
	}
	
	if(str == '')
	{
		eraseCookie("hide");
	}
	else
	{
		setCookie("hide", str, 365);
	}
	
}

var hidden = Array();
function toggle(opt)
{
	if(typeof opt != 'string' || opt.length <= 0)
	{
		return;
	}
	
	if(hidden[opt] == true)
	{
		show(opt);
	}
	else
	{	hide(opt);
	
	}
	
	adjustCookie();
}

var isEmptyLoad = false;
function isEmpty()
{
	var showing = $(".quote_post > div:visible").length;
	if(showing <= 0)
	{
		$('#nomorewarn').show();
		if(!isEmptyLoad)
		{
			isEmptyLoad = true;
			loadMore();
		}
	}
	else
	{
		isEmptyLoad = false;
		$('#nomorewarn').hide();
	}
}

function show(opt)
{
	if(typeof opt != 'string' || opt.length <= 0)
	{
		return;
	}
	
	$('.'+opt).show();
	$('#toggle_'+opt+' img').attr("src", root_url+"public/images/check.png");
	hidden[opt] = false;
	isEmpty();
}

function hide(opt)
{
	if(typeof opt != 'string' || opt.length <= 0)
	{
		return;
	}
	
	$('.'+opt).hide();
	$('#toggle_'+opt+' img').attr("src", root_url+"public/images/cross.png");
	hidden[opt] = true;
	isEmpty();
}

function hideThem()
{
	for(var i in hidden)
	{
		hide(i);
	}
}

function analyzeCookie()
{
	var cookie = readCookie('hide');
	if(typeof cookie == 'string')
	{
		cookie = cookie.split('|');
		
		for(var i=0;i<cookie.length;i++)
		{
			var opt = cookie[i];
			hide(opt);
		}
	}
}

function checkForNew()
{
	
	$.getJSON(root_url+"spy.php", { id: $('#quote_container').data("lastUpdate") },
        function(data){
		  $.each(data, function(i,item){
		    var div = document.createElement('div');
		    div.className="quote_post";
		    div.id = item.id;
		    div.innerHTML=quote(item);
		    
		    $('#quote_container').prepend(div);
		    $(div).hide();
		    $(div).fadeIn('slow');
		    
		  });
		  
		  $('#quote_container').data("lastUpdate", $('.quote_post:first').attr('id'));
		  hideThem();
		  
        });


}

function quote(item)
{
	var html =  "<div class='"+item.classr+"'>";
	html += "<div class='block'><div class='tl'></div><div class='top'></div><div class='tr'></div>";
	html += "<div class='inner'><div class='left site_title'><a href='"+item.url+"' rel='nofollow'>"+item.site+"</a><br />";
	if(item.isnew == '1')
	{
		html += "<img src='"+root_url+"public/images/new.png' alt='New' /> &nbsp;";
	}
	html += "<a href='"+item.url+"' title='permalink'><img src='"+root_url+"public/images/link_go.png' alt='view' /></a> ";
	html += "</div><div class='actual_quote'>"+item.content+"</div><div class='clear'></div></div>";
	html += "<div class='bl'></div><div class='bottom'></div><div class='br'></div></div><br /><br /></div>";
	return html;
}

var pageVar = 1;
function loadMore()
{

	//pageVar = pageVar + 1;
	
	$('#view_more_text').html("<img src='"+root_url+"public/images/refresh.gif' alt='' />");
	//$.getJSON(root_url+"spy.php", { page: pageVar },
	
	$.getJSON(root_url+"spy.php", { lastid: $('.quote_post:last-child').attr('id') },
        function(data){
		var count = 0;
		  $.each(data, function(i,item){
		  
			  if($('#'+item.id).length == 0)
			  {
			    var div = document.createElement('div');
			    div.className="quote_post";
			    div.id = item.id;
			    div.innerHTML=quote(item);
			    
			    $('#quote_container').append(div);
			    $(div).hide();
			    $(div).fadeIn('slow');
			    count++;
			    }
		  });
		  hideThem();
		  $('#view_more_text').html("view more");
		  
		  if(count <= 0)
		  {
		  	$('#view_more').hide();
		  }
		  
        });
        return false;

}