Photos = {
  photosURL: location.href, // not dry
  photoGalleriesURL: location.href,
  
  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());
    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) {
	  if (a.href.match("\\?(.+)")){
      	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;
      });
    });

    window.pe = Photos.checkHash();
  }
}

document.observe("dom:loaded", function() {
  Photos.addListeners();
});