// JavaScript Document
function is_null(value)
{
	var reg		= /^ *$/gi;
	return reg.test(value);
}
jQuery.extend({
                          avatar: function(callback, username, domainname, servicename, part, width, height)
                          {
                                  if(is_null(username) || is_null(domainname) || is_null(servicename) || isNaN(width) || isNaN(height) || is_null(part))
                                  {
                                          return false;
                                  }
                                  var serverurl = "http://avatar.cartoonnetworkhq.com/?c=interface.avatar&f=jsonp&jsonp_callback=?";
                                  var datastr   = "username="+username+"&servicename="+servicename+"&domainname="+domainname+"&part="+part;
                                  var image             = new Object();
                                  image.oxml            = null;
                                  image.keyerr          = "";
                                  image.syserr          = "";
                                  image.arrayImage      = new Array();
                                  try
                                  {
                                        $.getJSON(serverurl + "&" + datastr, function(json)
                                           {
						var ret_obj = new Object();
//						if(json.user.name == username){
							$.each(json.item, function(i,item){
								if((item.width==width)&&(item.height==height)){
									ret_obj.username=username;
									ret_obj.url=item.url;
									ret_obj.profilelink=json.user.profilelink;
								}
							 });
//						}
						callback(ret_obj);
                                           });
                                  }catch(e)
                                  {
                                          image.syserr  = e;
                                  }
				  
				  image.getAvatarURL	= function()//return image url string
				  {
					  if(this.syserr!="")
					  {
						  return false;
					  }
					  if(this.keyerr!="")
					  {
						  return false;
					  }
					  if($(this.oxml).find("item[@width='"+width+"'][@height='"+height+"']").length==0)
					  {
						  return $(this.oxml).find("defaultItem[@width='null'][@height='null']").eq(0).attr("url");
					  }
					  
					  return $(this.oxml).find("item[@width='"+width+"'][@height='"+height+"']").eq(0).attr("url");
				  }
				  
				  image.getUserInfo		= function()//return user info
				  {
					  if(this.syserr!="")
					  {
						  return false;
					  }
					  if(this.keyerr!="")
					  {
						  return false;
					  }
					  return {UserName: $(this.oxml).find("user").eq(0).attr("name"),
					  		ProfileLink: $(this.oxml).find("user").eq(0).attr("profilelink")};
				  }
				  
				  image.getAvatarInfo	= function()
				  {
					  $(this.oxml).find("item").each(
													 function(i)
													 {
														 image.arrayImage[i]	= {avatarname: $(this).attr("avatarname"), width: $(this).attr("width"),height: $(this).attr("height"),url: $(this).attr("url")};
													 }
													 );
					  if(this.arrayImage.length==0)
					  {
						  return false;
					  }
					  return this.arrayImage;
				  }
				  
				  image.tag	= function(type, parentid, selfid)
				  {
					  var tempobj	= this.getCurrentImage();
					  if(!tempobj)
					  {
						  return false;
					  }
					  
					  $(tempobj).attr("id", selfid);
					  if(type=="zoom")
					  {
						  $(tempobj).mouseover(
											   function()
											   {
												   $(this).attr("width", parseInt($(this).attr("width"))+30);
												   $(this).attr("height", parseInt($(this).attr("height"))+30);
											   }
											   );
						  $(tempobj).mouseout(
											  function()
											  {
												  $(this).attr("width", parseInt($(this).attr("width"))-30);
												  $(this).attr("height", parseInt($(this).attr("height"))-30);
											  }
											  );
					  }else if(type=="shade")
					  {
						  $(tempobj).mouseover(
											   function()
											   {
												   $(this).css({filter: "alpha(opacity=100)"});
												   $(this).css("-moz-opacity", "1.0"); 
											   }
											   );
						  $(tempobj).mouseout(
											  function()
											  {
												  $(this).css({filter: "alpha(opacity=30)"});
												  $(this).css("-moz-opacity", "0.3");
											  }
											  );
						  $(tempobj).css({filter: "alpha(opacity=30)"});
						  $(tempobj).css("-moz-opacity", "0.3");
					  }else if(type=="default")
					  {
						  
					  }
					  $(tempobj).appendTo($("#"+parentid));
				  }
				  
				  image.getCurrentImage	= function()//return image object
				  {
					  var url	= this.getAvatarURL();
					  if(!url)
					  {
						  return false;
					  }
					  var img	= document.createElement("img");
					  $(img).attr("src", url);
					  $(img).attr("width", width);
					  $(img).attr("height", height);
					  
					  $(img).mousedown(
										function()
										{
										}
										);
					  $(img).mouseup(
									 function()
									 {
									 }
									 );
					  $(img).mouseover(
									   function()
									   {
									   }
									   );
					  $(img).mouseout(
									  function()
									  {
									  }
									  );
					  return img;
				  }
				  
				  return image;
			  }
			  })
