// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

var ScrollBox_jquery = Class.create({
  initialize: function(classname) {
		
		count = 0;
               
		$("div", classname).each(function () {
                      	var childTag = $(this).children().get(0).tagName;
			if (childTag == "DIV") {
				if (count > 0) {
					$(this).hide();
				}
				count++;
				 
			}
			
			
                 });

		
	},


});

toggleit= function(n,classname) {
		
		var count = 0;
		$("div", classname).each(function () {
                      	var childTag = $(this).children().get(0).tagName;
			if (childTag == "DIV") {
				
				if (count == n) {
					$(this).show();
				} else {
					$(this).hide();
				}
				count++;
			}
						
                 });
}

switchit= function(classname,direction) {
		
     		var n = $("div",classname).length;
		var y = 0;
		var count = 0;
		var totalCount = 0;
		var currentCount = 0;
		$("div", classname).each(function () {
                      		var childTag = $(this).children().get(0).tagName;
				if (childTag == "DIV") {
					
					var style = $(this).attr("style");
					var fs = "";
					if (style != null) {
						fs = style.substring(9,13);
					}
			
					if (fs == "none") {
						
						
					} else {
						
						currentCount = totalCount;
						
					}
					totalCount++;
				}
			
                 });
		
		if (direction == "r") {
			count = (currentCount == totalCount - 1) ? 0 : currentCount + 1;
		} else {
			count = (currentCount == 0) ? totalCount -1 : currentCount - 1;
		}
		
		toggleit(count,classname);
               
}

var ScrollBox = Class.create({
  initialize: function(classname) {
		
		this.count = 0;
		this.classname = classname;
		this.items = $$(classname);
		
		if (this.items.length > 1) {
			
			this.active = this.items[0];
			this.items.invoke('hide');
			this.active.show();
			this.observe_buttons();				
		}
	},

	toggle: function() {
		this.active.hide();
		this.active = this.items[this.count];
		this.active.show();
	},
	
	observe_buttons: function() {
		$$(this.classname +' .arrow-right, '+ this.classname +' .arrow-left').invoke('observe', 'click', function(el) {
			var btn = Event.element(el);
			
			if (btn.className == 'arrow-right') {
				this.count = (this.count == this.items.length - 1) ? 0 : this.count + 1;
			} else {
				this.count = (this.count == 0) ? this.items.length - 1 : this.count - 1;
			}
			this.toggle();
			Event.stop(el);
		}.bind(this));		
	}
});

var ScrollImages = Class.create({
  initialize: function(el) {
		this.el = el;
		
		if ($(this.el)) {
			this.scroller = $(this.el).down('ul');
			this.distance = 85;
			this.direction = 1;
			this.count = 0;
			this.num_images = (this.scroller.immediateDescendants().length - 6) * -1;
			this.observe_buttons();
			this.msg_text = $('image-text').innerHTML;
			$(el).down('ul').setStyle({
				width:this.distance * this.scroller.immediateDescendants().length + "px"
			});
		}
	},
	
	observe_buttons: function() {
		$$('#'+ this.el +' .arrow-right, #'+ this.el +' .arrow-left').invoke('observe', 'click', function(el) {
			this.direction = (Event.element(el).className == 'arrow-right') ? -1 : 1;
			this.scroll();
			Event.stop(el);
		}.bind(this));
		
		$$('#scroll-images img').invoke('observe', 'mouseover', function(el) {
			var img = Event.element(el);
			$('image-text').update(img.readAttribute('alt'));
			Event.stop(el);
		});
		
		$$('#scroll-images img').invoke('observe', 'mouseout', function(el) {
			$('image-text').update(this.msg_text);
			Event.stop(el);
		}.bind(this));
	},
	
	scroll: function() {
		tmp_count = this.count + this.direction;

		if (tmp_count <= 0 &&  tmp_count >= this.num_images) {
			this.count = tmp_count;
		  new Effect.MoveBy( this.scroller, 0, this.distance * this.direction, {duration: 0.5,  transition: Effect.Transitions.sinoidal} );
		}
	}
});


fix_ie_arrows = function() {
	if (Prototype.Browser.IE) {
		$$("h2 .arrow, h3 .arrow, h4 .arrow").each( function(el) {
			var img = el.getStyle('backgroundImage').sub(/url\((.*?)\)/, '<img src=#{1} />');
			el.insert(img);
			el.setStyle({'backgroundImage':'none'});
		});
	}		
}

Event.observe(window, 'load', fix_ie_arrows);
