/*
 ______  _____   _   __
   / ____/ / ___/  / | / /
  / __/    \__ \  /  |/ / 
 / /___ _ ___/ / / /|  /_ 
/_____/(_)____(_)_/ |_/(_)

*/

// TOGGLE VACANCIES SECTIONS

window.addEvent('domready', function() {
	if (!$('accordion')) return;
	var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
	var myAccordion = new Accordion($('accordion'),'.component-toggler', '.element', {
			opacity: true,  
			alwaysHide: true, // lets you open and close
			display: -1, // all closed on page load
			onActive: function(toggler, element){
					 toggler.addClass('active'); 
			},
			onBackground: function(toggler, element){
				toggler.removeClass('active');  // removing the class
			}
		,
	// check if height gets adjusted accordingly
				onComplete: function(toggler, element) { 
					var el = $(this.elements[this.previous]);
				if (isIE6) { 
					  var contentC = $('content-c').getStyle('height');
					  $('outer').setStyle('height',contentC);
					}
				else { 
					if (el) {
						  // fix to toggle sections full height
					if (el.offsetHeight > 0) el.setStyle('height', '');
				}
			}
		}
	});
});

// CHECK VALUES FORM FIELDS
window.addEvent('domready', function(){
	   $$('input[placeholder]').each(function(el){
		  new PlaceholderInput(el);
	   });

	   $$('label.placeholder').each(function(el){
			var target = $pick($(el.get('for')), el.getNext());
			if (target && (target.get('type') == 'text' || target.get('tag') == 'textarea')){
			target.set('placeholder', el.get('text'));
			el.destroy();
			new PlaceholderInput(target);
		}
	});
});


window.addEvent('domready', function(){
	// REMOVE OUTLINE
	$$('a img, a').each(function(el) { 
		el.addEvent('focus', function(e) {
			this.blur();  
		});  
	});
	// PRINT FUNCTIONS
	var printBttn = $('printBttn');
	if ($('printBttn')) printBttn.addEvent('click',function () { window.print();});
	
	function printFriendlyGoogleMap(){
		var MapPrintFriendlyPage = window.open("print-esn-map.html","")
		var oMap = $('gmap').innerHTML;
	
		MapPrintFriendlyPage.document.open();
		MapPrintFriendlyPage.document.write(oMap);
		MapPrintFriendlyPage.document.close();
		MapPrintFriendlyPage.print();
	} 
	
	// CAPTURE GOOGLEMAP AND PRINT
	var printMapBttn = $('print-google-esn-map');
	if (printMapBttn) printMapBttn.addEvent('click',function (evt) {evt.stop();printFriendlyGoogleMap();
	});

	// FORM CHECK FOR VALUES
	var formSubmit = $('log');
	if (formSubmit) formSubmit.addEvent('click',function (event) { alert("Username and password are not correct");event.preventDefault(); });
});



// fix hash for vacancies
var idToggler;
window.addEvent('domready',function(){
$$('.component-toggler').addEvent('click',function (ele) { 
		var oLink = window.location.href;
		idToggler = this.id;											   
		
		if (window.location.hash) {
			var oLink = oLink.split('#');
			var oLinkFirstPart = oLink[0];
			var LinkWithHashArray = new Array(oLinkFirstPart,idToggler);
			var newLink =  LinkWithHashArray.join('#');
			window.location =  newLink;
			}
		else {
				var oLinkArray = new Array(oLink,idToggler);
				var newLink =  oLinkArray.join('#');
			  	window.location = newLink;
			 }
		});
});

// open selected vacancy
function checkHash(){
	if(document.location.hash) {
		var togglerId = window.location.hash.substr(1);
		if ($$('.component-toggler').length > 0)  {
		// fire click event to open section of accordion
			if ($(togglerId)) { 
				$(togglerId).fireEvent('click');
				var skroll = new Fx.Scroll(window).toElement($(togglerId));  
			}
		}
	//	if ( $$('.smoothAnchors').length > 0)  {
//		//
//		}
	}
}
  
window.addEvent('domready', function() {
		(function() {checkHash(); }).delay(1000);
});


/* lazyload: implement later */
var LazyLoad = new Class({
	
	Implements: [Options,Events],
	
	/* additional options */
	options: {
		range: 200,
		image: 'blank.gif',
		resetDimensions: true,
		elements: 'img',
		container: window
	},
	
	/* initialize */
	initialize: function(options) {
		
		/* vars */
		this.setOptions(options);
		this.container = $(this.options.container);
		this.elements = $$(this.options.elements);
		this.containerHeight = this.container.getSize().y;
		this.start = 0;
	
		/* find elements remember and hold on to */
		this.elements = this.elements.filter(function(el) {
			/* reset image src IF the image is below the fold and range */
			if(el.getPosition(this.container).y > this.containerHeight + this.options.range) {
				el.store('oSRC',el.get('src')).set('src',this.options.image);
				if(this.options.resetDimensions) {
					el.store('oWidth',el.get('width')).store('oHeight',el.get('height')).set({'width':'','height':''});
				}
				return true;
			}
		},this);
		
		/* create the action function */
		var action = function() {
			var cpos = this.container.getScroll().y;
			if(cpos > this.start) {
				this.elements = this.elements.filter(function(el) {
					if((this.container.getScroll().y + this.options.range + this.containerHeight) >= el.getPosition(this.container).y) {
						if(el.retrieve('oSRC')) { el.set('src',el.retrieve('oSRC')); }
						if(this.options.resetDimensions) {
							el.set({
								width: el.retrieve('oWidth'),
								height: el.retrieve('oHeight') 
							});
						}
						this.fireEvent('load',[el]);
						return false;
					}
					return true;
				},this);
				this.start = cpos;
			}
			this.fireEvent('scroll');
			/* remove this event IF no elements */
			if(!this.elements.length) {
				this.container.removeEvent('scroll',action);
				this.fireEvent('complete');
			}
		}.bind(this);
		
		/* listen for scroll */
		this.container.addEvent('scroll',action);
	}
});

// tooltips

window.addEvent('domready', function(){
		if ($$('a.tipper').length > 0) {	 
		new MooTooltips({
			hovered:'.tipper',		// the element that when hovered shows the tip
			ToolTipClass:'ToolTips',	// tooltip display class
			toolTipPosition:-1, // -1 top; 1: bottom - set this as a default position value if none is set on the element
			showDelay: 100,
			sticky: true,		// remove tooltip if closed
			fromTop: -10,		// distance from mouse or object fromTop: -184
			fromLeft: -250,	// distance from left
			duration: 300,		// fade effect transition duration
			fadeDistance: 20    // the distance the tooltip starts the morph
			});	
		}	
// SCROLLER

	if($$('.smoothAnchors').length > 0)	{
		//smooooooth scrolling enabled
		new SmoothScroll({ duration:700 }, window); 
		//transition: Fx.Transitions.sineInOut});
	}
});