$(document).ready(function() {

	if($("#location_all")) {
		$("#location_all").click(function()
		{
      var locs_array = $('#locations_checkboxes input');
			var all_status = this.checked;
			for(var i = 0; i < locs_array.length; i++)
			{
        if (locs_array[i].id != 'location_all')
        {
          locs_array[i].checked = all_status;
        }
			}
		});
	}

	if($("#types_all")) {
		$("#types_all").click(function()
		{
      var types_array = $('#types_checkboxes input');
			var all_status = this.checked;
			for(var i = 0; i < types_array.length; i++)
			{
        if (types_array[i].id != 'types_all')
        {
          types_array[i].checked = all_status;
        }
			}
		});
	}

  // replace any found labels
  js_input_labels();
  
  // stop 0 submits on the find a branch form
  if ($('#find_a_branch_form').length > 0)
  {
    $('#find_a_branch_form').submit(function()
    {
      if ($('#find_a_branch_form select')[0].value == 0)
      {
        return false;
      }
    });
  }
  
  // stop navs from breaking
  $('#services_menu').hover(function() {
    $('#services_link a.menu_hover').addClass('js_on_nav');
  }, function() {
    $('#services_link a.menu_hover').removeClass('js_on_nav');
  });
  $('#contact_menu').hover(function() {
    $('#contact_link a.menu_hover').addClass('js_on_nav');
  }, function() {
    $('#contact_link a.menu_hover').removeClass('js_on_nav');
  });
  
  // try and get IE6 menus working
  $('#services_link').hover(function() {
    $('#services_link').addClass('nav_over');
  }, function() {
    $('#services_link').removeClass('nav_over');
  });
  $('#contact_link').hover(function() {
    $('#contact_link').addClass('nav_over');
  }, function() {
    $('#contact_link').removeClass('nav_over');
  });
  
  // Make search results breadcrumb bar link work
  $('#search_results_link').click(function() {
    $('#refine_search_results').submit();
    return false;
  });
  
  // set up send to a friend links
  if ($('#send_to_a_friend_link').length > 0)
  {
    $('#send_to_a_friend_link').click(function() {
      // check for previous entries
      var current = $('#accordion').accordion('option', 'current_header');
      if (current && current[0] && current[0].id == 'send_to_a_friend')
        return redirect_to_send_to_friend();
      
      // set up the open
      $('#accordion').bind('accordionchange', redirect_to_send_to_friend);
      $('#accordion').accordion('activate', '#send_to_a_friend');
      return false;
    });
    $('#accordion').bind('accordionchange', function (event, ui) {
      $('#accordion').accordion('option', 'current_header', ui.newHeader);
    });
  }
  
  // set up main description link
  if ($('#main_description_link').length > 0)
  {
    $('#main_description_link').click(function() {
      // check for previous entries
      var current = $('#accordion').accordion('option', 'current_header');
      if (current && current[0] && current[0].id == 'main_description_h3')
        return redirect_to_main_description();
      
      // set up the open
      $('#accordion').bind('accordionchange', redirect_to_main_description);
      $('#accordion').accordion('activate', '#main_description_h3');
      return false;
    });
  }
  
  // set up onchange events for the pagers
  if ($('#pager_form_pagTop .top_pager_row select').length > 0)
  {
    $('#pager_form_pagTop .top_pager_row select').change(function() {
      var form = find_parent_form(this);
      form.submit();
    });
  }
  
  // set up onchange events for the pagers
  if ($('#pager_form_pagFoot .top_pager_row select').length > 0)
  {
    $('#pager_form_pagFoot .top_pager_row select').change(function() {
      var form = find_parent_form(this);
      form.submit();
    });
  }
  
  // set up onchange events for the pagers
  if ($('#pager_form_pagTop input:checkbox').length > 0)
  {
    $('#pager_form_pagTop input:checkbox').click(function() {
      var form = find_parent_form(this);
      form.submit();
    });
  }
  
  // set up onchange events for the pagers
  if ($('#pager_form_pagFoot input:checkbox').length > 0)
  {
    $('#pager_form_pagFoot input:checkbox').click(function() {
      var form = find_parent_form(this);
      form.submit();
    });
  }
  
  // set up the click for house on search
  if ($('#search_types_DETA').length > 0)
  {
    $('#search_types_DETA').click(function() {
      $('#search .hidden_fields').css('display', this.checked ? 'block' : 'none');
      if (this.checked)
        $('#search .hidden_fields input').attr('checked', 'checked');
      else
        $('#search .hidden_fields input').removeAttr('checked');
    });
  }
  
  // set up the popups for the list page
  var is_fully_out = true;
  if ($('#search_list .thumb').length > 0)
  {
    $('#large_thumb_image').mouseover(function() {
      is_fully_out = false;
    }).mouseout(function() {
      is_fully_out = true;
      $('#large_thumb_main').css('display', 'none');
    });  
  
    $('#search_list .thumb').mouseover(function() {
      var pos = get_position($('#search_list'), $(this));
      // change the links
      $('#large_thumb_link').attr('href', $('#' + this.id + ' a')[0].href);
      
      // swap the image out and show it
      $('#large_thumb_image').attr('src', $(this).attr('rel'));
      $('#large_thumb_main').css('left', (Math.round(pos.left) + 133 - 150) + 'px').css('top', (Math.round(pos.top) + 3) + 'px').css('display', 'block');
    }).mouseout(function() {
      setTimeout(function() {
        if (is_fully_out)
        {
          $('#large_thumb_main').css('display', 'none');
        }
      }, 1);
    });
  }
  
  // set up the guildford clicks for the search engine
  if ($('#search_locations_Guildford').length > 0)
  {
    $('#search_locations_Guildford').click(function() {
      if ($(this).attr('checked'))
      {
        $('#search_locations_GuildfordBW').attr('checked', 'checked');
      }
    });
    $('#search_locations_GuildfordBW').click(function() {
      if ($(this).attr('checked'))
      {
        $('#search_locations_Guildford').attr('checked', 'checked');
      }
    });
  }
  
  
  // set up the guildford clicks for the search engine
  if ($('#sidebar_locs_Guildford').length > 0)
  {
    $('#sidebar_locs_Guildford').click(function() {
      if ($(this).attr('checked'))
      {
        $('#sidebar_locs_GuildfordBW').attr('checked', 'checked');
      }
    });
    $('#sidebar_locs_GuildfordBW').click(function() {
      if ($(this).attr('checked'))
      {
        $('#sidebar_locs_Guildford').attr('checked', 'checked');
      }
    });
  }
});

// gets the distance between one element and another
// ================================================================================================
function get_position(parent, child, pos)
{
  // init pos
  if (!pos) var pos = {'left': 0, 'top': 0};  

  // check to see if the parent == child
  if (parent[0] == child[0])
    return pos;
  
  // get the left and the top to add to the pos totals
  var position = child.position();
  pos.left += position.left;
  pos.top += position.top;
  return get_position(parent, child.parent(), pos);
}

// set up hiding the on market status
// ================================================================================================
function on_market_status()
{
  if ($('#register_on_market').length > 0)
  {
    $('#register_on_market').change(function() {
      $('#market_status_box').css('display', this.value == 'yes' ? 'block' : 'none');
      $(this).parent().toggleClass('radiotime');
    });
    
    var register = $('#register_on_market')[0];
    if (register.value == 'no')
    {
      $('#market_status_box').css('display', 'none');
      $(register).parent().toggleClass('radiotime');
    }
  }
}

// needed for redirect to send to a friend
// ================================================================================================
function redirect_to_send_to_friend()
{
  window.location.href = '#send_friend';
  $('#accordion').unbind('accordionchange', redirect_to_send_to_friend);
}

// needed for redirect to main description
// ================================================================================================
function redirect_to_main_description()
{
  window.location.href = '#description';
  $('#accordion').unbind('accordionchange', redirect_to_main_description);
}

// sets up the default labels for the required text fields
// ================================================================================================
function js_input_labels()
{
  set_default_text($('#contact_name'), 'Name');
  set_default_text($('#contact_email'), 'Email Address');
  set_default_text($('#contact_phone'), 'Phone Number');
  set_default_text($('#contact_enquiry'), 'Enquiry');
}

// sets up the actual text and events for the default labels
// ================================================================================================
function set_default_text(element, text)
{
  // capture errors
  if (element.length == 0) return;
  
  // set default value if needed
  element[0].value = !element[0].value ? text : element[0].value;
  
  // set up focus and blurs
  element.focus(function() {if (this.value == text) this.value = '';});
  element.blur(function() {if (this.value == '') this.value = text;});
}

/**
 *
 * @access public
 * @return void
 **/
function pager_page(element, loc, page, last_page)
{
  // find parent form node
  var parent = find_parent_form(element);
  var page_element = $('#' + parent.id + '_pg')[0];
  var page_num = page_element.value;
  if(loc == 'back')
  {
    if(page_num > 1)
    {
      page_num = Number(page_num) - 1;
    }
  }
  else if(loc == 'next')
  {
    if(page_num < last_page)
    {
      page_num = Number(page_num) + 1;
    }
  }
  else if(loc == 'jump')
  {
    page_num = Number(page);
  }
  
  // set the new page number and submit
  page_element.value = page_num;
  parent.submit();
}

// find the parent form for the pager item
function find_parent_form(element)
{
  if (element.parentNode.tagName.toLowerCase() == 'form')
    return element.parentNode;
  else if (element.parentNode.tagName.toLowerCase() == 'body')
    return false;
  else
    return find_parent_form(element.parentNode);
}

/**
 *
 * @access public
 * @return void
 **/
function load_div(action,id)
{
  var obj = $('#large_thumb_'+id);
  if(action == 'show')
  {
    obj.show();
  }
  else if(action == 'hide')
  {
    obj.hide();
  }
}

/**
 *
 * @access public
 * @return void
 **/
function property_get_dimensions(elements, set_width, set_height)
{
  // loop through the main image
  for (var i = 0; i < elements.length; i ++)
  {
    var height = elements[i].height;
    var width = elements[i].width;

    // change based on height
    if (set_height && height > set_height)
    {
      set_width = Math.round(set_height * (width / height));
    }
    else if (set_width && width > set_width)
    {
      set_height = Math.round(set_width * (height / width));
    }
    else
    {
      set_height = height;
      set_width = width;
    }
    elements[i].height = set_height;
    elements[i].width = set_width;
    elements[i].style.height = set_height + 'px';
    elements[i].style.width = set_width + 'px';
  }

  return elements;
}

function update_watch_property(var_1, var_2, var_3, var_4)
{
  alert(var_1);
  alert(var_2);
  alert(var_3);
  alert(var_4);
}