if (typeof(CIFS) == 'undefined') {
	CIFS	= {};
	CIFS.UI	= {};
}

CIFS.UI.Global	= function()
{
	
	var slideshowID = 'slideshow';
	var slideshow;
	var timer;
		
	var speed 	= 10; // seconds
	
	var init	= function() {
		$('body').addClass('js');
		initSlideshow();
		search_form();
		initLoginForm();
		init_dynamic_maps();
	};

	var initSlideshow = function() {
		slideshow = $('#'+slideshowID);
		
		if (slideshow.length) {
		
			initNav();
		
			$.get('/_ajax/slideshow/get', function(data){
				slideshow.find('img.feature:first').before(data);
				slideshow.find('img.initial').removeClass('initial');
				slideshow.find('img.feature:last').remove();
				startSlideshow();
			});
			
		}
	};
	
	var startSlideshow = function()	{
		timer = setInterval(function(){
			showNextImage();
		}, speed*1000);
		$('#stop-slideshow span').text('Stop Slideshow');
		$('#stop-slideshow').addClass('playing');
		$('#stop-slideshow').removeClass('paused');
	};
	
	var stopSlideshow = function() {
		clearInterval(timer);
		timer = false;
		$('#stop-slideshow span').text('Start Slideshow');
		$('#stop-slideshow').addClass('paused');
		$('#stop-slideshow').removeClass('playing');
	};
	
	var showNextImage = function() {
		var img = slideshow.find('img.feature:last');
		img.fadeOut(function(){
			img.prependTo(slideshow).show();
		});	
	};
	
	var showPrevImage = function() {
		slideshow.find('img.feature:first').hide().appendTo(slideshow).fadeIn();
	};
	
	var search_form = function() {
		$('#q').focus(function(){
			if (this.value == this.defaultValue) this.value = '';
		}).blur(function(){
			if (this.value == '') this.value = this.defaultValue;
		});
		
		$('#btnSearch').mouseover(function() {
			this.src = '/img/cifs/search-button-over.png';
		});
		$('#btnSearch').mouseout(function() {
			this.src = '/img/cifs/search-button.png';
		});
	};
	
	var initNav = function() {
		var nav_html = '<div id="slideshow-util"><a id="stop-slideshow" class="playing" href="#"><span></span></a><a id="slideshow-prev" href="#"><span>Back</span></a><a id="slideshow-next" href="#"><span>Next</span></a></div>';
		slideshow.append(nav_html);
		
		$('#stop-slideshow').click(function(e){
			e.preventDefault();
			if (timer) {
				stopSlideshow();
			}else{
				showNextImage();
				startSlideshow();
			}
			
		});
		
		$('#slideshow-prev').click(function(e){
			e.preventDefault();
			stopSlideshow();
			showPrevImage();
		});
		
		$('#slideshow-next').click(function(e){
			e.preventDefault();
			stopSlideshow();
			showNextImage();
		});
	};
	
	var initLoginForm = function() {
		$('#newpswd').click(function(e) {
			
			if($('#userid').val() == '') {
				e.preventDefault();
				alert('To request a new password, please enter your current username in the username box above.');
			} 
			
		});
	};
	
	var init_dynamic_maps = function() {
		$('img.static-map').each(function(i, o){
			var staticmap = $(o);
			var loc_string = staticmap.attr('title');
			staticmap.wrap('<div class="dynamic-map"></div>');
			var mapdiv = staticmap.parent('div');
			var latlng = new google.maps.LatLng(-34.397, 150.644);
			geocoder = new google.maps.Geocoder();
			var opts = {
				zoom: 17,
				centre: latlng,
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};
			map = new google.maps.Map(mapdiv.get(0), opts);
			
			geocoder.geocode( { 'address': loc_string}, function(results, status) {
			if (status == google.maps.GeocoderStatus.OK) {
					map.setCenter(results[0].geometry.location);
					var marker = new google.maps.Marker({
						map: map,
						position: results[0].geometry.location
					});
				}
			});
		});
		
	};

	return {
		init: init
	};
	
}();



jQuery(function($) { CIFS.UI.Global.init(); });
