// #############################################################################
// VARIABLES
// #############################################################################

var ActiveRegionID = 99; // This variable determines which region is highlighted - 99 means no region


// TEMP: this will be drawn from the db in future
var RegionsArray = new Array();
RegionsArray[0] = "Costa Almeria";
RegionsArray[1] = "Costa Calida";
RegionsArray[2] = "Mallorca";
RegionsArray[3] = "Costa Blanca";
RegionsArray[4] = "Algarve";
RegionsArray[5] = "Silver Coast";

// This is the blurb that goes in the transparent box
//var RegionBlurb = "Some text about <strong>" + RegionsArray[ActiveRegionID] + "</strong>";
//var defaultBlurb = "Welcome to Parador Properties";

// #############################################################################
// FUNCTIONS
// #############################################################################

//----------------------------------------------------------------------------------------------
function showLatestProperties()
{
/*
Slides out the "Latest Properties" box on selection of a country or region
on the front page. The function hides the section's blurb, slides out the box and
then pulls properties in from the database based on SectionID
*/

  // Slide the box out, which we achieve by increasing the width of the div
  $("#region_prop_block").animate({ width: "250px" }, 400, function(){

    // And now we're ready to pull the properties in from the database
    getLatestPropertyData();
    
  });
  
  // Change the handle from "open" to "close"
  $("#prop_block_handle").removeClass("prop_block_handle");
  $("#prop_block_handle").addClass("prop_block_handle_close");
  
  // If the relevant menu button isn't highlighted, highlight it now
  if (ActiveRegionID != 99) {
    if ($("#menu_item_region" + (ActiveRegionID + 1) ).css("margin-left") == "10px"  )
    {
      $("#menu_item_region" + (ActiveRegionID + 1) ).addClass("regions_menu_selected"); // Sets the menu item
      $("#menu_item_region" + (ActiveRegionID + 1) ).animate({ marginLeft: "0px" }, 200); // Extend the region button to touch the photo
    }
  }
  
  
  
 
}

//----------------------------------------------------------------------------------------------
function hideLatestProperties()
{
/*
Hides the "Latest Properties" box. The function simply closes the div containing the thumbnails of
the properties, and slides the box back in.
*/
 
  // Hide the properties
  $("#region_properties").hide(1, function(){
  
    // Then slide the box in

    $("#region_prop_block").animate({ width: "0px" }, 400);
    $("#prop_block_handle").removeClass("prop_block_handle_close");
    $("#prop_block_handle").addClass("prop_block_handle");
    $("#property_load_area_wait").html("&nbsp;");
    $("#property_load_area").hide();
              
  });

}

//----------------------------------------------------------------------------------------------
function loadPropertyThumbs()
{
/*
BETA: loads the property thumbs for the selected region
*/
if (ActiveRegionID == 99) {
  
  // No region selected, so tell the script to get the latest properties from all regions
  $("#property_load_area_wait").html("<img src='" + ImgPath + "loading.gif' alt='Loading...' />");
  $("#property_load_area").fadeOut("200", function(){
    $("#property_load_area").load( "/properties/latestfeed/noregion", function(){
      $("#property_load_area_wait").html("&nbsp;");
      $("#property_load_area").fadeIn("200");
    });
  });
  
} else {

  $("#property_load_area_wait").html("<img src='" + ImgPath + "loading.gif' alt='Loading...' />");
  $("#property_load_area").fadeOut("200", function(){
    $("#property_load_area").load( "/properties/latestfeed/" + str_replace(" ", "+", RegionsArray[ActiveRegionID]), function(){
      $("#property_load_area_wait").html("&nbsp;");
      $("#property_load_area").fadeIn("200");
    });
  });

} 

}

//----------------------------------------------------------------------------------------------
function getLatestPropertyData()
{
/*
Pulls in the latest properties for the selected region and populates the "Latest Properties"
box with thumbnails
*/

  $("#region_properties").show(); // this is the inner div where the property data is actually contained
  loadPropertyThumbs();

}

//----------------------------------------------------------------------------------------------
function toggleLatestProperties()
{
/*
Checks if the "Latest Properties" box is open already and opens or closes as 
appropriate.
*/
  if ( !checkLatestPropertiesOpen() ) 
  {
    showLatestProperties();
  }
  else
  {
    hideLatestProperties();
  }
  
}

//----------------------------------------------------------------------------------------------
function toggleSelectedRegion()
{
/*
Activated when a region or country is highlighted in the right hand menu
*/

 
  // Since ActiveRegion is an array, we need to increment its index by 1 to get the integer
  // needed for selecting region images etc (there is no region0.jpg!)
  var regionRef = ActiveRegionID + 1; 
 
  // Remove the selected class from any other menu item, and remove the photo
  $(".regions_menu_item").each( 
    function(intIndex)
    {
      $(this).removeClass("regions_menu_selected");
      $("#region_photo_block").removeClass("region_" + (intIndex + 1));
      
      // If the menu button is extended, let's animate it back into place
      if ($(this).css("margin-left") == "0px") {
        $(this).animate({ marginLeft: "10px" }, 200);
      } 
        

    }
  );
  
  $("#region_photo_block").addClass("region_" + regionRef); // Adds the right photo
  $("#menu_item_region" + regionRef).addClass("regions_menu_selected"); // Sets the menu item

  $("#menu_item_region" + regionRef).animate({ marginLeft: "0px" }, 200); // Extend the region button to touch the photo
  
  // Open the "Latest Properties" box if it isn't already open
  if ( !checkLatestPropertiesOpen() )
  {
    // Slide out the box and display
    //showLatestProperties();
  }
  else
  {
  
    // Just change the properties in the opened box
    getLatestPropertyData();
  }
  
  

}

//----------------------------------------------------------------------------------------------
function checkLatestPropertiesOpen()
{
/* 
A simple function to check if the Latest Properties box is open. Returns true if so, false if not
*/

  if ($("#region_properties").css("display") != "none" ) { return true; } else { return false; }

}

//----------------------------------------------------------------------------------------------
function closeAllRegions()
{
/*
Closes the properties box and resets all menu items to non-selected state. Triggered when
the user has moved on from this part of the screen
*/

  // If the "Latest Properties" box is open, close it 
  if ( checkLatestPropertiesOpen() ) { hideLatestProperties(); }
    
  // Remove the class being displayed on both the photo and the menu item
  $(".regions_menu_item").each( 
    function(intIndex)
    {
      $(this).removeClass("regions_menu_selected");
      $("#region_photo_block").removeClass("region_" + (intIndex + 1) );
      
      // If the menu button is extended, let's animate it back into place
      $(this).animate({ marginLeft: "10px" }, 200);
    }

  ); 
  
  // Add the photo back in - the last region selected
  $("#region_photo_block").addClass("region_default"); 
  $(".blurb").each(function(){ $(this).hide(); });
  $("#blurb_default").show();
  ActiveRegionID = 99;
    
}

// #############################################################################
// PAGE READY / ON LAUNCH
// #############################################################################

$(document).ready(function() {

//----------------------------------------------------------------------------------------------
/* Set HoverIntent configuration */


//----------------------------------------------------------------------------------------------
/* Region selection buttons */

  // hoverIntent config for this
  var RegionMenuConfig = {    
     sensitivity: 3, // number = sensitivity threshold (must be 1 or higher)    
     interval: 100, // number = milliseconds for onMouseOver polling interval    
     over: RegionMenuOver, // function = onMouseOver callback (REQUIRED)    
     timeout: 50, // number = milliseconds delay before onMouseOut    
     out: RegionMenuOut // function = onMouseOut callback (REQUIRED)    
  };
  
  function RegionMenuOver()
  {
    // Mouseover function for the region menu items
        
    // First we need to get the ID to pass to to ActiveRegionID
    var thisID = $(this).attr("id");
    
    var string_length = thisID.length;
    var last_char_1 = thisID.charAt(string_length-1); // The last number
    var last_char_2 = thisID.charAt(string_length-2); // The second last number
  
    // Now check to see if both are numbers or not
    if ( isNaN(last_char_2) )
    {
      // The second-last character is not a number, so ignore it
      selectedID = last_char_1 - 1;
    } 
    else
    {
      selectedID = (last_char_2 + last_char_1) - 1;
    }
    
    // Hide the blurb for the active region
    $(".blurb").each(function(){ $(this).hide(); });
    
    ActiveRegionID = selectedID; // Set the selected region ID
     
    // Toggle the correct blurb
    if ( $("#blurb_region" + (selectedID + 1)).css("display") == "none" ) {
      // Blurb not active - activate it
      $("#blurb_region" + (selectedID + 1)).show();
    }
    
    if ( $(this).css("margin-left") == "10px") { 
    
      toggleSelectedRegion(); 
    }  
  }

  function RegionMenuOut()
  {
    // Mouseout function for the region menu - blank for now
  }

  $(".regions_menu_item").each( function(intIndex){
    
    // Mouseover - opens the Latest Properties box
    $(this).hoverIntent(RegionMenuConfig);
    
    // Click - takes you to the property search page for the region/country  
    $(this).click(function(){ 
        window.location = "/properties/search/" + str_replace(" ", "+", RegionsArray[intIndex]);
    });
    
  });

//----------------------------------------------------------------------------------------------
/* Handle for the Latest Properties box */
		
  $("#prop_block_handle").click(function(){ 
    toggleLatestProperties();
  });



//----------------------------------------------------------------------------------------------
/* Areas that de-activate the Latest Properties region selection on mouseover */
    
  $(".search_box_main").mouseover( function(){ 
    setTimeout("closeAllRegions()", 500); 
  }); // the search box area
    
  $("#index_row_2").mouseover( function(){ 
    setTimeout("closeAllRegions()", 500);
  }); // the second row on the page, where the POTM etc is

//----------------------------------------------------------------------------------------------
/* Property of the Month functions */
    
  $("#POTM_header img").click( function(){
    
    // This slides the POTM box up or down depending on current state  
    if ( $("#POTM_content").css("display") != "none" )
    {
      $("#POTM_content").slideUp(300);
      $('img#POTM_header_arrow').attr("src", ImgPath + "icon_arrow_small_down.gif");
      $('img#POTM_header_arrow').attr("alt","Click to roll down");
    } 
    else
    {
      $("#POTM_content").slideDown(300);
      $('img#POTM_header_arrow').attr("src", ImgPath + "icon_arrow_small_up.gif");
      $('img#POTM_header_arrow').attr("alt","Click to roll up");
    }
    
  });

//----------------------------------------------------------------------------------------------
/* Opens a modal window with property details  */
  $(".properties_thumb").each( function(){ 
    
    $(this).click( 
      function(){
        $("#thumbs_detail").html("Some stuff");
        alert("HERE I AM");
        $("#thumbs_detail").jqm({ 
                      overlay: 30,
                      onShow: function(h){
                        h.w.fadeIn(200);
                      },
                      onHide: function(h){
                      h.w.fadeOut(200, function(){ 
                        if(h.o) { h.o.remove(); }
                      });
                      } 
        });
        $("#thumbs_detail").jqmShow(); 
   
      }
    );
  });
    
// #############################################################################
// CLOSE 
// #############################################################################   
});

// End of File

