(function($){
  
  if (bIsMobileVisitor == false)
  {
    //SET CLICK BOX DIMENSIONS ON WINDOW LOAD SO ALL IMAGES HAVE FULLY LOADED
    //AND WE KNOW THE FINAL DIMENSIONS OF THE PAGE
    var $popupClickBox;
    $popupClickBox = $("<div id='popupClickBox' style='top: 0; left: 0; position: absolute; z-index: 999; display: none;'></div>"); 
    $(window).bind("load", function()
    {
      //GET BODY DIMENSIONS INCLUDING BORDER, PADDING, AND MARGIN
      $popupClickBox.width($("body").outerWidth(true));
      $popupClickBox.height($("body").outerHeight(true));
    });
    
    $(document).ready(function()
    {   
      $popupClickBox.click(function()
      {
        $(".popupDiv").ibeHide();
        $(this).hide();
      });
      
      $("body").append($popupClickBox);
      
	    $(".popupLink").click(
		    function(){
			    //HIDE ANY EXISTING OPEN POPUPS
			    $(".popupDiv").ibeHide();
  			  
			    $popupClickBox.show()
  			  
			    //SHOW CLICKED POPUP
			    $popupDiv = $(this).next();
			    $popupDiv.ibeShow();
  			  
			    ScrollToPopup($popupDiv)
  			  
			    //IF ROOM POPUP THEN LET THE THE GALLERY KNOW IT'S SHOWN
			    if ($popupDiv.hasClass("hasPhotoGallery"))
			    {
			      ActivatePhotoGallery($popupDiv);
			    }
		    }
	    );

      //CLOSE THE POPUP WHEN CLOSE LINK IS CLICKED
	    $(".popupDiv .closePopup").click(
		    function(){
			    $(this).parents(".popupDiv:first").ibeHide();
			    $popupClickBox.hide();
			    return false;
		    }
	    );
  	  
	    //MAKE THE LINKS ALONG THE BOTTOM OF THE ROOM POPUP SCROLL TO THE PROPER
	    //SECTIONS WITHIN THE POPUP WITHOUT SCROLLING THE WHOLE WINDOW
	    $("a.resultPopupRatePolicyLink").click(function()
	    {
	      $(this).parents(".resultpopupShell:first").find(".resultinfoShell").scrollTo(".resultPopupRatePolicy");
	      return false;
	    });
	    $("a.resultPopupTermsLink").click(function()
	    {
	      $(this).parents(".resultpopupShell:first").find(".resultinfoShell").scrollTo(".resultPopupTerms");
	      return false;
	    });
  	  
	    
	  });
	}
	
	//SCROLL THE WINDOW DOWN IF THE POPUP CAN'T BE FULLY SEEN
  function ScrollToPopup($popup)
  {	    
    var windowBottom = $(window).scrollTop() + $(window).height();
    var popupTop = $popup.offset().top;
    var popupBottom = popupTop + $popup.outerHeight(true);
    if (popupBottom > windowBottom)
    {
      var scrollTo = (popupBottom-windowBottom + 30);
      $.scrollTo("+="+parseInt(scrollTo, 10)+"px", 800, {axis:'y'});
    }
  }
	
	function ActivatePhotoGallery($shell)
  {
    var $list = $(".resultpopupRoomPhotosList", $shell);
    var $listItems = $("li", $list);
    var listItemsCount = $listItems.length; //HIDE ITEMS
    var $prev = $(".resultpopupRoomPhotoPrev", $shell);
    var $next = $(".resultpopupRoomPhotoNext", $shell);
    var $count = $(".resultpopupRoomPhotoCount", $shell);	
    var of = $("#ctl00_hidOf").val();
    
    //IF THE COUNT TEXT HAS ALREADY BEEN FILLED THEN THE GALLERY
    //HAS ALREADY BEEN ACTIVATED SO DON'T DO IT AGAIN
    if ($count.text() == "")
    {
      var $firstImageLI = $listItems.eq(0);
      $firstImageLI.ibeShow();
      
      $count.text("1 "+of+" "+listItemsCount);
    
      $prev.click(function()
      {
        MoveToImage(-1);
        return false;
      });
      $next.click(function()
      {
        MoveToImage(1);
        return false;
      });
    }
    
    function MoveToImage(iDirection)
    {
      //currentImage IS REALLY THE LI
      var $currentImageLI = $list.children(":visible");
      var iCurrentIndex = $listItems.index($currentImageLI);
      var iNewIndex = iCurrentIndex + iDirection;
      
      //IF THEY CAN GO THE DIRECTION THEY WANT
      if (iNewIndex >= 0 && iNewIndex < listItemsCount)
      {
        $currentImageLI.ibeHide();	        
        $currentImageLI = $listItems.eq(iNewIndex)
        $currentImageLI.ibeShow();
        $count.text((iNewIndex+1)+" "+of+" "+listItemsCount);
      }
    }
    
    function PositionImage($imageLI)
    {
      //ONLY NEED TO CENTER VERTICALLY SINCE IMAGE IS CENTERED HORIZONTALLY THROUGH CSS FINE
      var $image = $imageLI.find("img");
      //var liWidth = $imageLI.width();
      var liHeight = $imageLI.height();
      //var imageWidth = $image.width();
      var imageHeight = $image.height();
      //var imageMarginLeft = Math.floor(((liWidth - imageWidth) * .5));
      var imageMarginTop = Math.floor(((liHeight - imageHeight) * .5));
      //$image.css("margin-left", imageMarginLeft);
      $image.css("margin-top", imageMarginTop);	      
    }	    
	}
})(jQueryIBE);

