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

@author: Johan Van Den Rym
@updated: 26.12.2011
*/


var Esn = {

	init: function(){
		//toolbar
		Esn.formInputPlaceholder();
		Esn.loginMessage();
		Esn.printPage();
		//scroll & lightbox (can be used on any page)
		Esn.smoothScroll();
		Esn.lightBox();
		//homepage
		var newsBoxes = $$(".three-col-gray-box-col");
		if(newsBoxes)Esn.equalHeightBox();
		//portfolio
		var tooltips = $$('a.tipper');
		if(tooltips) {Esn.portfolioToolTip();}
		//contact
		Esn.printGmap();
		//vacancies
		var accordion = $('accordion');	
		if(accordion){Esn.vacanciesToggler();}
		
	},
	//Clears and resets to default value inputs of login form
	formInputPlaceholder: function(){
		var inputPlaceHolder = $$('input[placeholder]');
		var labelPlaceHolder = $$('label.placeholder');
			
		inputPlaceHolder.each(function(inp){
			new PlaceholderInput(inp);
		 });
		
		labelPlaceHolder.each(function(lbl){
			var target = $pick($(lbl.get('for')), lbl.getNext());
			if (target && (target.get('type') == 'text' || target.get('tag') == 'textarea')){
			target.set('placeholder', lbl.get('text'));
			lbl.destroy();
			new PlaceholderInput(target);
			}
		});
	},
	//Toggles job offers on working at esn page
	vacanciesToggler: function(){
	  //Default all job offers closed
		var displayValue = -1;
		
		//Opens selected vancancy based on anchor in uri
		var togglers = $$(".component-toggler");
		if(document.location.hash && togglers.length>0) {
			var togglerId = window.location.hash.substr(1);
			togglers.each(function(toggler,index){
			//Checks if toggler id exists
				if(toggler.id == togglerId) {
						//Sets which toggler to open
						displayValue = index;
						var skroll = new Fx.Scroll(window).toElement(toggler);  
				}
			});	 
		}
		// vacancies accordion
		
		var isIE6 = /msie|MSIE 6/.test(navigator.userAgent);
		var myAccordion = new Accordion(this.accordion,'.component-toggler', '.element', {
			opacity: true,  
			alwaysHide: true, // lets you open and close
			display: displayValue, // determines which to toggle
			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', '');
					}
				}
			}
		});
	},
	//Open vacancy on page load
	
	//Page print button
	printPage: function(){
		var printBttn = $('printBttn');
			if (printBttn) {
				printBttn.addEvent('click',function () { 
				window.print();
				});
			}	
	},
	//Gmap print link
	printGmap: function(){
		var printMapBttn = $('print-google-esn-map');
		if (printMapBttn) {
			printMapBttn.addEvent('click',function (evt) {
				evt.stop();Esn.printFriendlyGoogleMap();
			});
		}
	},
	//Captures innerHTML of gMap in new page and initiates print dialogue box
	printFriendlyGoogleMap: function(){
		var gmap = $('gmap');
		var MapPrintFriendlyPage = window.open("http://www.esn.eu/print-esn-map.html","");
		var oMap = gmap.innerHTML;
		
		MapPrintFriendlyPage.document.open();
		MapPrintFriendlyPage.document.write(oMap);
		MapPrintFriendlyPage.document.close();
		MapPrintFriendlyPage.print();
	}, 
	//Animated styled tooltips for portfolio pages
	portfolioToolTip: 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
			});	
		}		
	},
	//See portfolio detail pages... intended to scroll from one tab to other smoothly
	smoothScroll: function(){
		var smoothAnchors = $$('.smoothAnchors');
			if(smoothAnchors.length > 0)	{
				new SmoothScroll({ duration:700, transition: Fx.Transitions.sineInOut }, window); 
		}	
	},
	//Lightbox can be used on any page.
	lightBox: function () {
		var lightBoxLinks = $$('a.boxed');
		if (lightBoxLinks && lightBoxLinks.length > 0) {
			SqueezeBox.assign(lightBoxLinks, {
				parse: 'rel'
			});
		}
	},
	//Adds equal height to homepage's  news' section
	equalHeightBox: function () {
		var isIE6 = (navigator.userAgent.toLowerCase().indexOf('msie 6') != -1);
		var oCols = $$(".three-col-gray-box-col");
		var max_height = 0;
		
		if (oCols.length > 0) {
			oCols.each(function(item2,ix) { 
				max_height = Math.max(max_height, item2.getSize().y);
			});
		}	
			
		oCols.setStyle("min-height",max_height);
		if (isIE6) {oCols.setStyle("height",max_height);}
	},
	//login
	loginMessage: function () {
		var formSubmit = $('log');
		if (formSubmit) {
			formSubmit.addEvent('click',function (event) { event.preventDefault();alert("Username and password are not correct"); });
		}
	}
};

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