$(function() {

	$("#submitbutton").click (function () {
	
	//Get the data from all the fields
		var name = $('input[name=name]');
		var email = $('input[name=email]');
		var comment = $('textarea[name=comment]');
		var elementID = $('input[name=elementID]');
		//Simple validation to make sure user entered something
		//If error found, add hightlight class to the text field
		
		var error = false;
		
		if (name.val()=='') {
			name.addClass('hightlight');
			error = true;
		} else name.removeClass('hightlight');
		
		if (email.val()=='') {
			email.addClass('hightlight');
			error = true;
		} else email.removeClass('hightlight');
		
		if (comment.val()=='') {
			comment.addClass('hightlight');
			error = true;
		} else comment.removeClass('hightlight');
		
		if (error) {
		alert ("Please complete the required fields.");
		return false;
		}
	
	  var data = 'elementID=' + elementID.val () + '&name=' + name.val() + '&email=' + email.val() + '&comment='  + encodeURIComponent(comment.val());  
	
		
	
		
	
	 $.ajax({  
	   type: "POST",  
	   url: $("#mainform").attr ('action'),  
	   data: data,  
	   success: function(msg) { 
	   //	alert (msg); 
	     $("#commentform").fadeOut (500, function () { $("#commentsubmitted").fadeIn (); });
	     
	   }  
	 });  
	
	return false;
	
	})


	if (dateString != "") {
		var vals = dateString.split ("/");
		newActive ($("#yearheader" + vals[2]));
	} else {
		$(".yearheader:first").each (function () {
			newActive ($(this));
		});
	}
		
	$(".yearheader").click (function () {
		newActive ($(this));
	});
	
	$(".navli").each (function () {
		
		if ($(this).find ("div:first ul li").size () > 0 ) {
			$(this).find ("a:first").click (function () { return false;});
			$(this).find ("a:first").attr('href','');
			$(this).find ("a:first").addClass ('nohoverstate');
		//alert ($(this).find ("a:first").text ());
		}

	})
	
	$(".navli ul li ").each (function () {
		if ($(this).find ("ul li").size () > 0 ) {
			$(this).find ("a:first").click (function () { return false;});
			$(this).find ("a:first").attr('href','');
			$(this).find ("a:first").addClass ('nohoverstate');
		//alert ($(this).find ("a:first").text ());
		}
		/*if ($(this).find ("div:first ul li").size () > 0 ) {
			$(this).find ("a:first").click (function () { return false;});
			$(this).find ("a:first").attr('href','');
			$(this).find ("a:first").addClass ('nohoverstate');
		//alert ($(this).find ("a:first").text ());
		}*/

	})

	
	
	
	
	

	$(".navli").each (function () {
		$(this).hover (function ()  {
			if ($(this).find ("div:first ul li").size () > 0) {	
				$(this).children ("div:first").css ('width','0px');
				
				$(this).children ("div:first").hide().addClass ('hiddenclass').animate ({width:"1px"}, 200, function () {   $(this).removeClass ('hiddenclass').show ().animate ({width:"205px"}, 200)   });
				//$(this).children ("div:first").show();
				//$(this).children ("div:first").removeClass ('hiddenclass').show ().animate ({width:"205px"}, 200).show();

			//	$(this).children ("div:first").show();
			//	$(this).children ("div:first").supersleight();
			}
		}, function () {
			if ($(this).find ("div:first ul li").size () > 0) {	
			
				$(this).children ("div:first").stop (true,true);
				$(this).children ("div:first").addClass ('hiddenclass')
				$(this).children ("div:first").hide ();
				//$(this).children ("div:first").width (1);
				//$(this).children ("div:first").animate ({width:"1px"}, 200);
			}
		});
	});
	
	$(".subservicelink").each (function () {
		$(this).hover (function ()  {
		
			if ($(this).find ("li").size () > 0) {	
			
			//	$(this).children ("ul").show();
			//	$(this).children ("div:first").supersleight();
			}
			else {
			
			}
		}, function () {
			if ($(this).find ("li").size () > 0) {	
				
		//		$(this).children ("ul").hide ();
			}
		});
	});
	
	if ($("#widget").length  > 0) {
	
		initSlideshow ();
	
	}
	
	
	
	
	if (document.getElementById ("map_canvas")) {
		map = new GMap2(document.getElementById("map_canvas"));
		
		setMarker (officetoopen);
	}
	$(".maplabel").click (function () {
		var id = $(this).attr('id').substr (7,1);
		setMarker (id);
	});

	$("#resetbtn").click (function () {$("#mainform").each ( function () {this.reset ()})});
	

});

function validateContact () {
	var error = false;
	var msg = "Your must complete all the required fields:\n\n";
	if ($("#name").val () == "") {
		error = true;
		msg += "Please enter your name\n";
	}
	if ($("#email").val () == "" && $("#phone").val () == "") {
		error = true;
		msg += "Please enter your telephone number, or your email address\n";
	}
	if ($("#subject").val () == "") {
	error = true;
		msg += "Please enter a subject\n";
	}
	if (error) {
	alert (msg);
	return false;
	}

	return true;
}

function validateCharity () {
	var error = false;
	var msg = "Your must complete all the required fields:\n\n";
	if ($("#name").val () == "") {
		error = true;
		msg += "Please enter your name\n";
	}
	if ($("#email").val () == "") {
		error = true;
		msg += "Please enter your email address\n";
	}
	if ($("input[@name='charity']:checked").length  == 0) {
	error = true;
		msg += "Please choose a charity\n";
	}
	if (error) {
	alert (msg);
	return false;
	}

	return true;
}


function validateRegister () {
	var error = false;
	var msg = "Your must complete all the required fields:\n\n";
	
	if ($("#email").val () == "") {
		error = true;
		msg += "Please enter your email address\n";
	}

	if (error) {
	alert (msg);
	return false;
	}

	return true;
}

function validateCV () {
	var error = false;
	var msg = "Your must complete all the required fields:\n\n";
	if ($("#name").val () == "") {
		error = true;
		msg += "Please enter your name\n";
	}
	if ($("#email").val () == "" ) {
		error = true;
		msg += "Please enter your email address\n";
	}
	if ($("#subject").val () == "") {
		error = true;
		msg += "Please enter a subject\n";
	}
	if ($("#file").val () == "") {
	error = true;
		msg += "Please select a file to upload\n";
	}
	if (error) {
	alert (msg);
	return false;
	}

	return true;
}

function setMarker (id) {
		
		$("#maplink1").removeClass ('activemap');
		$("#maplink2").removeClass ('activemap');
		$("#maplink3").removeClass ('activemap');
		$("#maplink" + id).addClass ('activemap');
		

		var long = Number ($("#long" + id).html ());
		var lat = Number ($("#lat" + id).html ());
				

		map.setCenter(new GLatLng(long,lat ), 15);
		var mapControl = new GMapTypeControl();
		map.addControl(mapControl);
		map.addControl(new GSmallMapControl());
		
		marker = new GMarker(new GLatLng(long,lat ));
		
		
 		 GEvent.addListener(marker, "click", function() {
   			 marker.openInfoWindowHtml($("#address" + id).html ());
  		  });

		//marker.openInfoWindowHtml($("#address" + id).html ());
		pendingId = id;
		setTimeout ("doIt ()",250);
		
		map.addOverlay (marker);
		
}

function doIt () {
marker.openInfoWindowHtml($("#address" + pendingId).html ());
}

var pendingId = -1;
var marker;

function newActive ($item) {
	
	$(".yearheader").each (function () {
		$(this).removeClass ("activeYear");
	});
	var id = $item.attr('id').substr (10,4);
	$(".yearcontent").each (function () {
		$(this).hide ();
	});
	$("#yearcontent" + id).show ();
	$item.addClass ("activeYear");
}



var currPosition = 0;
var oldPosition = -1;

function initSlideshow () {
	
		
	$('#slideshow').cycle({
	    pause: true,
	    pager: "#pager",
	    pagerEvent : "mouseover",
	    timeout: 6000,
	    speed:500,
	    cleartype: 0,
	    fastOnEvent: true ,
	    pagerAnchorBuilder: function(idx, slide) { 
        	// return selector string for existing anchor 
       	 	return '#slideshowthumb' + (idx+6); 
   	 		}
	}); 
	
	//$('#pager').hover (function () {$('#slideshow').cycle('pause');}, function () {$('#slideshow').cycle('resume');});

}
