// JavaScript Document

$(document).ready(function(){

	// Internet Explorer 5.5 compatibility
	if (jQuery.browser.msie&&(jQuery.browser.version<6)) {
		if (document) {
			if (!document.nodeType) document.nodeType = 9;
			if (!document.nodeName) document.nodeName = '#document';
		}
	}			

	$("a#chauffeurs").hover(function() {
		$("span#chauffeur-grey").animate({marginTop: "65px"}, { "duration": 700, queue: false });
		$("span#chauffeur-text").animate({marginTop: "65px"}, { "duration": 700, queue: false });
	}, function() {
		$("span#chauffeur-grey").animate({marginTop: "165px"}, { "duration": 700, queue: false });
		$("span#chauffeur-text").animate({marginTop: "165px"}, { "duration": 700, queue: false });
	});
	$("a#receptionists").hover(function() {
		$("span#receptionists-grey").animate({marginTop: "65px"}, { "duration": 700, queue: false });
		$("span#receptionists-text").animate({marginTop: "65px"}, { "duration": 700, queue: false });
	}, function() {
		$("span#receptionists-grey").animate({marginTop: "165px"}, { "duration": 700, queue: false });
		$("span#receptionists-text").animate({marginTop: "165px"}, { "duration": 700, queue: false });
	});
	$("a#security").hover(function() {
		$("span#security-grey").animate({marginTop: "65px"}, { "duration": 700, queue: false });
		$("span#security-text").animate({marginTop: "65px"}, { "duration": 700, queue: false });
	}, function() {
		$("span#security-grey").animate({marginTop: "165px"}, { "duration": 700, queue: false });
		$("span#security-text").animate({marginTop: "165px"}, { "duration": 700, queue: false });
	});


    $("input.field").focus(function () {
      $(this).addClass("fieldfocus");
    });
    $("input.field").blur(function () {
      $(this).removeClass("fieldfocus");
    });
    $("textarea.field").focus(function () {
      $(this).addClass("fieldfocus");
    });
    $("textarea.field").blur(function () {
      $(this).removeClass("fieldfocus");
    });

    // lunch signup listeners
    $(document).ready(function() {
      $("#signupemail").keyup(function() {
        if ($("#signupemail").val() != "") {
          $("#signupemail").removeClass("required");
        }
        else {
          $("#signupemail").addClass("required");
        }
      });
      
      $("#signupemail").change(function() {
        $("#signupemail").removeClass("required");
      });

      $("#signuporganisation").keyup(function() {
        if ($("#signuporganisation").val() != "") {
          $("#signuporganisation").removeClass("required");
        }
        else {
          $("#signuporganisation").addClass("required");
        }
      });
    });

    $("#moveableone").click(function () {
      $("#moveablemove").animate({marginLeft: "-600px"}, { "duration": 700 });
			if (strVisual2!="") $(".visual .large").css({backgroundImage: 'URL(/images/services/'+strVisual2+')'});
    });
    $("#moveabletwo").click(function () {
      $("#moveablemove").animate({marginLeft: "-1200px"}, { "duration": 700 });
			if (strVisual3!="") $(".visual .large").css({backgroundImage: 'URL(/images/services/'+strVisual3+')'});
    });
    $("#moveablethree").click(function () {
      $("#moveablemove").animate({marginLeft: "-1800px"}, { "duration": 700 });
			if ((strVisual2!="")|(strVisual3!="")) $(".visual .large").css({backgroundImage: 'URL(/images/services/'+strVisual1+')'});
    });
    $("#moveablelast").click(function () {
      $("#moveablemove").animate({marginLeft: "0px"}, { "duration": 1000 });
			if ((strVisual2!="")|(strVisual3!="")) $(".visual .large").css({backgroundImage: 'URL(/images/services/'+strVisual1+')'});
    });

    $("#moveablelast a").click(function () {
			document.location.href = this.href; return false;
    });



	$("#moveableone").hover(function() {
		$("#moveableone .next").css({backgroundPosition: '0 -399px'});
	}, function() {
		$("#moveableone .next").css({backgroundPosition: '0 -199px'});
	});
	$("#moveabletwo").hover(function() {
		$("#moveabletwo .next").css({backgroundPosition: '0 -399px'});
	}, function() {
		$("#moveabletwo .next").css({backgroundPosition: '0 -199px'});
	});
	$("#moveablelast").hover(function() {
		$(this).css({backgroundPosition: '20px -2000px'});
	}, function() {
		$(this).css({backgroundPosition: '20px -1000px'});
	});

	jQuery.voormedia = {};

	jQuery.voormedia.flyoutmenu = {
		options: {
			showDelay: 300,
			offMenuDelay: 200,
			tabs: new Array("services","group","jobs"),
			spriteHeight: 150,
			useFader: true,
			opacity: 0.2
		}, 
		initialize: function(options)
		{
			jQuery.extend(this.options, options);
			this.useFader = this.decideOnFader();
			this.tabs = this.options.tabs; // Not really necessary, but because container.tabs is used so much, this is more readable.
			showTimer = null;
			offMenuTimer = null;
			overMenu = false;
			var container = this;

			$(this).children("a").click(function(){
				if (container.isTab()) {
					container.showMenu();
					return false;
				} else {
					return true;
				}
			});

			$(this).hover(function(){
				if (container.isTab()) {
					container.delayShow();
				}
			}, function(){
				if (container.isTab()) {
					container.delayHide();
				}
			});
			
		},
		decideOnFader: function() {
			var container = this;
			if (container.options.useFader && !(jQuery.browser.msie && (jQuery.browser.version<6))) {
				return true;
			}
			return false;
		},
		isTab: function() {
			var container = this;
			for (key in container.tabs) {
				if (container.hasClass(container.tabs[key])) {
					return true;
				}
			}
			return false;
		},
		getTabIndex: function() {
			var container = this;
			for (key in container.tabs) {
				if (container.hasClass(container.tabs[key])) {
					return key;
				}
			}
		},
		showMenu: function() {
			var container = this;
			var tabIndex = container.getTabIndex();
			var totalOffset = (parseInt(tabIndex) + 1) * container.options.spriteHeight;
			container.showTabs("-" + totalOffset);
			container.children("div.submenu").show();
			if (container.useFader) {
				container.fadePage();
			}
		},
		hideMenu: function() {
			var container = this;
			container.showTabs(0);
			container.children("div.submenu").hide();
			if (container.useFader) {
				container.restorePage();
			}
		},
		showTabs: function(offset) {
			$("div#mainmenu").css({backgroundPosition: '0px ' + offset + 'px'});
		},
		fadePage: function() {
			var container = this;
			$("div.visual").css({opacity: container.options.opacity});
			$("div.content").css({opacity: container.options.opacity});
			$("div.footer").css({opacity: container.options.opacity});
			$("span.blueborder").hide();
			$("span.inner").hide();
		},
		restorePage: function() {
			$("div.visual").css({opacity: "1"});
			$("div.content").css({opacity: "1"});
			$("div.footer").css({opacity: "1"});
			$("span.blueborder").show();
			$("span.inner").show();
		},
		delayShow: function() {
			var container = this;
			if (overMenu) {
				container.showMenu();
				clearTimeout(offMenuTimer);
			} else {
				showTimer = setTimeout(function(){
					overMenu = true;
					container.showMenu();
					}, container.options.showDelay);				
			}
		},
		delayHide: function() {
			var container = this;
			container.hideMenu();
			clearTimeout(showTimer);
			offMenuTimer = setTimeout(function(){
				overMenu = false;
				}, container.options.offMenuDelay);
		}
	};

	jQuery(function() {
		jQuery("div#mainmenu li").each(function() {
			$(this).extend(jQuery.voormedia.flyoutmenu).initialize({});
		});
	});
	
	jQuery.voormedia.addLocation = {
		options: {
			parentselector: "div.scheduler",
			hideRow: "tr.departuretime",
			blockprefix: "location",
			currentId: 0
		},
		initialize: function(options) {
			jQuery.extend(this.options, options);
			this.currentId = this.options.currentId;
			var container = this;
			
			$(this).click(function(){
				container.appendLocation();
				return false;				
			});
			
		},
		appendLocation: function() {
			var container = this;
			nextId = container.currentId + 1;
			// Append an empty div to the parent element with the right ID and class name. 
			$(container.options.parentselector).append('<div class="' + container.options.blockprefix + '" id="' + container.options.blockprefix + nextId + '"></div>');
			// Use AJAX to get a new piece of location form. The nextId is used to make all the form fields unique.
			$.get("/includes/form-location-template.php", {id:nextId}, function(returnedData) {
				// Append the empty div just made with the returned data of the AJAX request
				$("#" + container.options.blockprefix + nextId).append(returnedData);
			});
			// If a new location is appended, the departure time of the last location should become visible. As the currentId
			// is not yet updated with nextId, the right departure time fields will be shown.
			container.showDeparturetime();
			container.currentId = nextId;
		},
		showDeparturetime: function() {
			var container = this;
			$("#" + container.options.blockprefix + container.currentId + " " + container.options.hideRow).show();
		}
	};

	jQuery(function() {
		jQuery("a#addLocation").each(function() {
			$(this).extend(jQuery.voormedia.addLocation).initialize({});
		});
	});
	
});

// lunch signup validation
function validate() {
  var valid = true;
  if ($("#signupemail").val() == "") {
    $("#signupemail").addClass("required");
    valid = false;
  }
  if ($("#signuporganisation").val() == "") {
    $("#signuporganisation").addClass("required");
    valid = false;
  }
  return valid;
}