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

Novae.UI.Global	= function()
{
	
	var slideshowID = 'slideshow';
	var slideshow;
	var timer;
		
	var speed 	= 4; // seconds
	
	var init	= function() {
		$('body').addClass('js');
		
		init_homeboxes();
		//initSlideshow();
		search_form();
		init_video_content();
		init_dynamic_maps();
	};

	var init_homeboxes = function() {
		var homeboxes = $('#homeboxes');
		if (homeboxes.length) {
			var boxes = homeboxes.find('div.box1, div.box2, div.box3');
			boxes.each(function(i,o){
				var self = $(o);
				self.addClass('dynamic');
				var h2 = self.find('h2');
				var inner = self.find('.inner');
				var bottom = 0-(inner.height()-h2.height());
				inner.css('bottom', bottom+'px');
				self.hover(function(){
					inner.animate({
						'bottom': 0
					});
				}, function(){
					inner.animate({
						'bottom': bottom
					});
				});
			});	
		}
	};
	
	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/search-button-purple.gif';
		});
		$('#btnSearch').mouseout(function() {
			this.src = '/img/search-button-grey.gif';
		});
	};

	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');
	};
	
	var stopSlideshow = function() {
		clearInterval(timer);
		timer = false;
		$('#stop-slideshow span').text('Start Slideshow');
		$('#stop-slideshow').addClass('paused');
	};
	
	var showNextImage = function() {
		var img = slideshow.find('img.feature:last');
		img.fadeOut(function(){
			img.prependTo(slideshow).show();
		});
		
	};
	
	var initNav = function() {
		var nav_html = '<div id="slideshow-util"><a id="stop-slideshow" class="playing" href="#"><span></span></a></div>';
		slideshow.append(nav_html);
		
		$('#stop-slideshow').click(function(e){
			e.preventDefault();
			if (timer) {
				stopSlideshow();
			}else{
				showNextImage();
				startSlideshow();
			}
			
		});
	};

	var init_video_content = function() {
		
	};
	
	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($) { Novae.UI.Global.init(); });
