// JavaScript Document
function trace(s) {  try { console.log(s) } catch (e) { alert(s) }}; // only for debug
var lastClicked;
var frameSrc;
var myURI = new URI();

var setLang = function() {
	if (myURI.get('directory').search(/fra/i) >=0 ) MooTools.lang.setLanguage("fr-FR"); 
	if (myURI.get('directory').search(/cat/i) >=0 ) MooTools.lang.setLanguage("ca-CA"); 
	if (myURI.get('directory').search(/esp/i) >=0 ) MooTools.lang.setLanguage("es-ES"); 
}

var changeHeight = function() {
//	return true;
// set up the page height
	pageDims = $('bodyMain').getCoordinates() ; // returns left top height width right integer numbers
	headerDims = $('header').getCoordinates() ; // returns left top height width right integer numbers
	contentDims = $('contentLeft').getCoordinates() ; // returns left top height width right integer numbers
//	smallestMenu = $('topOfBungalows').getCoordinates().height + $('midOfBungalows').getCoordinates().height + $('botOfBungalows').getCoordinates().height;
	footerDims = $('footer_div').getCoordinates() ; // returns left top height width right integer numbers
	if( ( pageDims.height - headerDims.height - contentDims.height - footerDims.height > 40) ) { // page is big enough for inner scrollbar
		bottomShadow = 0;
		$('wrapperBorder').setStyle('height','100%');  // changes the outer height
//		$('wrapperBorder').setStyle('height',$('wrapperBorder').getCoordinates().bottom - bottomShadow);  // changes the outer height
		$('footer_div').setStyles({'bottom':'0','position':'absolute'});  // set footer a little higher for the shadow
		mainHeight = $('footer_div').getCoordinates().top - contentDims.top - 5;  // reasmple where the footer is
//		wrapperHeight = mainHeight +  headerDims.height;
//		$('wrapper').setStyle('height',wrapperHeight);
		$('frame_div').setStyles({'height':mainHeight});
//trace('page height:'+pageDims.height+' header height:'+headerDims.height+' content height:' + contentDims.height +' footer height:' + footerDims.height+' main calc:' + mainHeight+' engine:' + Browser.Engine.name+Browser.Engine.version);
	}
}

var pageSetUp = function() {
	$('linksBottom').setStyle('position','absolute'); 
	var linksBottom = $('linksBottom').getCoordinates() ; // returns left top height width right integer numbers
	var footerDims = $('footer_div').getCoordinates() ; // returns left top height width right integer numbers
	verticalDown = footerDims.bottom - linksBottom.bottom - 15;
//	trace('linksb:'+linksBottom.bottom+' linkst:'+linksBottom.top+' footerb:'+footerDims.bottom+' linksh:'+linksBottom.height);
	$('linksBottom').setStyle('top',verticalDown+'px'); 
	
	return true;
	var myMenu = 'none';
//	if ($chk(frameSrc) && $chk(frameSrc.toURI().get('query') )) {
//		if( $chk(frameSrc.toURI().get('query').parseQueryString().menu )) myMenu= frameSrc.toURI().get('query').parseQueryString().menu ;
//	}	// myMenu contains none or camping or bungalows or activities

//if (myURI.get('directory').search(/esp/i) >=0 ) MooTools.lang.setLanguage("es-ES"); 
	currentFile = (myURI.get('file'));
	if ($chk($('contentLeft').getElement('a[href='+currentFile+']'))) {
		currentPageLink = $('contentLeft').getElement('a[href='+currentFile+']');
		var pageIndicator  = new Element('div', {id: 'pageIndicator'});
		pageIndicator.inject(currentPageLink, 'before');
		pageIndicator.setStyle('opacity',0.7);
		currentPageLink.setStyles({'opacity':0.7,'color':'#222'});
	//	currentPageLink.set('class','temp');
	}

	myMenu = $('contentLeft').getElements('a[href='+currentFile+']').getParent().get('rel'); // myMenu contains none or info or camping or bungalows or activities

	myMenus = $$('#midOfInfo','#midOfCamping','#midOfBungalows','#midOfActivities');
	myMenus.each( function(el) { // each of the menus to move
		if(el.get('rel')==myMenu || el.get('id') == 'midOfInfo' ) return true;
		el.set('myHeight',el.getSize().y); // adds default size to element
    (function(){el.setStyle('height',0)}).delay(0); // close the unused menus menu
		myMenuComponents = $('contentLeft').getElements('div[rel='+el.get('rel')+']');
		el.set('morph', {duration: 1000, transition: 'bounce:out'});
			myMenuComponents.addEvent('mouseenter', function () {
				if(!this.get('clicked') ) {
					el.morph({'height':el.get('myHeight')});
				};
			});
			myMenuComponents.addEvent('mouseleave', function () {
				if(!this.get('clicked') ) {
					el.morph({'height':0});
				};
			});
//			if(el.get('rel')==myMenu) {myMenuComponents.set('clicked',1); lastClicked = el;} else (function(){el.fireEvent('mouseleave')}).delay(0); // set the open menu
//			if(el.get('rel')!=myMenu) (function(){el.setStyle('height',0)}).delay(0); // close the unused menus menu
//			if(el.get('rel')==myMenu) {myMenuComponents.set('clicked',1);} else (function(){el.setStyle('height',0)}).delay(0); // set the open menu
	})
}

function setUpSlimboxSlides() {
	if(!$chk($('photos')) ) return true;
	var mcgPhotos = $('photos').getElements('a');
	mcgPhotos.slimbox({
		loop: false,
//			initialWidth: 1024,
//			initialHeight: 768,
		overlayOpacity: 0.7,
		overlayFadeDuration: 700,
		imageFadeDuration: 1000,
		resizeDuration: 0,
		captionAnimationDuration: 0,
		resizeTransition: Fx.Transitions.Elastic.easeOut,
		//counterText: "slide/diapositiva <strong>{x}</strong> / <strong>{y}</strong>. <br />Arrow/flecha <-- (p) previous/anterior <br />Arrow/flecha --> (n) next/ proxima.<br />esc (q, x) Close/cerrar.",
//		counterText: "This is slide <strong>{x}</strong> of a total of <strong>{y}</strong>.Use left arrow (or p) or right arrow (or n) for previous and next slides. esc (or q) to quit.",
		closeKeys: [27, 88, 67, 81],
		previousKeys: [37, 80, 16],
		nextKeys: [39, 78, 17]
	});
	$('photos').getElements('img').setStyle('opacity',0.7);
	$('photos').getElements('img').addEvent('mouseenter', function () {
		this.setStyle('opacity',1);
	});
	$('photos').getElements('img').addEvent('mouseleave', function () {
		this.setStyle('opacity',0.7);
	});
}

var addContactFormValidation = function() {
	var myValidator = new FormValidator.Inline($('frmEmail'), {
		useTitles:true,
		onFormValidate: function(passed, form, event) {
//			event.preventDefault(); //stop the normal behavior of submitting the form.
			if (passed) {
//				form.send(); //send via ajax
				return true;
//				postPage( form );
			}
		}
	});
	//add a validator for captcha
	myValidator.add('WorxTuringTest', {
		errorMsg: 'Try entering the security code again',
		test: function(element){
			if(element.value.length ==0) return false;
			if(hex_md5(element.value) != $('captcha').get('src').replace(/^.*Captcha\.php\?/,'') ) return false;
			else {return true;}
		}
	});/**/
}

var addCaptchaClick = function() { // change click on captcha image
	// add click to captcha image
	if( $chk( $('captcha') )) {
		$('captcha').removeEvents();
		$('captcha').addEvent('click', function() {
			var req = new Request({ //New object
//				method: 'get',
				url:'/Library/3dCaptcha-1.0.0/src/TextGen.php', //Catches the href of the link
				data:'showWord=true',
				evalScripts: false,  // no js is executed from the loaded page
				onSuccess: function(html) { 
					$('captcha').set('src','/Library/3dCaptcha-1.0.0/src/3DCaptcha.php?'+html);
				},
				onFailure: function() {
					alert('failed, sorry')
	//				$('frame_div').set('text', 'The request failed.<br>URL:' + url);
				}
			}).get();
			return false;  // stops the original link from working
		})
	}
}

var test = function() {
/*
	trace('hello');
//	DD_roundies.addRule('.curve15', '10px');
// DD_roundies.addRule('.curve15', '15px');
					$$('.curve5').each(function(el){el.borderRadius( {'radius': 25} );});
          $$('.curve10').each(function(el){el.borderRadius( {'radius': 50} );});
          $$('.curve30').each(function(el){el.borderRadius( {'radius': 150} );});
          $$('h4').each(function(el){el.borderRadius( {'radius': 25} );});
          $$('h3').each(function(el){el.borderRadius( {'radius': 50} );});
          $$('h2').each(function(el){el.borderRadius( {'radius': 100} );});
// 					$('#menu').getElements('a').each(function(el){el.borderRadius( {'radius': 150} );});
          $$('h3').each(function(el){el.borderRadius( {'radius': 100} );});
 */
 //$('wrapperBorder').borderRadius( {'radius': 10} );
}

window.addEvent('domready', function() { //here we add functions that fire when the dom is ready.. images probably not ready
	setLang();
	changeHeight();
	pageSetUp();
	setUpSlimboxSlides();
//	test();
	if(myURI.get('file').search('contact.php') >= 0) { addContactFormValidation();addCaptchaClick() }
	try{pageTracker._trackPageview(href); } catch(err) {};  // this for google analytics
});

window.addEvent('load', function() { //here we add functions that fire when the page is ready
//	probably nothing here;
});

