var NewsSlide = Class.create()
NewsSlide.prototype = {
    initialize: function(container, options) {
		NewsSlide.container = container;
        NewsSlide.options = options;
        var automated;
        var direction;
        var hideshow_flag;
        NewsSlide.hideshow_flag = true;
        if (NewsSlide.options) {
            NewsSlide.direction = NewsSlide.options.direction
        } else {
            NewsSlide.direction = "forward"
        };
        if (NewsSlide.direction != "pause") {
            if (NewsSlide.options.auto == "loop" || NewsSlide.options.auto == "once") {
				var interval = (NewsSlide.options.time/1000);
				NewsSlide.automated = new PeriodicalExecuter(NewsSlide.prototype.klizi_int,interval);
				//NewsSlide.automated = new PeriodicalExecuter(function(pe) {   if (!confirm('Want me to annoy you again later?'+ NewsSlide.container))     pe.stop(); },interval);
				
				//= this.klizi.periodical(NewsSlide.options.time, this, $(NewsSlide.container))
            } else {
				NewsSlide.prototype.klizi($(NewsSlide.container));
            }
        }
    },
    naprijed: function(type) {
        if (NewsSlide.options.auto == "loop" || NewsSlide.options.auto == "once") {
            this.pauza()
        };
        NewsSlide.options.type = type;
        NewsSlide.direction = "forward";
        NewsSlide.prototype.klizi($(NewsSlide.container));
    },
    nazad: function(type) {
        if (NewsSlide.options.auto == "loop" || NewsSlide.options.auto == "once") {
            this.pauza()
        };
        NewsSlide.options.type = type;
        NewsSlide.direction = "back";
        NewsSlide.prototype.klizi($(NewsSlide.container));
    },
    pauza: function() {
        NewsSlide.direction = "pause";
        NewsSlide.prototype.klizi($(NewsSlide.container));
    },
    radi: function() {
        NewsSlide.direction = "forward";
		var interval = (NewsSlide.options.time/1000);
		NewsSlide.automated = new PeriodicalExecuter(this.klizi_int,interval);
        //NewsSlide.automated = this.klizi.periodical(NewsSlide.options.time, this, $(NewsSlide.container))
    },
	klizi_int: function() {
		NewsSlide.prototype.klizi($(NewsSlide.container));
	},
    klizi: function(container) {
	
        if ((NewsSlide.options.auto == "once" || NewsSlide.options.auto == "loop") && NewsSlide.direction == "pause") {
            NewsSlide.automated.stop();
        } else {
            var child;
            var children = [];
			var children_raw = container.childElements()[0].childElements();
			for (i=0;i<children_raw.length;i++) {
				children[children.length] = children_raw[i];
			}
            children.each(function(e) {
                if (e.id == "currentChild") {
                    child = e
                }
            });
			
            if (!child && (NewsSlide.direction == "forward" || NewsSlide.direction == "pause")) {
                child = $(children[0]).next('div',0);
                child.id = "currentChild"
            } else {
                if (NewsSlide.direction == "forward") {
                    var lastElement = $(children[(children.length-1)]);
                    if (lastElement == child.next('div',0) && NewsSlide.options.auto == "once") {alert('stopped 1');
                         NewsSlide.automated.stop()
                    }
                    if (lastElement == child) {
                        child = children[0];
                    } else {
                        child = child.next('div',0);
                    }
                };
                if (NewsSlide.direction == "back") {
                    var firstElement = children[0];
                    if (firstElement == child.previous('div',0) && NewsSlide.options.auto == "once") { 
                         NewsSlide.automated.stop()
                    }
                    if (firstElement == child) {
                        child = $(children[(children.length-1)]);
                    } else {
                        child = child.previous('div',0);
                    }
                }
            };
            if (child && NewsSlide.direction != "pause") {
                if (NewsSlide.options.type == "scroll") {
                    this.scroll(container, children, child)
                } else if (NewsSlide.options.type == "fade") {
                    this.fade(container, children, child)
                } else if (NewsSlide.options.type == "scrollfade") {
                    this.scrollfade(container, children, child)
                }
            }
        }
    },
    init_vertical: function() {
        $(NewsSlide.container).setStyle('position', 'relative');
        var children = $(NewsSlide.container).getChildren().getChildren()[0];
        var i;
        var s_h;
        var s_w;
        var b_w;
        b_w = $(NewsSlide.container).getStyle('border-width').toInt();
        s_h = $(NewsSlide.container).getSize().size.y - 2 * b_w;
        s_w = $(NewsSlide.container).getSize().size.x - 2 * b_w;
        i = 0;
        children.each(function(e) {
            if (i !== 0) {
                e.id = "currentChild";
                $('currentChild').setStyle('position', 'relative');
                $('currentChild').setStyle('left', 0 - s_w * i);
                $('currentChild').setStyle('top', 0 + s_h * i);
                e.id = ""
            };
            i++
        });
        children.each(function(e) {
            e.id = ""
        })
    },
    init_horizontal: function() {
        $(NewsSlide.container).setStyle('position', 'relative');
        var children = $(NewsSlide.container).getChildren().getChildren()[0];
        var i;
        var s_h;
        var s_w;
        var b_w;
        b_w = $(NewsSlide.container).getStyle('border-width').toInt();
        s_h = $(NewsSlide.container).getSize().size.y - 2 * b_w;
        s_w = $(NewsSlide.container).getSize().size.x - 2 * b_w;
        i = 0;
        children.each(function(e) {
            if (i !== 0) {
                e.id = "currentChild";
                $('currentChild').setStyle('position', 'relative');
                $('currentChild').setStyle('left', 0);
                $('currentChild').setStyle('top', 0);
                e.id = ""
            }
            i++
        })
    },
    scroll: function(container, children, child) {
        var scroll = new Fx.Scroll(container, {
            duration: NewsSlide.options.duration,
            onComplete: function() {
                children.each(function(e) {
                    e.id = ""
                });
                child.id = "currentChild"
            }
        }).toElement(child)
    },
	ScrollTo: function(container, element) {
	 
	  return false;
	},
	restorefade : function (container, children, child, dtime) {
		children.each(function(e) {
			e.id = ""
			e.style.display = "none"
		});
		child.id = "currentChild";
	
		child.style.display = '';
		Position.prepare();
		container_y = Position.cumulativeOffset($(container))[1];
		element_y = Position.cumulativeOffset($(child))[1];
		new Effect.Scroll($(container), {x:0, y:(element_y-container_y)});
		new Effect.Opacity(container.id, { from: 0, to: 1.0, duration: dtime });
		
		document.getElementById('pozicija').style.background = "transparent";
	},
    fade: function(container, children, child) {
		var dtime = (NewsSlide.options.duration / 1000);
		document.getElementById('pozicija').style.background = "url("+NewsSlide.options.loadingimg+") no-repeat center center";
		new Effect.Opacity(container.id, { from: 1.0, to: 0, duration: dtime });

		//$(container).fade({ duration: (NewsSlide.options.duration / 1000) });
		
		this.restorefade.delay(dtime, container, children, child, dtime);
		
		//Effect.ScrollTo(child,{duration: 1 }); 
		
		
		//new Fx.Style(container, 'opacity').start(0.01, 1);
		//document.getElementById('pozicija').style.background = "transparent"
        
       
    },
    scrollfade: function(container, children, child) {
        var durationInt = NewsSlide.options.duration.toInt();
        var fade = new Fx.Style(container, 'opacity', {
            duration: (durationInt / 2)
        });
        document.getElementById('pozicija').style.background = "url("+NewsSlide.options.loadingimg+") no-repeat center center";
        fade.start(1, 0.01).chain(function() {
            fade.start(0.01, 1);
            document.getElementById('pozicija').style.background = "transparent"
        });
        new Fx.Scroll(container, {
            duration: durationInt,
            onComplete: function() {
                children.each(function(e) {
                    e.id = ""
                });
                child.id = "currentChild"
            }
        }).toElement(child)
    },
    hide_and_show_fade: function() {
        var fade = new Fx.Style($(NewsSlide.container), 'opacity', {
            duration: NewsSlide.options.duration
        });
        if (NewsSlide.hideshow_flag) {
            fade.start(1, 0.01)
        } else {
            fade.start(0.01, 1)
        }
        NewsSlide.hideshow_flag = !NewsSlide.hideshow_flag
    }
};

Effect.Scroll = Class.create();
Object.extend(Object.extend(Effect.Scroll.prototype, Effect.Base.prototype), {
  initialize: function(element) {
    this.element = $(element);
    var options = Object.extend({
      x:    0,
      y:    0,
      mode: 'absolute'
    } , arguments[1] || {}  );
    this.start(options);
  },
  setup: function() {
    if (NewsSlide.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(NewsSlide.options.mode == 'absolute') {
      NewsSlide.options.x -= this.originalLeft;
      NewsSlide.options.y -= this.originalTop;
    } else {

    }
  },
  update: function(position) {   
    this.element.scrollLeft = NewsSlide.options.x * position + this.originalLeft;
    this.element.scrollTop  = NewsSlide.options.y * position + this.originalTop;
  }
});
