var maxHeight = 0;
var maxWidth = 0;

var rawX=1280;
var rawY=1280;
var scaledX=640;
var scaledY=640;
var postX=200;
var postY=200;

var imgPath=new Array();
var imgCaption=new Array();
var imgH=new Array();
var imgW=new Array();
var imgCount=0;
var currentPhoto=0;

var fullScale="toFit";
var returnTo="";
var displayMode="photoView";


function debug(s){
 if( typeof( window.innerHeight ) == 'number' ){
 //console.log(s);
 }
}

function load(){
    debug('load');
    showThumbs();
    resizeBits();
}

function resizeBits(){
    debug('resizeBits');
  maxHeight=getMaxHeight();
  maxWidth=getMaxWidth();
  if(document.getElementById("photoPageFrameThumbsScroller") != null ){
    document.getElementById("photoPageFrameThumbsScroller").style.height=String(getMaxHeightThumbs())+"px";
    document.getElementById("photoGridView").style.height=String(getMaxHeightThumbs())+"px";

    if(displayMode=="photoFullView"){
      showPhotoFullView(fullScale);
    }else{
      showPhoto(currentPhoto);
    }
  }
}

function getMaxHeight(){
  //debug('getMaxHeight');
  var m=getPageHeight() ;
  m=m-getElementHeight("prefBlock");
  m=m-getElementHeight("photoNav");

  if(displayMode=='photoFullView'){
    m=m-getElementHeight("photoFullViewTitle");
    m=m-50;
   }else{
    m=m-getElementHeight("photoPageFrameHeader");
    m=m-getElementHeight("photoCaption");
    m=m-getElementHeight("photoPageFrameFooter");
  }
  debug('getMaxHeight ret:'+m);
  return m;
}

function getMaxHeightThumbs(){
  //debug('getMaxHeightThumbs');
  return getMaxHeight();
}

function getMaxWidth(){
  //debug('getMaxWidth');

   if(displayMode=="photoFullView"){
     w=document.body.offsetWidth-30;
   }else{
     w=document.body.offsetWidth;
     w=(w*.7) -50;
   }
   return parseInt(w);
}

function getElementHeight(id){
  var myHeight = 0;
  if(document.getElementById(id)==null)alert(id+" is null");
  debug(id+' h:'+document.getElementById(id).offsetHeight);
  //return document.getElementById(id).style.height;
  return document.getElementById(id).offsetHeight;
}

function getPageHeight() {
  var myHeight = 0;
  if( typeof( window.innerHeight ) == 'number' ) {
    //Non-IE
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myHeight = document.body.clientHeight;
  }
  return myHeight;
}

function showThumb(path,rawx,rawy, caption){
 var s='<div  class="albumThumbs">';
 var thumbpath = path.replace('/raw/','/square_thumbnail/');
 s=s+ '<a href="#" onclick="showPhoto('+imgCount+'); return false;">';
 s=s+ '<img  src="/img/pubzap/' + thumbpath + '"  alt=""></a>';
 s=s+ '</div>';
 document.getElementById("photoPageFrameThumbsScroller").innerHTML=
 document.getElementById("photoPageFrameThumbsScroller").innerHTML + s;

 imgPath[imgCount]=path;
 imgCaption[imgCount]=caption;
 imgH[imgCount]=rawy;
 imgW[imgCount]=rawx;
 imgCount++;
}

function showPhoto(id){
  debug('showPhoto id:'+id);
 displayMode='photoView';
 currentPhoto=id;
 caption=imgCaption[id];
 href="";
 link=getImagePath(id,'toFit','showPhotoFullViewId('+id+');return false;');

 document.getElementById("photoSizer").innerHTML=link;
 document.getElementById("photoCaption").innerHTML=caption;
 photoNextPrev(id);
}

function getImagePath(id,scale,onclick,divId){
 var path=imgPath[id];
 var w=imgW[id];
 var h=imgH[id];
 var sW=w;
 var sH=h;
 var xdebug="";

 debug('getImagePath '+id+' scale:'+scale);

 if(scale=="toFit"){
   if(w==0 || h==0){
     w=rawX;
     h=rawY;
   }

   resize=1;
   scale='raw';

   maxWidth=getMaxWidth();
   maxHeight=getMaxHeight();
   debug('maxWidth:'+maxWidth+' maxHeight:'+maxHeight);
   /* first calc best res image to display */

   if((maxWidth<scaledX && w>=h) ||(maxHeight<scaledY && w<=h ))
     scale='scaled';

   if((maxWidth<postX && w>=h) || (maxHeight<postY && w<=h))
     scale='post';

   path=path.replace('/raw/','/'+scale+'/');

   xdebug='';
   size='';
   /* scale the image down */
   if(h>maxHeight || w>maxWidth){
     xdebug=xdebug+'scaled by ';
     if( (w/h < maxWidth/maxHeight) && h>maxHeight){ /* letterbox view area and the image is taller than the view area */
       // resize=maxHeight/h;
       h=maxHeight;
       w='auto';
       size=" height="+ String(h);
      xdebug = xdebug + 'height';
     }else if(w>maxWidth){
       xdebug = xdebug + 'width ['+w+','+maxWidth+']';
      //resize=maxWidth/w;
       h='auto';
       w=maxWidth;
       size=" width="+ String(w);

     }else{
       h='auto';
       w='auto';
       size='';
       xdebug=' using default image size';
     }
   }

  }else{
     path=path.replace('/raw/','/'+scale+'/');
     /* set the size to the correct value */
     if(w>=h){
       size=' width=';
       
       if(scale=='scaled')size=size+Math.min(scaledX,imgW[id]);
       if(scale=='post')size=size+Math.min(postX,imgW[id]);
       if(scale=='raw')size=size+Math.min(rawX,imgW[id]);

     }else{
       size=' height=';
       if(scale=='scaled')size=size+Math.min(scaledX,imgH[id]);
       if(scale=='post')size=size+Math.min(postX,imgH[id]);
       if(scale=='raw')size=size+Math.min(rawX,imgH[id]);
     }

  }
   xdebug= xdebug + '<br>' + scale + ' o[' + sW +','+ sH +'] sized[' + w +','+ h +'] avail['
           + maxWidth + ',' +maxHeight +'] scaled dim:[' + scaledX + ',' + scaledY +'] /img/pubzap/'+path+" "+size;

   onclick=onclick.replace("$scale$",fullScale);


   if(document.getElementById("photoFullView").style.display=="block" && divId==null)divId='id="photoFullImg"';
   if(divId==null)divId='';
   debug(xdebug);
   link='<img "'+divId+'"';
   link=link + ' src="/img/pubzap/' + path + '" ' + size + ' onclick="'+onclick+'" >';
   return link;
}

function showPhotoFullView(scale){
 debug('showPhotoFullView');
 fullScale=scale;
 displayMode="photoFullView";
 window.status = "showPhotoFullView";
 path=imgPath[currentPhoto].replace('/raw/','/'+scale+'/');
 onclick='hidePhotoFullView();return false;';
 link=getImagePath(currentPhoto,scale,onclick,'');
 document.getElementById("photoFullViewImage").innerHTML=link;
 switchDisplayMode("photoFullView");
}

function showPhotoGridView(){
   debug('showPhotoGridView');
 window.status = "showPhotoFullView";
  showPhotoGrid();
  switchDisplayMode("photoGridView");
}

function showPhotoRollView(){
  debug('showPhotoGridView');
  window.status = "showPhotoFullView";
  showPhotoRoll();
  switchDisplayMode("photoRollView");
}

function switchDisplayMode(mode){
   debug('switchDisplayMode from:'+ displayMode+' to:'+mode);

  //if(mode!=displayMode){
  	displayMode=mode;
    document.getElementById("photoView").style.display="none";
    document.getElementById("photoFullView").style.display="none";
    document.getElementById("photoGridView").style.display="none";
    document.getElementById("photoRollView").style.display="none";
    document.getElementById(mode).style.display="block";
  //}
}

function showPhotoFullViewId(id){
  debug('showPhotoFullViewId');
switchDisplayMode("photoFullView");
 displayMode='photoFullView';
 if(arguments.length>0){
  currentPhoto=id;
 }
 scale=fullScale;
 onclick='hidePhotoFullView();return false;';
 link=getImagePath(currentPhoto,scale,onclick,'');
 document.getElementById("photoFullViewImage").innerHTML=link;
 
 photoNextPrev(currentPhoto);
}

function hidePhotoFullView(){
  debug('hidePhotoFullView');
  
  if(returnTo=='photoGridView'){
     switchDisplayMode("photoGridView");
  }else{
     showPhotoView();
  }
}

function showPhotoView(){
  debug('showPhotoView');
  switchDisplayMode("photoView");
  displayMode='photoView';
  returnTo='photoView';
  if(slideshowstopped==0)slideitplaypause();
  slideshowstopped=1;
  showPhoto(currentPhoto);


}

function showPhotoGrid(){
  debug('showPhotoGrid');
  returnTo='photoGridView';
  if(slideshowstopped==0)slideitplaypause();
  slideshowstopped=1;

  if(document.getElementById("photoGridViewPhotos").innerHTML==''){
  for (n=0;n<(imgCount);n++){
    document.getElementById("photoGridViewPhotos").innerHTML=
    document.getElementById("photoGridViewPhotos").innerHTML + '<div class="photoGridImageblob"><div class="photoGridImage" style="background-image: url(/img/pubzap/'+ imgPath[n].replace('/raw/','/post/')+')" onclick="showPhotoFullViewId('+n+');return false;"></div><div class="photoGridCaption">'+imgCaption[n]+'</div></div>';
  }
  }
 }

function showPhotoRoll(){
  debug('showPhotoRoll');
  returnTo='photoGridView';
  if(slideshowstopped==0)slideitplaypause();
  slideshowstopped=1;

  if(document.getElementById("photoRollViewPhotos").innerHTML==''){
  for (n=0;n<(imgCount);n++){
    document.getElementById("photoRollViewPhotos").innerHTML=
    document.getElementById("photoRollViewPhotos").innerHTML + '<div class="photoRollImageblob"><div class="photoRollImage"><img src="/img/pubzap/'+ imgPath[n].replace('/raw/','/scaled/')+'" onclick="showPhotoFullViewId('+n+');return false;"></div><div class="photoRollCaption">'+imgCaption[n]+'</div></div>';
  }
  }
 }


	function photoNextPrev(id){
	 s="";
	 if(id>0){
	   s=s+'<a href="" onclick="showPhoto('+String((id-1))+');return false;"> << </a>';
	 }else{
	   s=s+"<<";
	 }
	 s=s+"("+String(id+1)+" of "+imgCount+")";
	 if(id<(imgCount-1)){
	   s=s+'<a href="" onclick="showPhoto('+String((id+1))+');return false;"> >> </a>';
	 }else{
	   s=s+">>";
	 }
	 /* document.getElementById("photoNextPrev").innerHTML=s; */
	 document.getElementById("photoNextPrevFull").innerHTML=s.replace(/showPhoto/g,'showPhotoFullViewId');
	}
var slideshowstopped=1;
var slideshowspeed=3000;
var slideShowID="photoFullViewImage";

function slideit(){
if (!imgPath || slideshowstopped==1)
 return;
 if (currentPhoto<(imgCount-1))
  currentPhoto++;
 else
  currentPhoto=0;
  slideShow();
  setTimeout("slideit()",slideshowspeed);
 echoPage();
}

function slideitnext(){
 if (!imgPath)return;
 if (currentPhoto<imgCount-2)
  currentPhoto++;
 else
  currentPhoto=0;
 alert(currentPhoto+" of "+imgCount);
 slideShow();
 echoPage();
}

function slideitprevious(){
 if (!imgPath)return;
 if (currentPhoto>0)
  currentPhoto--;
 else
  currentPhoto=imgCount-1;

 slideShow();
 echoPage();
}
function echoPage(){
  /* document.getElementById('thisImageId').innerHTML=(currentPhoto+1); */
}

function slideShow(){
  photoNextPrev(currentPhoto);
  if(currentPhoto>=0){
    if(currentPhoto<imgCount-1)document.getElementById("photoFullViewCache").innerHTML=getImagePath(currentPhoto+1,fullScale,'showPhoto('+currentPhoto+');hidePhotoFullView();return false;');
    //swapfade(document.getElementById("photoFullImg"),'','5','a test');    
    document.getElementById(slideShowID).innerHTML=getImagePath(currentPhoto,fullScale,'showPhoto('+currentPhoto+');hidePhotoFullView();return false;');
    //swapfade(document.getElementById("photoFullImg"),'','5','a test');
  }
}

function slideitplaypause(){
  if(slideshowstopped==1){
     slideshowstopped=0;
     document.getElementById('playpause').src="/img/pubzap/images/pause.jpg";
     /* cache the next one */
     if(currentPhoto<imgCount-1)document.getElementById("photoFullViewCache").innerHTML=getImagePath(currentPhoto+1,fullScale,'showPhoto('+currentPhoto+');hidePhotoFullView();return false;');

     setTimeout("slideit()",slideshowspeed);

  }else{
     document.getElementById('playpause').src="/img/pubzap/images/play.jpg";
      slideshowstopped=1;
  }
}
// ISF1.11 :: Image swap-fade 
// *****************************************************
// DOM scripting by brothercake -- http://www.brothercake.com/
//******************************************************
//global object
var isf = { 'clock' : null, 'fade' : false, 'count' : 1 }
/*******************************************************



/*****************************************************************************
 List the images that need to be cached
*****************************************************************************/

isf.imgs = [
	'buttons/udm4-whitebutton88x31.gif',
	'buttons/udm4-greenbutton88x31.gif',
	'buttons/udm4-purplebutton88x31.gif'
	];

/*****************************************************************************
*****************************************************************************/



//cache the images
isf.imgsLen = isf.imgs.length;
isf.cache = [];
for(var i=0; i<isf.imgsLen; i++)
{
	isf.cache[i] = new Image;
	isf.cache[i].src = isf.imgs[i];
}


//swapfade setup function
function swapfade()
{

	//if the timer is not already going
	if(isf.clock == null)
	{
		//copy the image object 
		isf.obj = arguments[0];
		
		//copy the image src argument 
		isf.src = arguments[1];
		
		//store the supported form of opacity
		if(typeof isf.obj.style.opacity != 'undefined')
		{
			isf.type = 'w3c';
		}
		else if(typeof isf.obj.style.MozOpacity != 'undefined')
		{
			isf.type = 'moz';
		}
		else if(typeof isf.obj.style.KhtmlOpacity != 'undefined')
		{
			isf.type = 'khtml';
		}
		else if(typeof isf.obj.filters == 'object')
		{
			//weed out win/ie5.0 by testing the length of the filters collection (where filters is an object with no data)
			//then weed out mac/ie5 by testing first the existence of the alpha object (to prevent errors in win/ie5.0)
			//then the returned value type, which should be a number, but in mac/ie5 is an empty string
			isf.type = (isf.obj.filters.length > 0 && typeof isf.obj.filters.alpha == 'object' && typeof isf.obj.filters.alpha.opacity == 'number') ? 'ie' : 'none';
		}
		else
		{
			isf.type = 'none';
		}
		
		//change the image alt text if defined
		if(typeof arguments[3] != 'undefined' && arguments[3] != '')
		{
			isf.obj.alt = arguments[3];
		}
		
		//if any kind of opacity is supported
		if(isf.type != 'none')
		{

			//copy and convert fade duration argument 
			//the duration specifies the whole transition
			//but the swapfade is two distinct transitions
			isf.length = parseInt(arguments[2], 10) * 500;
			
			//create fade resolution argument as 20 steps per transition
			//again, split for the two distrinct transitions
			isf.resolution = parseInt(arguments[2], 10) * 10;
			
			//start the timer
			isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);
		}
		
		//otherwise if opacity is not supported
		else
		{
			//just do the image swap
			//isf.obj.src = isf.src;
		}
		
	}
};


//swapfade timer function
isf.swapfade = function()
{
	//increase or reduce the counter on an exponential scale
	isf.count = (isf.fade) ? isf.count * 0.9 : (isf.count * (1/0.9)); 
	
	//if the counter has reached the bottom
	if(isf.count < (1 / isf.resolution))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//do the image swap
		isf.obj.src = isf.src;

		//reverse the fade direction flag
		isf.fade = false;
		
		//restart the timer
		isf.clock = setInterval('isf.swapfade()', isf.length/isf.resolution);

	}
	
	//if the counter has reached the top
	if(isf.count > (1 - (1 / isf.resolution)))
	{
		//clear the timer
		clearInterval(isf.clock);
		isf.clock = null;

		//reset the fade direction flag
		isf.fade = true;
		
		//reset the counter
		isf.count = 1;
	}

	//set new opacity value on element
	//using whatever method is supported
	switch(isf.type)
	{
		case 'ie' :
			isf.obj.filters.alpha.opacity = isf.count * 100;
			break;
			
		case 'khtml' :
			isf.obj.style.KhtmlOpacity = isf.count;
			break;
			
		case 'moz' : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.MozOpacity = (isf.count == 1 ? 0.9999999 : isf.count);
			break;
			
		default : 
			//restrict max opacity to prevent a visual popping effect in firefox
			isf.obj.style.opacity = (isf.count == 1 ? 0.9999999 : isf.count);
	}
};






