// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

// fix problematic layout elements when user resizes the browser window (ie6 & ie7 but not ie8)
function fix_resize() {
  $('header', 'container', 'footer').each(function(e) { e.disabled = true; e.disabled = false; });
}

function redirect_to_new_browse_url_on_select(select) {
  window.location = select.options[select.selectedIndex].value;
}

function redirect_to_new_agent_directory_url_on_select(select) {
  window.location = select.options[select.selectedIndex].value;
}

function redirect_to_new_shortlist_sort_url_on_select(select, properties_per_page) {
  window.location = '/shortlists?sort_order=' + select.options[select.selectedIndex].value + '&properties_per_page=' + properties_per_page;
}

function redirect_to_new_shortlist_properties_per_page_url_on_select(select, sort_order) {
  window.location = '/shortlists?sort_order=' + sort_order + '&properties_per_page=' + select.options[select.selectedIndex].value;
}

function MM_openBrWindow(theURL,winName,features) { //v2.0
  window.open(theURL,winName,features);
}

function admin_translations_change_lang(view) {
  window.location = "/admin/translations?view=" + view + "&lang=" + $('lang').options[$('lang').selectedIndex].value;
}

function show_full_property_description(hide_word, show_word) {
  Element.show('hidden_description')
  Element.update('description_toggle', '&nbsp;<a href="#" onclick="hide_full_property_description(\'' + hide_word + '\', \'' + show_word + '\'); return false">' + hide_word + '</a>');
}

function hide_full_property_description(hide_word, show_word) {
  Element.hide('hidden_description');
  Element.update('description_toggle', '&nbsp;...&nbsp;<a href="#" onclick="show_full_property_description(\'' + hide_word + '\', \'' + show_word + '\'); return false">' + show_word + '</a>');
}

function show_navigation() {
  if (user['type'] == 'None' || user['type'] == 'Visitor') {
    $('user_account').show();
    $('my_shortlist').show();
    $('property_alerts').show();
  } else if (user['type'] == 'Customer' || user['type'] == 'Administrator') {
    $('user_name').show();
    $('user_name').innerHTML = '<em>' + user['user_name'] + '</em>';
    $('logout_button').show();
    $('my_shortlist').show();
    $('property_alerts').show();
    $('my_details').show();
    $('help').show();
  } else if (user['type'] == 'Translator') {
    $('user_name').show();
    $('logout_button').show();
    $('user_name').innerHTML = '<em>' + user['user_name'] + '</em>';
    $('translations').show();
  } else if (user['type'] == 'Agent' || user['type'] == 'FreeAgent')  {
    $('user_name').show();
    $('logout_button').show();
    $('user_name').innerHTML = '<em>' + user['user_name'] + '</em>';
    $('agent_account').show();
  }
  $('user_navigation').show();
}

function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

function property_is_unavailable() { 
 	var cookie_contents = readCookie('flash');
	return (cookie_contents != null);
}

function page_has_properties() { 
	return $('no-properties') == undefined; 
}

function show_tool_tip(msg) {
	var tip = '<script type="text/javascript">new Tip("similar_properties_tip", "' + msg + '")</script>';
	$('similar_properties_tip').insert({ after : tip });
}

function show_property_unavailable_message_and_erase_cookie_afterwards() {
  var cookie_contents = readCookie('flash');
  if (cookie_contents == null) return;
  
	var message = unescape(cookie_contents.replace(/\+/g, ' '));
	if (property_is_unavailable()) {
		var flash_message = '<div class="flash warning" id="property_unavailable"><p>' + message + '</p></div>';
		if (page_has_properties()) {
			$('sort_opts').insert({ before : flash_message });
		}
		else {
			$('no-properties').insert({ before : flash_message });
		}
	}
	eraseCookie('flash');
}

function fill_user_feedback() {
	if (user['type'] != 'None') {
		$('feedback_name').value = user['user_name']
		$('feedback_email').value = user['email']
	}
}

function spinner(spinner_id) {
  return "<img src='http://images0.kyero.com/images/new/spinner.gif' id='spin-" + spinner_id + "' style='display: none' class='spin' />"
}

function add_to_shortlist(property_id) {
  new Ajax.Request('/search/add_favorite/' + property_id,
    { asynchronous:true, 
      evalScripts:true,
      onFailure:function(request) {Element.hide('spin-' + property_id);Element.show('fav_icon_' + property_id)},
      onLoading:function(request) {Element.show('spin-' + property_id);Element.hide('fav_icon_' + property_id)}
    }
  );
}

function add_to_shortlist_link(property_id) {
  link = spinner(property_id) + "<a id='add_to_shortlist_link_" + property_id + "' href='#' onclick='add_to_shortlist(" + property_id + "); return false'>" +
  "<img alt='" + translate['Add to shortlist'] + "' height='14' id='fav_icon_" + property_id + "' src='http://images2.kyero.com/images/final/add.gif' width='14' />" +
  translate['Save to shortlist'] + "</a>"
  new Insertion.Top($('favorite_' + property_id), link)
}

function remove_from_shortlist(property_id) {
  new Ajax.Request('/search/remove_favorite/' + property_id,
    { asynchronous:true,
      evalScripts:true,
      onFailure:function(request) {Element.hide('spin-' + property_id);Element.show('fav_icon_' + property_id)},
      onLoading:function(request) {Element.show('spin-' + property_id);Element.hide('fav_icon_' + property_id)}
    }
  )
}

function remove_from_shortlist_link(property_id, fade) {
  if (fade == true) {
    remove_method = "remove_from_shortlist_and_fade("+ property_id + ")"
  } else {
    remove_method = "remove_from_shortlist("+ property_id + ")"
  }
  link = spinner(property_id) + 
  "<img alt='" + translate['Shortlisted'] + "' height='14' id='fav_icon_" + property_id + "' src='http://images1.kyero.com/images/final/tick.gif' width='14' />" +
  translate['Saved in shortlist'] + 
  "<small><a id='remove_from_shortlist_link_" + property_id + "' href='#' onclick='" + remove_method + "'>" + translate['undo'] + "</a></small>"
  new Insertion.Top($('favorite_' + property_id), link)
}

function remove_from_shortlist_and_fade(property_id) {
  new Ajax.Request('/search/remove_favorite/' + property_id,
    { asynchronous:true,
      evalScripts:true,
      onFailure:function(request)  {Element.hide('spin-' + property_id);Element.show('fav_icon_' + property_id)},
      onLoading:function(request)  {Element.show('spin-' + property_id);Element.hide('fav_icon_' + property_id)},
      onComplete:function(request) {new Effect.BlindUp('property_box_' + property_id)}
    }
  )
}

function add_shortlist_links(property_ids, fade) {
  for (var i=0; i < property_ids.length; i++) {
    if (user['shortlist'].include(property_ids[i])) {
      remove_from_shortlist_link(property_ids[i], fade)
    } else {
      add_to_shortlist_link(property_ids[i])
    }
  };
}

function clickclear(thisfield, defaulttext, color) {
  if (thisfield.value == defaulttext) {
    thisfield.value = "";
  }
}

function clickrecall(thisfield, defaulttext, color) {
  if (thisfield.value == "") {
    thisfield.value = defaulttext;
  }
}

function clickreset(field, defaulttext) {
  if (field.value == defaulttext) {
    field.value = '';
  }
}

// 
// Methods for the location drill-down in the searchbar
// 

// the onclick method for select boxes
function submit_location_select(payment_scheme) {
  var location_id = $('location_select').options[$('location_select').selectedIndex].value;
  if (isNaN(location_id)) return;
  update_location_select(location_id, $('location_select_wrap'), payment_scheme)
}

// the ajax call used by select onclick and a links
function update_location_select(location_id, replaced_element, payment_scheme) {
  new Ajax.Updater('location_selector', '/browse/location_selector/' + location_id + '?payment_scheme=' + payment_scheme, {
    asynchronous:true, 
    evalScripts:true,
    method: 'get',
    onLoading:function() {
      replaced_element.replace(spinner('location'));
      Element.show('spin-location');
    },
    onComplete:function() {
      new Effect.Highlight('location_selector', {queue: 'end'});
    }
  });
}

function update_location_prices(select) {
	new Ajax.Updater('location_prices', '/agent_directory/location_prices/' + select.options[select.selectedIndex].value, {
	    asynchronous:true, 
	    evalScripts:true,
	    method: 'get',
	    onLoading:function() {
	      replaced_element.replace(spinner('location'));
	      Element.show('spin-location');
	    },
	    onComplete:function() {
	      new Effect.Highlight('location_prices', {queue: 'end'});
	    }
	  });
}

function display_feedback_sent_message() {
	if (window.location.href.match('#feedback_sent')) {
		msg = '<div class="flash notice"><p>Thank you for your feedback</p></div>';
		Element.insert($('content'), { top: msg });
	}
}


// the property pulse banner
function show_property_pulse() {
  if(readCookie('hide_property_pulse') != 'true') {
    Effect.SlideDown('property_pulse_banner', 'fast');
    window.setTimeout(function() { Effect.Appear('property_pulse_content'); }, 500);
  }
}

function hide_property_pulse() {
  createCookie('hide_property_pulse', 'true', 7);
  Effect.Fade('property_pulse_content', { duration:0.5 });
  window.setTimeout(function() { Effect.SlideUp('property_pulse_banner', 'fast'); }, 500);
  return false;
}