/* ---------------------------------------------------------------------------------------------------- */
/* =outbox.js */
/* ---------------------------------------------------------------------------------------------------- */

/* constructor */
function outboxframework () {};
var outbox = new outboxframework();

/* init */
outboxframework.prototype.init = function () {
	this.navigation.init();
}


/* -------------------------------------------------- */
/* =navigation */

/* constructor */
function navigation () {};
outboxframework.prototype.navigation = new navigation();

/* init */
navigation.prototype.init = function () {
	this.container = mxm.dom.getElement("main-navigation");
	this.mainnav_items = mxm.dom.getElementsByTagAndClassName("li","main",this.container);
	this.attach();
}

/* attach */
navigation.prototype.attach = function () {
	for (var i=0; i < this.mainnav_items.length; i++) {
		this.mainnav_items[i].onmouseover = this.subnav_show.bind(this, this.mainnav_items[i]);
		this.mainnav_items[i].onmouseout = this.subnav_hide.bind(this, this.mainnav_items[i]);
	}
}

/* subnav_show */
navigation.prototype.subnav_show = function (mainnav_item) {
	mxm.dom.addElementClass(mainnav_item,"over");
}

/* subnav_hide */
navigation.prototype.subnav_hide = function (mainnav_item) {
	mxm.dom.removeElementClass(mainnav_item,"over");
}




/* -------------------------------------------------- */
/* =favourites */

function Favorites() {
    if ((navigator.appName == "Microsoft Internet Explorer") && (parseInt(navigator.appVersion) >= 4)) {
		var url="http://www.outbox.ch/"
        var title="Outbòx - Bringt Ideen zum Ausdruck."
        window.external.AddFavorite(url,title)
    } else {
        var FavText = "Outbòx.ch";
        if(navigator.appName == "Netscape") FavText += " mit den Tasten STRG+D.";
        FavText += " zu den Favoriten hinzufügen";
        alert(FavText);
    }
}


/* ---------------------------------------------------------------------------------------------------- */
/* =writeSWF */
/* ---------------------------------------------------------------------------------------------------- */

function writeSWF (obj, params, vars) {
	this.obj = obj;
	this.params = params;
	this.vars = vars;
	this.swf = new SWFObject(this.obj.url,this.obj.name,this.obj.width,this.obj.height,"8","#EDEDED");
	for (key in this.vars) this.swf.addParam(key, this.vars[key]);
	for (key in this.vars) this.swf.addVariable(key, this.vars[key]);
	this.swf.write(this.obj.container);
}

/* -------------------------------------------------- */
/* =news */
function news() {};
outboxframework.prototype.news = new news();

/* init */
news.prototype = {
	init: function () {
		this.container = $('sidebar-wrapper');
		this.containerHeight = 0;
		this.position = 0;
		
		//
		// sidebar
		//
		this.sidebar = $('sidebar');
		this.sidebar.setStyles({
			'width': 2000,
			'position': 'absolute',
			'left': '0',
			'top': '0'
		});
		
		// animation
		this.fx = new Fx.Tween(this.sidebar, {
			transition: 'sine:in:out',
			duration: 500,
			link: 'cancel'
		});
		this.fx.set('left', 0);
		
		//
		// news-item
		//
		this.elements = this.sidebar.getElements('.news-item');
		this.elements.each(function (item) {
			// calc max container height
			var height = item.getStyle('height').toInt();
			if(height > this.containerHeight) {
				this.containerHeight = height;
			}
			// styles
			item.setStyles({
				'width': 150,
				'float': 'left',
				'margin-right': 20
			});
		}, this);
		
		// container
		this.container.setStyles({
			'overflow': 'hidden',
			'height': this.containerHeigt,
			'width': '170px'
		});
		
		//
		// triggers
		//
		this.triggers = {
			next: $('news-triggers').getElement('a[href$=next]'),
			back: $('news-triggers').getElement('a[href$=back]')
		}
		this.triggers.next.getParent().setStyle('display', 'inline');
		this.triggers.next.addEvent('click', this.move.bindWithEvent(this, 'next'));

		this.triggers.back.getParent().setStyle('display', 'inline');
		this.triggers.back.addEvent('click', this.move.bindWithEvent(this, 'back'));
		
		// start timer
		this.timer = (function () { this.moveNext(); }).bind(this).periodical(5000);
	},
	move: function (event, dir) {
		event.stop();
		
		$clear(this.timer);
		
		if((this.position > ((this.elements.length-1) * -170)) && (dir == 'next')) {
			this.moveNext();
		} else if((this.position < 0) && (dir == 'back')) {
			this.moveBack();
		} else {
			this.fx.start('left', 0);
			this.position = 0;
		}
	},
	moveNext: function () {
		if(!(this.position > ((this.elements.length-1) * -170))) {
			this.fx.start('left', 0);
			this.position = 0;
		} else {
			this.position = this.position -170;
			this.fx.start('left', this.position);
		}
	},
	moveBack: function () {
		this.position = this.position +170;

		this.fx.start('left', this.position);
	}
}

window.addEvent('domready', function () {
	if($('sidebar-wrapper')) news.prototype.init();
});
