var Site = {
	
	address_flag: 0,
	
	
	start: function(){
		MooTools.lang.setLanguage("en-US");
		
		Site.attachPageActions();
		
		
		$$('.mooTable .sdTooltipA').each(function(tooltip,j) {  
			var contents = tooltip.get('title').split('::');  
			tooltip.store('tip:title', contents[0]);  
			tooltip.store('tip:text', contents[1]);  
		});
		
		// Tooltip override handling
		var tooltips = new Tips($$('.mooTable .sdTooltipA'), {
			'maxTitleChars': 	500,
			'className': 		'sdTooltip'
		});
	},
	
	
	attachPageActions: function(){

		// Launch-in-new-window links automagically created
		var extLinks = $$('a.external');
		if ( extLinks.length ) {
			extLinks.each(function(elem, idx) { 
				elem.setProperty('target', '_blank');
			});
		}
		
		
		// Safari Suckerfish 'fix'
		if ( navigator.appVersion.toLowerCase().indexOf('safari') != -1 ) {
			$$('#navigation li a').each(function(elem, idx) {
				elem.set('title', '');
			});
		}
		
		if ( $('text_smaller_link') ) {
			$('text_smaller_link').addEvent('click', function() {
				scaleDown();
				return false;
			});
		}
		if ( $('text_bigger_link') ) {
			$('text_bigger_link').addEvent('click', function() {
				scaleUp();
				return false;
			});
		}
		
		
		// Homepage Feature
		if ( $('home_feature') ) {
			var so = new SWFObject('/swfs/hero.swf','hero-swf','628','245','9');
			so.addParam("wmode","opaque");
			so.useExpressInstall("/swfs/expressinstall.swf");
			so.write('home_feature');
		}
		
		
		// Adding bookmarks
		if ( $('bookmark_link') ) {
			
			if (window.opera) {
				$('bookmark_link').rel = 'sidebar'; // this makes it work in Opera 7+
				$('bookmark_link').title = "Bookmark: " + document.title;
				$('bookmark_link').href = $('bookmark_link').href.substring(0, $('bookmark_link').href.indexOf("#"));
			} else {
				$('bookmark_link').addEvent('click', function() {
					var ua = navigator.userAgent.toLowerCase();
					var isKonq = (ua.indexOf('konqueror') != -1);
					var isSafari = (ua.indexOf('webkit') != -1);
					var isMac = (ua.indexOf('mac') != -1);
					var buttonStr = isMac ? 'Command/Cmd' : 'CTRL';
					try {
						if(window.external && (!document.createTextNode ||
							(typeof(window.external.AddFavorite)=='unknown'))) {
							window.external.AddFavorite(window.location, document.title); // IE/Win
						} else if(isKonq) {
							alert('Please press CTRL + B to bookmark this page.');
						} else if(window.opera) {
							void(0); // do nothing here (Opera 7+)
						} else if(window.home || isSafari || !window.print || isMac) { // Firefox, Netscape, Safari, iCab, IE5/Mac and Safari 1.0
							alert('Please press ' + buttonStr + ' + D to bookmark this page.');
						} else {
							alert('In order to bookmark this page you need to do so manually through your browser.');
						}
					} catch(err) {
						// IE might error out
						alert("Error: " + err.description);
					}
					return false;
					
				});	
			}
		}
		
		
		// Form validation automagic
		var valForms = $$('form.validate-form');
		if ( valForms.length ) {
			valForms.each(function(elem, idx) { 
				new FormValidator.Inline(elem, {
					onFormValidate: Site.formHandler
				});
			});
		}
		
		// Donation Form
		
		var is_gift_group= new Fx.Reveal('donation_memory_of_group');
		var send_acknowledgement_group= new Fx.Reveal('donation_send_acknowledgement_group');
		
		var is_gift_group_validated_fields = $$('#donation_memory_of_group .required');
		var send_acknowledgement_group_validated_fields = $$('#donation_send_acknowledgement_group .required');
		
		var send_acknowledgement_group_restore_validation = function() {
			send_acknowledgement_group_validated_fields.each(function(field) {
				field.addClass('required');
			});
		}
		
		var send_acknowledgement_group_remove_validation = function() {
			send_acknowledgement_group_validated_fields.each(function(field) {
				field.removeClass('required');
			});
		}
		
		var is_gift_group_restore_validation = function() {
			is_gift_group_validated_fields.each(function(field) {
				field.addClass('required');
			});
		}
		
		var is_gift_group_remove_validation = function() {
			is_gift_group_validated_fields.each(function(field) {
				field.removeClass('required');
			});
		}
		
		//Other Purpose Actions
		
		if ($('direction_purpose') && $('other_purpose_container')) {
		
			var other_purpose_container = new Fx.Reveal('other_purpose_container');
			
			if ($('direction_other_purpose').get('value').length >= 1) {
				other_purpose_container.reveal();
				$$('#other_purpose_container input').addClass('required');
			} else {
				other_purpose_container.dissolve();
				$$('#other_purpose_container input').removeClass('required');
			}
			
			$('direction_purpose').addEvent('change',function(event){
				if ($('direction_purpose').get('value') == 'Other, please specify') {
					other_purpose_container.reveal();
					$$('#other_purpose_container input').addClass('required');
				} else {
					other_purpose_container.dissolve();
					$$('#other_purpose_container input').removeClass('required');
				}
			});
		}
		
		//Memory of groups
		
		if ($('donation_memory_of_group')) {
			if ($('is_gift').get('value') !='1') {$('donation_memory_of_group').hide()};
			if ($('send_acknowledgement').get('value') !='1') {$('donation_send_acknowledgement_group').hide()};
			is_gift_group_remove_validation();
			send_acknowledgement_group_remove_validation();
		}
		
		if ($('is_gift')) {
			$('is_gift').addEvent('change', function(event) {
				if ($('is_gift').get('value') == '1') {
					is_gift_group.reveal();
					is_gift_group_restore_validation();			
				} else {
					is_gift_group.dissolve();
					is_gift_group_remove_validation();			
				}
			});
		}
		
		if ($('send_acknowledgement')) {
			$('send_acknowledgement').addEvent('change', function(event) {
				if ($('send_acknowledgement').get('value') == '1') {
					send_acknowledgement_group.reveal();
					send_acknowledgement_group_restore_validation();
				} else {
					send_acknowledgement_group.dissolve();
					send_acknowledgement_group_remove_validation();
				}
			});
		}
		
		//Amount checkboxes
		
		var donate_amount_checkboxes = $$('.donation-amount input[type=checkbox]');
		var donate_amount_other = $('donate_amount_other');
		var donate_amount = $('amount');
		
		if (donate_amount_checkboxes && donate_amount_other && donate_amount) {
		
			donate_amount_checkboxes.addEvent('click', function(e) {
				if (this.checked) {
					donate_amount_checkboxes.set('checked',false);
					this.set('checked',true);
					if (this.get('rel') && this.get('rel') != null) donate_amount.value = this.get('rel');
				} else {
					return false;
				}
			});
			
			donate_amount_other.addEvent('click', function(e) {
				if (this.checked) {
					donate_amount.set('value','');
					donate_amount.focus();
				}
			});
			
			donate_amount.addEvent('focus', function(e) {
				donate_amount_checkboxes.set('checked',false);
				donate_amount_other.set('checked',true);
			});
		
		}
		
		//Fake radio checkboxes
		//If for some reason the client requests the same functionality elsewhere, just wrap your checkboxes in a container with a class of fake_radio, and add a rel attribute to the inputs you wish to be grouped togethor. 
		$$('.fake_radio input[type=checkbox]').each(function(checkbox){
			checkbox.addEvent('click',function(e){
				$$('.fake_radio input[type=checkbox][rel='+this.get('rel')+']').set('checked',false);
				this.set('checked', true);
			});
		});
		
		
		//Company and ABN fields
		
		var abn_setup = function() {
			
			if ($('company') && $('abn')) {
				if ($('company').value.length >=1) {
					$('abn_container').show();
					$('abn').addClass('required');
				} else {
					$('abn_container').hide();
					$('abn').removeClass('required');
				}
			}
						
		}
		
		abn_setup();
		
		if ($('company')) $('company').addEvent('keydown',function() {
			abn_setup();
		});		
		
		
		
		/*
		var donationLookup = ['0','10','20','50','100','250','500'];
		
		if ($('donation_slider')) {
			var donationSlider = new Slider('donation_slider', 'donation_slider_knob', {
				range: [0, 6],
				steps: 6,
				snap: true,
				onChange: function(value){
					$('amount').value = donationLookup[value];
				}
			});
		}
		*/
		if ($('ccv_details')) {
			$('ccv_details').hide();
		
			$('ccv_help').addEvent('click',function() {
				$('ccv_details').toggle();
				return false;
			});
		}
		
		if ($('print_page')) {
			$('print_page').addEvent('click',function(e){
				window.print();
			});
		}
		
		// Country List
		
		if ($('country_list')) {
			$$('#country_list li a').each(function(element,index) {
				element.store('tip:text',  element.get('title'));
			});
			
			var countryTipMandarin = new Tips($('language_mandarin'), {
				fixed: false,
				className: 'tip-container',
				offsets: {x: -53, y: 24 }
				/*offsets: {x: -26, y: 24 }*/
			});
			
			var countryTipCroation = new Tips($('language_croatian'), {
				fixed: false,
				className: 'tip-container',
				offsets: {x: -53, y: 24 }
				/*offsets: {x: -25, y: 24 }*/
			});
			
			var countryTipHindi = new Tips($('language_hindi'), {
				fixed: false,
				className: 'tip-container',
				offsets: {x: -53, y: 24 }
				/*offsets: {x: -17, y: 24 }*/
			});
			
			var countryTipCambodian = new Tips($('language_cambodian'), {
				fixed: false,
				className: 'tip-container',
				offsets: {x: -53, y: 24 }
				/*offsets: {x: -32, y: 24 }*/
			});
			
			$$('#country_list li a img').each(function(elem, idx) {
				elem.set('alt', '');
			});
		}
		
		
		if ( $('query') ) {
			$('query').addEvent('focus', function() {
				if ($('query').value == 'Search') {
					$('query').value = '';
				}
			});
			$('query').addEvent('blur', function() {
				if ($('query').value == '') {
					$('query').value = 'Search';
				}
			});
		}
		
		if ( $('top_username') ) {
			$('top_username').addEvent('focus', function() {
				if ($('top_username').value == 'Username') {
					$('top_username').value = '';
				}
			});
			$('top_username').addEvent('blur', function() {
				if ($('top_username').value == '') {
					$('top_username').value = 'Username';
				}
			});
		}
		
		if ( $('top_password') ) {
			$('top_password').addEvent('focus', function() {
				if ($('top_password').value == 'Password') {
					$('top_password').value = '';
				}
			});
			$('top_password').addEvent('blur', function() {
				if ($('top_password').value == '') {
					$('top_password').value = 'Password';
				}
			});
		}
		
		// Hospital List Drop-down
		if ($('hospital_box')) {
			var hospital_slide = new Fx.Slide('hospital_list',
				{ 
					duration: 400
				}
			);

			$('hospital_list').style.display = 'block';
			hospital_slide.hide();
			
			// Hospital Select Show
			/*$('hospital_box').addEvent('mouseenter', function(e) {
				e.stop();
				hospital_slide.slideIn();
			});
			
			// Hospital Select Hide
			$('hospital_box').addEvent('mouseleave', function(e) {
				e.stop();
				hospital_slide.slideOut();
			});*/
			
			// Hospital Select Hide
			$('hospital_box_title').addEvent('click', function(e) {
				e.stop();
				hospital_slide.toggle();
			});
			
			$('hospital_box_title').addEvent('mouseenter', function(e) {
				this.style.cursor = 'pointer';
			});
		}	
		
		
		if ( $('staff_login') && $$('#staff_login h2 a')[0] ) {
			
			// Staff Login Show
			$('staff_login').addEvent('mouseenter', function(e) {
				$$('#staff_login h2 a')[0].addClass('active');
				$('staff_login_container').style.display = 'block';
			});
			
			// Staff Login Hide
			$('staff_login').addEvent('mouseleave', function(e) {
				$('staff_login_container').style.display = 'none';
				$$('#staff_login h2 a')[0].removeClass('active');
			});
			/*
			$$('#staff_login h2 a')[0].addEvent('click', function(e) {
				alert("Staff Portal Coming Soon");
				return false;
			});
			*/
			if ( $('staff_portal') && $('top_login_form') ) {				
				
				$('staff_portal').addEvent('change', function(e) {
					$('top_login_form').action = "http://staff.southernhealth.org.au/page/" + this.value;
					if ( this.value != "" ) {
						$('top_login_form').action += "/";
					}
				});
			}
		}
		
		
		// supporters rotation
		if ( $('our_supporters_list') ) {
			$$('#our_supporters_list .supporter_logo').each(function(elem, idx) {
				
				if (idx != 0) {
					elem.fade('out');
				}
				
			});
			if ($$('#our_supporters_list .supporter_logo').length > 0)
				setTimeout("Site.rotateNextElement('our_supporters_list', 0)", 7000);
		}
		
		// google addresses
		if ( $$('.address_details')[0] && $$('.address_more_info')[0] ) {
			var address_slides = new Object();
			for (var i=0; i < $$('.address_details').length; i++ ) {
				
				address_slides[i] = new Fx.Slide($$('.address_details')[i],
					{ 
						duration: 400
					}
				);
				address_slides[i].hide();
			}
			for (var i=0; i < $$('.address_more_info').length; i++ ) {			
				$$('.address_more_info')[i].addEvent('click', function(e) {
					for (var j=0; j < $$('.address_more_info').length; j++ ) {
						if (this == $$('.address_more_info')[j] && $$('.address_details')[j]) {
							address_slides[j].toggle();
						}
					}
					return false;
					
				});
			}
			
			
		}
		
		// Contacts Map
		if ( $('hospital_addresses_title') 
			&& $('chs_addresses_title') 
			&& $('community_rehabilitation_addresses_title') 
			&& $('aged_care_addresses_title') 
			&& $('hospital_addresses') 
			&& $('chs_addresses') 
			&& $('community_rehabilitation_addresses') 
			&& $('aged_care_addresses') 
			&& $('map'))
		{
			$('hospital_addresses_title').addEvent('click', function(e) {
				var open_section = false;
								
				if ($('hospital_addresses').style.display == 'none') {
					open_section = true;
				} else  {
					open_section = false;
				}
				  Site.closeContactElements(); 
                  
				if (open_section) {
					$('hospital_addresses').style.display = 'block';
					$('hospital_addresses_title').addClass('toggler_active');
				}
				Site.updateGMapNodes();
				return false;
			});
			$('chs_addresses_title').addEvent('click', function(e) {
				var open_section = false;
				
				if ($('chs_addresses').style.display == 'none') {
					open_section = true;
				} else  {
					open_section = false;
				}
				
				Site.closeContactElements();
				
				if (open_section) {
					$('chs_addresses').style.display = 'block';
					$('chs_addresses_title').addClass('toggler_active');
				}
				Site.updateGMapNodes();
				return false;
			});
			$('community_rehabilitation_addresses_title').addEvent('click', function(e) {
				var open_section = false;
				
				if ($('community_rehabilitation_addresses').style.display == 'none') {
					open_section = true;
				} else  {
					open_section = false;
				}
				
				Site.closeContactElements();
				
				if (open_section) {
					$('community_rehabilitation_addresses').style.display = 'block';
					$('community_rehabilitation_addresses_title').addClass('toggler_active');
				}
				Site.updateGMapNodes();
				return false;
			});
			$('aged_care_addresses_title').addEvent('click', function(e) {
				var open_section = false;
				
				if ($('aged_care_addresses').style.display == 'none') {
					open_section = true;
				} else  {
					open_section = false;
				}
				
				Site.closeContactElements();
				
				if (open_section) {
					$('aged_care_addresses').style.display = 'block';
					$('aged_care_addresses_title').addClass('toggler_active');
				}
				Site.updateGMapNodes();
				return false;
			});
			$('hospital_addresses_title').addEvent('mouseenter', function(e) {
				this.style.cursor = 'pointer';
			});
			$('chs_addresses_title').addEvent('mouseenter', function(e) {
				this.style.cursor = 'pointer';
			});
			$('community_rehabilitation_addresses_title').addEvent('mouseenter', function(e) {
				this.style.cursor = 'pointer';
			});
			$('aged_care_addresses_title').addEvent('mouseenter', function(e) {
				this.style.cursor = 'pointer';
			});
			
			Site.closeContactElements();
			
			GEvent.addDomListener($('map'), "DOMMouseScroll", wheelevent);
			$('map').onmousewheel = wheelevent; 
			Site.updateGMapNodes();
		}
		
		// google map scroller
		if ( $('map') && Site.address_flag ) {
			GEvent.addDomListener($('map'), "DOMMouseScroll", wheelevent);
			$('map').onmousewheel = wheelevent; 
			loadGMap();
		}
		
		// accordion listing
		if ( $('accordion') ) {
			
			//create our Accordion instance
			var myAccordion = new Accordion($('accordion'), 'h3.toggler', 'div.element', {
				alwaysHide: true,
				display: -1,
				opacity: false,

				onActive: function(toggler, element){
					toggler.addClass('toggler_active');
				},
				onBackground: function(toggler, element){
					toggler.removeClass('toggler_active');
				}
			});
			
			// Change the cursor to a hand-pointer
			$$('h3.toggler').each(function(elem, idx) {
				elem.addEvent('mouseenter', function() {
					this.style.cursor = 'pointer';
				});
			});
		}
		
	},
	
	
	closeContactElements: function () {
		$('hospital_addresses').style.display = 'none';
		$('hospital_addresses_title').removeClass('toggler_active');
		$('chs_addresses').style.display = 'none';
		$('chs_addresses_title').removeClass('toggler_active');
		$('community_rehabilitation_addresses').style.display = 'none';
		$('community_rehabilitation_addresses_title').removeClass('toggler_active');
		$('aged_care_addresses').style.display = 'none';
		$('aged_care_addresses_title').removeClass('toggler_active');
	},
	
	rotateNextElement: function(parentId, elementPosition) {
		var nextPosition = 0;
		if ( $(parentId) && $(parentId).children[elementPosition] ) {
			$(parentId).children[elementPosition].get('tween').options.duration=1000;
			$(parentId).children[elementPosition].fade('out');
			
			if ($(parentId).children[elementPosition + 1]) {
				nextPosition = elementPosition + 1;
			}
			
			$(parentId).children[nextPosition].style.position = 'absolute';
			$(parentId).children[nextPosition].get('tween').options.duration=1000;
			$(parentId).children[nextPosition].fade('in');
			
			setTimeout("Site.rotateNextElement('" + parentId + "', " + nextPosition + ")", 4500);
		}
		
	},
	
	updateGMapNodes: function() {
		Site.address_flag = 0;
		if ( $('hospital_addresses') && $('chs_addresses') ) {
			for ( var i=0; i < index; i++ ){
				if ( i <= 4 ) {
					if ( $('hospital_addresses').style.display == 'none') {
						places[i].status = 'Failed';
					} else {
						places[i].status = 'Success';
						Site.address_flag = 1;
					}
				} else if ( i >= 14 && i <= 17 ) {
					if ( $('community_rehabilitation_addresses').style.display == 'none') {
						places[i].status = 'Failed';
					} else {
						places[i].status = 'Success';
						Site.address_flag = 1;
					}
				} else if ( i >= 18 && i <= 22 ) {
					if ( $('aged_care_addresses').style.display == 'none') {
						places[i].status = 'Failed';
					} else {
						places[i].status = 'Success';
						Site.address_flag = 1;
					}
				} else {
					if ( $('chs_addresses').style.display == 'none') {
						places[i].status = 'Failed';
					} else {
						places[i].status = 'Success';
						Site.address_flag = 1;
					}
				}
			}
		}
		
		if ( Site.address_flag ) {
			places[index-1].status = 'Failed';
		} else {
			places[index-1].status = 'Success';
		}
		
		loadGMap();
	},
	
	
	formHandler: function(pass, form, submitEvent) {
		// Do anything necessary here
	}
	
};


// Do stuff on load
window.addEvent('load', Site.start);