/* EZGallery JavaScript File
 * version 1.0 Pre RC1 20060111
 *
 * This file is used to generate the sales items for the EZSales system used with
 * EZUpdates. Please do not make any changes to this file. If you require an updated
 * version of this file please see http://www.collisionsoftware.com/ for a 
 * possible download or contact us on that page and request an updated version.
 *
 * Future enhancements
 * - Gallery / Image Customization
 * - Image Captioning
 * - Pagnation System
 * - Thumbnail Sorting
 * - New Identifier
 *
 * BUGS
 * - IE can't use back button (? because it thinks it's the same page)
 */

//Varibles
//////////////////////////////////////////////////
var EZGallery = new Array();

var galleries = new Array();
var next_pic_icon = "Next";
var prev_pic_icon = "Previous";
var top_level_icon = "Top";
var up_level_icon = "Up";
var url = document.URL;

//Primary Methods
//////////////////////////////////////////////////
function EZImage(gallery, url, turl, caption, size, date, filename, id){
  this.gallery  = gallery;
  this.url      = url;
  this.turl     = turl;
  this.caption  = caption;
  this.size     = size;
  this.date     = date;
  this.filename = filename;
  this.id       = id;
}

function Gallery(id, name){
  this.id = id;
  this.name = name;
}
 
function StartEZGallery(url){
  //this function will be used to determine the appropriate display function
  if(url == ""){
    url = document.URL;
  } else {
    this.url = url;
  }
  SetGalleryArray(EZGallery);
  for(var x=0; x<EZGallery.length; x++){
//  	alert("gID:"+EZGallery[x].gallery+" | gName:"+GetGalleryName(EZGallery[x].gallery)+" | ifilename:"+EZGallery[x].filename);
  }
}

//Methods
//////////////////////////////////////////////////
function displayEZGallery(){
  /*TODO*/
}

function getGalleryById(id){
  /*TODO*/
}

function getImageById(iId){
  for(var x=0; x<EZGallery.length; x++){
    if(EZGallery[x].id == iId){
	  return EZGallery[x];
	}
  }
}

function getImagesByGalleryId(gId){
  var ImagesArray = new Array();
//  var gallName = GetGalleryName(gId);
  for(var x=0; x<EZGallery.length; x++){
    if(EZGallery[x].gallery == gId){
	  ImagesArray[ImagesArray.length] = EZGallery[x];
	}
  }
  return ImagesArray;
}

function getGalleries(){
  /*TODO*/
}

function getImagesForToday(){
  /*TODO*/
}

function getGalleriesForToday(){
  /*TODO*/
}

function getEZGalleryDates(){
  /*TODO*/
}

function searchEZGalleryByDate(date){
  /*TODO*/
}

function searchEZGalleryByFilename(filename){
  /*TODO*/
}

function searchEZGalleryByGallery(gallery){
  /*TODO*/
}

function searchEZGalleryBySize(size){
  /*TODO*/
}

function searchEZGalleryByCaption(caption){
  /*TODO*/
}

function convertDateStringToDateObject(date){
  var aDate = date.split("/");
  var objDate = new Date(aDate[2],aDate[0],aDate[1],0,0,0);
  return objDate;
}

function GalleryNameSort(arg1, arg2){
  if(arg1.gallery < arg2.gallery)
    return -1;
  if(arg1.gallery > arg2.gallery)
    return 1;
  if(arg1.gallery == arg2.gallery)
    return 0;
}

function FilenameNameSort(arg1, arg2){
  if(arg1.filename < arg2.filename)
    return -1;
  if(arg1.filename > arg2.filename)
    return 1;
  if(arg1.filename == arg2.filename)
    return 0;
}

/*
 * This function is passed an array of the pictures, in the specified structure, 
 * and then the user can access the jsDypicgen functions to display the gallery.
 */
function SetGalleryArray(newgalleries){
  EZGallery = newgalleries;
  //this for loop populates the galleries array and reconfigures images array
  var found = false;
  for(var x=0; x<EZGallery.length; x++){
    for(var y=0; y<galleries.length; y++){
      if(galleries[y].name == EZGallery[x].gallery){
        found = true;
      }
    }
    if(!found){ //add to gallery list
      galleries[galleries.length] = new Gallery(galleries.length, EZGallery[x].gallery);
    }
    EZGallery[x].gallery = GetGalleryID(EZGallery[x].gallery);
    found = false;
  }
  galleries.sort(GalleryNameSort);
}

function GetGalleryName(id){
  for(var i=0; i<galleries.length; i++){
    if(galleries[i].id == id){
      return galleries[i].name;
    }
  }
  return null;
}
function GetGalleryID(name){
  for(var i=0; i<galleries.length; i++){
    if(galleries[i].name == name){
      return galleries[i].id;
    }
  }
  return null;
}

function displayGalleryList(){
  for(var i=0; i<galleries.length; i++){
    document.write("<a href=\""+url+"?gal="+galleries[i].id+"\">");
    document.write(galleries[i].name);
    document.write("</a>");
    document.write("<br>");
  }
}

function displayImageList(gallery, url){
  var imagedata = "<table>";
  imagedata += "<tr><td>"+constructNavigation(1, gallery)+"</td></tr>";
  imagedata += "<tr><td>";
  for(var i=0; i<EZGallery.length; i++){
    if(gallery == EZGallery[i].gallery){
      imagedata += "<a href=\""+url+"?img="+i+"\">";
      imagedata += "<img src=\""+EZGallery[i].turl+"\">";
      imagedata += "</a>";
    }
  }
  imagedata += "</td></tr>";
  imagedata += "</table>";
  document.write(imagedata);
}

function displayImage(imgref){
  var imagedata = constructNavigation(2, imgref);
  document.write(imagedata);
}

function constructNavigation(level, imgdata){
  var navidata = "";
  if(level == 1){
    //draw up level
	 navidata += "<a href=\""+url+"\">"+top_level_icon+"</a>";
  }
  if(level == 2){
    navidata +="<table>";
    navidata +="<tr><td colspan='3'>";
    //draw up level
	 navidata += "<a href=\""+url+"?gal="+EZGallery[imgdata].gallery+"\">"+GetGalleryName(EZGallery[imgdata].gallery)+"</a> ";
	 //draw top
	 navidata += " <a href=\""+url+"\">"+top_level_icon+"</a>";
    navidata +="</td></tr>";
	 
	 //draw previous
	 x = imgdata-1;
	 if(x < 1){ x = EZGallery.length-1; }
    navidata +="<tr><td valign='bottom'>";
 	 navidata += "<a href=\""+url+"?img="+x+"\">"+prev_pic_icon+"</a>";
    navidata +="</td>";
	 
    navidata +="<td>";
	 navidata += "<img src=\""+EZGallery[imgdata].url+"\">";
    navidata +="</td>";
	 
	 //draw next
	 x = eval(imgdata)+1;
	 if(x > EZGallery.length-1){ x = 1; }
    navidata +="<td valign='bottom'>";
 	 navidata += "<a href=\""+url+"?img="+x+"\">"+next_pic_icon+"</a>";
    navidata +="</td></tr>";

  }
  
  return navidata;
}

function GalleryNameSort(arg1, arg2){
  if(arg1.name < arg2.name)
    return -1;
  if(arg1.name > arg2.name)
    return 1;
  if(arg1.name == arg2.name)
    return 0;
}
/*
//THIS CODE WOULD BE BETTER SUITED FOR WHEN NAVIGATION IS IMPLEMENTED

//Code for use
if(opener.imgarray.length == 0){
    document.writeln("No Image gallery.");
} else if(eval(param('id'))>opener.imgarray.length-1) {
    document.writeln("<img src=\""+opener.imgarray[0][0]+"\">");
} else {
    document.writeln("<img src=\""+opener.imgarray[param('id')][0]+"\">");
}

if(opener.imgarray.length > 1){
    if(eval(param('id'))>0){
      document.writeln("<td align=\"left\"><a href=\"ImageViewer.html?id="+eval(eval(param('id'))-1)+"\">Previous</a></td>");
    } else {
      document.writeln("<td align=\"left\">Previous</td>");
    }
    
    if(eval(param('id'))<opener.imgarray.length-1){
      document.writeln("<td align=\"right\"><a href=\"ImageViewer.html?id="+eval(eval(param('id'))+1)+"\">Next</a></td>");
    } else {
      document.writeln("<td align=\"right\">Next</td>");
    }
}
*/
