var l_IEWidthCorrection = 0;
var l_IEHeightCorrection = 0;

var galleryItemOnMouseOverFnc = null;
var galleryItemOnMouseOutFnc = null; 

var galleriesCommentsRequest = null;
var awaitingGalleryCommentsData = false;
var galleryCommentsOverlay = null;

var GalIDs = '';

PageSpecificOnLoad = getCommentsCount; //getCommentCount might be run in middle of the page by inlucded galleries.html 

function galleryItemOnMouseOver(in_GalleryID) {
  if (galleryItemOnMouseOverFnc) galleryItemOnMouseOverFnc(in_GalleryID);
}

function galleryItemOnMouseOut(in_GalleryID) {
  if (galleryItemOnMouseOutFnc) galleryItemOnMouseOutFnc(in_GalleryID);
}

function galleriesCommentsLoaded(in_RequestObj,in_Status) {
  if (in_Status==200) {
    try {
      var l_JsonObj=in_RequestObj.getResponseJSON();
      l_Length = l_JsonObj.galleryIDs.length; 
      for (var i=0;i<l_Length;i++) {
        updateCommentCount(l_JsonObj.galleryIDs[i] , l_JsonObj.commentCounts[i]);
      }	
    } catch(e) {};
  }
  galleriesCommentsRequest = null; //free ajax object
}

function galleryCommentsLoaded(in_RequestObj,in_Status) {
	var l_Overlay = document.getElementById('gpGalleryCommentsOverlay');
	if (!l_Overlay) return;
  if ((in_Status==200)&& (awaitingGalleryCommentsData)) {
	  try {
			galleriesCommentsLoaded(in_RequestObj,in_Status);
		  var l_Div = document.createElement('div');
			l_Div.setAttribute('id','gpCommentsDisplayBox');
		  var l_Code = '<table summary="">'+"\n";
      var l_JsonObj = in_RequestObj.getResponseJSON();      
		  var l_Items = l_JsonObj.items; 
      var l_len = l_Items.length;
      var l_oddline = true;
      var l_Line;
      var l_Id = l_JsonObj.galleryIDs[0];
      var l_GalCode = l_JsonObj.code;
      var l_Part;
      for (i=0;i<l_len;i++) {
        
        if (l_Items[i].pic) {
          l_Part = l_Items[i].part;
          l_Line = '<a href="galleries/gallery.php?id='+l_Id+'&part='+l_Part+'&picname='+l_Items[i].pic+".jpg\" onclick=\"return showGallery("+l_Id+','+l_Part+",'"+l_Items[i].pic+
            ".jpg')\"><img src=\"../images/photo_icon.png\" class=\"miniicon\" onmouseover=\"return floatThumb('"+l_GalCode+'/part'+l_Part+'/'+l_Items[i].pic+
            "_thumb.jpg',"+l_Part+")\" onmouseout=\"nd()\"></a>";
        } else {
          l_Line = '<img src="images/empty.png" class="miniicon">';
        }
        l_Code += "<tr class=\""+(l_oddline?'oddline':'evenline')+"\">\n<td class=\"firstcolumn\">"+
				  l_Items[i].time+" by "+l_Items[i].author+
					"</td><td>"+l_Items[i].comment+"</td>\n<td style=\"width: 16px;\">"+
          l_Line+"</td>\n</tr>\n";
				l_oddline = (l_oddline?false:true);	
			}
			
			l_Code +="</table>\n";
			
			l_Div.innerHTML = l_Code;
			l_Overlay.replaceChild(l_Div,document.getElementById('gpLoadingComments'));
		}	
    catch(e) {
      l_Overlay.innerHTML = e.message;
    }
  }
}

function getCommentsCount() {
  
  galleriesCommentsRequest=new AjaxRequest();

  galleriesCommentsRequest.onload=galleriesCommentsLoaded;
  galleriesCommentsRequest.params['GalIDs']=GalIDs;
  
  try {
    galleriesCommentsRequest.getRequest('ajax/commentcounts.php');
  } catch(e) {};
}

function showGallery(in_GalleryID,in_Part,in_PicName) {
 l_address = 'galleries/gallery.php?id='+in_GalleryID+'&part='+in_Part;
 if (in_PicName) l_address += '&picname='+in_PicName;
 l_params="menubar=0,toolbar=0,location=0,resizable=1,left=0,top=0,screenX=0,screenY=0";
 var l_scwidth = 1024;
 var l_scheight= 768;
 if (screen) {
   if (screen.availWidth) l_scwidth = screen.availWidth;
   if (screen.availHeight) l_scheight = screen.availHeight;
   }
 l_scwidth -= l_IEWidthCorrection;
 l_scheight -= l_IEHeightCorrection;
 l_params += ',width='+l_scwidth+',height='+l_scheight;
 window.open(l_address,'GalleryWindow_'+in_GalleryID+'_'+in_Part,l_params);
 return false;
}

function showGalleryComments(in_GalleryID) {
	//create display area
	var l_Html = '\n<div id="gpGalleryCommentsOverlay">\n<div id="gpCommentsTitle">\n<img  class="handicon" src="images/close_icon.gif" alt="Close comments" onclick="hidePageOverlay();" title="Close comments">\n';
	l_Html += '<span>'+galleryTitles[in_GalleryID]+'</span>\n</div>\n';
	l_Html += '<div id="gpLoadingComments"><img  src="images/loading.gif" alt="Loading..."></div>\n</div>\n';
	showPageOverlay(l_Html, true);
	//request data
	galleriesCommentsRequest=new AjaxRequest();

  galleriesCommentsRequest.onload=galleryCommentsLoaded;
  galleriesCommentsRequest.params['galleryid']=in_GalleryID;
	
	awaitingGalleryCommentsData = true;

  try {
    galleriesCommentsRequest.getRequest('ajax/gallerycomments.php');
  } catch(e) {};

	return false;
}
	
function updateCommentCount(in_GalleryID, in_Count) {
  l_CommentsTag=document.getElementById('Comment'+in_GalleryID);
  if (l_CommentsTag) {
    if (in_Count>0) {
			l_CommentsTag.innerHTML='Comments: <a class="basecolor" href="javascript:void(0)" onclick="return showGalleryComments('+in_GalleryID+');" target="_self">'+in_Count+'</a>';
			l_CommentsTag.className+=' basecolor';
    } else {
			l_CommentsTag.innerHTML='Comments: 0';
    }
  }  
}

function floatThumb(in_Thumb, in_Part) {
  return overlib('<div class="centered font80 opaquebkgrnd deftxtcolor"><img src="galleries/'+in_Thumb+'"><br><span class="bold">From </span><span class="bold italic">Part '+in_Part+'</span><br>Click to show</div>', WRAP);
};
