window.addEvent('domready', function(){
	initSizeSelector();
	initFacilityToggle();
	
	if ($$('div.topBannerCarousel').length) {
		new TopBannerCarousel($$('div.topBannerCarousel')[0]);
	}
	
	if ($$('div.village_contact')) {
		$$('div.village_contact').each(function(villageContact) {
			villageContact.addEvent('mouseenter', function(e) {
				//e.target.tween('width', '200px');
				villageContact.setStyle('width', '350px');
			});
			villageContact.addEvent('mouseleave', function(e) {
				//e.target.tween('width', '168px');
				villageContact.setStyle('width', '168px');
			});
		});
	}
	
	if($('flashMap')){
		initFlashMap();
	}
	
	//create our Accordion instance
	if($('accordion')){
		var display = -1;
		
		if(location.hash == "#NSW"){
			display = 0;
		}
		
		
		
		var myAccordion = new Accordion($('accordion'), 'span.toggler', 'div.hidden_element', {
			opacity: false,
			onActive: function(toggler, element){
				//toggler.setStyle('color', '#41464D');
			},
			onBackground: function(toggler, element){
				//toggler.setStyle('color', '#528CE0');
			},
			display:display,
			alwaysHide: true
		});
	}	
	
	
	//resize facebook iFrame to fit its own scrollheight
	if(document.getElement('.social_box iframe')){
		
		//var iframe = document.getElement('.social_box iframe');
		//iframe.setStyle('height', '85px');
		//iframe.setStyle('height', (iframe.scrollHeight+23)+'px'); 
		//console.log(iframe.scrollHeight);
		//console.log(iframe.getElement('.name'))
		
	}
		
	
});





/* inits google map on villages homepage */
function initFlashMap(){
	var flashMap;
	var themePath;
	
	if(location.href.indexOf('retireaustralia.knd.com.au') > 0){
		themePath = 'http://retireaustralia.knd.com.au/wp-content/themes/Retire-Australia';
		apiKey  = 'ABQIAAAA_kpM9lMeP1gJ5BZggQ2PjhQeRR9PBOHqatp6TL7vhAVRGOusOxRpDFrJJxxouZBzgUUysCe83FADxQ';
	}else{
		//themePath = 'http://retireaustralia.com.au/wp-content/themes/Retire-Australia';
		//apiKey  = 'ABQIAAAAADWTM0yYFcrurhPyxzGVWxRQNIMSnH4urCL-3lgI9T9Z3V4GkhSKU2H2NN8zzqBZONrKZzftAI7p7Q';
		themePath = 'http://www.retireaustralia.com.au/wp-content/themes/Retire-Australia';
		apiKey = 'ABQIAAAA7nY_zNPAWS6mrI0V2sRjthRQNIMSnH4urCL-3lgI9T9Z3V4GkhTD9L_0axjSBitllCryIhor6au5WQ';
	}

    var so = new SWFObject(themePath+"/includes/VillagesMap.swf", "Map", "510", "450", "9", "#ffffff");
	so.addParam("allowScriptAccess", "sameDomain");
	so.addParam("allowFullScreen","false");
	so.addParam("quality", "high");
	so.addVariable("key",apiKey); 
	so.addVariable("xmlPath",themePath+"/includes/vm.xml");
	so.addVariable("cssPath",themePath+"/maps.css");
	so.useExpressInstall(themePath+'/includes/expressinstall.swf');
	so.write("flashMap");
}


/* map Rollover function */
function map_onRollover(myId) {
	try{
   		$('Map').nav_Rollover(myId);
    }catch(err){ }
}


/* Shows additional facilities  */
function initFacilityToggle(){

	if($('facilitiesToggle')){

		$('facilitiesToggle').addEvent('click', function(ev){
			if($('facilitiesToggle').hasClass('open')){
				//close it
				
				$('facilitiesToggle').removeClass('open')	
				$('facilitiesToggle').set('html', 'Show All &raquo;');
				$('additional_facilities').morph({opacity: 0, width: 0});
				
			}else{
				//open it
				$('facilitiesToggle').addClass('open')
				$('facilitiesToggle').set('html', 'Hide All &laquo;');
				$('additional_facilities').morph({opacity: 1, width: 190});
				
			}
		});
	}
}


/* Inits font size selector */
function initSizeSelector(){
	if ($('sizeSelector')) {
		$('sizeSelector').addEvent('change', function(){
	
			var req = new Request({
				url:'/',
				async:'false', 
				onSuccess:  function(text){ 

				}.bind(this)		
			}).send("text_size="+$('sizeSelector').get('value'));
		
		
			$$('p,li').each(function(e, i){
			
				if($('sizeSelector').get('value') == 'large'){
					e.setStyle('font-size', 14);
				}else if($('sizeSelector').get('value') == 'xlarge'){
					e.setStyle('font-size', 16);
				}else{
					e.setStyle('font-size', 11);
					if(e.getParent('#sidebar')){
						e.setStyle('font-size', 12);
					}
				}
			
			})
		});
	}
}

var TopBannerCarousel = new Class({
	Implements: [Events, Options],
	
	initialize: function(el) {
		this.el = el;
		this.ctrlEls = el.getElements('ul.carouselCtrl a.ctl');
		this.playCtlEl = el.getElement('ul.carouselCtrl a.playBtn');
		this.pauseCtlEl = el.getElement('ul.carouselCtrl a.pauseBtn');
		this.imageEls = el.getElements('ul.carousel li');
		this.imageElCount = 0;
		
		this.interval = 6000;
		this.periodical = null;
		this.currentImageIndex = null;
		
		if (this.imageEls.length) {
			this.imageElCount = this.imageEls.length;
			this.start();
			
			this.ctrlEls.each(function(ctrl) {
				ctrl.addEvent('click', function(e) {
					e.preventDefault();
					clearInterval(this.periodical);
					var index = e.target.getProperty('href').substr(1);
					this.show(index);
					this.periodical = this.nextItem.periodical(this.interval, this);
					
				}.bind(this));
			}.bind(this));
			
			this.playCtlEl.addEvent('click', function(e) {
				e.preventDefault();
				this.resume();
			}.bind(this));
			
			this.pauseCtlEl.addEvent('click', function(e) {
				e.preventDefault();
				this.pause();
			}.bind(this));
		}
	},
	
	show: function(index) {
		try {
			if (index !== this.currentImageIndex) {
				// Fade out current image
				if (this.currentImageIndex !== null) {
					//new Fx.Tween(this.imageEls[currentIndex], { duration: 250, link: 'ignore' }).start('opacity', 1, 0);
					this.imageEls[this.currentImageIndex].fade('out');
					this.ctrlEls[this.currentImageIndex].removeClass('current');
				}
			
				// Update current image to the new index passed
				this.currentImageIndex = index;
				// Fade in new current Image
				//new Fx.Tween(this.imageEls[currentIndex], { duration: 250, link: 'ignore' }).start('opacity', 0, 1);
				this.imageEls[this.currentImageIndex].fade('in');
				this.ctrlEls[this.currentImageIndex].addClass('current');
			}
		} catch (e) {
			clearInterval(this.periodical);
			this.currentImageIndex = null;
			this.start();
		}
	},
	
	start: function() {
		if (this.imageElCount) {
			this.show(0);
			this.periodical = this.nextItem.periodical(this.interval, this);
		}
	},
	
	pause: function() {
		clearInterval(this.periodical);
	},
	
	resume: function() {
		this.periodical = this.nextItem.periodical(this.interval, this);
	},
	
	nextItem: function() {
		var index = this.currentImageIndex + 1;
		if (index >= this.imageElCount) index = 0;
		this.show(index);
	}
});
