var ns = (navigator.appName.indexOf("Netscape") != -1);
var d = document;
var totalFadeTime = 1600;
var totalPauseTime = 4000;
var scrollHeightTrigger = 500;
var lastScrollY = 0;
var thisScrollY = 0;
var showing_message = false;
var property_message_shown = false;
var anchor_points = [];
var anchors_shown = [];
var scroll_message_div;
var anchor_messages = ['<table style="margin-top:10px;"><tr><td width="40"><img src="/images/messages/down_arrow.png" border="0"></td><td class="scroll_message_text">Photos, descriptions and frequently asked questions about this property can be found below</td></tr></table>',
                       '<table style="margin-top:10px;"><tr><td width="40"><img src="/images/messages/down_arrow.png" border="0"></td><td class="scroll_message_text">Location information, including map, holiday recommendations and nearby amenities, is below</td></tr></table>',
                       '<table style="margin-top:10px;"><tr><td width="40"><img src="/images/messages/down_arrow.png" border="0"></td><td class="scroll_message_text">Interested in this property? Check availability and prices for your dates and book online</td></tr></table>',
                       '<table style="margin-top:10px;"><tr><td width="40"><img src="/images/messages/pp_info_8.png" border="0"></td><td class="scroll_message_text">To check availability and price, click your check-in day on the calendar, then your check-out day</td></tr></table>',
                       '<table style="margin-top:10px;"><tr><td width="40"><img src="/images/messages/pp_info_8.png" border="0"></td><td class="scroll_message_text">Not the right property for you? Use the recommendations to find your perfect holiday rental</td></tr></table>'
                       ];

function initPropertyPage1()
{
   // PROPERTY IMAGES
   loadPropertyImages();
   
   // PRELOAD FLAT IMAGES
   preloadFlatImages();
   
   // MAP
   load();
   
   // STREET VIEW
   loadStreetViewIfAvailable();
   
   // SCROLL MESSAGE
   JSFX_FloatDiv('scroll_message', -310,-110).floatIt();
   
   // SCROLL POINTS
   if(anchor_points.length < anchor_messages.length)
   {
      for(var i = 1; i <= anchor_messages.length; i++)
      {
         anchor_points[i-1] = findPosY(document.getElementById('anchor_point_'+i));
      }
   }   
   
   // SCROLL MESSAGE FORMATTING
   scroll_message_div = document.getElementById('scroll_message');   
   MsgSetOpac('scroll_message',0);
   scroll_message_div.style.display = 'block';     
   
   // PROPERTY AND SCROLL MESSAGE
   shouldMessageBeTriggered();
}

function preloadFlatImages()
{
   var prefix = 'preload_property_image_';
   var area_prefix = 'preload_area_image_';
   for(var i = 0; i < 4; i++)
   {
      var obj = document.getElementById(prefix+''+i);
      if(obj)
      {
         obj.src = obj.longDesc;
         obj.style.width = '364px';
	  }
      var area_obj = document.getElementById(area_prefix+''+i);
      if(area_obj)
      {
         area_obj.src = area_obj.longDesc;
         area_obj.style.width = '260px';
	  }	  
   }
}

function MsgFadeIn($WhichDiv) 
{
   MsgSetOpac($WhichDiv,0);
   for(var i = 0 ; i <= 100 ; i++) 
   {
      (function(opacity)
      {
         setTimeout(function() { MsgSetOpac($WhichDiv,opacity); } , (totalFadeTime/100) * i );
	  })(i);
   }
}

function MsgFadeOut($WhichDiv) 
{
   for( var i = 0 ; i <= 100 ; i++ ) 
   {
      (function(opacity)
      {
         setTimeout(function() { MsgSetOpac($WhichDiv,(100 - opacity)); } , (totalFadeTime/100) * i );
	  })(i);
   }
}

function MsgSetOpac( $WhichDiv, value ) {
   document.getElementById($WhichDiv).style.opacity = value / 100;
   document.getElementById($WhichDiv).style.filter = 'alpha(opacity=' + value + ')';
}


function showPropertyMessage()
{
    wm = document.getElementById('watermark');
	MsgSetOpac('watermark',0);
	wm.style.display = 'block';
	wm.style.top = (thisScrollY+10)+'px';
	wm.style.right = '10px';
    MsgFadeIn('watermark'); 	
    setTimeout(function() { MsgFadeOut('watermark'); },totalFadeTime+totalPauseTime);
}

function in_array(v,a)
{
   for(var i = 0; i < a.length; i++)
   {
      if(a[i] == v)
      {
      	  return true;
	  }
   }	
   return false;
}

function shouldMessageBeTriggered()
{
   thisScrollY = getScrollXY()[1];
   /*
   if(!showing_message)
   {
	   for(var i = anchor_points.length-1; i >= 0; i--)
	   {
	      if(thisScrollY >= anchor_points[i]-50 && thisScrollY >= anchor_points[i]+200 && thisScrollY == lastScrollY && !in_array(i,anchors_shown))
	      {
   			  anchors_shown[anchors_shown.length] = i;
   			  scroll_message_div.innerHTML = anchor_messages[i];
			  showing_message = true;
   			  MsgFadeIn('scroll_message'); 	
   			  setTimeout(function() { MsgFadeOut('scroll_message'); },totalFadeTime+totalPauseTime); 
   			  setTimeout(function() { showing_message=false; }, (totalFadeTime*2)+totalPauseTime);
	          break;
		  }    
	   }
   }
   */
   if(!property_message_shown && !showing_message && thisScrollY >= scrollHeightTrigger && thisScrollY == lastScrollY && document.getElementById('watermark')) 
   {
   	   showPropertyMessage();	
   	   property_message_shown = true;
   }
   lastScrollY = thisScrollY;
   setTimeout(shouldMessageBeTriggered,2500);
}

function JSFX_FloatDiv(id, sx, sy)
{
    if(!showing_message) 
    {
       setTimeout(function() { scroll_message_obj.floatIt(); }, 40); 
	}
	var el=d.getElementById?d.getElementById(id):d.all?d.all[id]:d.layers[id];
	var px = document.layers ? "" : "px";
	window[id + "_obj"] = el;
	if(d.layers)el.style=el;
	el.cx = el.sx = sx;el.cy = el.sy = sy;
	el.sP=function(x,y){this.style.left=x+px;this.style.top=y+px;};

	el.floatIt=function()
	{
		var pX, pY;
		pX = (this.sx >= 0) ? 0 : ns ? innerWidth : 
		document.documentElement && document.documentElement.clientWidth ? 
		document.documentElement.clientWidth : document.body.clientWidth;
		pY = ns ? pageYOffset : document.documentElement && document.documentElement.scrollTop ? 
		document.documentElement.scrollTop : document.body.scrollTop;
		if(this.sy<0) 
		pY += ns ? innerHeight : document.documentElement && document.documentElement.clientHeight ? 
		document.documentElement.clientHeight : document.body.clientHeight;
		this.cx += (pX + this.sx - this.cx)/8;this.cy += (pY + this.sy - this.cy)/8;
		this.sP(this.cx, this.cy);
		setTimeout(function() { scroll_message_obj.floatIt(); }, 40);
	}
	return el;
}

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 ];
}
