// JavaScript Document

// Blocks
var BlockLinks = {
	  
		config: {
			hoverColor: '#252C30',
			duration: 200
		},
		
	  init: function(options){
			  
				options = Object.extend(this.config, options || {});
				
				var blockSet = $$('.block');
				if (blockSet) {
					 blockSet.each(function(element){
							
							var fx = new Fx.Style(element, 'background-color', { duration: options.duration, wait: false });
							
							var bubble = element.getElement('p[class=bubble]');
							if (bubble) {
								  // bubble clon
							    var tempBubble = bubble.clone().injectAfter('body').addClass('temp-bubble').setStyle('display','block');
									var tempBubbleFx = new Fx.Style(tempBubble, 'opacity', { duration: options.duration, wait: false }).hide();
									// setting right position
									tempBubble.setStyles({
									  'left' : (element.getCoordinates().left - $('page-wrapper').getCoordinates().left)+10,
										'top' : (element.getCoordinates().top)+40
									});
							}
							
							
							element.setStyle('cursor','pointer');
							
							// Disable link
							element.getElement('a').onclick = function(event){
								 event = new Event(event);
								 event.preventDefault();
							}
							
							// Effect
							element.addEvent('mouseenter', function(){
								 fx.start(options.hoverColor);
								 if (bubble){
  								   tempBubbleFx.start(1);
								 }
							});
							element.addEvent('mouseleave', function(){
								 fx.start('#384046');
								 if (bubble){
									   tempBubbleFx.start(0);
								 }
							});
							
							// Onclick event						
							element.addEvent('click', function(){
								window.location=this.getElement('a').getProperty('href');
							});
					});
				}
				
		}
};

var Reference = {
	  
		config: {
	    toggleSelector : '.info-toggler',
			slideSelector : '.info',
			duration : 500
		},
		
		init: function(options){
			  
				options = Object.extend(this.config, options || {});
				
				var togglers = $$(options.toggleSelector);
				var slides = $$(options.slideSelector);

				togglers.each(function(element){
				    
						// img
						var img = new Element('img',{ 'src' : '/img/info-button-down-off.gif' });
						img.injectInside(element);
						
						var slideFx = new Fx.Slide(slides[togglers.indexOf(element)], { duration: options.duration, transition: Fx.Transitions.Cubic.easeOut }).hide();
						
						element.addEvent('click', function(e){
						  e = new Event(e);
							if (this.hasClass('on')) {
						      slideFx.slideOut();
									this.getElement('img').setProperty('src','/img/info-button-down-off.gif');
							} else {
								  slideFx.slideIn();
									this.getElement('img').setProperty('src','/img/info-button-up-off.gif');
							}
							this.toggleClass('on');
							e.stop();
						});
				});
				
		}
};

var pageScroll = {
	roll: function() { new SmoothScroll ({duration: 800,transition:Fx.Transitions.Expo.easeInOut}); }
};

var BlockScroll = {
	  
		init: function(){
			var slideEffect = new Fx.Style('link-block', 'margin-top', {wait:false, duration:800, transition:Fx.Transitions.Cubic.easeOut});
			//window.addEvent('load', function() {
			window.addEvent('domready', function() {
				var top = $('link-block').getPosition().y - 30;
				slideEffect.start.delay(0, slideEffect, Math.max(0, document.documentElement.scrollTop - top).toInt());
				window.addEvent('scroll', function(){
					slideEffect.start.delay(0, slideEffect, Math.max(0, document.documentElement.scrollTop - top).toInt());
				});
			});
		}
};

window.addEvent('domready', function(){
																		 
	BlockLinks.init();
	
	if ($('page-reference')) {
		       Reference.init();
					 pageScroll.roll();
					 BlockScroll.init();
					 
					 
	}
	
	// Giant logo
	$('giant-img').setStyle('cursor','pointer');
	
});