Photos = {
  photosURL: '/photos/main', // not dry
  photoGalleriesURL: '/photo_galleries/main', // need to use better_js_routes
  
  getContent : function(params) {
    params = params.sub('#', '');
    var contentType = params.toQueryParams().type;
    var url = (contentType == 'Album') ? Photos.photoGalleriesURL : Photos.photosURL;
    new Ajax.Request(url, {asynchronous:true, evalScripts:true, parameters:params.sub('#', ''),
						onLoading: function(request){
							if ($('spinner_loader')){
								$j('span#spinner_loader').show();
							}
							if ($('spinner_loader_2')){
								$j('span#spinner_loader_2').show();
							}
						},
						onComplete: function(request){
							if ($('spinner_loader')){
								$j('span#spinner_loader').hide();
							}
							if ($('spinner_loader_2')){
								$j('span#spinner_loader_2').hide();
							}
						}
					});
  },

  updateHash : function(s_param) {
    Photos.updatedByClick = true;
    var params = new Hash(s_param.toQueryParams());
	if (typeof(params.get('page')) == "undefined"){
		params.set('page', 1);
	}
    var hash_params = new Hash(document.location.hash.sub('#', '').toQueryParams());
	if (params.get('query') == ""){
		params.unset('query');
		hash_params.unset('query');
	}
    document.location.hash = hash_params.merge(params).toQueryString();
    return document.location.hash;
  },

  checkHash : function() {
    if (window.pe)
      window.pe.stop();
    var current_value = document.location.hash;
    return new PeriodicalExecuter(function(){
      if (Photos.updatedByClick) {
        Photos.updatedByClick = false;
        return;
      }
      if (document.location.hash != current_value) {
        Photos.getContent(document.location.hash);
      }
        
      current_value = document.location.hash;
    }, 1);
  },	

  addListeners : function() {
    $$('.pagination a').each(function(a) {
      a.href = "#" + a.href.match("\\?(.+)").last();
      a.addClassName('remote');
    });

    $$('.remote').each(function(a) {
      a.observe('click', function(e) {
        var el = e.element();
        var params = Photos.updateHash(el.href.match("#(.+)").last());
        Photos.getContent(params);
        Event.stop(e);
        return false;
      });
    });

    $('search_button').observe('click', function(e) {
      var params = Photos.updateHash("type=Photo&query=" + $('search_photos').value + "&page=1")
      Photos.getContent(params);
      Event.stop(e);
      return false;
    });
	
	$('search_photos').observe('keyup', function(e) {
	  if (e.keyCode == Event.KEY_RETURN){
      	var params = Photos.updateHash("type=Photo&query=" + $('search_photos').value + "&page=1")
      	Photos.getContent(params);
      	Event.stop(e);
	  }
      return false;
    });
	
	if ($('clear_search_results')){
		$('clear_search_results').observe('click', function(e) {
	        var params = Photos.updateHash("query=");
	     	Photos.getContent(params);
	     	Event.stop(e);
	        return false;
      	});
	}
	
    window.pe = Photos.checkHash();
  }
}

document.observe("dom:loaded", function() {
  if (document.location.hash.blank() || document.location.href.match('photo_galleries'))
    Photos.updateHash('type=Album');
  Photos.addListeners();
});