function GetMoon() {

    this.date = new Date();
    
    this.date.setTime(this.date.getTime() + (this.date.getTimezoneOffset()*60000));
    
    var blueMoonDate = new Date(2010, 0, 15, 7, 13, 0);
    
    this.lunarPeriod  = 29*(24*3600*1000) + 12*(3600*1000) + 44*(60*1000) + 2900;
    
    this.moonPhaseTime = (this.date.getTime() - blueMoonDate.getTime()) % this.lunarPeriod;
    this.moonPhaseDate = Math.floor(this.moonPhaseTime/(24*3600*1000));
    
    this.moonPhaseImage = Math.ceil(this.moonPhaseDate/2)+1;
    
    this.percentRaw = (this.moonPhaseTime / this.lunarPeriod);
    this.percent    = Math.round(100*this.percentRaw) / 100;

    if(this.percentRaw < 0.05){
        this.growPhase = 0;
    } else if(this.percentRaw < 0.49){
        this.growPhase = 1;
    } else if(this.percentRaw < 0.55){
        this.growPhase = 2;
    } else if(this.percentRaw <= 0.75){
        this.growPhase = 3;
    } else {
        this.growPhase = 4;
    }
    
}

(function(){

var read = function(option, element){
	return (option) ? ($type(option) == 'function' ? option(element) : element.get(option)) : '';
};

this.Tips = new Class({

	Implements: [Events, Options],

	options: {
		/*
		onAttach: $empty(element),
		onDetach: $empty(element),
		*/
		onShow: function(){
			this.tip.setStyle('display', 'block');
            this.tip.setStyles({
                'marginTop': -this.tip.getElements('.tip-text')[0].getSize().y-46,
                'opacity': 0,
                'visibility': 'hidden'
            }).set('tween', {
                'duration': 200
            }).tween('opacity', 1);
            
		},
		onHide: function(){
			this.tip.setStyle('display', 'none');
		},
		title: 'title',
		text: function(element){
			return element.get('rel') || element.get('href');
		},
		showDelay: 100,
		hideDelay: 100,
		className: 'tip-wrap',
		offset: {x: 16, y: 16},
		fixed: false
	},

	initialize: function(){
		var params = Array.link(arguments, {options: Object.type, elements: $defined});
		this.setOptions(params.options);
		document.id(this);
		
		if (params.elements) this.attach(params.elements);
	},

	toElement: function(){
		if (this.tip) return this.tip;
		
		this.container = new Element('div', {'class': 'tip'});
		return this.tip = new Element('div', {
			'class': this.options.className,
			styles: {
				position: 'absolute',
				top: 0,
				left: 0
			}
		}).adopt(
			new Element('div', {'class': 'tip-top'}),
			this.container,
			new Element('div', {'class': 'tip-bottom'})
		).inject(document.body);
	},

	attach: function(elements){
		$$(elements).each(function(element){
			var title = read(this.options.title, element),
				text = read(this.options.text, element);
			
			element.erase('title').store('tip:native', title).retrieve('tip:title', title);
			element.retrieve('tip:text', text);
			this.fireEvent('attach', [element]);
			
			var events = ['enter', 'leave'];
			if (!this.options.fixed) events.push('move');
			
			events.each(function(value){
				var event = element.retrieve('tip:' + value);
				if (!event) event = this['element' + value.capitalize()].bindWithEvent(this, element);
				
				element.store('tip:' + value, event).addEvent('mouse' + value, event);
			}, this);
		}, this);
		
		return this;
	},

	detach: function(elements){
		$$(elements).each(function(element){
			['enter', 'leave', 'move'].each(function(value){
				element.removeEvent('mouse' + value, element.retrieve('tip:' + value)).eliminate('tip:' + value);
			});
			
			this.fireEvent('detach', [element]);
			
			if (this.options.title == 'title'){ // This is necessary to check if we can revert the title
				var original = element.retrieve('tip:native');
				if (original) element.set('title', original);
			}
		}, this);
		
		return this;
	},

	elementEnter: function(event, element){
		this.container.empty();
		
		['title', 'text'].each(function(value){
			var content = element.retrieve('tip:' + value);
			if (content) this.fill(new Element('div', {'class': 'tip-' + value}).inject(this.container), content);
		}, this);
		
		$clear(this.timer);
		this.timer = this.show.delay(this.options.showDelay, this, element);
        
        var position = element.getPosition();
        var rel = parseInt(element.get('rel')) || 0;
        position.y -= rel;
		this.position((this.options.fixed) ? {page: position} : event);
        this.tip.setStyles({
            'marginTop': -this.tip.getElements('.tip-text')[0].getSize().y-46,
            'opacity': 0,
            'visibility': 'hidden'
        }).set('tween', {
            'duration': 200
        });
	},

	elementLeave: function(event, element){
		$clear(this.timer);
		this.timer = this.hide.delay(this.options.hideDelay, this, element);
		//this.fireForParent(event, element);
	},

	fireForParent: function(event, element){
		if (!element) return;
		parentNode = element.getParent();
		if (parentNode == document.body) return;
		if (parentNode.retrieve('tip:enter')) parentNode.fireEvent('mouseenter', event);
		else this.fireForParent(parentNode, event);
	},

	elementMove: function(event, element){
		this.position(event);
	},

	position: function(event){
		var size = window.getSize(), scroll = window.getScroll(),
			tip = {x: this.tip.offsetWidth, y: this.tip.offsetHeight},
			props = {x: 'left', y: 'top'},
			obj = {};
		
        
        obj['top'] = event.page.y + this.options.offset.y;
        obj['left'] = event.page.x + this.options.offset.x;
        
        if(event.page.x > 850){
            this.tip.addClass('right-tip');
        } else {
            this.tip.removeClass('right-tip');
        }
        
		this.tip.setStyles(obj);
	},

	fill: function(element, contents){
		if(typeof contents == 'string') element.set('html', contents);
		else element.adopt(contents);
	},

	show: function(element){
		this.fireEvent('show', [this.tip, element]);
	},

	hide: function(element){
		this.fireEvent('hide', [this.tip, element]);
	}

});

})()


function insaturate(obj, link1, link2){
    obj.addEvents({
        'mouseover': function(){
            obj.set('src', link2)
        },
        'mouseout': function(){
            obj.set('src', link1)
        }
    });
}

window.addEvent('domready', function(){
    $$('img.desaturate').each(function(item){
        $(new Image()).set('src', item.get('rel'));
        insaturate(item, item.get('src'), item.get('rel'));
    });
    
    
    var onclick = function(){
        $(this).retrieve('fx').toggle();
        return false;
    }
    $$('a.open-next').each(function(item){
        var fx = new Fx.Slide(item.getParent('.level').getNext());
        item.store('fx', fx).addEvent('click', onclick);
        fx.hide();
    });
    
    
    
	$$('.tipz').each(function(element,index) {
		element.store('tip:text', element.getParent('p').getNext('.popup').get('html'));
	});
	var tipz = new Tips('.tipz.white:not(.disable)',{
        className: 'tip-wrap-red',
		fixed: true,
		hideDelay: 100,
		showDelay: 200
	});
	var tipz = new Tips('.tipz.gray:not(.disable)',{
		fixed: true,
		hideDelay: 100,
		showDelay: 200
	});
   
});

