var oPub = new public();

function public(){
  this.oGallery;
  this.checkSearch = checkSearch;
  this.setGallery = setGallery;
  this.addImgToGallery = addImgToGallery;
  this.prevGallery = prevGallery;
  this.nextGallery = nextGallery;
}
function checkSearch(txt){
  if(document.getElementById('f_search').value == ''){
    alert(txt);
    return false;
  }else return true;
}

function setGallery(relPath){
  this.oGallery = new gallery(relPath);
}

function addImgToGallery(imgId,imgFile,w,h,x,y){
  this.oGallery.addImg(imgId,imgFile,w,h,x,y);
}

function prevGallery(){
  this.oGallery.prev();
}

function nextGallery(){
  this.oGallery.next();
}
//gallery
function gallery(relPath){
  this.relPath = relPath;
  this.numImgs = 0;
  this.curIndex = 0;
  this.aImgs = new Array();
  this.oSelectedImg;
  this.oSelectedLink;
  
  this.addImg = addImg;
  this.prev = prev;
  this.next = next;
  this.updateImg = updateImg;
  this.selectImg = selectImg;
}

function addImg(imgId,imgFile,w,h,x,y){
  this.aImgs.push(new galleryImg(imgId,imgFile,w,h,x,y));
  this.numImgs++;
}

function prev(){
  if(!this.oSelectedImg) this.selectImg();
  this.curIndex = ((this.curIndex - 1) >= 0) ? (this.curIndex - 1) : (this.numImgs - 1);
  this.updateImg();
}

function next(){
  if(!this.oSelectedImg) this.selectImg();
  this.curIndex = ((this.curIndex + 1) < this.numImgs) ? (this.curIndex + 1) : 0;
  this.updateImg();
}

function updateImg(){
  this.aImgs[this.curIndex].display();
  this.selectImg();
}

function selectImg(){
  this.oSelectedImg = document.getElementById('img_'+this.aImgs[this.curIndex].imgId);
  this.oSelectedLink = document.getElementById('link_'+this.aImgs[this.curIndex].imgId);
}

//image
function galleryImg(imgId,imgFile,w,h,x,y){
  this.imgId = imgId;
  this.imgFile = imgFile;
  this.w = w;
  this.h = h;
  this.x = x;
  this.y = y;
  this.display = display;
}

function display(){
  var oImg = oPub.oGallery.oSelectedImg;
  var oDate = new Date();
  oImg.src = oPub.oGallery.relPath+'libs/scripts/ajax.php?class=procedures&method=disp_uploaded_file_cus_thumb&par[0]='+this.imgId+'&par[1]='+oDate.getTime();
  oImg.style.width = this.w+'px';
  oImg.style.height = this.h+'px';
  oImg.style.marginLeft = '-'+this.x+'px';
  oImg.style.marginTop = '-'+this.y+'px';
  oImg.id = 'img_'+this.imgId;
  var oLink = oPub.oGallery.oSelectedLink;
  oLink.href = oPub.oGallery.relPath+'files/'+this.imgFile;
  oLink.id = 'link_'+this.imgId;
}
