/*------------------------------------------------------------------------------
	Clients Fader
------------------------------------------------------------------------------*/
	
//	Image1= new Image(870,129);
//	Image1.src = "http://www.thg.com.au/workspace/css/images/header-back.jpg";

/*------------------------------------------------------------------------------
	Clients Fader
------------------------------------------------------------------------------*/
	$(window).load(function() {
		$("#clients p.logo img").hide();
		$("#clients p.logo img:first-child").show();
	});
	
	function clients_fader() {
		setTimeout(clients_fader_tick, 6000);
	}
	
	function clients_fader_tick() {
		var current = $('#clients p.logo img.current');
		var next = current.next();
		
		// Restart:
		if (current.get(0) == $('#clients p.logo img:last').get(0)) {
			next = $('#clients p.logo img:first');
		}
		
		// Fade across:
		next.fadeIn(2000, function() {
			current.fadeOut(2000, function() {
				current.removeClass();
				next.removeClass();
				next.addClass("current");
			});
		});
		
		clients_fader();
	}
	
/*------------------------------------------------------------------------------
	Slideshow Fader
------------------------------------------------------------------------------*/
	
	function slideshow_fader() {
		setTimeout(slideshow_fader_tick, 12000);
	}
	
	function slideshow_fader_tick() {
		var current = $('#clients p.slideshow img.current');
		var next = current.next();
		
		// Restart:
		if (current.get(0) == $("#clients p.slideshow img:last").get(0)) {
			next = $("#clients p.slideshow img:first");
		}
		
		// Fade across:
		next.fadeIn(2000, function() {
			current.fadeOut(2000, function() {
				current.removeClass();
				next.removeClass();
				next.addClass("current");
			});
		});
		
		slideshow_fader();
	}
	
/*------------------------------------------------------------------------------
	Awards Fader
------------------------------------------------------------------------------*/
	
	function awards_fader() {
		setTimeout(awards_fader_tick, 6000);
	}
	
	function awards_fader_tick() {
		var current = $('#awards img.current');
		var next = current.next();
		
		// Restart:
		if (current.get(0) == $("#awards img:last").get(0)) {
			next = $("#awards img:first");
		}
		
		// Fade across:
		next.fadeIn(2000, function() {
			current.fadeOut(2000, function() {
				current.removeClass();
				next.removeClass();
				next.addClass("current");
			});
		});
		
		awards_fader();
	}
	
/*------------------------------------------------------------------------------
	Contact Form
------------------------------------------------------------------------------*/
	
	function contact_form() {
		var location = document.location.toString();
		
		// Adjust date:
		$('#meeting-values select').change(function() {
			$('#meeting-values .date-total').val(
				$('#meeting-values .date').val()
				+ ' ' + $('#meeting-values .time').val()
			);
			
			$('#meeting-values .location-total').val(
				$('#meeting-values .location').val()
			);
		});
	}
	
/*----------------------------------------------------------------------------*/
	
	$(document).ready(function() {
		awards_fader();
		clients_fader();
		slideshow_fader();
		contact_form();
		
		// Clear search box:
		$('#search .input, #newsletter-left .input').focus(function() {
			var self = $(this);
			
			if (!self.hasClass('ready')) self.addClass('ready').val('');
		});
		
		$('#newsletters a').addClass('external');
		
		// Open external links in a new window:
		$('a.external').click(function() {
			window.open($(this).attr('href'));
			
			return false;
		});
		
		// Has Google map:
		if ($('#map').length && GBrowserIsCompatible()) {
			var map = new GMap2(document.getElementById("map"));
			
			map.setCenter(new GLatLng(-25.085599, 140.009766), 2);
			map.setMapType(G_HYBRID_MAP);
			map.addControl(new GSmallMapControl());
			
			$.get("/map-locations/", function(xml) {
				$(xml).find('coord').each(function() {
					map.addOverlay(new GMarker(new GLatLng(
						$(this).attr('latitude'),
						$(this).attr('longitude')
					)));
				});
				
				$('.locations a.active').each(function() {
					var self = $(this);
					var id = self.attr('class').split('location-')[1];
					
					$.get("/map-locations/" + id + "/", function(xml) {
						var xml = $(xml);
						var coord = xml.find('coord:first');
						
						map.setCenter(new GLatLng(
							coord.attr('latitude'),
							coord.attr('longitude')
						), 14);
					});
				});
			});
		}
		
		// Gallery Test:
		if ($('.col-left #project-details').length) {
			var body = ($.browser.msie ? $('#ie') : $('body'));
			var location = document.location.toString();
			var project = location.match(/\/[^\/]+\/$/);
			
			// Add gallery button:
			$('#project-details dl').after('\
				<p id="view-project-gallery">\
					<a href="#">View Project Gallery</a>\
				</p>\
			');
			
			$('#project-details p').css('cursor', 'pointer');
			
			// Load gallery:
			$('#view-project-gallery a, #project-details p').click(function() {
				var clicked = $(this);
				
				// Show image blurb:
				function createBlurb() {
					$('#gallery p').append('\
						<span>' + $('#gallery img.current').attr('alt') + '</span>\
					');
					$('#gallery p span').fadeIn(500);
				}
				
				// Fade between images:
				function fadeBetween(from, to) {
					to.css('z-index', '601');
					from.css('z-index', '600');
					$('#gallery p span').fadeOut(250).remove();
					
					$('body').focus();
					
					to.fadeIn(250, function() {
						from.hide();
						from.removeClass();
						to.removeClass();
						to.addClass("current");
						
						createBlurb();
					});
				}
				
				$.get("/project-gallery" + project, function(xml) {
					body.append(xml);
					
					if (clicked.hasClass('second')) {
						$('#gallery img.current').removeClass('current');
						$('#gallery img').eq(1).addClass('current');
					}
					
					createBlurb();
					
					// Single or no images:
					if ($('#gallery img').length <= 1) {
						$('#gallery li.prev, #gallery li.next').remove();
					}
					
					// Close when overlay is clicked:
					$('#overlay, #gallery li.close a').click(function() {
						$('#overlay').fadeOut(function() {
							$(this).remove();
						});
					});
					
					// Ignore clicks on gallery:
					$('#gallery').click(function() {
						return false;
					});
					
					$('#gallery .prev a').click(function() {
						var current = $('#gallery p img.current');
						var prev = current.prev();
						
						// Restart:
						if (current.get(0) == $('#gallery p img:first').get(0)) {
							prev = $('#gallery p img:last');
						}
						
						fadeBetween(current, prev);
						
						return false;
					});
					
					$('#gallery .next a').click(function() {
						var current = $('#gallery p img.current');
						var next = current.next();
						
						// Restart:
						if (current.get(0) == $('#gallery p img:last').get(0)) {
							next = $('#gallery p img:first');
						}
						
						fadeBetween(current, next);
						
						return false;
					});
				});
				
				return false;
			});
		}
		
		// Button popups:
		$('p.button').each(function() {
			var self = $(this);
			var value = self.find('a').attr('alt');
			
			if (value = value.match(/:\s+(.*)/)) {
				self.prepend('<span class="popup">' + value[1] + '</span>')
			}
			
			self.hover(function() {
				self.find('.popup').show();
			}, function() {
				self.find('.popup').hide();
			});
		});
	});
	
/*----------------------------------------------------------------------------*/
