
Ajax.Responders.register({
  onCreate: function() {
    if($('indicator') && Ajax.activeRequestCount>0)
      $('indicator').show();
  },
  onComplete: function() {
    if($('indicator') && Ajax.activeRequestCount==0)
      $('indicator').hide();
  }
});


var Slideshow = Class.create();
Slideshow.prototype = {
		current: 0,
		caption_id: "hero_slide_caption",
		image_id: "hero_slide_image",
		pager: "slide_pager",
		pager_prefix: "hero_slide_pager_",
		paused: false,
		cycle_time:5,
		close_time: .5,
		open_time: 1,
		initialize: function(slides) {
			this.slides = slides;
			this.preloadSlides(slides);
		},
		preloadSlides: function(slides) {
			$A(slides).each(function (el,index) {
				MM_preloadImages(el.public_url);
			});
		},
		startup: function() { 
			new PeriodicalExecuter(function(pe) {
				if (this.paused) {
					pe.stop();
				} else {
					this.go();
				}
			}.bind(this), this.cycle_time); // change image every 5 seconds
		},
		current_slide: function() {
			return this.slides[this.current];
		},
		prev_id: function() {
			var prev = this.current-1;
			if (prev < 0) {
				prev = this.slides.length-1;
			}
			return prev;
		},
		next_id: function() {
			var next = this.current+1;
			if (next > this.slides.length-1) {
				next = 0;
			}
/*			alert(next);*/
			return next;
		},
		next: function () {
			this.go();
		},
		prev: function () {
			this.current = this.prev_id();
			this.goToCurrent();
		},
		go: function(slide_id, witheffect) {
			if (slide_id != undefined) {
				this.current = slide_id;
			} else {
				this.current = this.next_id();
			}
			this.goToCurrent(witheffect);
		},
		cycle: function(htid, newcontent, witheffect) { 
			if (witheffect != undefined && witheffect == false) {
					$(htid).update(newcontent);
			} else {
			this.closeEffect(htid, { 
								duration: this.close_time, 
								fps: 24,
								afterFinish: function() {
										$(htid).update(newcontent);
										this.openEffect(htid, { duration: this.open_time,	fps: 24, queue:'end'}); 
								}.bind(this)
			});
			}
		},
		pause: function(pause_for) {
			this.paused = true;
		},
		goToCurrent: function(witheffect) {
			//cycle image
			this.cycle(this.image_id,this.image_html(this.current_slide()),witheffect);
			//cycle caption_id
			this.cycle(this.caption_id,this.caption_html(this.current_slide()),witheffect);
			this.selectCurrentPager();
		},
		selectCurrentPager: function () {
			$$("#"+this.pager+" li").each(function(el,i) {
					el.removeClassName('selected');
			});
			$(this.pager_prefix + this.current).addClassName('selected');
		},
		caption_html: function(slide) {
			var html = "<p>"+slide.caption
			if (slide.url != "") {
				html += "&nbsp;<a href=\""+slide.url+"\">Read More</a></p>";
			}
			return html;
		},
		image_html: function(slide) {
			var html = "";
			if (slide.url != "") { html += "<a href=\""+slide.url+"\">";	}
			html += "<img src=\""+slide.public_url+"\" alt=\""+slide.title+"\" />";
			if (slide.url != "") { html += "</a>"; }
			return html;
		},
		openEffect: Effect.Appear,
		closeEffect: Effect.Fade
}; 

var Lightbox = Class.create();
Lightbox.prototype = Object.extend(new Slideshow,{
	overlay: 'overlay',
	lb_element: 'lightbox',
	caption_id: "lb_current_caption",
	image_id:	"lb_current_image",
	cycle_time: 8,
	initialize: function(lbel,slides) {
		this.lb_element = lbel;
		this.slides 		= slides;
		this.preloadSlides(slides);
	},
	open: function() {
		$(this.overlay).show();
		this.openEffect(this.lb_element);
	},
	close: function() {
		this.closeEffect(this.lb_element);
		setTimeout(function() {this.closeEffect(this.overlay); }.bind(this),1000);
		this.pause();
		this.current = 0;
	},
	selectCurrentPager: function () {},
	caption_html: function(slide) {
		return "<p>"+slide.content+"</p>";
	},
	image_html: function(slide) {
		return "<img src=\""+slide.public_url+"\" alt=\""+slide.title+"\" />";
	},
	openEffect: Effect.Appear,
	closeEffect: Effect.Fade
});

var Effies = {
	imageSelector: {
		select: function(new_src,form_id,im_id) {
			var form_num = form_id.replace(/image_id_/,'');

			$(form_id).value = im_id;
			$('current_image_'+form_num).update(this.currentImageHTML(new_src,form_id));
			$('current_image_'+form_num).show();
/*						alert(form_num);*/
/*			return false;*/
		},
		deselect: function(form_id) {
			var form_num = form_id.replace(/image_id_/,'');
			$(form_id).value = "";
			$('current_image_'+form_num).update('');
		},
		currentImageHTML: function(src,form_id) {
			return "<h4>Current Image</h4><img src=\""+src+"\" alt=\"Current Image\" /><br /><a href=\"#\" onclick=\"Effies.imageSelector.deselect('"+form_id+"');return false;\">Deselect Image</a>";
		}
	},
	nav: {
		updateSort: function(sortable_id) {
			new Ajax.Request(
					'/pages/update_nav_sort',
					{
						method: 'post',
						evalScripts:true, 
						parameters: Sortable.serialize(sortable_id)
					});
		},
		toggleSort: function() {
			$('navigations').toggleClass('sorting');
		}
	}
};
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}
Element.addMethods({
	toggleClass: function(elem,css) { 
		if (Element.hasClassName(elem,css)) {
			Element.removeClassName(elem,css);
			return false;
		} else {
			Element.addClassName(elem,css);
			return true;
		}
	}
});
function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
}