var $id = function(id) { return document.getElementById(id); };
var $body = function() { return document.doctype ? document.documentElement : document.body; }
var bb = { };

bb.loadingPanel = {
	block: function() {
	 	if(!document.getElementById("blockDiv")) {
		 	var blockDiv = document.createElement("div");
		 	if(document.addEventListener) {
			 	blockDiv.style.opacity = "0.30";
			}
			else { 	
				blockDiv.style.filter = "alpha(opacity=30)";
			}
			
		 	blockDiv.setAttribute("id","blockDiv");
		 	blockDiv.style.backgroundColor = "white";
		 	blockDiv.style.position = "absolute";
		 	blockDiv.style.left = "0%";
		 	blockDiv.style.top = "0%";
		 	blockDiv.style.width = "100%";
		 	blockDiv.style.height = "300%";
		 	$body().appendChild(blockDiv);
	 	}
	 	if(!document.getElementById("blockMessageDiv")) {
		 	var blockMessageDiv = document.createElement("div");	
		 	blockMessageDiv.className = "defaultFontFamily";		
		 	blockMessageDiv.setAttribute("id","blockMessageDiv");
		 	blockMessageDiv.style.backgroundColor = "transparent";
		 	blockMessageDiv.style.position = "absolute";
		 	blockMessageDiv.style.left = "20%";
		 	blockMessageDiv.style.top = "20%";
		 	blockMessageDiv.style.width = "50%";
		 	blockMessageDiv.style.height = "30%";
		 	blockMessageDiv.style.fontWeight = "bolder";
		 	blockMessageDiv.style.fontSize = "32px";
		 	blockMessageDiv.style.color ="white";
		 	$body().appendChild(blockMessageDiv);
	 	}
		document.getElementById("blockMessageDiv").innerHTML = "<img src=\"images/12171494295.gif\">";
		document.getElementById("blockMessageDiv").style.display = "";		
	 	document.getElementById("blockDiv").style.display = "";		
	},
	unblock_success: function() {
		document.getElementById("blockMessageDiv").style.display = "none";	
		document.getElementById("blockDiv").style.display = "none";		
	},
	unblock_failure: function() {
		document.getElementById("blockMessageDiv").innerHTML = "...Error ......";
		setTimeout("bb.loadingPanel.unblock_failure2()",1000);	
	},
	unblock_failure2: function() {
		document.getElementById("blockMessageDiv").style.display = "none";	
		document.getElementById("blockDiv").style.display = "none";		
	}	
};

bb.Net = function() 
{	
	this.newRequest = function() 
	{
		var req = null;
		if(typeof ActiveXObject != "undefined") {
			req = new ActiveXObject("Microsoft.XMLHTTP");	
		}
		else if(typeof XMLHttpRequest != "undefined") {
			req = new XMLHttpRequest();	
		}
		else {
			// should throw exception	
		}		
		return req;
	};
	
	this.loadFileFromUrl = function(url) {
		bb.loadingPanel.block();
		
		var req = this.newRequest();		
		try {
	 		req.open("GET", url, false);
	 	}
	 	catch(e) {
	 		// should throw exception again	indicating user security preference has specifically disallowed ajax
	 	}	 	
	 	
		req.send(null);    
		if (req.status == 200 || req.status == 0) {			
			bb.loadingPanel.unblock_success();		
		}	
		else {
			bb.loadingPanel.unblock_failure();
		}
		return req.responseText;			
	};
	
	this.post = function(url, keyValuePairs) {
		bb.loadingPanel.block();

		var req = this.newRequest();		
		try {
	 		req.open("POST", url, false);
	 	}
	 	catch(e) {
	 		// should throw exception again	indicating user security preference has specifically disallowed ajax
	 	}
 		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	 	 
		req.send(keyValuePairs);    
		if (req.status == 200 || req.status == 0) {
			bb.loadingPanel.unblock_success();		
		}	
		else {
			bb.loadingPanel.unblock_failure();
		}
		return req.responseText;
	
	};
	
	this.asyncPost = function(url, keyValuePairs, callback, feedbackType, quiet) {
		if(!quiet) 
		{ 
			bb.loadingPanel.block(); 
		}
		
		var req = this.newRequest();		
	 	req.onreadystatechange = function() {
	 		if(req.readyState==4) {
	 			var status = null;
	 			try { status = req.status;}
				catch(e) {
					bb.loadingPanel.unblock_failure();
					throw e;
				}	 			
	 			if (status==200) {
					if (feedbackType != "XML") 
						callback(req.responseText);
					else {
						callback(req.responseXML);
					}
					if(!quiet) { bb.loadingPanel.unblock_success();	}
				} 	
				else {
					bb.loadingPanel.unblock_failure();
				}		 			
	 		}
	 	} 
	 	
		try {
	 		req.open("Post", url, true);
	 	}
	 	catch(e) {
	 		// should throw exception again	indicating user security preference has specifically disallowed ajax
	 	}
 		req.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
		req.send(keyValuePairs);	
	};
			
	this.loadClass = function(url) {
		var res = this.loadFileFromUrl(url);
		return eval(res);
	}
		
};

bb.net = new bb.Net();

function opentwitterfeedback(target)
{
 var URL = "http://twitter.com/home?status=@" + target;
 window.open(URL);
 return false;
}

function openfanfo(target)
{
	var URL = "http://fanfou.com/privatemsg.create/" + target;
	window.open(URL);
	return false;
}

var PAGE_SIZE = 10;
function aliccinitial()
{
	var start = 0;
	var param = "startindex=" + start;
	bb.net.asyncPost("GetAliccMessage.jsp",param,msgfeedback);
	//$id("pageindex").value = 0;
}

nextpage = function()
{
	var index = parseInt($id("pageindex").value);
	index += 1;
	var start = index * PAGE_SIZE;
	var param = "startindex=" + start;
	bb.net.asyncPost("GetAliccMessage.jsp",param,msgfeedback);
	$id("pageindex").value = index;		
	return false;
}

prepage = function()
{
	var index = parseInt($id("pageindex").value);
	index -= 1;
	var start = index * PAGE_SIZE;
	var param = "startindex=" + start;
	bb.net.asyncPost("GetAliccMessage.jsp",param,msgfeedback);
	$id("pageindex").value = index;	
	return false;
}

function msgfeedback(result)
{
	try
	{
	 eval(result);
	 
	 var main = $id("ccmessagepanel");	
	 if (result.count == 0)
	 {
	 	main.innerHTML = "目前没有任何资料";
			return;
	 }
 
	 var pageCtrlCode = "";
	 var index = parseInt($id("pageindex").value);
	 if (index > 0)
	 {
	 	pageCtrlCode += "<a href=\"#\" onclick=\"return prepage();\">上一页</a>&nbsp;&nbsp;";
	 } 
	 pageCtrlCode += "第" + (index + 1) + "页";
	 
	 var leaveCount = result.totalcount - (index * PAGE_SIZE + result.count);
	 if (leaveCount > 0)
	 {
	 	pageCtrlCode += "&nbsp;&nbsp;<a href=\"#\" onclick=\"return nextpage();\">下一页</a>";
	 }
	 $id("pagectrl").innerHTML = pageCtrlCode;
	 
	 var s = "";	  
		s += "<table id=\"yoholist\" width=\"100%\" border=\"0\"  style=\"color:#474a4d;text-align:left\">";
		s +="<tr class=\"yoholisttitle\"><td colspan=\"2\">&nbsp;最新吆喝</td><td align=\"right\" > <img src=\"images/yoho/title_bg_r.png\"/></td></tr>";
		for (i = 0; i < result.count ; i++)
		{		
			var item = result.data[i];  
			s +="<tr><td style=\"padding:4px; width:64px; text-align:center; vertical-align:top;\">";
			var profileImage = item.profileimage;
			if (profileImage == "" || profileImage == "null")
				s += "<div class=\"pimagebk\"><img width=\"80px\" height=\"80px\" src=\"user/profileimage/po1024768.jpg\" /></div>";
			else
				s += "<div class=\"pimagebk\"><img width=\"80px\" height=\"80px\" src=" + profileImage + " /></div>";
			
			s += "</td>"; 	
			s +="<td style=\"padding:4px; vertical-align:top;\">";
									
			s +="<p><b>" + item.userId + ":</b>";	
			var fanfouser = item.fanfoPosterId;
			var jiwaiuser = item.jiwaiPosterId;
			var zuosauser = item.zuosaPosterId;
			var twitteruser = item.twitterPosterId;
			var plurkuser = item.plurkPosterId;	
			var buboouser = item.bubooPosterId;
		
			var content = item.content;
			s +=  parsestring(content) + "</p>";
			s += "<p style=\"color:#888e7e;padding:6px 0px\">" + item.createtime+ "</p>";    
			s += "<p><span style=\"color:#d3381c;\">吆喝胡同:&nbsp;&nbsp;</span>"; 		
       
			if (fanfouser != "")
			{
				s += "<a target=_blank href=\"http://fanfou.com/" + fanfouser + "\">饭否</a>&nbsp;";
			}   
			if (jiwaiuser != "")
			{
	var jiwaipostingid = item.jiwaiPostingId;
				s += "<a target=\"_blank\" href=http://jiwai.de/" + jiwaiuser + "/thread/" + jiwaipostingid + "/" + jiwaipostingid + ">叽歪</a>&nbsp;";
			}
			if (zuosauser != "")
			{
	var zuosapostinid = item.zuosaPostingId;
				//s += "<a target=\"_blank\" href=http://zuosa.com/reply?eid=" + zuosapostinid + ">做啥</a>&nbsp;";
	s += "<a target=\"_blank\" href=http://zuosa.com/" + zuosauser + ">做啥</a>&nbsp;";
			}
		 if (buboouser != "")
	  {
				var buboopostinid = item.bubooPostingId;
	   	s += "<a target=\"_blank\" href=http://buboo.tw/" + buboouser + ">巴布</a>&nbsp;";
	  }      
	  if (twitteruser != "")
	  {
				s += "<a target=_blank href=\"http://twitter.com/" + twitteruser + "\" >twitter</a>&nbsp;";	
	  }   
   if (plurkuser != "")
   {
				var plurkpostingid = item.plurkPostingId;
    s += "<a target=\"_blank\" href=http://www.plurk.com/p/" + plurkpostingid + " >plurk</a>&nbsp;&nbsp;";
   }  
			s += "</p>";
			s +="</td>";
			var productimage = item.productimage;
			if (productimage != "") {
				s += "<td style=\"padding:4px; text-align:center; vertical-align:top; \">";
				s += "<div class=\"pimagebk\"><a target=_blank href=\"" + productimage + "\"><img style=\"width:80px; height:80px\" src=\"" + productimage + "\" /></a></div></td>";
			}
			else
				s += "<td style=\"text-align:center; vertical-align:top;\"></td>";
				s+= "</td></tr>";     
   }
   s+= "</table>";
	  main.innerHTML = s;
	}
	catch (e)
	{
		alert("error");
	}
}

parsestring = function(str)
{
	var temparray = new Array(10);
	var str1 = str.toLowerCase();
	var i = 0;var j = 0;var k=0;var l=0;var m = 0;
	var index = 0;
	i = str1.indexOf("http",i);
	var breaklexicon = [' ',')','\n','[',']','{','}','\t'];
	while (i != -1)
	{
		var min = 1024;		
		for (n = 0; n < breaklexicon.length; n++)
		{
			m = str.indexOf(breaklexicon[n],i);
			if (m != -1 && m < min)
			{
				min = m;
			}
		}
		
		if (min != 1024)
		 j = min;
		else
		  j = -1;
			
		if (j != -1)
		{
			temparray[index] = str.substring(i,j);
			index++;	
		}
		else
		{
			temparray[index] = str.substring(i,str.length);
			index++;
			break;
		}
		
		i = str1.indexOf("http",j);
	}
	
	var replace = "";
	var imageReplace = "";
	var len = 0;
	var newstr = "";
	for(i =0 ;i < index; i++)
	{
		len = temparray[i].length;
		
		var lowercaseStr = temparray[i].toLowerCase();
		var acceptPos = len - 4;
		var acceptPos1 = len - 5;
		if (lowercaseStr.lastIndexOf(".jpg") == acceptPos || lowercaseStr.lastIndexOf(".gif")== acceptPos || lowercaseStr.lastIndexOf(".png")== acceptPos )
		{
			imageReplace = "<a href=\""+ temparray[i] + "\" target=\"_blank\" ><img style=\"max-height: 40px; max-width: 48px;\" src=\"" + temparray[i] + "\" /></a>";
			str = str.replace(temparray[i],imageReplace);
			continue;
		}
		else if (lowercaseStr.lastIndexOf(".jepg") == acceptPos1)
		{
			imageReplace ="<a href=\""+ temparray[i] + "\" target=\"_blank\" ><img style=\"max-height: 40px; max-width: 48px;\" src=\"" + temparray[i] + "\" /></a>";
			str = str.replace(temparray[i],imageReplace);
			continue;	
		}
		
		
		replace = "<a href=\""+ temparray[i] + "\" target=\"_blank\">" + temparray[i] + "</a>";
		if (len <= 40)
		{
			replace = "<a href=\""+ temparray[i] + "\" target=\"_blank\">" + temparray[i] + "</a>";		
		}
		else
		{
			replace = "<a href=\""+ temparray[i] + "\" target=\"_blank\">" + temparray[i].substr(0,40) + "...</a>";
		}
		str = str.replace(temparray[i],replace);	
	}	
	return str;
} 

function trim(stringToTrim){ return stringToTrim.replace(/^\s+|\s+$/g,"");}




