
function ld_imagebank(elname, currvalue, uploadTargetURL, userid, needstr)
{

  return {
  
    slider : null,
    imgQuery : 'abc',
    imgCache : new Array(),
    currSelectedImg : null,
    currValue: currvalue,
    elementName : elname,
    currSection : null,
    uploadTargetURL : uploadTargetURL,
    privateUserId : userid,
    needString : needstr,
    defaultImage : false,
    
    //drawComponent : function()
    setup : function()
    {
      this.getUploadIFrameRef().write(this.getUploadIFrameContent());
      
      this.slider = new LD_SliderJS();

      this.getImageUrls()
      if (navigator.appName=='Microsoft Internet Explorer')
      {
        document.body.attachEvent('onmouseup',function() { pselect.slider.releaseSlider() });
        document.body.attachEvent('onmousemove',function(event) { pselect.slider.moveSlider(event) });
      }
      else
      {
        document.body.addEventListener('mouseup',function() { pselect.slider.releaseSlider() },true);
        document.body.addEventListener('mousemove',function(event) { pselect.slider.moveSlider(event) },true);
      }
    },

    getUploadIFrameContent : function()
    {
      uploadForm='<html><head><link rel="stylesheet" type="text/css" href="./css/ld_imagebank.css"></head><body topmargin="0" leftmargin="0" class="PICSEL_BG">';
      uploadForm=uploadForm+"<p class='PICSEL_SMALLTEXT'>Click on the browse button to locate one of your own images stored on your computer, then click upload to bring it into your personal image bank.</p>";
      uploadForm=uploadForm+'<form ID="PICSEL_UPLOAD_FORM" action="'+this.uploadTargetURL+'" method="POST" class="PICSEL_PARA" enctype="multipart/form-data">';
      uploadForm=uploadForm+'<input type="hidden" name="MAX_FILE_SIZE" value="500000" />';
      uploadForm=uploadForm+'<img id="PICSEL_UPLOAD_STATUS" src="./images/progress_bar.gif" style="display: none"/><input ID="PICSEL_NEW_IMAGE" type="file" name="new_image" /> <input id="PICSEL_SUBMIT" type="submit" name="submit" value="Upload" onclick="return uploadClicked(this);"/>';
      uploadForm=uploadForm+'<p>Image files should be in either gif or jpeg format, and no larger than 500kb in size<p>';
      uploadForm=uploadForm+'</form>';
      uploadForm=uploadForm+'<script language=javascript>';
      uploadForm=uploadForm+'function uploadClicked(frm){';
      uploadForm=uploadForm+'frm.form.new_image.style.display="none";';
      uploadForm=uploadForm+'frm.form.submit.style.display="none";';
      uploadForm=uploadForm+'if (elem=document.getElementById("PICSEL_UPLOAD_STATUS")) elem.style.display="inline";';
      uploadForm=uploadForm+'return true; }';
      uploadForm=uploadForm+'</script>';
      uploadForm=uploadForm+'</body></html>';
      return uploadForm;
    },

    getUploadIFrameRef : function()
    {
      return window.frames['picsel_upload_frame'].document;
    },

    removeUploadForm : function()
    {
      if (elem=document.getElementById('ABC123'));
      {
        elem.parentNode.removeChild(elem);
      }
    },

    uploadClicked : function(frm)
    {
      frm.form.new_image.style.display="none";
      frm.form.submit.style.display="none";
      frm.form.new_image_public.disabled=true;
      if (elem=this.getUploadIFrameRef().getElementById('PICSEL_UPLOAD_STATUS')) elem.style.display='inline';
      return true;
    },
  
    fileUploadReturn : function(result, msg, newfilename)
    {
      this.getUploadIFrameRef().write(this.getUploadIFrameContent());
      // set current value to selected image
      if (result==true)
      {
        this.setCurrentValue(newfilename); 
        this.setBigImage(newfilename);
        // need to specify userid in here as second param
        this.switchTo('private', this.privateUserId);
      } else alert(msg);
    },
  
    setQuery : function (query)
    {
      this.imgQuery=query;
    },

    getImageUrls : function(keywords)
    {
      // check first if we already have the image urls cached in imgCache
      // get images using imgQuery and idx
	if (keywords)
	{
		xajax_GetImageUrls('pselect.recvImageUrls', keywords);
	} else xajax_GetImageUrls('pselect.recvImageUrls',this.needString);
    },

    // function is a callback function to be called from php side
    recvImageUrls : function(imgUrlsArray)
    {
      this.currSelectedImg=false;
      newlist='';
      cval=document.getElementById("PICSEL_HIDDEN").value;
      for(var x in imgUrlsArray)
      {
        a=imgUrlsArray[x].split("/");
        a=a[a.length-1];
        if (a==cval)
        {
          this.currSelectedImg='PICSEL_THUMB_'+x;
          document.getElementById('PICSEL_LARGE_VIEW').src=imgUrlsArray[x].replace(/\/thumb/, '');
          newlist=newlist+'<div class="PICSEL_SMALL_VIEW" style="border-color: #FF0000">';
        } else newlist=newlist+'<div class="PICSEL_SMALL_VIEW">';
        newlist=newlist+'<img id="PICSEL_THUMB_'+x+'" class="PICSEL_SMALL_VIEW" onclick="pselect.itemSelected(this)" src="'+imgUrlsArray[x]+'"/>';
        newlist=newlist+'</div>';
      }
      if (elem=document.getElementById('PICSEL_LIST'))
      {
        w=((x/2)+2) * 66; //88
        elem.style.width=w+'px' ;
        elem.innerHTML=newlist;
      }
      if (this.currSelectedImg!=false) this.currSelectedImg=document.getElementById(this.currSelectedImg);
    },

    setThumbsToLoading : function(bLoading)
    {
      bimage='url(./images/roller.gif)';
      if (bLoading==false) bimage=''; 
    },
  
    sliderSlidCallback : function(v)
    {
      if (elem=document.getElementById('PICSEL_SCROLL_WINDOW')) elem.scrollLeft=v;
    },
  
    itemSelected : function(clickedObj)
    {
      if (clickedObj)
      {
        imgsrc=clickedObj.src;
        imgsrc=imgsrc.replace(/\/thumb/, '');
        this.setCurrentValue(imgsrc);
        this.setBigImage(imgsrc);
        if (this.currSelectedImg) this.currSelectedImg.parentNode.style.borderColor="#000000";
        clickedObj.parentNode.style.borderColor="#FF0000";
        this.currSelectedImg=clickedObj;
      }
    },
    
    setBigImage: function(imgsrc)
    {
      bigview=document.getElementById('PICSEL_LARGE_VIEW');
      if (bigview) bigview.src=imgsrc;
    },
    
    setCurrentValue: function(val)
    {
      if (elem=document.getElementById("PICSEL_HIDDEN"))
      {  
        a=val.split("/");
        elem.value=a[a.length-1];
      }
    },

		setDefaultImage: function(val)
		{
			this.defaultImage = val;
		},
    
    switchTo : function(section, uid)
    {
      if (elem=document.getElementById('PICSEL_LIST')) elem.innerHTML='';
      this.currSection=section;
      
      privDiv = document.getElementById('PICSEL_TAB_PRIVATE');
      pubDiv = document.getElementById('PICSEL_TAB_PUBLIC');
      skipDiv = document.getElementById('PICSEL_TAB_SKIP');
      
      if (section=='private')
      {
        document.getElementById('PICSEL_IMAGE_SKIP').style.display='none';
        document.getElementById('PICSEL_SCROLL_WINDOW').style.display='block';
				document.getElementById('PICSEL_IMAGE_SELECTION').style.display='block';
        document.getElementById('PICSEL_UPLOAD_CONTAINER').style.display='block';
        pubDiv.className='PICSEL_TAB';
        skipDiv.className='PICSEL_TAB';
        privDiv.className='PICSEL_TAB_SELECTED';
      }
      else if(section=='public')
      {
        document.getElementById('PICSEL_IMAGE_SKIP').style.display='none';
        document.getElementById('PICSEL_UPLOAD_CONTAINER').style.display='none';
        showthem = ['PICSEL_IMAGE_SELECTION','PICSEL_SCROLL_WINDOW','PICSEL_LARGE_VIEW_LABEL','PICSEL_SCROLLER_CONTAINER','PICSEL_SLIDER_MSG'];
				for(var i in showthem) { if (z=document.getElementById(showthem[i])) z.style.display='block'; }
				
        pubDiv.className='PICSEL_TAB_SELECTED';
        privDiv.className='PICSEL_TAB';
        skipDiv.className='PICSEL_TAB';
      }
      else if (section=='skip')
      {
      	hidethem = ['PICSEL_SCROLL_WINDOW','PICSEL_LARGE_VIEW_LABEL','PICSEL_SCROLLER_CONTAINER','PICSEL_SLIDER_MSG','PICSEL_UPLOAD_CONTAINER'];
				for(var i in hidethem) { if (z=document.getElementById(hidethem[i])) z.style.display='none'; }

				document.getElementById('PICSEL_IMAGE_SKIP').style.display='block';
      	skipDiv.className='PICSEL_TAB_SELECTED';
      	privDiv.className='PICSEL_TAB';
      	pubDiv.className='PICSEL_TAB';
      	this.defaultImgSelected(true);
      }
      
      if (section=='private' && uid!=null)
      {
        xajax_GetUserImageUrls('C'+uid, 'pselect.recvImageUrls');
      } else this.getImageUrls();
    },
    
    defaultImgSelected : function(val)
    {
    	bigview=document.getElementById('PICSEL_LARGE_VIEW');
    	if (val==true && this.defaultImage!=false)
			{
				bigview.src=this.defaultImage;
				this.setCurrentValue(this.defaultImage);
			}
			else if(val==false)
			{
				bigview.src='/images/noimage.jpg';
				this.setCurrentValue('0.jpg');
			}
    },
    
    dragSlider : function(dragOjb, callbackFunc)
    {
      totwidth=document.getElementById('PICSEL_LIST').clientWidth-document.getElementById('PICSEL_LIST').parentNode.clientWidth;
      this.slider.dragSlider(dragOjb, 0, totwidth, callbackFunc);
    }
  
  }

}


