window.addEvent('domready', function() 
{
	currentImage = 0;
	bgImg = $$('#bg .img-container img.bg-img');
	scImg = $('showcase');
	scText = $('caption-copy');
	scTitle = $('title-copy');
	
	if( $('bg').getStyle('position') == 'absolute' )
	{
		movebg_IE6();
	}
	
	// Reset Search box
	resetSearch();
	
	selectCaseStudyHover();
	giveDirections();
	changeLocation();
	setupNewWindow();
	
	if( $('contact-us-frame') &&  $('contact-box-close'))
	{
		$('contact-us-frame').addEvent('click', function(e) {
			e.stop();
			contactBox( this.href );
			trackPage('/can-we-help.php');
		});
		
		$('contact-box-close').addEvent('click', function(e) {
			e.stop();
			boxClose();
		});
	}

	
	if( $('enquireMail') )
	{
		$('enquireMail').addEvent('click', function(e) {
			trackPage('/top-email.php');
		});
		
	}
});

window.addEvent('load', function() 
{
	if( $('map_canvas') )
	{
		initialize();
	}
});

window.addEvent('unload', function() 
{
	if( $('map_canvas') )
	{
		GUnload();
	}
});

function initialize() {
  if (GBrowserIsCompatible()) {
	var map = new GMap2(document.getElementById("map_canvas"));
	map.setCenter(new GLatLng(googleLat, googleLong), 15);

	// Add 10 markers to the map at random locations
	var bounds = map.getBounds();
	var southWest = bounds.getSouthWest();
	var northEast = bounds.getNorthEast();
	var lngSpan = northEast.lng() - southWest.lng();
	var latSpan = northEast.lat() - southWest.lat();
	var latlng = new GLatLng(googleLat, googleLong);
	map.addOverlay(new GMarker(latlng));
  }
}

function movebg_IE6()
{
	var bg = $('bg');
	window.addEvent('resize', function() { bg.setStyle('height', window.getSize().y); });
	window.addEvent('scroll', function() {
		var bg_height = bg.getSize().y;
		var scrolled_to = window.getScroll().y;
		var doc_height = window.getScrollSize().y;
		
		var bg_pos = 0;
		if(scrolled_to + bg_height > doc_height)
		{
			bg_pos = doc_height - bg_height;
		}
		else
		{
			bg_pos = scrolled_to;
		}
		bg.setStyle('top', bg_pos);
	});
}

function changeLocation() {
	if($('office-list')) {
		//Remove the 'no-script' class from the container, as we obviously
		//have Javascript enabled to get this far!
		$('contact-details').removeClass('no-script');
		//Make sure none of the map points are active
		$$('#office-locations div').removeClass('active');
		//Set London as active on the map
		$('london-point').addClass('active');

		$$('#contact-details div.location-item').addClass('hide');
		$$('#contact-details #london').removeClass('hide');
		
		var locations = $('office-list').getElements('a');
		var mapItems = $('office-locations').getElements('div');
		
		locations.addEvents ({
				'click' : function(e) {
					changeDetails(this, locations, mapItems);
				},
				'mouseover' : function() {
					this.setProperty('href', 'http://www.golleyslater.co.uk/locations/'+this.id.replace('-link',''));
					changeDetails(this, locations, mapItems);
				}
		});
		
		function changeDetails(object, locations, mapItems) {
			var getDetails = object.id.split('-');
				getDetails = getDetails[0];

			locations.addClass('inactive');
			locations.removeClass('active');
			mapItems.removeClass('active');
			object.toggleClass('active');
			object.toggleClass('inactive');
			
			//Hide all details...
			$('contact-details').getElements('div.location-item').addClass('hide');
			//... then unhide the one you are clicking
			var getLocation = $(getDetails);
				getLocation.toggleClass('hide');
				getLocation.toggleClass('');
				
			var mapLocation = $(getDetails + '-point');
				mapLocation = mapLocation.addClass('active');
		}
	}
}

function resetSearch()
{
	if( $('search') )
	{
		var el = $('search');
		var searchTxt = 'Search the site';
		
		el.addEvent('focus', function(){
		
			if( this.value == searchTxt )
			{
				this.value = '';
			}
		
		});
		
		el.addEvent('blur', function(){
		
			if( this.value == '' )
			{
				this.value = searchTxt;
			}
		
		});
		
	}
}

function selectCaseStudyHover() {
	if( $('client-work-sectors') ) {
		var aTags = $('client-work-sectors').getElements('a');
		var itemClass;
		
		aTags.each(function(item) {
			item.addEvent('mouseover', function() {
				itemClass = item.className;
				caseStudySetActive(itemClass);
			});
			item.addEvent('mouseout', function() {
				itemClass = item.className;
				caseStudySetInactive(itemClass);
			});
			item.addEvent('click', function(e) {
				//do something
				e = new Event(e).stop();
				itemClass = item.className;
				caseStudySetActiveClick(itemClass, this);
			});
		});
	}
}

function caseStudySetActive(itemClass) {
	//set class of active
	$$('#case-studies-container a.' + itemClass).addClass('active');
}

function caseStudySetInactive(itemClass) {
	//remove class of active
	$$('#case-studies-container a.' + itemClass).removeClass('active');
}

function caseStudySetActiveClick(itemClass, object) {
	
	itemClass = itemClass.replace(' active','');
	var spanTag = $('client-work-sectors').getElement('span.'+itemClass);
	
	spanTag.toggleClass('hide');	
	object.toggleClass('active');
}

function giveDirections() {
	if($('get-directions')) {
		$('directions').removeClass('no-script');
		var locations = $('locations').getElements('a');
				
		$('get-directions').getElements('a').addEvent('click', function(e) {
			e.stop();
			changeLocation(this);
		});
		
		$('get-directions').getElements('a').addEvent('mouseover', function() {
			changeLocation(this);
		});
		
		function changeLocation(object) {
			var getLocation = object.id.split('-');
			var clickedLocations = getLocation[0];
			$('get-directions').getElements('a').addClass('inactive');
			$('get-directions').getElements('a').removeClass('active');
			
			object.toggleClass('active');
			object.toggleClass('inactive');
			
			$('directions').getElements('div').addClass('hide');
			
			var getDirection = $(clickedLocations);
				getDirection.toggleClass('hide');
				getDirection.toggleClass('');	
		}
	}	
}

function contactBox( href )
{

	var iframe = new Element('iframe', {title: 'contact-box-iframe', scrolling: 'no', frameborder: '0'});
	var iframeWrapper = $('contact-box-iframe-wrapper');
	iframe.inject(iframeWrapper);
	$(document.body).getElement('iframe').src = href;
	$('contact-box-bg').removeClass('hide');
	$('contact-box-bg').setStyle('display', 'block');
	$('contact-box-overlay').removeClass('hide');
}

function boxClose()
{
	$(document.body).getElement('iframe').dispose();
	$('contact-box-bg').addClass('hide');
	$('contact-box-bg').setStyle('display', 'none');
	$('contact-box-overlay').addClass('hide');
}

function setupNewWindow()
{
	var els = $$('.new-window');
	
	for( i=0; i<els.length; i++ )
	{
		els[i].addEvent('click', function(e)
		{
			e.stop();
			newWindow( this );
		});
	}
}

function trackPage(pagename)
{
	try
	{
		var pageTracker = _gat._getTracker("UA-5625339-1");
		pageTracker._trackPageview(pagename);
		/*
		if(typeof console != "undefined")
		{
			console.log(_gat);
			console.log(pageTracker);
		}
		*/
		//alert( pageTracker + 'pageTracker._trackPageview("/find-out-more.php");');
	}
	catch(err)
	{}
}

function newWindow( el )
{
	window.open(el.href, 'golley');
}