var mapLoader = {
	
	refreshLocations : function() {
		mapLoader.loading();
		//Ignore search q
	  new Ajax.Request('/map_locations/list', {parameters :  mapLoader.queryString()});
	},
	
	loading : function() {
		$('loadingBox').show()
	},
	
	hideLoading : function() {
		$('loadingBox').hide()
	},
	
	stringifyCats : function() {
		return Form.getElements('mapForm').inject([],function(acc,element) {
			if (element.name == 'c[]' && element.checked) {
				acc.push(element.value)
			}
			return acc;
	  }).join(',')
	},
	
	queryString : function() {
		return 'c=' + mapLoader.stringifyCats() + '&q=' + $F('q') 
	}
	
}

function dirTest() {
  var dir =  new GDirections(map);
	dir.load("from: 1937 Christy Drive, Jefferson City, MO 65109 to: 1309 Jefferson Street, Jefferson City, MO 65109");
}

var mapLocation = {
	  addFileField : function() {
    var ps = $('image_div').getElementsByTagName('p')
    for(var i=0;i<ps.length;i++){
      if (ps[i].getElementsByTagName('input')[0].value == '') return false
    }
    var newField = ps[0].cloneNode(true)
    newField.getElementsByTagName('input')[0].value = ''
    $('image_div').appendChild(newField)
  },
	deleteImage : function(el,id) {
    if (!confirm('Are you sure?')) return false
    Element.remove(el)
    html = '<input name="dead_images[]" value="' +id + '" type="hidden"/>'
    new Insertion.Bottom('image_div', html)
  }
}

var galleries = {
	changeMainImage : function(id, image) {
		$('first_image_img').src = image
	},
	showImage : function(el,options) {
    var tag  = options && options.tag ? options.tag : 'img'
    var imgs = $(el).getElementsByTagName(tag)
    for(var i=0;i<imgs.length;i++){
      if (imgs[i].style.display != 'none') {
        Element.hide(imgs[i])
      }
    }
		Element.show(options.current)
	}
}
var SproutSlideShow = Class.create();

SproutSlideShow.prototype = {
 initialize: function(container,options) {
	  this.container = container
		this.parent    = this.container+'_holder'
		this.pe        = null
		if (!options || !options.height) {
			alert('You need to set a height for the slideshow!');
			return
		}
		this.options   = {
		  seconds  : 5,
			fadeDuration : 1,
			height : 0
		}
		Object.extend(this.options, options || {});
		this.createParent();
	},
	
	start: function() { 
    this.pe = new PeriodicalExecuter(this.cycle.bind(this), this.duration())  
  }, 
	
	stop : function() {
		this.pe.stop()
	},
	
	cycle: function() { 
    new Effect.Fade(this.container, {  
      duration: this.options.fadeDuration, 
      fps: 50, 
      afterFinish: this.finish.bind(this)
		})
  },
	
	finish : function() {
		var imgs = this.elements();
	  for(var i = 0;i<imgs.length;i++) {
					if (Element.visible(imgs[i])) {
						Element.hide(imgs[i])
						Element.show(imgs[(i+1)%imgs.length])
						break
					}
				}
    new Effect.Appear(this.container, {
       duration: this.options.fadeDuration,
       fps: 50
    })
	},
	
	elements : function() {
	  return $(this.container).immediateDescendants();
	},
	
	duration : function() {
		return this.options.fadeDuration * 2 + this.options.seconds;
	},
	
	//Create parent DIV to control height, when hiding/showing elements
	createParent : function() {
	  new Insertion.Before(this.container,'<div id="'+this.parent+'" style="height:'+this.options.height+'px"></div>');
		var html = $(this.container).innerHTML
		Element.remove(this.container);
		$(this.parent).update('<div id="'+this.container+'">'+html+'</div>');
	}
	
}