Ajax.Responders.register({
	onCreate: function() {
		if($('notification') && Ajax.activeRequestCount > 0)
		Effect.Appear('notification',{duration: 0.25, queue: 'end'});
	},
	onComplete: function() {
		if($('notification') && Ajax.activeRequestCount == 0)
		Effect.Fade('notification',{duration: 0.25, queue: 'end'});
	}
});

function send_field_value_to_remote(element_id, url) {
	var pars = 'field_name' + "=" + element_id + "&field_value=" + $F(element_id);
	new Ajax.Updater("", url, { method: 'post', parameters: pars });
}

function toogle_subcategories(category_id) {
  elem = "sub_categories_" + category_id
	Element.toggle(elem);
  icon = Element.visible(elem) ? 'delete.png' : 'add.png';
  $("category_handle_icon_" + category_id).src = '/images/icons/' + icon;
}

function businessPhotosBrowser (field_name, url, type, win) {
  var cmsURL = window.location.pathname;
  cmsURL = cmsURL.substr(0, cmsURL.lastIndexOf('/')) + "/photos";
  
  tinyMCE.openWindow({
    file : cmsURL, title : "File Browser",
    width : 800, height : 600, close_previous : "no"
  }, {
    window : win, input : field_name,
    resizable : "yes", inline : "yes",
    editor_id : tinyMCE.getWindowArg("editor_id")
  });
  return false;
}

var RSRUtility = {
	selectorLink: function(selector){
		if (selector.options[selector.selectedIndex].value != "")
		          window.location.href = selector.options[selector.selectedIndex].value;
	}
}

Event.addBehavior({
	"#img_btn:click": function(){
		// $('img_btn').src = "/images/search_btn_over.jpg";
		if ($("search_query").getValue() == "electricians, pizza, mechanics, etc."){
			$("search_query").clear();
		}
	},

	"input#search_query:focus": function(){
		if ($("search_query").getValue() == "electricians, pizza, mechanics, etc."){
			$("search_query").clear();
		}
		$("search_query").removeClassName("kw");
	},
	"#per_page_selector:change": function(){
		RSRUtility.selectorLink(this);
	},
	"#sort_by_selector:change": function(){
		RSRUtility.selectorLink(this);
	}
});

function updateCharsCountFor(elem, notificationElementId, maxChars) {
  total = $F(elem).length;
  if (total > maxChars) {
    alert ("Maximum characters count for element exceeded");
    elem.value = $F(elem).slice(0, maxChars);
    total = maxChars;
  }
  $(notificationElementId).innerHTML = "Characters remain: " + (maxChars - total);
}
