// We don't want to use the dollar sign to refer to jQuery as the sign is used by velocity.
// Change it to letter 'j'.
var j = jQuery.noConflict();

var pageNumber;

jQuery.mask.masks.integer_aus = {mask: '999,999,999', type: 'reverse'};
j(document).ready(function () {
	var inputFields = j('fieldset input:text');
	inputFields.removeClass("inputHighlighted").addClass("inputUnHighlighted");
	inputFields.focus(function() {
		j(this).removeClass("inputUnHighlighted").addClass("inputHighlighted");
		if(this.value != '') {
			this.select();
		}
	});
	inputFields.blur(function() {
		j(this).addClass("inputUnHighlighted").removeClass("inputHighlighted");
		if (j.trim(this.value) == '') {
			this.value = j.trim(this.value);
		}
	});
}); 	


// On submit a request is created in form of JSON.
// This is where a request is generated and sent to the server to fetch listings.
j(document).ready(function () {
  j('#searchForm').submit(function (e) {
    e.preventDefault();
  	Event.fire(document, "allhomes:initiateSearch");
  });
});

function changePage(pg) {
  Event.fire(document, "allhomes:initiateSearch", {pageNumber:pg});
}

// Responsible for toggling the search criteria.
function initMenu() {

  // Add the 'contract' class to any 'legend' blocks that aren't marked as expand
  j('#searchForm legend[class!=expand]').addClass('contract');
	
  j('#searchForm legend').toggle(
      function () {
        j(this).next().slideToggle('fast');
        j(this).removeClass('contract');
        j(this).addClass('expand');
      },
      function () {
        j(this).next().slideToggle('fast');
        j(this).removeClass('expand');
        j(this).addClass('contract');
      }
  );
  
  j('#searchForm legend[class=expand]').trigger('click');
  
}

// Listen for anyone triggering the search
Event.observe(document, "allhomes:initiateSearch", function(event) {
	  
	// Whenever a user changes the search criteria we should take the user
	// to the first page with the matching results rather than 
	// leaving whatever the page number the user is on.
	if (event.memo.pageNumber == undefined) {
		// pageNumber is a global variable.
		pageNumber = 1;
	}
	else {
		pageNumber = event.memo.pageNumber; 
	}
	
	  searchCriteriaController.submitAction();
});

// Wait until search has been fully initialised
Event.observe(document, "allhomes:searchReady", function() {
	  initMenu();

	  searchCriteriaController.initialiseFromCookie();
	  Event.fire(document, "allhomes:initiateSearch", {initialSearch:true, pageNumber:pageNumber});
});

// Changes the style of the submit button
function buttonPressed(obj) {
	j(obj).addClass('pressedButton');
}
// Changes the style of the submit button
function buttonReleased(obj) {
	j(obj).removeClass('pressedButton');
}

//
// Based on the advanced search cookies generates a link back to the advanced search.
//
function generateBackToSearchLink(ahasCookieName, ahbsCookieName, containerName, advancedSearchLink) {
	if (jQuery.cookie(ahasCookieName) != null && jQuery.cookie(ahbsCookieName) != null) {
		var backToSearchLink = new Element('a', {href: '#'}).update('Return to Search');
		backToSearchLink.observe('click', goBackToAdvancedSearch.bindAsEventListener(this, ahbsCookieName));
		$(containerName).insert(backToSearchLink);
	}
}

//
// 'Return to Search' action.
//
function goBackToAdvancedSearch(event, ahbsCookieName) {
	if (jQuery.cookie(ahbsCookieName) != null) {
		var json = jQuery.cookie(ahbsCookieName).evalJSON();
		window.location.href = json.url.href;
		Event.stop(event);
	}
}

// Removes 'Return to Search' link and deletes the ahbs cookie.
function removeBackToSearch(cookieName, containerName) {
	jQuery('#' + containerName).html('&nbsp');
	jQuery.cookie(cookieName, null, {path: '/ah'});
}
