// JavaScript Document
var com = {
	wearenotmachines : {
		validation : {
			defaultValues : {
				yourName: "Your name",
				yourEmail: "Your email address",
				yourPhoneNumber: "Your phone number",
				theLocation: "The location",
				yourMessage: "Your message"
			},
			requiredFields : new Array("yourName", "yourEmail", "yourPhoneNumber", "yourMessage"),
			init:function() {
				com.wearenotmachines.validation.form = $(document.forms[0]);
				$("input,textarea").click(function(e) {
					if (this.value==com.wearenotmachines.validation.defaultValues[this.id]) {
						this.value="";	
					}
				});
				$("input,textarea").blur(function(e) {
					if (this.value=="") {
						this.value=com.wearenotmachines.validation.defaultValues[this.id]
					}
				});
				com.wearenotmachines.validation.form.submit(function(e) {
					var valid = true;
					var fields = $("input, textarea");
					$(".error").remove();
					$("input,textarea").css("backgroundColor", "#ffffff");
					for (var i=0; i<fields.length; i++) {
						if (jQuery.inArray(fields[i].id, com.wearenotmachines.validation.requiredFields)>-1 && (fields[i].value=="" || fields[i].value==com.wearenotmachines.validation.defaultValues[fields[i].id])) {
							$(fields[i]).css("backgroundColor", "#FFC6C6");	
							valid = false;
						}
						if (fields[i].id=="yourEmail") {
							var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/	
							if (!reg.test(fields[i].value)) {
								$(fields[i]).css("backgroundColor", "#FFC6C6");	
								valid = false;
							}
						}
					};
					if (!valid) {
						$(document.forms[0]).before("<p class='error'>There were errors with your submission. Please check the highlit fields and try again.</p>");
					} else {
						$("#messageProgress").css("display", "block");
						$("#messageProgress").html("<p>Sending your message, please wait</p><img src='../images/icons/ajax-loader.gif' />");
						$.ajax({
							type : 'post',
							'url' : 'doMail.php',
							'dataType' : 'text',
							async : true,
							data : {'yourName': $("#yourName").val(), 'yourEmail' : $("#yourEmail").val(), 'yourMessage' : $("#yourMessage").val(), 'yourPhoneNumber' :  $("#yourPhoneNumber").val(), 'theLocation' : $("#theLocation").val()},
							success : function(data) {
								if (data=="1") {
						$("#contactForm").html("<p>Your message has been sent.  Thanks for getting in touch.</p>");
								} else {
						$("#contactForm").html("<p>There was a problem delivering your message - please <a href='mailto:mick@kentbuildingservices.com'>click here</a> to send an email using your email editor or call us on 0800 633 5929.</p>");
								}
							}
						});
					}
					return false;
				});
			}
		},
		panelImages : new Array(),
		focusPanel : function(jid) {
			var mask = document.createElement("div");
			mask.id = "mask";
			$(mask).css("height", $(window).height()+"px");
			document.body.appendChild(mask);	
			$(mask).click(com.wearenotmachines.removeFocusPanel);
			var panel = document.createElement("div");
			panel.id = "focusPanel";
			document.body.appendChild(panel);
			var loader = new Image();
			loader.src = "../images/icons/ajax-loader2.gif";
			loader.id = "loader";
			$(panel).append(loader);
			$.ajax({
				'url' : 'getJob.php',
				'method' : 'get',
				'dataType' : 'xml',
				data : {
					id : jid	
				},
				success : function(data) {
					com.wearenotmachines.setupFocusPanel(data);
				}
			});
		},
		setupFocusPanel : function(xml) {
			com.wearenotmachines.panelImages = new Array();
			$(xml).find("image").each(function() {
				var im = new Image();
				im.src = $(this).text();
				im.onload = function() {
					var maxWidth = Math.max(1000, $(window).width());
					var maxHeight = Math.max(750, $(window).height());
					var sizedImage = com.wearenotmachines.calculateImageSize(this, 800, 550)
					com.wearenotmachines.panelImages.push(sizedImage);
					if (com.wearenotmachines.panelImages.length==1) {
						com.wearenotmachines.displayMainImage();	
					}
				}
			});
		},
		displayMainImage : function() {
			$("#loader").remove();
			com.wearenotmachines.panelImages[0].id = "mainImage";
			$(com.wearenotmachines.panelImages[0]).hide();
			$("#focusPanel").animate(
				{
					height : com.wearenotmachines.panelImages[0].height,
				 	width : com.wearenotmachines.panelImages[0].width,
					marginLeft : ((parseInt(com.wearenotmachines.panelImages[0].width)/2)+20)*-1
				}, {
					duration : 750,
					easing : "easeInOutSine",
					complete : function() {
						$(com.wearenotmachines.panelImages[0]).show();
					}
				}
			);
			$("#focusPanel").append(com.wearenotmachines.panelImages[0]);	
		},
		removeFocusPanel : function() {
			$("#focusPanel").remove();
			$("#mask").remove();
		},
		carousel : {
			//define the left positions of the container
			positions : new Array(0, -700,-1500,-2300, -3000),
			//define the controls
			controls: new Array("#slideshow1", "#slideshow2","#slideshow3","#slideshow4", "#slideshow5"),
			counter: 3,
			initControls: function() {
				var that = this;
				for(var i=0; i<this.controls.length; i++) {
					$(this.controls[i]).click(function(e) {
						var control = this;
						com.wearenotmachines.carousel.counter = this.id.substr(-1);
						$(".slideshowcontrol").removeClass("focal");
						$(this).addClass("focal");
						$("#slideshowImageContainer").animate({
							marginLeft:that.positions[this.id.substr(-1)-1]+"px"
						}, {
							duration: 1000,
							easing: 'easeInBack',
							complete: function() {
								$("#slideshowImageContainer>img").removeClass("focal");
								$("#slideshowimage"+control.id.substr(-1)).addClass("focal");
							}
						});
						clearInterval(window.carouselAdvance);
						window.carouselAdvance = setInterval(com.wearenotmachines.carousel.advanceCarousel, 5000);
						return false;	
					});
				}
			},
			advanceCarousel: function() {
				$(".slideshowcontrol").removeClass("focal");
				$(com.wearenotmachines.carousel.controls[com.wearenotmachines.carousel.counter]).addClass("focal");
				$("#slideshowImageContainer").animate({
					marginLeft:com.wearenotmachines.carousel.positions[com.wearenotmachines.carousel.counter]+"px"
				}, {
					duration: 1000,
					easing: 'easeInBack',
					complete: function() {
						$("#slideshowImageContainer>img").removeClass("focal");
						$("#slideshowimage"+(com.wearenotmachines.carousel.counter+1)).addClass("focal");
						if (com.wearenotmachines.carousel.counter<4) {
							com.wearenotmachines.carousel.counter++;
						} else {
							com.wearenotmachines.carousel.counter=0;
						}
					}
				});
			}
		},
		servicesCarousel : {
			init : function() {
				//set the width of the container div equal to the sum of the widths of the images - get the widths from the onload
				$("#servicesCarouselContainer>a>img").load(function(e) {
					$("#servicesCarouselContainer").css("width", (parseInt($("#servicesCarouselContainer").css("width"))+this.width+10)+"px");
				});
			},
			//define the left positions of the container
			positions : new Array(0, -600, -1200, -1800),
			//define the controls
			controls: new Array("#slideshow1", "#slideshow2","#slideshow3","#slideshow4"),
			counter: 1,
			initControls: function() {
				var that = this;
				for(var i=0; i<this.controls.length; i++) {
					$(this.controls[i]).click(function(e) {
						var control = this;
						com.wearenotmachines.servicesCarousel.counter = this.id.substr(-1)-1;
						$(".slideshowcontrol").removeClass("focal");
						$(this).addClass("focal");
						$("#servicesSlideshowContainer").animate({
							marginLeft:that.positions[this.id.substr(-1)-1]+"px"
						}, {
							duration: 1000,
							easing: 'easeOutQuad',
							complete: function() {
								$("#slideshowimage"+control.id.substr(-1)).addClass("focal");
								if (com.wearenotmachines.servicesCarousel.counter<3) {
									com.wearenotmachines.servicesCarousel.counter++;
								} else {
									com.wearenotmachines.servicesCarousel.counter=0;
								}
							}
						});
						clearInterval(window.carouselAdvance);
						window.carouselAdvance = setInterval(com.wearenotmachines.servicesCarousel.advanceCarousel, 5000);
						return false;	
					});
				}
			},
			advanceCarousel: function() {
				$(".slideshowcontrol").removeClass("focal");
				$(com.wearenotmachines.servicesCarousel.controls[com.wearenotmachines.servicesCarousel.counter]).addClass("focal");
				$("#servicesSlideshowContainer").animate({
					marginLeft:com.wearenotmachines.servicesCarousel.positions[com.wearenotmachines.servicesCarousel.counter]+"px"
				}, {
					duration: 1000,
					easing: 'easeOutQuad',
					complete: function() {
						if (com.wearenotmachines.servicesCarousel.counter<3) {
							com.wearenotmachines.servicesCarousel.counter++;
						} else {
							com.wearenotmachines.servicesCarousel.counter=0;
						}
					}
				});
			}
		},
		portfolio : {
			jobs : new Array(),
			images : new Array(),
			container : "",
			addJob : function(url, id) {
				this.jobs.push({'url' : url, 'id' : id, 'loaded' : false});	
			},
			init : function(container) {
				$("#carouselLeft").click(function(e) {
					com.wearenotmachines.portfolio.scrollLeft(); 
					return false;
				});
				$("#carouselRight").click(function(e) {
					com.wearenotmachines.portfolio.scrollRight();
					return false;
				});
				com.wearenotmachines.portfolio.container = container;
				for (var j in com.wearenotmachines.portfolio.jobs) {
					jid = this.jobs[j].id;
					this.images[j] = new Image();
					this.images[j].id = jid;
					this.images[j].src = this.jobs[j].url;
					this.images[j].onload = function() {
						com.wearenotmachines.portfolio.addImage(this.id, this);	
					}
				}
			},
			addImage : function(id, image) {
				var theLink = document.createElement("a");
				theLink.id = id;
				theLink.href = id;	
				theLink.className = "portfolioImage";
				image = com.wearenotmachines.calculateImageSize(image, 400, 300);
				$(theLink).click(function(e) {
					com.wearenotmachines.focusPanel(this.id);
					return false;
				});
				$(theLink).append(image);
				$(com.wearenotmachines.portfolio.container).append(theLink);
				var contWidth = parseInt($(com.wearenotmachines.portfolio.container).css("width"));
				$(com.wearenotmachines.portfolio.container).css("width", (contWidth+image.width+20)+"px");
				if (parseInt($(com.wearenotmachines.portfolio.container).css("width"))<=900) {
					$("#carouselLeft").hide();
					$("#carouselRight").hide();
				} else {
					$("#carouselLeft").show();
					$("#carouselRight").show();
				}

			},
			scrollLeft : function(e) {
				var pos = $(com.wearenotmachines.portfolio.container).css("marginLeft").substr(0,$(com.wearenotmachines.portfolio.container).css("marginLeft").length-2);
				if (pos<0) {
					$(com.wearenotmachines.portfolio.container).animate(
						{ marginLeft : '+=400' },
						500
					);
				}
				return false;
			},
			scrollRight : function(e) {
				var pos = $(com.wearenotmachines.portfolio.container).css("marginLeft").substr(0,$(com.wearenotmachines.portfolio.container).css("marginLeft").length-2);
				if (pos>(1000-parseInt($(com.wearenotmachines.portfolio.container).css("width")))) {
					$(com.wearenotmachines.portfolio.container).animate(
						{ marginLeft : '-=400' },
						500
					);
				}
				return false;
			}
		},
		calculateImageSize : function(image, maxWidth, maxHeight) {
			var landscape = image.width>image.height;
			if (landscape) {
				var ratio = image.width>maxWidth ? maxWidth/image.width : 1;	
			} else {
				var ratio = image.height>maxHeight ? maxHeight / image.height : 1;
			}
			image.width = (image.width * ratio).toFixed(0);
			image.height = (image.height * ratio).toFixed(0);
			return image;
		},
		serviceRoller : {
			addImage : function(url, service) {
				var im = new Image();
				im.src = url;
				im.onload = function() {
					var ratio = 1;
					if (im.height>100) {
						ratio = 100/im.height;
						im.width *=ratio;
						im.height *= ratio;	
					}
					$(this).click(function(e) {
						window.location = "http://www.kentbuildingservices.com/services?service="+service;
					});
					$("#slideshowRollerContainer").append(this);
				}
			}
		}
	}
}
