window.addEvent('domready', function() {
	site.init();
});

var site = $H({
	Events: {},
	
	init: function(){
		new site._CSS_Browser_Selector();
		this.Links();
		new Fx.SmoothScroll();
		if (Browser.Engine.trident4) {
			site.trident.inlinePng();
		}
	},
	
	_CSS_Browser_Selector: new Class({
		/*
		CSS Browser Selector v0.3.2
		Rafael Lima (http://rafael.adm.br)
		http://rafael.adm.br/css_browser_selector
		License: http://creativecommons.org/licenses/by/2.5/
		Contributors: http://rafael.adm.br/css_browser_selector#contributors
		*/
		ua: navigator.userAgent.toLowerCase(),
		initialize: function () {
			var g = 'gecko';
			var w = 'webkit';
			var s = 'safari';
			var h = document.getElementsByTagName('html')[0];
			var b = [(!(/opera|webtv/i.test(this.ua)) && (/msie\s(\d)/).test(this.ua)) ? ('ie ie' + RegExp.$1) : this.is('firefox/2') ? g + ' ff2' : this.is('firefox/3') ? g + ' ff3' : this.is('gecko/') ? g : /opera(\s|\/)(\d+)/.test(this.ua) ? 'opera opera' + RegExp.$2 : this.is('konqueror') ? 'konqueror' : this.is('chrome') ? w + ' chrome' : this.is('applewebkit/') ? w + ' ' + s + (/version\/(\d+)/.test(this.ua) ? ' ' + s + RegExp.$1 : '') : this.is('mozilla/') ? g : '', this.is('j2me') ? 'mobile' : this.is('iphone') ? 'iphone' : this.is('ipod') ? 'ipod' : this.is('mac') ? 'mac' : this.is('darwin') ? 'mac' : this.is('webtv') ? 'webtv' : this.is('win') ? 'win' : this.is('freebsd') ? 'freebsd' : (this.is('x11') || this.is('linux')) ? 'linux' : '', 'js'];
			var c = b.join(' ');
			h.className += ' ' + c;
			return c;
		},

		is: function (t) {
		        return this.ua.indexOf(t) > -1;
		}
	}),
	
	Links: function(){
		$('prolongplus').addEvent('click:relay(a[rel=external])', function(event, clicked){
  			clicked.set('target','_blank');
		});
	}
});

$extend(site.Events, new Events());


 /**
  * @section Trident
  */


site.trident = {
	inlinePng: function(){
		$$('img').each(function(img) {
			if (img.get('src').test('.png')) {
				img.setStyles({
					'filter': 'progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled="true", src="' + img.get('src') + '", sizingMethod="image")'
				});
				img.set('src', '/images/clear.gif');
			}
		}.bind(this));
	}
};


 /**
  * @section Element Helpers
  */


Element.implement({
	
	
	 /**
	  * @method Center_x
	  *
	  * Centers element horizontally within parent element
	  */

	
	Center_x: function(){
		var width = this.measure(function(){
			return this.getSize().x;
		});
		this.setStyle('left',(this.getParent().offsetWidth * 0.5) - (width * 0.5));
	}
});


 /**
  * @section Smooth Scroll without the hash
  */


SmoothScroll.implement({
    initialize: function (options, context) {
        context = context || document;
        this.doc = context.getDocument();
        var win = context.getWindow();
        this.parent(this.doc, options);
        this.links = this.options.links ? $$(this.options.links) : $$(this.doc.links);
        var location = win.location.href.match(/^[^#]*/)[0] + '#';
        this.links.each(function (link) {
            if (link.href.indexOf(location) != 0) {
                return;
            }
            var anchor = link.href.substr(location.length);
            if (anchor) this.useLink(link, anchor);
        },
        this);
    }
});

/*
---
description:     ScrollSpy

authors:
  - David Walsh (http://davidwalsh.name)

license:
  - MIT-style license

requires:
  core/1.2.1:   '*'

provides:
  - ScrollSpy
...
*/
var ScrollSpy = new Class({

	/* implements */
	Implements: [Options,Events],

	/* options */
	options: {
		min: 0,
		mode: 'vertical',
		max: 0,
		container: window,
		onEnter: $empty,
		onLeave: $empty,
		onScroll: $empty,
		onTick: $empty
	},

	/* initialization */
	initialize: function(options) {
		/* set options */
		this.setOptions(options);
		this.container = document.id(this.options.container);
		this.enters = this.leaves = 0;
		this.max = this.options.max;
	
		/* fix max */
		if(this.max == 0) { 
			var ss = this.container.getScrollSize();
			this.max = this.options.mode == 'vertical' ? ss.y : ss.x;
		}
		/* make it happen */
		this.addListener();
	},

	/* a method that does whatever you want */
	addListener: function() {
		/* state trackers */
		this.inside = false;
		this.container.addEvent('scroll',function(e) {
			/* if it has reached the level */
			var position = this.container.getScroll();
			var xy = position[this.options.mode == 'vertical' ? 'y' : 'x'];
			/* if we reach the minimum and are still below the max... */
			if(xy >= this.options.min && xy <= this.max) {
					/* trigger Enter event if necessary */
					if(!this.inside) {
						/* record as inside */
						this.inside = true;
						this.enters++;
						/* fire enter event */
						this.fireEvent('enter',[position,this.enters,e]);
					}
					/* trigger the "tick", always */
					this.fireEvent('tick',[position,this.inside,this.enters,this.leaves,e]);
			}
			else {
				/* trigger leave */
				if(this.inside)  {
					this.inside = false;
					this.leaves++;
					this.fireEvent('leave',[position,this.leaves,e]);
				}
			}
			/* fire scroll event */
			this.fireEvent('scroll',[position,this.inside,this.enters,this.leaves,e]);
		}.bind(this));
	}
});
