// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults


// rails auth token enabled in jquery
$(document).ajaxSend(function(event, request, settings) {
      if (settings.type == 'GET' || settings.type == 'get' || typeof(AUTH_TOKEN) == "undefined") return;
      settings.data = settings.data || "";
      settings.data += (settings.data ? "&amp;" : "") + "authenticity_token=" + encodeURIComponent(AUTH_TOKEN);
});
  
// add javascript request type
jQuery.ajaxSetup({
	'beforeSend': function(xhr) {xhr.setRequestHeader("Accept", "text/javascript")}
});

$(".signup").livequery('click',function(){
        $.get($(this).attr('href'),function(data){   
                $.blockUI({message: $("#modal_window").html(data), css: {width:"400px",
                margin:"-200px 0 0 -200px", left:"50%", padding: "4px 4px 4px 15px", textAlign: "left"}});
        });
        return false;
});

var parent_div_of_form;
$('.ajax_new_user').livequery('submit',function(){
        parent_div_of_form = $(this).parent();
        $.post($(this).attr("action"), $(this).serialize(), null, "script");
        return false;
});

$("#modal_window .close").livequery('click',function(){
        $.unblockUI();
        window.location.reload();
});

// tab toggling for events/reviews
$(document).ready(function(){
  $(".toggleNav > ul").tabs();
});

// tab toggling for schools
$(document).ready(function(){
  // initial school display state (only first highlighted, only first showing)
  $("#school_chart tbody tr:first").addClass('highlight');
  $('.propDetails_schoolsDetails').addClass('display_none');
  $('.propDetails_schoolsDetails:first').removeClass('display_none');
  // when a row is clicked, highlight that row and display corresponding school
  $("#school_chart tbody tr").click(function() {
  		$(this).addClass('highlight');
  		$(this).siblings().removeClass('highlight');
  		$('.propDetails_schoolsDetails').addClass('display_none');
  		$($('a[href]',this).attr('href')).removeClass('display_none');
  	})
  $("#school_chart tr").click(function() {
    return false;
  })
});

// eventful and yelp links open in new page
$(document).ready(function(){
  $("#happenings_events a, #happenings_reviews a").click(function() {
    window.open(this.href);
    return false;	
  })
});


// extract the form errors from json into an unordered list
function error_messages(response_text){
   var json = eval(response_text);
   var error_text = "";
   var len = json.length;
   for (var x = 0; x <len; x++)
   {
      error_text += "<li>" + json[x][0] + ": " + json[x][1] +"</li>";
   }
   if (len> 0){
      error_text = "<ul>" + error_text + "</ul>";
   }
   return error_text;
}

// send all links to fully qualified urls into a new window
$(document).ready(function(){
    //var h = window.location.host.toLowerCase();
    $("a[href^='http:']").not("[href*='apartmentguide.com']").attr('target','_blank');
    clear_suggest(); //needed to clear input#user_search when user clicks inside input box
    unclear_suggest(); //neeed to revert the default/suggested text in input#user_search when user leaves search box i.e. input#user_search loses focus
    
});

function clear_suggest() {
  $("input#user_search").click(function () {
		var current_text = $('input#user_search').val();
    if (is_suggestion_text(current_text))
			$('input#user_search').val('');
  });
  
}

function unclear_suggest() {
  $("input#user_search").blur(function () { 
    if ($('input#user_search').val() == '')
      $('input#user_search').val('City, State or Zip');
  });
}

function is_suggestion_text(query) {
	if (query == "City, State or Zip" || query == "") {
	  return true;
	}
	return false;
}
