	  //JavaScript的数组对象为你的代码使用队列提供了两个方法：
		// shift：删除和返回数组的第一个元素。 
		// push：允许你向数组的结尾添加一个或者多个元素。
		//unshift，它允许你在数组的开头插入一个或者多个元素，这与pop方法截然相反，后者用来在数组的结尾加入项目
		function ArrayList()
		{
			// 初始化一个空数组,用于存放观察者对像
			this.aList = []; //initialize with an empty array
		}
				
		ArrayList.prototype.Count = function()
		{
			// 取得有多少个的观察者
			return this.aList.length;
		}
				
		ArrayList.prototype.Add = function( object )
		{
			// 添加观察者
			return this.aList.push( object ); //Object are placed at the end of the array
		}

		// 取得指定的观察者对象
		ArrayList.prototype.GetAt = function( index ) //Index must be a number
		{		
		if( index > -1 && index < this.aList.length )
			return this.aList[index];
		else
			return undefined; //Out of bound array, return undefined
		}
				
		// 清除所有的观察者,即将数组清空
		ArrayList.prototype.Clear = function()
		{
		this.aList = [];
		}

		// 删除指定的某一个观察者
		ArrayList.prototype.RemoveAt = function ( index ) // index must be a number
		{
		var m_count = this.aList.length;
					
		if ( m_count > 0 && index > -1 && index < this.aList.length ) 
		{
			switch( index )
			{
				case 0:
				// 删除和返回数组的第一个元素
					this.aList.shift();
					break;
				case m_count - 1:
					//Pop方法被用来移除和返回数组的最后一个元素。一般来说，它会弹出加到数组里的最后一个元素
					this.aList.pop();
					break;
				default:
					// 取得index之前的元素组成一个新的数组
					var head   = this.aList.slice( 0, index );
					// 取得index之后的所有元素组成一个新的数组
					var tail   = this.aList.slice( index + 1 );
					// concat是链接两个数组,并将链接后的数组赋给给的数组.以此完成数组元素的删除操作
					this.aList = head.concat( tail );
					break;
			}
		}
		}

		// 插入一个新的观察者对象,并返回新插入的对象在数组中的位置
		ArrayList.prototype.Insert = function ( object, index )
		{
		var m_count       = this.aList.length;
		var m_returnValue = -1;
					
		if ( index > -1 && index <= m_count ) 
		{
			switch(index)
			{
				case 0:
					// unshift，它允许你在数组的开头插入一个或者多个元素
					this.aList.unshift(object);
					m_returnValue = 0;
					break;
				case m_count:
					this.aList.push(object);
					m_returnValue = m_count;
					break;
				default:
					var head      = this.aList.slice(0, index - 1);
					var tail      = this.aList.slice(index);
					this.aList    = this.head.concat(tail.unshift(object));
					m_returnValue = index;
					break;
			}
		}
					
		return m_returnValue;
		}

		// 查找对象在数组中的位置
		ArrayList.prototype.IndexOf = function( object, startIndex )
		{
		var m_count       = this.aList.length;
		var m_returnValue = - 1; // 找不到时的返回值
					
		if ( startIndex > -1 && startIndex < m_count ) 
		{
			var i = startIndex;
						
			while( i < m_count )
			{
				if ( this.aList[i] == object )
				{
					m_returnValue = i;
					break;
				}
							
				i++;
			}
		}
					
		return m_returnValue;
		}
				
		// 查找对象在数组中的位置 从数组的尾部开始找起
		ArrayList.prototype.LastIndexOf = function( object, startIndex )
		{
		var m_count       = this.aList.length;
		var m_returnValue = - 1;
					
		if ( startIndex > -1 && startIndex < m_count ) 
		{
			var i = m_count - 1;
						
			while( i >= startIndex )
			{
				if ( this.aList[i] == object )
				{
					m_returnValue = i;
					break;
				}
							
				i--;
			}
		}
					
		return m_returnValue;
		}
		
		// 定义观察者对象
		function Observer()
		{
			this.Update = function(Oper)
			{
				var imgurl = "";
				
				// 如果满足了条件.则要将isvalid置为true,
				
				this.className = "tddiv";

				var pid = this.id.replace("div","");
				var Unic = pid.split("_");

				var overimgOBJ = document.getElementById("overimg"+Unic[0]+"_"+Unic[1]);
				var imgOBJ = document.getElementById("img"+Unic[0]+"_"+Unic[1]);
				var killOBJ = document.getElementById("kill"+Unic[0]+"_"+Unic[1]);

				if(this.isvalid == true)
				{
					// 此时观察者将作为主题成为被观察的对象.所以它要加入相应的观察者
					if(Oper=="Add")
					{
						imgurl = GetImgUrl(this,true);   // 获取亮图	
						overimgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/green.gif" ;
						imgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/"+imgurl ;
						killOBJ.innerText = this.CurrentHit+"/"+this.MaxHit ;	
					}
					else
					{
							imgurl = GetImgUrl(this,false);
							overimgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/grey.gif" ;
							imgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/"+imgurl ;
							killOBJ.innerText = this.CurrentHit+"/"+this.MaxHit ;					
					}
				}
				else
				{
							imgurl = GetImgUrl(this,false);
							overimgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/grey.gif" ;
							imgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/"+imgurl ;
							killOBJ.innerText = this.CurrentHit+"/"+this.MaxHit ;					
				}
				
			}
		}


		// 定义被观察者对象
		function Subject()
		{
			this.observers = new ArrayList();
			this.isvalid = false;
			this.CurrentHit = 0;  // 存放当前的已点的点数
			this.MaxHit = 0;      // 最大的可点击数,每个对象在初始化时均已设定
			this.Potential = 0;   // 所需潜力值
			this.iscaladd = false;
			this.iscalsub = false;
		}

		// 被观察者对象的通知信息
		Subject.prototype.AddPoint = function()
		{
			// 获取所有观察者的总数
			
			var m_count = this.observers.Count();
			
			if(this.isvalid == true)
			{
				var htmstr= this.innerHTML.split("<DIV");
				var id = this.id.replace("div","DivGrade");
				var pid = this.id.replace("div","");
				var Unic = pid.split("_");
				var p = GetPosition(Unic);
				stotalOBJ = document.getElementById("stotal");
				shitOBJ = document.getElementById("shit");
				
				var killOBJ =  document.getElementById("kill"+Unic[0]+"_"+Unic[1]);
				var overimgOBJ =  document.getElementById("overimg"+Unic[0]+"_"+Unic[1]);
				
				if((parseInt(stotalOBJ.innerText)+p )<= parseInt(shitOBJ.innerText))
				{			
				
							if(this.CurrentHit!=this.MaxHit)
							{
  						this.CurrentHit+= 1;		
							document.getElementById(id).innerHTML = this.CurrentHit+"/"+this.MaxHit;
							overimgOBJ.src = "http://www.sinaimg.cn/gm/ol/ct/mnq/green.gif";
							killOBJ.innerText = this.CurrentHit+"/"+this.MaxHit;
							
							for( var i = 0; i < m_count; i++ )
							{					
						  	CheckAddValue(this.observers.GetAt(i),this.CurrentHit,this.MaxHit);
							}		
							
							if(this.CurrentHit<=this.MaxHit && !this.iscaladd)
							{
								if(this.CurrentHit==this.MaxHit)
								{ this.iscaladd = true;}
								else
								{ 
									this.iscaladd = false;									
								}
								this.iscalsub = false;
								var spanpotential = document.getElementById("spanp"+Unic[0]);
								spanpotential.innerText = parseInt(spanpotential.innerText) + this.Potential;
								document.getElementById("total"+Unic[0]).innerText = spanpotential.innerText;					
								SetSpanUrl(this.CurrentHit,Unic);			
								SetTotalHit();  // 统计已用点数
							}
						}
				}
				else
					{
						alert("您的剩余点数已不足!");
					}
				
					
			}	
			
		}
		
		// 减点操作
		Subject.prototype.SubPoint = function()
		{
			// 获取所有观察者的总数
			var m_count = this.observers.Count();
			
			if(this.isvalid == true)
			{
				if(IsSubHit(this)==true)
				{
						if(this.CurrentHit!=0) 
						{
						this.CurrentHit -= 1;		
						var id = this.id.replace("div","DivGrade");
			    	var pid = this.id.replace("div","");
				    var Unic = pid.split("_");
						var killOBJ =  document.getElementById("kill"+Unic[0]+"_"+Unic[1]);
					
						document.getElementById(id).innerHTML = this.CurrentHit+"/"+this.MaxHit;
						killOBJ.innerText = this.CurrentHit+"/"+this.MaxHit;
						for( var i = 0; i < m_count; i++ )
						{
								CheckAddValue(this.observers.GetAt(i),this.CurrentHit,this.MaxHit);					
						}

						if(this.CurrentHit>=0 && !this.iscalsub)
						{
									if(this.CurrentHit==0){ this.iscalsub = true;}
									else
									{ this.iscalsub = false; }
									
									var spanpotential = document.getElementById("spanp"+Unic[0]);
									spanpotential.innerText = parseInt(spanpotential.innerText) - this.Potential;
									document.getElementById("total"+Unic[0]).innerText = spanpotential.innerText;
									SetSpanUrl(this.CurrentHit,Unic);
									SetTotalHit();  // 统计已用点数		
									
									
									this.iscaladd=false;					
					
						}
					}
				
						
				}
			}	
			
		}
		

		// 给主题对象添加观察者
		Subject.prototype.AddObserver = function( observer )
		{
		if( !observer.Update )
			throw 'Wrong parameter';
		
		this.observers.Add( observer );
		}

		// 删除观察者
		Subject.prototype.RemoveObserver = function( observer )
		{
		if( !observer.Update )
			throw 'Wrong parameter';
		// 从第0个元素查找该观察者在数组中的位置
		this.observers.RemoveAt(this.observers.IndexOf( observer, 0 ));
		}
		
		// 继承
		function inherits(base, extension)
		{
			for (var property in base)
			{
				try
				{

					extension[property] = base[property];
				}
				catch(warning)
				{
				}
			}
		}	
		

		
		
// 以下的判断应放到PHP文件中.在此为了做测试,先将它们用js来做判断
function CheckAddValue(Subject,CurrentHit,MaxHit)
{
			var opts1 = { requestParameters: ["act=add","c="+CurrentHit,"m="+MaxHit] };
			new callphp(Subject,fName,"add",opts1);  
}

function CheckSubValue(Subject,CurrentHit,MaxHit)
{
  var opts1 = { requestParameters: ["act=sub","c="+CurrentHit,"m="+MaxHit] };
	new callphp(Subject,fName,"sub",opts1);  		
}




