var	ie6 = false;
var	ie7 = false;
var servicePages;

window.addEvent('domready', function(){
	ie6 = (navigator.appVersion.indexOf("MSIE 6.")==-1) ? false : true;
	ie7 = (navigator.appVersion.indexOf("MSIE 7.")==-1) ? false : true;
	reel();
	hideDescriptions();
	homeRandomDisplay();
	footer();
	if( ie6 || ie7 ){
		var outer = $('bottomContent').getElement('.outer');		
		if(outer != ''){
			outer.setStyles({"opacity":0.85,"background":"#F0F0F0"});	
		}
	}
	initCufon();	
	embedFlash();
	embedMap();	
	formSubmission('callBack');
	formSubmission('contact');
});

// make sure valid fields are filled in
function formSubmission(formName){
	var form = $(formName);
	if(form){
		var errorVisible = false;
		var allInputs = new Array();
		allInputs = form.getElements('input');
		allInputs.each( function(input){
			if(input.get('type') == 'image' || input.get('type') == 'submit'){
				input.addEvent('click',function(evt){					
					var requiredInputs = new Array();
					requiredInputs = form.getElements('.required');
					requiredInputs.each( function(required){
						if(required.get('value') == ''){
							evt.stop();	
							if(!errorVisible){
								var errorDiv = new Element('div',{id: 'error'});
								errorDiv.set('html','<p>*Please complete all mandatory fields</p>');
								errorDiv.inject(input,'before');
								errorVisible = true;
							}
						}
					});
				
				});
			}
		});
		
		
	}
}

// displays random content on home page
function homeRandomDisplay(){
	var randomDiv = $('randomContent');
	if(randomDiv){
		var contentArr = new Array();
		var counter = 0;
		var liElements = randomDiv.getElements('li');
		var tempArr = new Array();
		liElements.each( function(liElement){
			if(counter == 2){
				contentArr[contentArr.length] = tempArr;
				tempArr = new Array();
				counter = 0;
			}
			tempArr[tempArr.length] = liElement.get('html');
			counter++;
		});
		var randomNumber = Math.floor(Math.random() * contentArr.length);
		$$('.colA')[0].set('html', contentArr[randomNumber][0]);
		$('mainContent').setStyle('background','url(' + contentArr[randomNumber][1] + ') no-repeat top right');
		
	}
}

function embedMap(){
	var map = $("mapStrat");
	var map2 = $("mapBristol");
	
	if( map ){
		var geocoder = new google.maps.Geocoder();
		if (geocoder) {
			geocoder.geocode( { 'address': "29 Wood Street, Stratford-Upon-Avon, Warwickshire, CV37 6JG"}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var myOptions = {
					  zoom: 16,
					  center: results[0].geometry.location,
					  mapTypeId: google.maps.MapTypeId.ROADMAP
					}
					gmap = new google.maps.Map(map, myOptions);
					var marker = new google.maps.Marker({
						map: gmap, 
						position: results[0].geometry.location,
						icon: "/style/images/mapMarker.png",
						title: "hatch chartered accountants Stratford"
					});
				}
				else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
	}
	
	if( map2 ){
		var geocoder2 = new google.maps.Geocoder();
		if (geocoder2) {
			geocoder2.geocode( { 'address': "24 berkeley square, Bristol, BS8 1HP"}, function(results, status) {
				if (status == google.maps.GeocoderStatus.OK) {
					var myOptions2 = {
					  zoom: 16,
					  center: results[0].geometry.location,
					  mapTypeId: google.maps.MapTypeId.ROADMAP
					}
					gmap = new google.maps.Map(map2, myOptions2);
					var marker2 = new google.maps.Marker({
						map: gmap, 
						position: results[0].geometry.location,
						icon: "/style/images/mapMarker2.png",
						title: "hatch chartered accountants Bristol"
					});
				}
				else {
					alert("Geocode was not successful for the following reason: " + status);
				}
			});
		}
	}
	
}

function hideDescriptions(id){
	
	var descriptions = $$('.serviceDescription');

	descriptions.each( function(description){	
		var fx = new Fx.Morph(description,{ duration: 'long' });	
		
		if(id == description.getProperty('id')){
			description.setStyle('display','block');
		}else{
			description.setStyle('display','none');
		}
						
	});
	footer();	
}

function highlightService(index){
	var boxes = $$('.serviceBar li');
	boxes.each( function(box){
						
		box.setStyle('background','#0091CE');						 
		var currentId = box.get('id');						 
		if(currentId == parseFloat(index)){
			box.setStyle('background-image','url(\'/style/images/highlightedCentre.png\')');	
		} 
		if(currentId ==  parseFloat(index)-1){
			box.setStyle('background-image','url(\'/style/images/highlightedLeft.png\')');	
		}
		if(currentId ==  parseFloat(index)+1){
			box.setStyle('background-image','url(\'/style/images/highlightedRight.png\')');	
		}
	});
}

// highlights the clients whose use a service by id
function highlightClients(serviceId){
	hideDescriptions(serviceId);
	var highlightPages = servicePages.get(serviceId);	
	var bottomContent = $('bottomContent');		
	if(bottomContent){
		var clients = bottomContent.getElements('.client');						
		var opacityValue = 1;
		var borderColor = null;
		
		clients.each( function(client){	
			var fx = new Fx.Morph(client,{ duration: 'long' });
																								
			if(highlightPages.contains(client.getElement('img').getProperty('id'))){
				fx.start({		 
					'opacity' : 1,
					'border-color' :'#00aeef' 
				});
			}else{
				fx.start({
					'opacity' : 0.5,
					'border-color' : '#d1d4d1'
				});
			}

		});
	}
}

function initCufon(){
	Cufon.replace("#topNav a", { fontFamily: 'Bauhaus Md BT' });
	Cufon.replace(".bauhaus", { fontFamily: 'Bauhaus Md BT' });
	Cufon.replace(".frutiger", { fontFamily: 'Frutiger LT 55 Roman' });
	Cufon.replace("h3,h2", { fontFamily: 'Helvetica CE 35 Thin' });
}

// Pins the footer to the bottom of the page
function footer(){
	var bottomContent = $('bottomContent');	
	if(bottomContent){		
		var containerCoords = $('container').getCoordinates();		
		bottomContent.setStyle('top',containerCoords.top + containerCoords.height);
		
		var inner = bottomContent.getElement('.inner');
		bottomContent.setStyle('height', inner.getCoordinates().height + 80);
	}
}

function embedFlash(){
	if ( $('servicesAnimation') ){
		var flashVars = {};
		
		var uri = new URI(location.href);
		var startingService = uri.getData('id');
		if ( startingService ){ flashVars['startingServiceId'] = 'sId'+startingService; }
		
		servicePages = new Hash();		
		services.each( function(item){
			flashVars[item.id] = item.image;
			servicePages.set(item.id, item.pages);
		});

		swfobject.embedSWF("/style/KamPortfolio.swf", "servicesAnimation", "990", "230", "9.0.0", null, flashVars);
	}
}

function reel(){
	var counter = $('counter');
	if(counter){
		moveReel(0);
	}
}

// Moves the reel 
function moveReel(index){
	var positionArr = new Array(126,152,177,-53,-28,-2,23,48,75,100);
	var config = { index: 3 };
	var roll = function(){ 
		if(this.index >= 0){
			var reels = $$('.reel');	
			var currentReel = reels[this.index];
			currentReel = $(currentReel);
			var value = numberOfClients.substr(this.index,1);
			
			currentReel.set('html',value);
			
			var fx = new Fx.Morph(currentReel, {
				'duration': 3000,				
				'transition': 'circ:out'
			});	
			fx.set({
				'background-position': '0 -635px'
			});	
			fx.start({
				'background-position': '0px ' + positionArr[value] + 'px'
			});	

			this.index--; 
		}
	};
	roll.periodical(500, config);	
}

