/**
 * photos.js - Objects and Functions for Photos
 * Author: William Bagby
 * $Id: photos.js,v 1.16 2007/07/11 18:58:16 wbagby Exp $ $State: Exp $
 */
var imagePath;
var prvSortedBy ='none'
function displayFullSizeImage(id) {
	
	if (currentFullSizeImage != '') {
		if (currentFullSizeImage == id) {
			return disableFullSizeImage(id);
		}
		toggleCssDisplay('fullsize-' + currentFullSizeImage, 'none');
	}
	toggleCssDisplay('fullsize-' + id, 'block');
	document.getElementById("image-"+id).src=BASEPATH+"getimage.do?id="+id;
	currentFullSizeImage = id;
	highlightThumb(currentFullSizeImage);
	renderThumbnails();
	return false;
}

function disableFullSizeImage(id) {
	toggleCssDisplay('fullsize-' + id, 'none');
	currentFullSizeImage = '';
	return false;
}

function thumbnailInfo( parentid, imgtag, width, height ) {
	var parentid = parentid;
	var imgtag = imgtag;
	var width = width;
	var height = height;
	this.getParentid	=	function () { return parentid; };
	this.getImgtag		=	function () { return imgtag; };
	this.getWidth		=	function () { return width; };
	this.getHeight		=	function () { return height; };
	
}

function thumbsInfo() {
	var thumbs = new Array();
	this.numThumbs	= function () 			{ return thumbs.length; };
	this.addThumb	= function (thumbnail) 	{ thumbs[thumbs.length] = thumbnail; };
	this.get		= function (index) 		{ return thumbs[index]; };
	this.getIndexByParentid = function (parentid) {
		var foundIndex = -1;
		for (i = 0; i < thumbs.length; i++) {
			if (thumbs[i].getParentid() == parentid) {
				foundIndex = i;
				break;
			}
		}
		return foundIndex; 
	}
	this.getParentidArray = function () {
		var parentIDs = new Array();
		for (i = 0; i < thumbs.length; i++) {
			parentIDs[i] = thumbs[i].getParentid();
		}
		return parentIDs; 
	}
}

var thumbnailsPerRow = 4;
var maxThumbWidth	= 100; // in pixels
var maxThumbHeight	= 100; // in pixels
function renderThumbnails() {
	var html = '<table id ="contestphotos-thumb"><tr>';
	for (i = 0; i < thumbnailsPerRow; i++) {
		if(thumbIndex + i < thumbList.numThumbs()) {
			var thumb = thumbList.get(thumbIndex + i);
			xPadding = Math.round((maxThumbWidth - thumb.getWidth()) / 2);
			yPadding = Math.round((maxThumbHeight - thumb.getHeight()) / 2);
			html += '<td><table ><tr><td height ="'+maxThumbHeight+'" width = "'+maxThumbWidth+'" class=\'thumb\' style="vertical-align: bottom; border:0px;padding:'+yPadding+'px '+xPadding+'px; "><a style="vertical-align: bottom;" href="#" onclick="return displayFullSizeImage(\'' + thumb.getParentid() + '\');" id="thumb-link-'+ thumb.getParentid() + '">'+ thumb.getImgtag() + '</a></td></tr>';
		  if(thumb.getCheers()==0){
		   html +='<tr><td align=\'center\' ><a class = "num-cheers " href="#" onclick="return displayFullSizeImage(\'' + thumb.getParentid() + '\');" id="thumb-cheer-link-'+ thumb.getParentid() + '">'+thumb.getCheers()+' cheers'+' </a><br><font class = "gray-text !important ">by</font> '+'<a  class="fullsize-meta" style = "color: #39f !important;" href="'+PROFILEPATH+thumb.getUserId()+'">' + thumb.getUserName() + '</a></td></tr></table></td>';
          }
          if(thumb.getCheers()>0){
           html +='<tr><td align=\'center\' ><a class = "num-cheers " href="#" onclick="return displayFullSizeImage(\'' + thumb.getParentid() + '\');" id="thumb-cheer-link-'+ thumb.getParentid() + '"><img src=\''+smileImagePath+'\' border="0"/>'+thumb.getCheers()+' cheer'+((thumb.getCheers()>1)?"s":"")+' </a><br><font class = "gray-text !important ">by</font> '+'<a  class="fullsize-meta" style = "color: #39f !important;" href="'+PROFILEPATH+thumb.getUserId()+'">' + thumb.getUserName() + '</a></td></tr></table></td>';
          }
		}
	}
	html +='</td></tr></table>'
	var paginate     = 'none';
	var paginateprev = 'none';
	var paginatenext = 'none';
	var fullIndex = thumbList.getIndexByParentid(currentFullSizeImage);
	
	if (thumbIndex > 0 || fullIndex > 0) {
		paginate = 'block';
		paginateprev = 'inline';
	}
	if ((thumbIndex + thumbnailsPerRow < thumbList.numThumbs()) || (fullIndex >= 0 && fullIndex != (thumbList.numThumbs() - 1))) {
		paginate = 'block';
		paginatenext = 'inline';
	}
	//debug("fullIndex: "+fullIndex);
	
	document.getElementById('photos-pagination').style.display = paginate; 
	document.getElementById('photos-pagination-prev').style.display = paginateprev;
	document.getElementById('photos-pagination-next').style.display = paginatenext;
	if (fullIndex != -1) {
		document.getElementById('fullsize-pagination-'+fullIndex).style.display = paginate; 
		document.getElementById('fullsize-pagination-prev-'+fullIndex).style.display = paginateprev;
		document.getElementById('fullsize-pagination-next-'+fullIndex).style.display = paginatenext;
	}
	setInnerHTMLById('photos', html);
	highlightThumb(currentFullSizeImage);
	return false;
}

function paginateThumbs(delta) {
	thumbIndex += delta;
	if (thumbIndex < 0) {
		thumbIndex = 0;
	} else if (thumbIndex >= thumbList.numThumbs()) {
		thumbIndex = thumbList.numThumbs() - 1;
	}
	paginateFullSize(delta);
	return renderThumbnails();
}

function paginateFullSize(delta) {
	if (currentFullSizeImage == '') {
		return false;
	}
	var newIndex = thumbList.getIndexByParentid(currentFullSizeImage);
	if (newIndex != -1) {
		newIndex += delta;
	}
	if (newIndex >= 0 && newIndex < thumbList.numThumbs()) {
		displayFullSizeImage(thumbList.get(newIndex).getParentid());
	}
	return false;
}

/*
Disabling this function - performance was AWFUL for 
lots of images, like on a photo contest page.
*/
function highlightThumb( id ) {
	return;
	if ( id == '' ) return;
	var pid;
	for ( var i=0,len=pids.length; i<len; i++ ) {
		pid = pids[i];
		try {
			var img = document.getElementById("thumb-link-" + pid).getElementsByTagName("img").item(0);
			if ( pid == id ) {
				img.className="on";
			} else {
				img.className="off";
			}
		} catch(e) {
			debug("img thumb-link-"+pid+" doesn't exist<br>", true);
		}
	}
		
}

function contestThumbsInfo() {
	var thumbs = new Array();
	this.numThumbs	= function () 			{ return thumbs.length; };
	this.addThumb	= function (thumbnail) 	{ thumbs[thumbs.length] = thumbnail; };
	this.get		= function (index) 		{ return thumbs[index]; };
	this.getIndexByParentid = function (parentid) {
		var foundIndex = -1;
		for (i = 0; i < thumbs.length; i++) {
			if (thumbs[i].getParentid() == parentid) {
				foundIndex = i;
				break;
			}
		}
		return foundIndex; 
	}
	this.getParentidArray = function () {
		var parentIDs = new Array();
		for (i = 0; i < thumbs.length; i++) {
			parentIDs[i] = thumbs[i].getParentid();
		}
		return parentIDs; 
	}
	
	this.setThumbnailOrderByCheersAsc = function () {
		   this.thumbs =  thumbs.sort(ascendingByChrs);
	     }
    this.setThumbnailOrderByDateAsc   = function () {
         this.thumbs =  thumbs.sort(ascendingByDate);
       }
   this.setThumbnailOrderByCheersDes = function () {
		   this.thumbs =  thumbs.sort(descendingByChrs);
	     }
    this.setThumbnailOrderByDateDes   = function () {
         this.thumbs =  thumbs.sort(descendingByDate);
       }    
       
    this.getThumbnailOrderByCheersAsc = function () {
		   return thumbs.sort(ascendingByChrs);
	     }
    this.getThumbnailOrderByDateAsc   = function () {
         return thumbs.sort(ascendingByDate);
       }   	     
	this.getThumbnailOrderByCheersDes = function () {
		   return thumbs.sort(descendingByChrs);
	     }
    this.getThumbnailOrderByDateDes   = function () {
         return thumbs.sort(descendingByDate);
       }
}

function contestThumbnailInfo( parentid, imgtag, width, height,cheers,userName,date,userId ) {
	var parentid = parentid;
	var imgtag = imgtag;
	var width = width;
	var height = height;
	var cheers = cheers;
	var userName = userName;
	var userId  = userId ;
	var date = date;
	this.getParentid	=	function () { return parentid; };
	this.getImgtag		=	function () { return imgtag; };
	this.getWidth		=	function () { return width; };
	this.getHeight		=	function () { return height; };
	this.getCheers      =   function () { return cheers; };
	this.getDate        =   function () { return date; };
	this.getUserId      =   function () { return userId; };
	this.getUserName    =   function () { return userName; };
	this.setCheers      =   function (numCheers) { cheers = numCheers; };
}

  function ascendingByChrs(a,b){
        return (a.getCheers()-b.getCheers());
  }
  
  function descendingByChrs(a,b){
        return (b.getCheers()-a.getCheers());
  }
  
  function ascendingByDate(a,b){
       
       return (Date.parse(a.getDate())-Date.parse(b.getDate()));
  }
  
  function descendingByDate(a,b){
       
       return (Date.parse(b.getDate())-Date.parse(a.getDate()));
  }
  
  function sortContestImageByChrs(){
        var sort = 'cheers'
        if(prvSortedBy !=sort){
        thumbList.setThumbnailOrderByCheersDes();
        toggleCssDisplay('uploadphotoform','none');
        thumbIndex = 0
        renderThumbnails();
		paginateThumbs(0);
		document.getElementById('photos-pagination-sort-bydate').style.display="block"
		document.getElementById('photos-pagination-sort-bycheeers').style.display="none"
		prvSortedBy = sort;
		}
		return false;
  }
  
  function sortContestImageByDate(){
        var sort = 'date';
        if(prvSortedBy !=sort){
        thumbList.setThumbnailOrderByDateDes();
        toggleCssDisplay('uploadphotoform','none');
        thumbIndex = 0
        renderThumbnails();
		paginateThumbs(0);
		document.getElementById('photos-pagination-sort-bydate').style.display="none"
		document.getElementById('photos-pagination-sort-bycheeers').style.display="block"
		prvSortedBy = sort;
		}
		return false;
  }