var 	xmlhttp;
var	pool = new Array() ;
var	res = '';
var	idx = 0 ;
var	curid = 0 ;
var	base = "http://www.qiushibaike.com/inspect" ;
var	qiutu = "<img src='http://www.qiushibaike.com/system/pictures/" ;
var Base64 = {

	// private property
_keyStr : "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",

	  // public method for encoding
	  encode : function (input) {
		  var output = "";
		  var chr1, chr2, chr3, enc1, enc2, enc3, enc4;
		  var i = 0;

		  input = Base64._utf8_encode(input);

		  while (i < input.length) {

			  chr1 = input.charCodeAt(i++);
			  chr2 = input.charCodeAt(i++);
			  chr3 = input.charCodeAt(i++);

			  enc1 = chr1 >> 2;
			  enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
			  enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
			  enc4 = chr3 & 63;

			  if (isNaN(chr2)) {
				  enc3 = enc4 = 64;
			  } else if (isNaN(chr3)) {
				  enc4 = 64;
			  }

			  output = output +
				  this._keyStr.charAt(enc1) + this._keyStr.charAt(enc2) +
				  this._keyStr.charAt(enc3) + this._keyStr.charAt(enc4);

		  }

		  return output;
	  },

	  // public method for decoding
decode : function (input) {
		 var output = [];
		 var chr1, chr2, chr3;
		 var enc1, enc2, enc3, enc4;
		 var i = 0;

		 input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");
		 var l = input.length;
		 while (i < l) {
			 enc1 = this._keyStr.indexOf(input.charAt(i++));
			 enc2 = this._keyStr.indexOf(input.charAt(i++));
			 enc3 = this._keyStr.indexOf(input.charAt(i++));
			 enc4 = this._keyStr.indexOf(input.charAt(i++));

			 chr1 = (enc1 << 2) | (enc2 >> 4);
			 chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
			 chr3 = ((enc3 & 3) << 6) | enc4;

			 output.push(String.fromCharCode(chr1));

			 if (enc3 != 64) {
				 output.push(String.fromCharCode(chr2));
			 }
			 if (enc4 != 64) {
				 output.push(String.fromCharCode(chr3));
			 }
		 }

		 return this._utf8_decode(output.join(''));

	 },

	 // private method for UTF-8 encoding
_utf8_encode : function (string) {
		       string = string.replace(/\r\n/g,"\n");
		       var utftext = "";

		       for (var n = 0; n < string.length; n++) {

			       var c = string.charCodeAt(n);

			       if (c < 128) {
				       utftext += String.fromCharCode(c);
			       }
			       else if((c > 127) && (c < 2048)) {
				       utftext += String.fromCharCode((c >> 6) | 192);
				       utftext += String.fromCharCode((c & 63) | 128);
			       }
			       else {
				       utftext += String.fromCharCode((c >> 12) | 224);
				       utftext += String.fromCharCode(((c >> 6) & 63) | 128);
				       utftext += String.fromCharCode((c & 63) | 128);
			       }

		       }

		       return utftext;
	       },

	       // private method for UTF-8 decoding
_utf8_decode : function (utftext) {
		       var string = [];
		       var i = 0, l = utftext.length;
		       var c = 0, c1 = 0, c2 = 0, c3 = 0;
		       while(i < l) {
			       c = utftext.charCodeAt(i);
			       if (c < 128) {
				       string.push(String.fromCharCode(c));
				       i++;
			       }else if((c > 191) && (c < 224)) {
				       c2 = utftext.charCodeAt(i+1);
				       string.push(String.fromCharCode(((c & 31) << 6) | (c2 & 63)));
				       i += 2;
			       }else{
				       c2 = utftext.charCodeAt(i+1);
				       c3 = utftext.charCodeAt(i+2);
				       string.push(String.fromCharCode(((c & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63)));
				       i += 3;
			       }

		       }

		       return string.join('');
	       }
}


function loadjson(url)
{
	xmlhttp=null;
	if (window.XMLHttpRequest)
	{// code for all new browsers
		xmlhttp=new XMLHttpRequest();
	}
	else if (window.ActiveXObject)
	{// code for IE5 and IE6
		try
		{
			xmlhttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch( e )
		{
			
			xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	if (xmlhttp!=null)
	{
		xmlhttp.onreadystatechange=state_Change;
		xmlhttp.open("GET",url,true);
		xmlhttp.send(null);
	}
}


function fetch_data()
{
	var rnd = Math.floor(Math.random()*5000 + 10000) ;
	$(".qiushi_body3").hide() ;
	if( idx < 35 )
		loadjson( '/new3/fetch' ) ;
	
}

function shuffle( o )
{
	for(var j, x, i = o.length; i; j = parseInt(Math.random() * i), x = o[--i], o[i] = o[j], o[j] = x);
	return o;
}
function state_Change()
{
	if (xmlhttp.readyState==4)
	{
		if (xmlhttp.status==200)
		{
			var list = new Array() ;
			list = eval(  xmlhttp.responseText) ;
			shuffle( list );
			//alert( list.length );
			append_list( list ) ;
			if( idx <= 0 || idx >= pool.length ) 
				forward() ;
		}
	}
}
function append_list( list ) 
{
	var i,j ;
	var	len = pool.length;
	for( i=0 ; i < list.length; i ++  ) 
	{
		for( j = 0 ; j < len ; j ++  )
			if( list[i]["id"] == pool[j]["id"] ) 
				break;
		if( j >= len ) 
			pool.push( list[i] ) ;
	}
}

function forward()
{
//	alert( 
	if( idx < pool.length) 
	{
//		alert( Base64.decode( pool[idx]["content"])  ) ;
		var	content = Base64.decode( pool[idx]["content"] )  ;
		if(content.length > 0) 
			document.getElementById( "content" ).innerHTML= content.replace( /\r\n/g, "<br />" );

		$("#tickets").removeClass( "fen" ) ;
		$(".qiushi_body3").hide() ;
		//alert( pool[idx]["tags"] );
		if( pool[idx]["tags"].length > 0) 
			document.getElementById( "tag_line" ).innerHTML= '<b style="color:green">标签</b>&nbsp;&nbsp;' + Base64.decode( pool[idx]["tags"] )  ;
		else 
			document.getElementById( "tag_line" ).innerHTML= '';

		curid = pool[idx]["id"];
		if( pool[idx]["img"].length > 0 ) 
		{

			pic = Base64.decode( pool[idx]["img"] );
			pic = pic.replace(/\.\w+$/,".jpg");
			//$("#tickets").addClass( "fen" ) ;
			document.getElementById( "pic" ).innerHTML= qiutu+curid.toString()
				+ "/original/" + Base64.decode( pool[idx]["img"] ) + "' />" ;
			$("#pic").append("<p style='color:gray;'>点击图片可放大</p>");
		}
		else 
			document.getElementById( "pic" ).innerHTML= '';
		if( pool[idx]['sim'] ) 
		{
			var fen =  pool[idx]['sim'][0];
			var text = Base64.decode( fen['content'] ).replace( /\r\n/g, "<br />" ) ;
			text += '<a href="http://www.qiushibaike.com/new2/article/' + fen['id'] +'">糗事#' + fen['id'] + '</a>';
			var prob = fen['weight'] ;
			$("#tickets").addClass( "fen" ) ;
			$(".qiushi_body3").show();
			$("#fen").html( text ) ;
			if( fen['img'].length > 0 ){
				$("#picfen").html( qiutu + fen['id'].toString() + '/original/' + Base64.decode( fen['img'] ) +"'/>" );
			$("#picfen").append("<p style='color:gray;'>点击图片可放大</p>");
			}
			prob = parseFloat( prob ) * 100  ;
			text = prob.toString() ;
			$("#fen_prob").text(  text.substring( 0,4) + '%' );	
		}	
//		window['aid'].innerHTML = pool[idx]["id"];
//		document.getElementById( "aid" ).innerHTML = curid.toString()  ;
		idx++;
	}
	if( idx + 5 > pool.length )
		fetch_data() ;	
}
function pingback( )
{
	if( res.length > 5 ) 
	{
		loadjson( base + "/v.php?t=" + Base64.encode(res) ) ;	
		res = '' ;
	}
}
function vote( act ) 
{
	loadjson( '/new3/inspect/'+curid+'/'+act ) ;
	window.setTimeout( "forward()",1000) ;
	disableVoteLink();
}

$(document).ready( fetch_data ) ;
