var newsChanger = new Class({
	Version: "1.3",
	Implements: [Options, Events],
	options: {
		holder: ".holder",
		elementsParent: "ol",
		elements: "li",
		nextItem: ".next",
		prevItem: ".prev",
		stop: ".stop",
		start: ".start",
		speed: 1000,
		duration: 10000,
		steps: 1,
		current: 0,
		transition: "sine:in:out",
		direction: "horizontal",
		mode: "circle",
		currentClass: "current",
		nextDisableClass: "next-disable",
		prevDisableClass: "prev-disable",
		paging: false,
		pagingEvent: "click",
		pagingHolder: ".paging",
		random: true,
		autoplay: true,
		autoplayOpposite: false,
		stopOnHover: true
		/* 
		onStart: $empty,
		onPlay: $empty,
		*/ 
	},
	initialize: function(options) {
		this.setOptions(options);
		this.gallery = $('news');
		this.holder = this.gallery.getElement(this.options.holder);
		this.itemsParent = this.holder.getElement(this.options.elementsParent);
		this.items = this.itemsParent.getChildren(this.options.elements);
		this.next = this.gallery.getElement(this.options.nextItem);
		this.prev = this.gallery.getElement(this.options.prevItem);
		this.stop = this.gallery.getElement(this.options.stop);
		this.start = this.gallery.getElement(this.options.start);
		this.current = this.options.current;
		this.bound = {rotate: this.rotate.bind(this) }
		
		Fx.implement({
			cancel: function() {
				if(!this.callChain()) this.fireEvent('chainComplete', this.subject);
				if(this.stopTimer()) this.onCancel();
				return this;
			}
		});
		
		if(this.options.direction == "horizontal") {
			this.direction = "margin-left";
			this.size = this.items[0].getWidth();
			this.visible = Math.round(this.holder.getWidth()/this.size);
		}
		else {
			this.direction = "margin-top";
			this.size = this.items[0].getHeight();
			this.visible = Math.round(this.holder.getHeight()/this.size);
		}

		if(this.items.length <= this.visible) {
			if(this.next) this.next.addClass(this.options.nextDisableClass).addEvent("click", function() {return false;});
			if(this.prev) this.prev.addClass(this.options.prevDisableClass).addEvent("click", function() {return false;});
			if(this.stop) this.stop.addEvent("click", function() {return false;});
			if(this.start) this.start.addEvent("click", function() {return false;});
			this.gallery.addClass("stopped no-active");
			this.fireEvent("start", this.current, this.visible, this.items.length, this.items[this.current]);
			return false;
		}
			
		this.options.steps = this.options.steps > this.visible ? this.visible : this.options.steps;
		this.options.duration = this.options.duration < 1000 ? 1000 : this.options.duration;
		this.options.speed = this.options.speed > 6000 ? 6000 : this.options.speed;
		if(this.options.speed > this.options.duration) this.options.speed = this.options.duration;
		
		this.fx = new Fx.Tween(this.itemsParent, {
			property: this.direction,
			duration: this.options.speed,
			transition: this.options.transition,
			link: "cancel",
			fps: 100
		});
	
		if(this.options.random) this.shuffle();
		this.getInitialCurrent();
		
		if(this.options.mode == "circle") {
			while(this.items.length < this.options.steps+this.visible) {
				this.itemsParent.innerHTML += this.itemsParent.innerHTML;
				this.items = this.itemsParent.getElements(this.options.elements);
			}
			for(var i=0; i<this.current; i++) {
				this.items[i].inject(this.itemsParent, "bottom");
			}
			this.options.paging = false;
		}
		else {
			if(this.options.paging) this.createPaging();
			this.play(false);
		}
		
		if(this.next) {
			this.next.addEvent("click", function() {
				this.nextSlide();
				return false;
			}.bind(this));
		}
		
		if(this.prev) {
			this.prev.addEvent("click", function() {
				this.prevSlide();
				return false;
			}.bind(this));
		}
		
		if(this.options.autoplay || this.options.autoplayOpposite) this.timer = this.bound.rotate.delay(this.options.duration);
		else this.gallery.addClass("stopped");
		
		if(this.start) {
			this.start.addEvent("click", function() {
				clearTimeout(this.timer);
				this.gallery.removeClass("stopped");
				this.timer = this.bound.rotate.delay(this.options.duration);
				return false;
			}.bind(this));
		}
		
		if(this.stop) {
			this.stop.addEvent("click", function() {
				this.gallery.addClass("stopped");
				clearTimeout(this.timer);
				return false;
			}.bind(this));
		}
		
		if(this.options.stopOnHover) {
			this.gallery.addEvent("mouseenter", function() {
				clearTimeout(this.timer);
			}.bind(this));
			this.gallery.addEvent("mouseleave", function() {
				if(!this.gallery.hasClass("stopped")) {
					clearTimeout(this.timer);
					this.timer = this.bound.rotate.delay(this.options.duration);
				}
			}.bind(this));
		}
		
		this.fireEvent("start", this.current, this.visible, this.items.length, this.items[this.current]);
	},
	getInitialCurrent: function() {
		var tempCurrent = this.items.get("class").indexOf(this.options.currentClass);
		if(tempCurrent != -1) this.current = tempCurrent;
		else {
			if(this.current > this.items.length-1) this.current = this.items.length-1;
			else	if(this.current < 0) this.current = 0;
		}
		if(this.options.mode != "circle" && this.visible+this.current >= this.items.length) this.current = this.items.length-this.visible;
		return this;
	},
	rotate: function() {
		if(!this.options.autoplayOpposite) this.nextSlide();
		else this.prevSlide();
		this.timer = this.bound.rotate.delay(this.options.duration);
		return this;
	},
	play: function(animate) {
		if(this.options.mode == "line") this.sidesChecking();
		if(animate) this.fx.start(-this.current*this.size);
		else this.fx.set(-this.current*this.size);
		if(this.options.paging) this.setActivePage();
		this.fireEvent("play", this.current, this.visible, this.items.length, this.items[this.current]);
		return this;
	},
	nextSlide: function() {
		if(this.options.mode != "circle") {
			if(this.visible+this.current >= this.items.length) {
				if(this.options.mode == "callback") this.current = 0;
			}
			else if(this.visible+this.current+this.options.steps >= this.items.length) {
				this.current = this.items.length-this.visible;
			}
			else this.current += this.options.steps;
			this.play(true);
		}
		else {
			var temp = this.current;
			if((this.current += this.options.steps) >= this.items.length) this.current -= this.items.length;
			this.fx.start(-this.size*this.options.steps).chain(function() {
				for(var i=0; i<this.options.steps; i++) {
					if(temp >= this.items.length) temp = 0;
					this.items[temp++].inject(this.itemsParent, "bottom");
				}
				this.fx.set(0);
			}.bind(this));
			this.fireEvent("play", this.current, this.visible, this.items.length, this.items[this.current]);
		}
		return this;
	},
	prevSlide: function() {
		if(this.options.mode != "circle") {
			if(this.current <= 0) {
				if(this.options.mode == "callback") this.current = this.items.length-this.visible;
			}
			else if(this.current-this.options.steps <= 0) {
				this.current = 0;
			}
			else	this.current -= this.options.steps;
			this.play(true);
		}
		else {
			for(var i=0; i<this.options.steps; i++) {
				if(this.current-1 < 0) this.current = this.items.length;
				this.items[--this.current].inject(this.itemsParent, "top");
			}
			this.fx.set(-this.size*this.options.steps).start(0);
			this.fireEvent("play", this.current, this.visible, this.items.length, this.items[this.current]);
		}
		return this;
	},
	sidesChecking: function() {
		this.next.removeClass(this.options.nextDisableClass);
		this.prev.removeClass(this.options.prevDisableClass);
		if(this.visible+this.current >= this.items.length) this.next.addClass(this.options.nextDisableClass)
		else if(this.current==0) this.prev.addClass(this.options.prevDisableClass);
		return this;
	},
	createPaging: function() {
		this.paging = new Element("ul");
		var pagingHold = this.gallery.getElement(this.options.pagingHolder);
		if(pagingHold != null) this.paging.injectInside(pagingHold);
		else this.paging.injectInside(this.gallery).addClass("paging");
		
		var length = Math.ceil((this.items.length-this.visible)/this.options.steps)+1;
		var str = "";
		for(var i=0; i<length; i++) {
			str += '<li><a href="#">' + parseInt(i+1) + '</a></li>';
		}
		this.paging = this.paging.set("html", str).getElements("a");
		this.paging.each(function(el, i) {
			el.addEvent(this.options.pagingEvent, function() {
				if(i < length-1) this.current = i*this.options.steps;
				else this.current = this.items.length-this.visible;
				this.play(true);
				return false;
			}.bind(this));
		}.bind(this));
		return this;
	},
	setActivePage: function() {
		this.paging.removeClass("active")[Math.ceil(this.current/this.options.steps)].addClass("active");
		return this;
	},
	shuffle: function() {
		var str = "";
		this.items.sort(function(){return 0.5 - Math.random()}).each(function(el) {
			str += new Element("div").adopt(el).get("html");
		});
		this.items = this.itemsParent.set("html", str).getElements(this.options.elements);
		return this;
	}
});

/*
---
name: Popup

version: 0.1.3

description: A easy way to create manipulate and pass data through your popup's

license: 
  - MIT-style

authors: 
  - Mient-jan Stelling <mientjan.stelling@gmail.com

requires:
  - core/1.3:Object
  - core/1.3:Array
  - core/1.3:Class
  - core/1.3:Class.Options
  - core/1.3:Core

provides: 
  - Popup
...
*/

var Popup = new Class({
	Implements: [Options],
	options:{
		// name:'empty', // window name
		status:0, // The status bar at the bottom of the window.
		toolbar:0, // The standard browser toolbar, with buttons such as Back and Forward.
		location:0, //, // 1 The Location entry field where you enter the URL.
		menubar:0, // The menu bar of the window
		directories:0, // The standard browser directory buttons, such as What's New and What's Cool
		resizable:0, // Allow/Disallow the user to resize the window.
		scrollbars:0, // Enable the scrollbars if the document is bigger than the window
		height:900, // Specifies the height of the window in pixels. (example: height='350')
		width:400, // Specifies the width of the window in pixels.
		x:'center', // position of popup relative to screen/window
		y:'center' // position of popup relative to screen/window

	},
	
	window:null,
	url:null,
	callback:null,
	callbackInterval:null,
	window:null,
	reference:0,
	
	initialize:function(url, options){
		this.url = url;
		this.setOptions(options);
		this.callback = [];
		
		this.reference = 'n'+Popup._referenceCount;
		Popup._referenceCount++;
		
		Popup._reference[this.reference] = this;
	},
	
	set:function(k,v){
		if( typeof(this.options[k]) !== 'undefined' ){
			this.options[k] = v;
			
			if( k === 'x' || k === 'y'){
				this._moveTo();
			}
		}
	},

	get:function(k){
		if( typeof(this.options[k]) !== 'undefined' ){
			return this.options[k];
		}

		return false;
	},
	
	open:function(){
		if( this.window !== null ){
			clearInterval( this.callbackInterval );
			
			// return; // window already opened
		}
		
		var params = [];
		Object.each(this.options, function(value, key){
			if(key=='x' || key=='y'){
				return;
			}
			
			if( key == 'width' || key == 'height' ){
				value = parseInt(value) + 'px';	
			}

			this.push( key + '=' + value );
		}, params );


		this.window = window.open(this.url, 'empty', params.join(',') );
		this._moveTo();
		// start callback checker
		this.callbackInterval = this.callbackIntervalFunction.periodical(200, this);
	},
	
	_moveTo:function(){
		if(!this.window){
			return;
		}
		
		var x = this.get('x');
		var y = this.get('y');
		
		if(typeof(x) == 'string'){
			switch(x){
				case 'center':{
					x = ( window.screen.width - this.get('width') ) / 2;
					break;
				}
				
				case 'left':{
					x = 0;
					break;
				}
				
				case 'right':{
					x = ( window.screen.width - this.get('width') );
					
					break;
				}
			}
		}
		
		if(typeof(y) == 'string'){
			switch(y){
				case 'center':{
					y = ( window.screen.height - this.get('height') ) / 2;
					break;
				}
				
				case 'top':{
					y = 0;
					break;
				}
				
				case 'bottom':{
					y = ( window.screen.height - this.get('height') );
					break;
				}
			}
		}
		
		this.window.moveTo(parseInt(x), parseInt(y));
	},
	
	close:function(){
		if( this.window === null ){
			return; // popup never opened or already closed
		}
		
		this.window.close();
		delete Popup._reference[this.reference];
		Popup._referenceCount--;
		
	},
	
	callbackIntervalFunction:function(){
		try { // try catch inplace because of popup being able to go crossdomain.
			if( this.window.Popup._callback.length > 0 ){
				
				Array.each(this.callback, function(fn, i){
					Array.each(this.window.Popup._callback, function(data, i){
						fn.call(fn, data);
					}, this );
				}, this );

				this.window.Popup._callback.length = 0;
			}
		} catch( err ){ }
	},
	
	addCallback:function(fn){
		this.callback.push(fn);
	},
	getWindow:function(){
		return this.window;
	}
});

Popup._referenceCount = 0;
Popup._reference = {};
Popup._callback = [];
Popup._closeInterval = null;

Popup.fireCallback = function( data ){
	this._callback.push(data);
}

Popup.close = function(){
	
	// popups have boon opened by this window and there for can not be closed
	if( Popup._referenceCount > 0 ){
		return; 
	}
	
	if( this._closeInterval !== null ) {
		if( this._callback.length <= 0 ){
			clearInterval(this._closeInterval);
			this._closeInterval = null;
			window.close();
		}
	} else if( this._closeInterval === null )
	{
		if( this._callback.length <= 0){
			window.close();
		} else {
			this._closeInterval = this.close.periodical(50, this);
		}
		
	}
	
}

window.addEvent('domready',function() {
	new newsChanger();
	
	$$('.newsLink').addEvents({
		mouseover:function() {
			this.getChildren('img').setProperty('src', 'images/pfeile-rot.gif');
		},
		mouseout:function(){
			this.getChildren('img').setProperty('src', 'images/pfeile-dunkel.gif');
		}
	});
	
	$$('.popup').addEvent('click',function(e) {
		e.preventDefault();
		console.log(this.getProperty('href'));
		new Popup(this.getProperty('href'), {
			width:534,
			height:750,
			x:'top',
			y:'left'
		}).open();
	});
});
