var div_index = 0;
var img_index = 0;
var thumbnails_to_display = server_thumbnails_to_display;
var number_of_divs = server_number_of_divs;
var thumbnail_str_1 = server_thumbnail_str_1;
var thumbnail_str_2 = server_thumbnail_str_2;
var pauseSlideshowScrollThreshold = 800;

function thumbnail(str,tn)
{
   i = str.lastIndexOf(".");
   if(i==0) {return ""; }
   l = str.length - i;
   return str.substr(0,i) + tn + str.substr(i,l);
}

function panLeft()
{
   doPan(-1);
}

function panRight()
{
   doPan(1);
}



var load_image_on_pan_completion = 0;


function doPan(direction,load_this_div_directly,i)
{
   var div_to_load = (load_this_div_directly == true) ? direction : (div_index+direction);
   if(!load_this_div_directly && direction == 1 && div_index == number_of_divs-1)
   {
      return;
   }
   if(!load_this_div_directly && direction == -1 && div_index == 0)
   {
      return;
   }
   if(!isNaN(i)) 
   { 
      load_image_on_pan_completion = i; 
   }
   else
   {
      load_image_on_pan_completion = 0;
	}
   preloadImageGroupThumbnails(div_to_load);
   var prop_img_thumbnails = document.getElementById('prop_img_thumbnails');
   var loading_thumbnails_div = document.getElementById('loading_thumbnails_div');
   prop_img_thumbnails.style.display = 'none';
   loading_thumbnails_div.style.display = '';
   var url = '/ajax/do_property_images_pan_image_set.ajax.php?id='+server_relevant_id+'&mode='+server_relevant_mode+'&page='+server_property_page+'&div_index='+div_to_load+'&LANGUAGE_CODE=1&feature_selector='+server_feature_selector+'&thumbnails_to_display='+server_thumbnails_to_display+'&thumbnails_to_display_per_row='+server_thumbnails_to_display_per_row;
   ajaxRequestAlt(url, true, 'doPanResponse');
   div_index = div_to_load;
   updateStatus();
}



function preloadImageGroup(idx)
{
   var offset = idx*server_thumbnails_to_display;
   var img_counter = 0;
   for(var i = offset; i < offset+server_thumbnails_to_display && i < imgs.length; i++)
   {
      MM_preloadImages(thumbnail(imgs[i][0],server_thumbnail_suffix));
   } 
}

function preloadImageGroupThumbnails(idx)
{
   var offset = idx*server_thumbnails_to_display;
   var img_counter = 0;
   for(var i = offset; i < offset+server_thumbnails_to_display && i < imgs.length; i++)
   {
      MM_preloadImages(thumbnail(imgs[i][0],"_tns"));
   } 
}


/*function waitForThumbnails()
{
   var start = (div_index*thumbnails_to_display)+1;
   var end = start+thumbnails_to_display>imgs.length ? imgs.length : start+thumbnails_to_display-1;
   var all_loaded = true;
   for(i = start-1; i < end; i++)
   {
      var obj = document.getElementById('image_'+i);
      if(obj.complete == false)
      {
         all_loaded = false;            
      }
   }
   return all_loaded;
}*/


var js_thumb_objs_to_load = 0;
var js_thumb_objs_loaded = 0;
var js_thumb_objs_load_start = false;
var js_thumb_objs = new Array();
var js_thumb_objs_counter = 0;
function waitForThumbnails()
{
   if(!js_thumb_objs_load_start)
   {
	   var start = (div_index*thumbnails_to_display)+1;
	   var end = start+thumbnails_to_display>imgs.length ? imgs.length : start+thumbnails_to_display-1;
	   js_thumb_objs_counter = 0;
	   js_thumb_objs_loaded = 0;
	   js_thumb_objs_to_load = end - (start-1);
	   for(var i = start-1; i < end; i++)
	   {
	      js_thumb_objs[js_thumb_objs_counter] = new Image();
	      js_thumb_objs[js_thumb_objs_counter].onload = function() 
	      { 
	         js_thumb_objs_loaded++; 
	      } 
	      js_thumb_objs[js_thumb_objs_counter].src = thumbnail(imgs[i][0],'_tns');
	      js_thumb_objs_load_start = true;
	      js_thumb_objs_counter++;
	   }
      return false;
	}
	else
	{
	   if(js_thumb_objs_loaded == js_thumb_objs_to_load)
	   {
		   js_thumb_objs_load_start = false; 
	      return true;
		}
		else
		{
		   return false;
		}
	}
}







var js_img_obj_loaded = false;
var js_img_obj_load_start = false;
function waitForMainImage(i)
{
   if(!js_img_obj_load_start)
   {
      var js_img_obj = new Image();
      js_img_obj.onload = function() { js_img_obj_loaded = true; } 
      js_img_obj.src = imgs[i][0];
      js_img_obj_load_start = true;
      return false;
	}
	else
	{
	   if(js_img_obj_loaded) js_img_obj_load_start = false;
	   return js_img_obj_loaded;
	}
}






function finishedLoading(i)
{
   var prop_img_thumbnails = document.getElementById('prop_img_thumbnails');
   var loading_thumbnails_div = document.getElementById('loading_thumbnails_div');
   loading_thumbnails_div.style.display = 'none'; 
   prop_img_thumbnails.style.display = ''; 
   if(!isNaN(i) && i > 0)
   {
      loadImage(i);
   }  
}


function doPanResponse()
{
   if (xmlHttpAlt.readyState==4)
   {
      rc = xmlHttpAlt.responseText;
      document.getElementById('prop_img_thumbnails').innerHTML = rc;
      preloadImageGroup(div_index);
      // doPan contains functionality to load an image
      // upon completion...
      if(!isNaN(load_image_on_pan_completion) && load_image_on_pan_completion>0)
      {
         setTimeout(function() { waitForThumbnailsAndTrigger(load_image_on_pan_completion); },1000);
	  }
	  else
	  {
	     setTimeout(waitForThumbnailsAndTrigger, 1000);
	  }
   }
}

function waitForThumbnailsAndTrigger(i)
{     
   if(!waitForThumbnails()) 
   {
      setTimeout(function() { waitForThumbnailsAndTrigger(i); },1000);
      return;
	}
   else
   {
      if(!isNaN(i) && i>0)
      {
         finishedLoading(i);
		}
		else
		{
		   finishedLoading();
		}
	}
}


function closeApplicableFAQs()
{
   document.getElementById("applicable_faqs_div").innerHTML = '';
   document.getElementById("applicable_faqs_div").style.display = 'none';
}

function findPosX(obj)
{
 var curleft = 0;
 if(obj.offsetParent)
     while(1)
     {
       curleft += obj.offsetLeft;
       if(!obj.offsetParent)
         break;
       obj = obj.offsetParent;
     }
 else if(obj.x)
     curleft += obj.x;
 return curleft;
}



function findPosY(obj)
{
 var curtop = 0;
 if(obj.offsetParent)
     while(1)
     {
       curtop += obj.offsetTop;
       if(!obj.offsetParent)
         break;
       obj = obj.offsetParent;
     }
 else if(obj.y)
     curtop += obj.y;
 return curtop;
}


function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function P7_Snap() { //v2.67 by PVII
 var g,x,y,ox,bx,oy,p,tx,a,b,k,d,da,e,el,tw,q0,xx,yy,w1,pa='px',args=P7_Snap.arguments;a=parseInt(a);
 if(document.layers||window.opera){pa='';}for(k=0;k<(args.length);k+=4){
 if((g=MM_findObj(args[k]))!=null){if((el=MM_findObj(args[k+1]))!=null){
 a=parseInt(args[k+2]);b=parseInt(args[k+3]);x=0;y=0;ox=0;oy=0;p="";tx=1;
 da="document.all['"+args[k]+"']";if(document.getElementById){
 d="document.getElementsByName('"+args[k]+"')[0]";if(!eval(d)){
 d="document.getElementById('"+args[k]+"')";if(!eval(d)){d=da;}}
 }else if(document.all){d=da;}if(document.all||document.getElementById){while(tx==1){
 p+=".offsetParent";if(eval(d+p)){x+=parseInt(eval(d+p+".offsetLeft"));y+=parseInt(eval(d+p+".offsetTop"));
 }else{tx=0;}}ox=parseInt(g.offsetLeft);oy=parseInt(g.offsetTop);tw=x+ox+y+oy;
 if(tw==0||(navigator.appVersion.indexOf("MSIE 4")>-1&&navigator.appVersion.indexOf("Mac")>-1)){
  ox=0;oy=0;if(g.style.left){x=parseInt(g.style.left);y=parseInt(g.style.top);}else{
  w1=parseInt(el.style.width);bx=(a<0)?-5-w1:-10;a=(Math.abs(a)<1000)?0:a;b=(Math.abs(b)<1000)?0:b;
  x=document.body.scrollLeft+event.clientX+bx;y=document.body.scrollTop+event.clientY;}}
 }else if(document.layers){x=g.x;y=g.y;q0=document.layers,dd="";for(var s=0;s<q0.length;s++){
  dd='document.'+q0[s].name;if(eval(dd+'.document.'+args[k])){x+=eval(dd+'.left');y+=eval(dd+'.top');
  break;}}}e=(document.layers)?el:el.style;xx=parseInt(x+ox+a),yy=parseInt(y+oy+b);
 if(navigator.appVersion.indexOf("MSIE 5")>-1 && navigator.appVersion.indexOf("Mac")>-1){
  xx+=parseInt(document.body.leftMargin);yy+=parseInt(document.body.topMargin);}
 e.left=xx+pa;e.top=yy+pa;}}}
}










var currentFadeImage = 0;
var pixelsPerChar = 7;
var imageSummaryText = server_imageSummaryText;
var notAvailableText = server_notAvailableText;

function rtrim(str, chars) 
{
    chars = chars || "\\s";
    return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

function loadImage(i,no_pause,delta)
{  
   // If last image in set of current thumbnails
   // and the slideshow is running, preload the next
   // image
   if(no_pause == true && i+1 < imgs.length && i % thumbnails_to_display == thumbnails_to_display-1)
   {
      MM_preloadImages(thumbnail(imgs[i+1][0],server_thumbnail_suffix));
   }
   
   // If loading but not from slideshow, pause slideshow
   if(no_pause != true) { pauseSlideshow(); }
   
   // Fade image out and start process
   if(server_map_present) closeMapSystem();
   var main_image_name_container = document.getElementById('main_image_name_container');
   var main_image_name = document.getElementById('main_image_name');  
   main_image_name_container.style.display = 'none';
   main_image_name.style.display = 'none';
   currentFadeImage = i;
   FadeOut('image_container_div',i);
   // When fade out finishes, call step 2
   setTimeout(function() { loadImageStep2(i,no_pause,delta); }, 8 * 120);
}

function loadImageStep2(i, no_pause, delta)
{
   // Scale container
   HorizScale('main_image_div',imgs[i][3]+14);  
   VertScale('main_image_div',imgs[i][4]+14);
   // When scale finishes, call step 3
   setTimeout(function() { loadImageStep3(i,no_pause,delta); }, 8 * 60);
}


function loadImageStep3(i, no_pause, delta)
{   
   var _img_name = imgs[i][1].replace(/\'/g,"\\'");
   if(_img_name == '') _img_name = server_notAvailableText;
   MM_changeProp('main_image','','src',thumbnail(imgs[i][0],server_thumbnail_suffix),'IMG');
   MM_changeProp('main_image_link','','title',_img_name,'A');
   
   // Links to
   var _next_img = i+1;
   if(_next_img >= imgs.length) _next_img = 0;
   var _main_img_href = server_main_image_href.replace('?1',_next_img).replace('?2',imgs[i][2]);
   MM_changeProp('main_image_link','','href',_main_img_href,'A');
   
   img_index = i;

   // Slider
   moveSliderTo(i);
   
   // Next Step
   setTimeout(function() { waitForMainImageAndTrigger(i,no_pause,delta); },500);

}

function waitForMainImageAndTrigger(i,no_pause,delta)
{
   if(!waitForMainImage(i)) 
   {
      setTimeout(function() { waitForMainImageAndTrigger(i,no_pause,delta); },500);
      return;
	}
   else
   {
      loadImageStep4(i,no_pause,delta);
	}
}

function loadImageStep4(i, no_pause, delta)
{
   // Fade in image
   FadeIn('image_container_div',i);
   ShowDiv('image_container_div');
   
   // Trim summary information
   var maxChars = Math.ceil(imgs[i][3]/pixelsPerChar);
   imgs[i][1] = (imgs[i][1].length+7>maxChars) ? rtrim(imgs[i][1].substring(0,maxChars-7))+'...' : imgs[i][1];
   imgs[i][5] = (imgs[i][5].length+13>maxChars) ? rtrim(imgs[i][5].substring(0,maxChars-13))+'...' : imgs[i][5];
   
   // Load image text
   var txt = imageSummaryText.replace("?1",imgs[i][1].replace(/\'/g,"\\'"));
   txt = (imgs[i][5].replace(/\'/g,"\\'")=="") ? txt.replace("?2",notAvailableText) : txt.replace("?2",imgs[i][5].replace(/\'/g,"\\'"));
   txt = txt.replace("?3",imgs[i][6].replace(/\'/g,"\\'"));   
   
   // Links to
   var _next_img = i+1;
   if(_next_img >= imgs.length) _next_img = 0;
   var _main_img_href = server_main_image_href.replace('?1',_next_img).replace('?2',imgs[i][2]); 
   txt = txt.replace("?4",_main_img_href);
   
   setTimeout(function() { if(isCurrentImage(i)){loadImageText(txt,imgs[i][3],imgs[i][4],i);}}, 8 * 120);
   
   // Accept mouse wheel movements in 3, 2, 1...
   setTimeout(function () { ready_to_roll = true; }, 8 * 120);
   
   // If slideshow running, move to next image
   if(slideshow_running)
   {
      setTimeout(moveToNextImageWhenReady,slideshow_duration*1000);
   }
   
   // If we need to start the slideshow
   if(slideshow_onload)
   {
      slideshow_initial_load = setTimeout(playSlideshow,slideshow_duration*1000);
      slideshow_onload = false;
	}
   
   // If from_dragstop, reset the drag image number
   if(from_dragstop)
   {
      drag_image_number = -1;
      from_dragstop = false;
	} 

   // Load the relevant thumbnails
   // after the main image is loaded
   setTimeout(function() { loadThumbnails(i,delta); }, 8 * 140);   
}

function isCurrentImage(i)
{
   return (i == currentFadeImage);  
}

function loadThumbnails(i, delta)
{
   // If new image number is the first or last in a series
   // and we have a direction (delta), or we're back to the
   // start of the image list, then pan the thumbnails
   if(i%thumbnails_to_display==0 && delta > 0) { panRight(); }
   if(i%thumbnails_to_display==thumbnails_to_display-1 && delta < 0) { panLeft(); }
   if(i == 0 && delta == 0) { doPan(0); }
}

function loadImageText(txt, width, height, i)
{
   var page_img_x = document.getElementById('main_image').offsetWidth;
   var page_img_y = document.getElementById('main_image').offsetHeight; 
   var actual_img_x = imgs[i][3];
   var actual_img_y = imgs[i][4];
   
   // If we don't have the correct dimensions, delay the snap
   if(page_img_x != actual_img_x || page_img_y != actual_img_y)
   {
      setTimeout(function() { loadImageText(txt,width,height,i); },250);
      return;
	}

   var main_image_name = document.getElementById('main_image_name');
   var main_image_name_container = document.getElementById('main_image_name_container');
   main_image_name.innerHTML = txt;
   main_image_name_container.style.width = width;
   main_image_name.style.width = width;
 
   P7_Snap('main_image','main_image_name_container',-6,height-59);
   P7_Snap('main_image','main_image_name',-6,height-59);
   main_image_name.style.display = '';
   main_image_name_container.style.display = '';
   
   // IF THERE IS GEO DATA ASSCIATED WITH THIS IMAGE
   var to_eval = 'if(typeof(loc'+i+')!="undefined"){loadMapData(i);}';
   eval(to_eval);
}

var current_icon_i = -1;
function loadMapData(i)
{
   if(current_icon_i != -1) highlight(current_icon_i,false);
   highlight(i,true);
   current_icon_i = i;
   snapMapToImage(i);
   if(current_minmax_status == 'maximised') maximiseMap();
   else minimiseMap();
}

function snapMapToImage(i)
{
   var offset_x = imgs[i][3] - server_map_size_x - 4;
   var offset_y = imgs[i][4] - server_map_size_y - 4 - 21;
   P7_Snap('main_image','map_maxbar',offset_x,offset_y + server_map_size_y);
   P7_Snap('main_image','map_holder',offset_x,offset_y);
}

function highlight(i,do_highlight)
{
   var icon = (do_highlight) ? 'currentPhotoIcon' : 'normalPhotoIcon';
   var to_eval = 'map.removeOverlay(loc'+i+');var latlng = loc'+i+'.getLatLng();loc'+i+' = new GMarker(latlng,'+icon+');map.addOverlay(loc'+i+');GEvent.addListener(loc'+i+',"click",function(){loadImage('+i+');});';
   eval(to_eval);  
   if(do_highlight)
   {
      var to_eval = 'map.panTo(loc'+i+'.getLatLng());'
      eval(to_eval);
	}
}

function updateStatus()
{
   if(imgs.length==1)
   {
      content = thumbnail_str_1;
   }
   else
   {
      var start = (div_index*thumbnails_to_display)+1;
      var end = start+thumbnails_to_display>imgs.length ? imgs.length : start+thumbnails_to_display-1;
      content = thumbnail_str_2.replace("?1",start).replace("?2",end).replace("?3",imgs.length);
   }
   MM_changeProp('thumbnail_status','','innerHTML',content,'DIV');
}


function wheel(event)
{
   if (!event) event = window.event;   
   if(ready_to_roll)
   {
      ready_to_roll = false;
      var delta = 0;
      if (event.wheelDelta)
      {
         delta = event.wheelDelta / 120;
      }
      else if (event.detail)
      {
         delta = -event.detail / 3;
      }
      
      if(delta > 0) delta = -1;
      else if(delta < 0) delta = 1; 
      if (delta) 
      {
         pauseSlideshow();
         handle(delta);
      }
   }
   if (event.preventDefault) event.preventDefault();
   event.returnValue = false;
}


function handle(delta)
{
   var new_index = img_index+delta;
   if(new_index>=0 && new_index < imgs.length)
   {
      var no_pause = false; // pause slideshow
      loadImage(new_index,no_pause,delta);
   }
}


function initMouseWheel()
{
   if(window.addEventListener) main_image_div.addEventListener('DOMMouseScroll', wheel, false);
   main_image_div.onmousewheel = wheel;
}















var slider_div = '';
var scrollbar_div = '';
var slider_counter_div = '';
var dragging = false;
var dragobject = null;
var dragx = 0;
var posx = 0;
var new_posx = 0;
var xstep = 0;
var scrollbar_width = 0;
var drag_image_number = -1;

function initMouseDrag()
{
   document.onmousemove = drag;
   document.onmouseup = dragstop;

   document.onselectstart = function ()
   {
      if (dragging == true)
      {
         return false;
      }
      else
      {
         return true;
      }
   }
}


function moveSliderTo(i)
{
   new_posx = (i*xstep);
   slider_div.style.marginLeft = "" + new_posx + "px";
   slider_counter_div.innerHTML = (i+1);  
}



function quickSlide(e)
{
   // If not dragging as well...
   if(drag_image_number == -1)
   {
      var clickx = document.all ? window.event.clientX : e.pageX;
      var startx = findPosX(scrollbar_div);
      var npx = clickx - startx - 5;
      var res = Math.round(npx/xstep);
      if(res >= 0 && res < imgs.length)
      {  
         pauseSlideshow(); 
         var div_to_show = Math.floor(res/thumbnails_to_display);
         if(div_to_show != div_index)
         {
            // Call doPan to show appropriate thumbnails
            // and pass in image number so loadImage is called
            // on completion
            doPan(div_to_show,true,res);
         }
         else
         {
            // Just load the image directly
            loadImage(res);  
         }
      }
   }
}

function findPosX(obj)
{
 var curleft = 0;
 if(obj.offsetParent)
     while(1)
     {
       curleft += obj.offsetLeft;
       if(!obj.offsetParent)
         break;
       obj = obj.offsetParent;
     }
 else if(obj.x)
     curleft += obj.x;
 return curleft;
}


function dragstart(element)
{
   dragobject = element;
   dragx = posx - dragobject.offsetLeft;
   drag_image_number = -1;
}

function drag(e)
{
   posx = document.all ? window.event.clientX : e.pageX;
   if(dragobject != null)
   {
      dragging = true;
      new_posx = (posx - dragx);

      if(new_posx < 0) new_posx = 0;
      if(new_posx > scrollbar_width - 30) new_posx = scrollbar_width - 30;

      var step_width = new_posx/((scrollbar_width-30)/imgs.length);
      var image_number = Math.round(step_width);
      dragobject.style.marginLeft = new_posx + 'px';
      drag_image_number = image_number-1;
      if(drag_image_number < 0) drag_image_number = 0;
   }
}

var from_dragstop = false;
function dragstop()
{ 
   dragobject = null;
   dragging = false;
   if(drag_image_number != -1)
   {  
      if(drag_image_number >= 0 && drag_image_number < imgs.length)
      {  
         pauseSlideshow(); 
         var div_to_show = Math.floor(drag_image_number/thumbnails_to_display);
         if(div_to_show != div_index)
         {
            // Call doPan to show appropriate thumbnails
            // and pass in image number so loadImage is called
            // on completion
            from_dragstop = true;
            doPan(div_to_show,true,drag_image_number);
         }
         else
         {
            // Just load the image directly
            from_dragstop = true;
            loadImage(drag_image_number);  
         }
      }
   }
}


function getKeyCode(event)
{
   event = event || window.event;
   return event.keyCode;
}



document.onkeydown = function(event)
{
   if(ready_to_roll)
   {
      ready_to_roll = false;
      var charCode  = getKeyCode(event);
      switch (charCode)
      {
         case 37:
            handle(-1);
            break;

         case 39:
            handle(1);
            break;
      }
   }
}


// Function used to show/hide the target div
function ToggleDiv($WhichDiv) {
   if (document.getElementById($WhichDiv).style.display == "none") {
      setOpac($WhichDiv,100); // set it to opaque (just in case)
      document.getElementById($WhichDiv).style.display = "block";
   } else {
      document.getElementById($WhichDiv).style.display = "none";
   }
}

function FadeToggleDiv($WhichDiv) {
   if (document.getElementById($WhichDiv).style.display == "none") {
      FadeIn($WhichDiv);
   } else {
      FadeOut($WhichDiv);
   }
}

function FadeIn($WhichDiv,onFadeImage) 
{
   setOpac($WhichDiv,0);
   for(var i = 0; i <= 100; i++) 
   {
      (function(opacity)
      {
         setTimeout(function() { setOpac($WhichDiv,opacity,onFadeImage); } , 8 * i );
	  })(i);         
   }
}

function FadeOut($WhichDiv,onFadeImage) 
{
   for(var i = 0; i <= 100; i++) 
   {
      (function(opacity)
      {
         setTimeout(function() { setOpac($WhichDiv,(100 - opacity),onFadeImage); } , 8 * i );
	  })(i);         
   }
}

// Function used to set the opacity of a div (from 0 to 10)
function setOpac( $WhichDiv, value, onFadeImage ) {
   if(onFadeImage == currentFadeImage)
   {
      document.getElementById($WhichDiv).style.opacity = value / 100;
      document.getElementById($WhichDiv).style.filter = 'alpha(opacity=' + value + ')';
   }
}


// Function used to shrink/grow a div vertically
function ToggleVert($WhichDiv, $MinHeight, $MaxHeight) {
   var $CurrHeight = parseInt(document.getElementById($WhichDiv).style.height);
   if ($CurrHeight < ($MaxHeight - 10)) {
      VertScale($WhichDiv,$MaxHeight);
   } else {
      VertScale($WhichDiv,$MinHeight);
   }
}

// Function used to animate the scaling of a div vertically
function VertScale($WhichDiv, $EndHeight) 
{
   var $StartHeight = parseInt(document.getElementById($WhichDiv).style.height);
   document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
   document.getElementById($WhichDiv).style.overflow = 'hidden'; // set the overflow to hidden
   for(var i = 0; i <= 50 ; i++) 
   {
	     (function(h)
	     {
	        setTimeout(function() { setHeight($WhichDiv, h); } , 8 * i);
		 })(($StartHeight - ((($StartHeight - $EndHeight) / 50) * (i-1))));         
   }
   if ($EndHeight == 0) 
   {
      setTimeout(function () { ToggleDiv($WhichDiv); }, 8 * i);
   }
}

function HorizScale($WhichDiv, $EndWidth) 
{
   var $StartWidth = parseInt(document.getElementById($WhichDiv).style.width);
   document.getElementById($WhichDiv).style.display = "block"; // display as block just in case
   document.getElementById($WhichDiv).style.overflow = 'hidden'; // set the overflow to hidden
   for(var i = 0; i <= 50; i++) 
   {
	     (function(w)
	     {
	        setTimeout(function() { setWidth($WhichDiv, w); } , 8 * i);
		 })(($StartWidth - ((($StartWidth - $EndWidth) / 50) * (i-1))));           
   }
   if ($EndWidth == 0) 
   {
      setTimeout(function() { ToggleDiv($WhichDiv); }, 8 * i);
   }
}

// Function used to set the height of a div
function setHeight( $WhichDiv, value ) {
   document.getElementById($WhichDiv).style.height = parseInt(value) + "px";
}

function setWidth( $WhichDiv, value ) {
   document.getElementById($WhichDiv).style.width = parseInt(value) + "px";
}



function HideDiv(div)
{
   document.getElementById(div).style.display = 'none';  
}

function ShowDiv(div)
{
   document.getElementById(div).style.display = 'block';  
}


var slideshow = null;
var slideshow_initial_load = null;
var slideshow_duration = 5;
var slideshow_running = false;
var ready_to_roll = false; // variable to reject mouse wheel movements and arrow presses, set to true whenever an image is loaded
var slideshow_onload = (imgs.length>1) ? true : false;

function playSlideshow()
{ 
   if(!slideshow_running)
   {
      slideshow = setTimeout(function() { slideshow_running = true; moveToNextImageWhenReady(); },1000);    
      document.getElementById('image_set_play_button').className = 'image_set_play_slideshow_inactive';
      document.getElementById('image_set_pause_button').className = 'image_set_pause_slideshow';
	}
}

function moveToNextImageWhenReady()
{
   if(slideshow_running)
   {
      var no_pause = true; // dont pause slideshow
      var delta = 1; // moving right
      var move_to = (img_index+1 >= imgs.length) ? 0 : (img_index+1);
      if(move_to == 0) { delta = 0; div_index = 0; }
      loadImage(move_to,no_pause,delta);  
   }
}

function pauseSlideshow()
{
   slideshow_running = false;
   slideshow_onload = false;
   if(slideshow != null)
   {
      clearTimeout(slideshow);
      slideshow = null;   
   }
   if(slideshow_initial_load != null)
   {
      clearTimeout(slideshow_initial_load);
      slideshow_initial_load = null;
	}
      document.getElementById('image_set_play_button').className = 'image_set_play_slideshow';
      document.getElementById('image_set_pause_button').className = 'image_set_pause_slideshow_inactive';	
}

function shouldSlideshowBePaused()
{
   if(getScrollXY()[1] >= pauseSlideshowScrollThreshold) pauseSlideshow();	
   else setTimeout(shouldSlideshowBePaused,3500);
}

function getScrollXY() 
{
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}


function loadPropertyImages()
{
   if(document.getElementById('property_images_div'))
   {
      shouldSlideshowBePaused();
      preloadImageGroup(0); 
      initMouseWheel();
      initMouseDrag();
      slider_div = document.getElementById('slider_div');
      slider_counter_div = document.getElementById('slider_counter_div'); 
      slider_div.onmousedown = function () { dragstart(this); };
      slider_div.style.cursor = 'hand';
      scrollbar_div = document.getElementById('scrollbar_div');
      scrollbar_div.onclick = quickSlide;
      scrollbar_width = server_scrollbar_width;
      xstep = Math.floor((scrollbar_width-30)/imgs.length);
      
      // BLOCK OFF DIV
      var shortfall = scrollbar_width - (xstep*imgs.length) - 30;
      scrollbar_div.style.width = (scrollbar_width-shortfall)+'px';
      server_scrollbar_width = (scrollbar_width-shortfall);
      scrollbar_width = (scrollbar_width-shortfall);
      
      document.getElementById('loading_images_div').style.display = 'none';
      document.getElementById('property_images_div').style.display = '';
      // CALCULATE PAN TO
      doPan(Math.floor((server_page-1)/server_thumbnails_to_display),true);
      loadImage(server_page-1,true);
   }
}

function adjustImageSummary()
{
   if(document.getElementById('main_image_name') && document.getElementById('main_image_name_container'))
   {
      var height = imgs[img_index][4];
      P7_Snap('main_image','main_image_name_container',-6,height-59);
      P7_Snap('main_image','main_image_name',-6,height-59);
   }
}


