
/* TipAFriend Layer */
function initTipAFriend () {
	var link = $(document.body).getElement('.tipAFriend');
	if (!$chk(link)) {
		return false;
	}
	
	var show = false;
			
	$(link).addEvent('click', function(){
		var container = $(document.body).getElement('.tipAFriendForm');
		if (!$chk(container)) {
			return false;
		}

		var containerSlide = new Fx.Morph(container, {duration: 'short', transition: Fx.Transitions.Sine.easeOut});
 		
 		if (show == false) {
			containerSlide.start({
			    'height': [0, 250] 
			});		
 			show = true;	
 		} else {
			containerSlide.start({
			    'height': [250, 0] 
			});		
 			show = false;	 		
 		}
		return false;
		
	});
} 

/* Search field in topnav */
function initSearchField () {
	var search = $('searchfieldHover');
	if (!$chk(search)) {
		return false;
 	}

	var searchfield = $('searchfield');
	if (!$chk(searchfield)) {
		return false;
 	}

	var searchInput = $('searchInput');
	if (!$chk(searchInput)) {
		return false;
 	}
 	
 	var defaultValue = $(searchInput).value;

	var searchSubmit = $('searchSubmit');
	if (!$chk(searchSubmit)) {
		return false;
 	}
	
	//form action dependant on radiobuttons
	$(searchSubmit).addEvent('click', function(){
		var searchForm = $('searchForm');
		var searchContent = $('searchContent');
		/*
		var searchProgram = $('searchProgram');
		*/
		if ($(searchInput).value == defaultValue) {
			$(searchInput).value = '';
		}
		
		//if ($(searchContent).checked == true) {
			//$(searchForm).action = $(searchContent).value;
			$(searchForm).submit();
		/*} else {
			$(searchForm).action = $(searchProgram).value + '&filter[text]=' + $(searchInput).value;
			$(searchForm).submit();
		}
		*/
		return false;
	});

	$(search).addEvent('mouseover', function(){
		this.addClass('searchfieldHover');
		$(searchfield).set('styles', {
			'display': 'block'
		});
		return false;
	});

	$(search).addEvent('mouseout', function(){
		this.removeClass('searchfieldHover');
		$(searchfield).set('styles', {
			'display': 'none'
		});
		return false;
	});

	var mouseoutEvent = function() {
		$(search).removeClass('searchfieldHover');
		this.set('styles', {
			'display': 'none'
		});
		return false;
	}

	var mouseoverEvent = function() {
		$(search).addClass('searchfieldHover');
		this.set('styles', {
			'display': 'block'
		});
		return false;
	}
	
	$(searchfield).addEvent('mouseout', mouseoutEvent);
	$(searchfield).addEvent('mouseover', mouseoverEvent);

	$(searchInput).addEvent('focus', function(){
		$(searchfield).removeEvent('mouseout',mouseoutEvent);
		return false;
	});

	$(searchInput).addEvent('blur', function(){
		$(searchfield).addEvent('mouseout',mouseoutEvent);
		return false;
	});

} 

/* Statements */
/*function initStatements () {
	var shown = Cookie.read('tx_unibaselstatements_pi1');
			if (shown == null) {
				return false;
			} else {
				checkShown = shown.split('-');
			}
	var img = $(document.body).getElements('img.tx-unibaselstatements-shown');	
	for (i=0; i < img.length; i++) {
		id = img[i].id.split('tx-unibaselstatements-');
		if (checkShown.contains(id[1])) {
			$(img[i]).tween('opacity',0.3);
		}
	}
}*/ 


/* Homepage slide */
function initHomeSlide() {
	var checkSlide = Cookie.read('unibasel_homepageslide');
	if (checkSlide != null) {
		return false;
	} else {
		Cookie.write('unibasel_homepageslide', 'slide');
	}
	
	var delayedSlide = function delayedHomeSlide() {
		var unibaselSlide = $('unibasel_slide');
		var footer = $('footer').getCoordinates();
		var footerObj = $('footer');
		if (!$chk(unibaselSlide)) {
			return false;
		}
		if (!$chk(footer)) {
			return false;
		}

		var makeSlide = new Fx.Morph(unibaselSlide, {fps: '200', duration: '1200', transition: Fx.Transitions.Expo.easeOut});	
		makeSlide.start({
			'height': [270, 0] 
		});
		var footerHeight = new Fx.Morph(footerObj, {fps: '200', duration: '1200', transition: Fx.Transitions.Expo.easeOut});	
		footerHeight.start({
			'height': [footer.height, (footer.height + 270)] 
		});

	}

	delayedSlide.delay('3000');
	return false;
}

window.addEvent('domready', function() {
	initHomeSlide();
	//initTipAFriend();
	initSearchField();
	//initStatements();
	
	initScroller();
});









/*
Script: MooScroller.js

Recreates the standard scrollbar behavior for elements with overflow but using DOM elements so that the scroll bar elements are completely styleable by css.

License:
	http://www.clientcide.com/wiki/cnet-libraries#license
*/
var MooScroller = new Class({
	Implements: [Options, Events],
	options: {
		maxThumbSize: 10,
		mode: 'vertical',
		width: 0, //required only for mode: horizontal
		scrollSteps: 10,
		wheel: true,
		scrollLinks: {
			forward: 'scrollForward',
			back: 'scrollBack'
		},
		hideWhenNoOverflow: false
//		onScroll: $empty,
//		onPage: $empty
	},

	initialize: function(content, knob, options){
		this.setOptions(options);
		this.horz = (this.options.mode == "horizontal");

		this.content = document.id(content).setStyle('overflow', 'hidden');
		this.knob = document.id(knob);
		this.track = this.knob.getParent();
		this.setPositions();
		
		if (this.horz && this.options.width) {
			this.wrapper = new Element('div');
			this.content.getChildren().each(function(child){
				this.wrapper.adopt(child);
			}, this);
			this.wrapper.inject(this.content).setStyle('width', this.options.width);
		}
		

		this.bound = {
			'start': this.start.bind(this),
			'end': this.end.bind(this),
			'drag': this.drag.bind(this),
			'wheel': this.wheel.bind(this),
			'page': this.page.bind(this)
		};

		this.position = {};
		this.mouse = {};
		this.update();
		this.attach();
		
		this.clearScroll = function (){
			$clear(this.scrolling);
		}.bind(this);
		['forward','back'].each(function(direction) {
			var lnk = document.id(this.options.scrollLinks[direction]);
			if (lnk) {
				lnk.addEvents({
					mousedown: function() {
						this.scrolling = this[direction].periodical(50, this);
					}.bind(this),
					mouseup: this.clearScroll.bind(this),
					click: this.clearScroll.bind(this)
				});
			}
		}, this);
		this.knob.addEvent('click', this.clearScroll.bind(this));
		window.addEvent('domready', function(){
			try {
				document.id(document.body).addEvent('mouseup', this.clearScroll);
			}catch(e){}
		}.bind(this));
	},
	setPositions: function(){
		[this.track, this.knob].each(function(el){
			if (el.getStyle('position') == 'static') el.setStyle('position','relative');
		});

	},
	toElement: function(){
		return this.content;
	},
	update: function(){
		var plain = this.horz?'Width':'Height';
		this.contentSize = this.content['offset'+plain];
		this.contentScrollSize = this.content['scroll'+plain];
		this.trackSize = this.track['offset'+plain];

		this.contentRatio = this.contentSize / this.contentScrollSize;

		this.knobSize = (this.trackSize * this.contentRatio).limit(this.options.maxThumbSize, this.trackSize);

		if (this.options.hideWhenNoOverflow) {
			this.hidden = this.knobSize == this.trackSize;
			this.track.setStyle('opacity', this.hidden?0:1);
		}
		
		this.scrollRatio = this.contentScrollSize / this.trackSize;
		this.knob.setStyle(plain.toLowerCase(), this.knobSize);

		this.updateThumbFromContentScroll();
		this.updateContentFromThumbPosition();
	},

	updateContentFromThumbPosition: function(){
		this.content[this.horz?'scrollLeft':'scrollTop'] = this.position.now * this.scrollRatio;
	},

	updateThumbFromContentScroll: function(){
		this.position.now = (this.content[this.horz?'scrollLeft':'scrollTop'] / this.scrollRatio).limit(0, (this.trackSize - this.knobSize));
		this.knob.setStyle(this.horz?'left':'top', this.position.now);
	},

	attach: function(){
		this.knob.addEvent('mousedown', this.bound.start);
		if (this.options.scrollSteps) this.content.addEvent('mousewheel', this.bound.wheel);
		this.track.addEvent('mouseup', this.bound.page);
	},

	detach: function(){
		this.knob.removeEvent('mousedown', this.bound.start);
		if (this.options.scrollSteps) this.content.removeEvent('mousewheel', this.bound.wheel);
		this.track.removeEvent('mouseup', this.bound.page);
		document.id(document.body).removeEvent('mouseup', this.clearScroll);
	},

	wheel: function(event){
		if (this.hidden) return;
		this.scroll(-(event.wheel * this.options.scrollSteps));
		this.updateThumbFromContentScroll();
		event.stop();
	},

	scroll: function(steps){
		steps = steps||this.options.scrollSteps;
		this.content[this.horz?'scrollLeft':'scrollTop'] += steps;
		this.updateThumbFromContentScroll();
		this.fireEvent('onScroll', steps);
	},
	forward: function(steps){
		this.scroll(steps);
	},
	back: function(steps){
		steps = steps||this.options.scrollSteps;
		this.scroll(-steps);
	},

	page: function(event){
		var axis = this.horz?'x':'y';
		var forward = (event.page[axis] > this.knob.getPosition()[axis]);
		this.scroll((forward?1:-1)*this.content['offset'+(this.horz?'Width':'Height')]);
		this.updateThumbFromContentScroll();
		this.fireEvent('onPage', forward);
		event.stop();
	},

	
	start: function(event){
		var axis = this.horz?'x':'y';
		this.mouse.start = event.page[axis];
		this.position.start = this.knob.getStyle(this.horz?'left':'top').toInt();
		document.addEvent('mousemove', this.bound.drag);
		document.addEvent('mouseup', this.bound.end);
		this.knob.addEvent('mouseup', this.bound.end);
		event.stop();
	},

	end: function(event){
		document.removeEvent('mousemove', this.bound.drag);
		document.removeEvent('mouseup', this.bound.end);
		this.knob.removeEvent('mouseup', this.bound.end);
		event.stop();
	},

	drag: function(event){
		var axis = this.horz?'x':'y';
		this.mouse.now = event.page[axis];
		this.position.now = (this.position.start + (this.mouse.now - this.mouse.start)).limit(0, (this.trackSize - this.knobSize));
		this.updateContentFromThumbPosition();
		this.updateThumbFromContentScroll();
		event.stop();
	}

});

function initScroller() {
	//console.log($(document.body).getElement('.scroller .scrollercontent'));
	//console.log($(document.body).getElement('.scroller .scrollKnob'));
	if ($chk($(document.body).getElement('.scroller')))
		new MooScroller($(document.body).getElement('.scroller .scrollercontent'), $(document.body).getElement('.scroller .scrollKnob'));
}









