
Effect.Scroll = Class.create(Effect.Base, {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (this.options.continuous && !this.element._ext ) {
      this.element.cleanWhitespace();
      this.element._ext=true;
      this.element.appendChild(this.element.firstChild);
    }

    this.originalLeft=this.element.scrollLeft;
    this.originalTop=this.element.scrollTop;

    if(this.options.mode == 'absolute') {
      this.options.x -= this.originalLeft;
      this.options.y -= this.originalTop;
    } else {

    }
  },
  update: function(position) {   
    this.element.scrollLeft = this.options.x * position + this.originalLeft;
    this.element.scrollTop  = this.options.y * position + this.originalTop;
  }
});

var ScrollContent = Class.create({
	initialize: function(container, url, orientation, loadingOptions) {
		this.container     = (typeof container == 'string' ? $(container) : container);
		this.url           = url;
		this.overflow_orig = this.container.style.overflow || null;
		this.overflow_paren_orig = this.container.parentNode.style.overflow || null;
		this.orientation   = orientation || "LEFT";
		this.loading       = loadingOptions || false;
		
		this.padding_left =  this.container.style.paddingLeft || '0px';
		this.padding_top =  this.container.style.paddingTop || '0px';
		this.scroll_x = this.padding_left;
		this.scroll_y = this.padding_top;

		if (this.container.parentNode.clientWidth > 0) {
			tWidth  = this.container.parentNode.clientWidth;
		} else {
			tWidth  = this.container.parentNode.offsetWidth;
		}
		if (this.container.parentNode.clientHeight > 0) {
			tHeight = this.container.parentNode.clientHeight;
		} else {
			tHeight = this.container.parentNode.offsetHeight;
		}
		if (this.orientation=="LEFT") this.scroll_x = tWidth+"px";
		if (this.orientation=="RIGHT") this.scroll_x = "-"+tWidth+"px";
		if (this.orientation=="TOP") this.scroll_y = tHeight+"px";
		if (this.orientation=="BOTTOM") this.scroll_y = "-"+tHeight+"px";
		
		var current = this;
		
		// Test if scroll active :
		if (!this.container.id) {
			var id = "ScrollContent";
			var cpt = 1;
			while ($(id+cpt)) { cpt++; }
			this.container.id = id+cpt;
		}
		if (ScrollContent.currents[this.container.id]) {
			return;
		} else {
			ScrollContent.currents[this.container.id] = this;
		}
		
		this.container.style.position = this.container.style.position || "relative";
		this.container.style.overflow = "visible";
		this.container.parentNode.style.overflow = "hidden";
		
//		alert(this.container.parentNode.getDimensions().width+", "+this.container.parentNode.getDimensions().height);
		var content_new = document.createElement('div');
		content_new.setAttribute("id", this.container.id+"_new");
		content_new.style.display  = 'block';
		content_new.style.position = 'absolute'; 
		content_new.style.left     = this.scroll_x;
		content_new.style.top      = this.scroll_y;
		content_new.style.width    = '100%';
		content_new.style.height   = '100%';
		content_new.style.margin   = '0';
		content_new.style.padding  = '0';

		this.container.appendChild(content_new);
		this.content_new = $(this.container.id+"_new");
		new Ajax.Updater(this.container.id+"_new", url, { method: 'get', evalScripts: true, onComplete: function() {current.scroll(current.container.id);} });
				
	},

	scroll: function() {
		Position.prepare();

		var current = this;
		
		element_x = this.content_new.positionedOffset()[0]*-1;
		element_y = this.content_new.positionedOffset()[1]*-1;
		
		images = this.content_new.getElementsByTagName("img");
		if (images.length > 0) {
			if (this.loading) {
				new Loading(this.container, this.loading);
			}
			for(var i=0, cpt=images.length; i < images.length; i++) {
				var img = new Image();
				img.onload = function() { 
					if (--cpt <= 0) {
						if (Loading.currents[current.container.id]) Loading.currents[current.container.id]._stop();
						new Effect.Move(current.container, {x:element_x, y:element_y, afterFinish: function() { current.clean(); }, mode: 'relative' });
					}
				};
				img.src = images[i].src;
			}
		} else {
			new Effect.Move(this.container, {x:element_x, y:element_y, afterFinish: function() { current.clean(); }, mode: 'relative' });
		}
	},

	clean: function() {
		// remove current
		child = this.container.firstChild
		while (child && this.container.childNodes.length > 1) {
			this.container.removeChild(child);
			child = this.container.firstChild;
		}
		// Add new
		while (child = this.content_new.firstChild) {
			this.container.appendChild(child);
		}
		this.container.removeChild(this.content_new);
		//this.container.innerHTML = this.content_new.innerHTML;
		this.container.style.position = "relative";
		this.container.style.left = "auto";
		this.container.style.top = "auto";
		
		try {
			if (this.overflow_orig != null) this.container.style.overflow = this.overflow_orig;
			else if (this.container.style.overflow) this.container.style.removeProperty("overflow");
		} catch (err) {
			this.container.style.overflow = "hidden";
		}

		try {
			if (this.overflow_parent_orig != null) this.container.parentNode.style.overflow = this.overflow_parent_orig;
			else this.container.parentNode.style.removeProperty("overflow");
		} catch (err) {
			this.container.parentNode.style.overflow = "hidden";
		}
		
		delete ScrollContent.currents[this.container.id];
		
		// Load functions :
		ScrollContent.afterFinish = ScrollContent.afterFinish.uniq();
		for (var i=0; i < ScrollContent.afterFinish.length; i++) {
			eval(ScrollContent.afterFinish[i]);
		}
	}
});

ScrollContent.currents = [];
ScrollContent.afterFinish = [];

if (typeof(Tabs) != "undefined") {
	ScrollContent.afterFinish.push("Tabs.initTabs(true, this.container)");
}


