function historyItem(type,id,subject,image,url,cn){

       if (arguments.length==1){
           var combineStr=arguments[0];
           var element=combineStr.split("\t");
           for (var i=0;i<element.length;i++){
              if(i==0) this.type=element[0];
              if(i==1) this.id=element[1];
              if(i==2) this.subject=element[2];
              if(i==3) this.image=element[3];
              if(i==4) this.url=element[4];
              if(i==5) this.cn=element[5];
              }
         
        }
        else{
	        this.type = type;
	        this.id = id;
	        this.subject = subject;
	        this.image = image;
			this.url = url;
			this.cn = cn;
	
		}
		this.toString=function() {
		    return ((type)?type:"") + 
                    ((id)?"\t"+id:"\t") + 
                    ((subject)?"\t"+subject:"\t") + 
                    ((image)?"\t" + image:"\t") + 
                    ((url)?"\t" + url:"\t") +
                    ((cn)?"\t" + cn:"") ;
            }  
}



function SimpleCollenction(maxlength,cookieStore){
     var maxlength = maxlength ?maxlength : 256;
     var cookieStore = cookieStore;
     var maxIndex =  maxlength - 1 
     var list  = new Array();
     var index = null;

     initFromCookie();
     
     
     
     this.put = function(key,value){
          if (this.containsKey(key)) return; 
          putToCookie(value);
          return putWithoutStore(key,value);
      }
     
     function putWithoutStore(key,value){
          if (index==null||index>=maxIndex) {
	             index=0;
	           }
	          else{
	            index=index+1;
	          }
	          list[index]=new Entry(key,value);
	          return index;
     
     }
     this.get = function(key){
          var len=list.length;
          for (var j=0;j<len;j++){
            if(typeof list[j] != "undefined")
              {
                  if( list[j].key == key ) return   list[j].value;
              
              }
          }
          return null;
     }
     this.containsKey = function (key){
           return this.get(key)? true:false;
     }
     this.size = function() {
          return list.length;
       }
       
    this.println = function() {
           for (var i=0;i<list.length;i++){
               alert("key="+list[i].key+",value="+list[i].value);
           }
     } 
     

     function putToCookie(value){
        if (cookieStore==null) return;
        cookieStore.addItemToCookie(value.toString());
     }  
     
     function initFromCookie(){
         if (cookieStore==null) return;
         var cookieValue=cookieStore.getCookie();
         if (cookieValue==null) return ;
         var itemElement=cookieValue.split("\n");
         
         for (var i=0;i<itemElement.length;i++){
             hsItem=new historyItem(itemElement[i]);
             putWithoutStore(hsItem.id,hsItem);
             }
         }
       
       
  }

  var Entry = function (key,value)
   {
        this.key = key;
        this.value = value;
   }
   
   
   function debug(msg){
         var debugLevel="noalert";
         if (debugLevel=="alert"){
             alert(msg);
         }
    }
   
   
   
   function HistoryBuilder(tag,maxItemShowed,maxItemCached,reqUrl,callbackFunc){
        //temporary cookie for cache some object
	    var tempCookieBuilder =new CookieBuilder(tag+"Temp",maxItemCached);
	    var localCache=new SimpleCollenction(maxItemCached,tempCookieBuilder);
	    //cookie forever
	    var cookieBuilder=new CookieBuilder(tag,maxItemShowed,noExpires);
		var compositList=new Array();
		//list that items had been showed
	    var showedList=new Array();
	    var has_showed_offer=0;
	    
	    //furture,can use index in cookie  to decide "begin" value
	    var begin=0;
	    
	    var tag = tag;
	    var maxItemShowed = maxItemShowed;
	    var maxItemCached = maxItemCached;
	    var reqUrl = reqUrl;
	    
	    
	    //before open a new Window,you should use this
	    this.addItemForOpenWin=function(id){
			 var name = navigator.appName;
			//check if pageload is over
			if ("undefined"==typeof pageLoaded||pageLoaded==false){
				this.addItemToCookie(id);
				}
			else{
				if (name == "Microsoft Internet Explorer"){
				    begin=cookieBuilder.getIndex();
				    this.addToNow(id);
				} else {
				    this.addItemToCookie(id);
				} 
		   }
	    }
	    	    
	    debug("init showedList="+showedList.length);
	    this.addToNow = function addToNow(id){
	         //here can be optimize,check exists before get object
		     //add to cookie forever
		     cookieBuilder.addItemToCookie(id);
			 //first search it from localCache by id
			 hsItem=localCache.get(id);
			 if (hsItem!=null){
			    var listToAdd = new Array();
			     listToAdd[0]=hsItem;
			     //now show it
			     debug("get object form cache by id "+id);
				 this.addList(listToAdd);
				 return ;
			 }
		     url=reqUrl+id;
			 loadscript(url,tag+"Form");
		    }
	     
	     
	    this.showAll = function showAllItem(){
	       if (compositList==null) return ;
		   compositList.splice(0,compositList.length);
		   var s=cookieBuilder.getCookie();
		   debug(s);
		   if (s==null) return ;
		   ids=s.split("\n");
		   var reqIds="";
		   for (var i=0;i<ids.length;i++){
		       //first search it from localCache by id
			  hsItem=localCache.get(ids[i]);
		      if (hsItem!=null){
			     compositList[compositList.length]=hsItem;
			     debug("get object form cache by id "+ids[i]);
			  }
			  else{
			     compositList[compositList.length]=null;

			     reqIds=reqIds+"\n"+ids[i];
			   }
		   }
		  if (reqIds.length>0){
		       //url like as 'http://idep2.hz.alibaba.com:9999/offer_maker.htm?init=y&offerId='+encode(reqIds);
		       debug("[show All] reguest from server:"+reqIds);
			   url=reqUrl+encode(reqIds)+"&init=y"
	    	   loadscript(url,tag+"Form");
	    	  
	  	   }
		   else{
		       //get all object from cache
			   debug("get all object form cache");
		       this.addList(compositList);
		   
		   }
	  }
	  
	 //clear cookie forever
	 this.clearForeverCookie = function clearForeverCookie(){
          cookieBuilder.deleteCookie();
      }
     
     //clear temporary cookie
     this.clearTempCookie = function clearTempCookie(){
          tempCookieBuilder.deleteCookie();
      }
	
	 //just add to cookie forever 
	 this.addItemToCookie = function addItemToCookie(id){
	      cookieBuilder.addItemToCookie(id);
	  }   
		     
	     
	     
	  //combine two histroy list,one maybe from cache,the other maybe from server
	 this.bindTwoHistoryList = function bindTwoHistoryList(listFromServer){
	 	   if (compositList==null)return ;
		   if (listFromServer==null) return ;
		   debug("list from server size="+listFromServer.length);
		   var j=0;
		   for (var i=0;i<compositList.length;i++){
			    if (compositList[i]==null){
			    if (j<listFromServer.length){
			       compositList[i]=listFromServer[j];
				   j++;
				 }
				else{
				  break;
				}
			  }
		  }
	  this.addList(compositList);
	  }
	     
	   
	
	this.addList= function addList(listToshow){
		
		 
	   	      for(var j=0;j<listToshow.length;j++){
	          if(begin> maxItemShowed - 1) begin=0;
				 var exists=false;
	             var item=listToshow[j];
				 if (item==null) continue;
				 //checked if it has beed showed
				 for (var k=0;k<showedList.length;k++){
				     if (showedList[k]==null) continue;
		     	     if (item.id==showedList[k].id){
					     exists=true;
					     break;
					 }
				 }
				 //put to cache
				 localCache.put(item.id,item);
					 
				 if (exists){
				    debug("exists:="+j+"---"+item.id);
					continue;
				 }
	 			 if (typeof callbackFunc=="function"){
				     if (!callbackFunc(begin,item))continue;
				     begin=begin+1;
			         has_showed_offer=has_showed_offer+1;
			         showedList[begin]=item;
				     
				 }
				 else{
				    debug("callback function not defined");
				 }
			}
	 }
       
   }
   function sellofferCallback(index,item){
             li=document.getElementById("li_selloffer_"+index);
			 if (li==null) return false;
			 im=document.getElementById("img_selloffer_"+index);
			 if (im!=null) {
			    im.src=item.image;
			  }
			 img_href=document.getElementById("img_href_selloffer_"+index);
			 if (img_href!=null){
			    img_href.href=item.url;
			 }
			 text_href=document.getElementById("text_href_selloffer_"+index);
			 if (text_href!=null){
			    text_href.href=item.url;
				text_href.innerHTML=item.subject;
			 }
		    li.style.display="";
			return true;
	}
	
	function buyofferCallback (index,item){
             li=document.getElementById("li_buyoffer_"+index);
			 if (li==null) return false;
			 text_href=document.getElementById("text_href_buyoffer_"+index);
			 if (text_href!=null){
			    text_href.href=item.url;
				text_href.innerHTML=item.subject+"<div>["+item.cn+"]</div>";
			 }
		    li.style.display="";
			return true;
	}
	
	function sampleCallback(index,item){
	         li=document.getElementById("li_sample_"+index);
			 if (li==null) return false;
			 im=document.getElementById("img_sample_"+index);
			 if (im!=null) {
			    im.src=item.image;
				im.alt=item.subject;
			 }
			 img_href=document.getElementById("img_href_sample_"+index);
			 if (img_href!=null){
			    img_href.href=item.url;
			 }
			 text_href=document.getElementById("text_href_sample_"+index);
			 if (text_href!=null){
			    text_href.href=item.url;
				text_href.innerHTML=item.subject;
			 }
		    li.style.display="";
			return true;
	}
	
	function companyCallback(index,item){
	         li=document.getElementById("li_company_"+index);
			 if (li==null) return false;
			 text_href=document.getElementById("text_href_company_"+index);
			 if (text_href!=null){
			    text_href.href=item.url;
				text_href.innerHTML=item.subject;
			 }
		    li.style.display="";
			return true;
	}

   function getSerachUrl(searchType,keyword){
       if ("selloffer"==searchType){
           return "http://www.alibaba.com/trade/search/1i1p5tyfchms/"+encodeKeyword(keyword)+".html";
       }else if ("buyoffer"==searchType){
           return "http://importer.alibaba.com/search/buyeroffers/i1ph0m/nocountry/"+encodeKeyword(keyword)+".html";
       }else if ("sample"==searchType){
           return "http://www.alibaba.com/trade/search/3i1p5tyfchms/"+encodeKeyword(keyword)+".html"; 
       } else if ("company"==searchType){
           return "http://www.alibaba.com/trade/search/2i1p5tyfchms/"+encodeKeyword(keyword)+".html"; 
       }
       
   }
   
   
   
   function getCategoryUrl(searchType,cateId,cateName){
       if ("selloffer"==searchType){
           return "http://www.alibaba.com/manufacturers/"+cateId+"/"+encodeKeyword(cateName)+".html";
       }else if ("buyoffer"==searchType){
           return "http://importer.alibaba.com/buy_leads/"+cateId+"c1p/"+encodeKeyword(cateName)+".html";
       }else if ("sample"==searchType){
           return "http://www.alibaba.com/catalogs/"+cateId+"/"+encodeKeyword(cateName)+".html";
       } else if ("company"==searchType){
           return "http://www.alibaba.com/companies/"+cateId+"/"+encodeKeyword(cateName)+".html"; 
       }
       
   }
   
   
   
   
   
   
   
   
   
   
   
   
