(function () {
	"use strict";

	function contactMapFancybox() {
		var contactMap = $(".col.left.contact .map .fancybox");
		if(!contactMap.length) {
			return false;
		}
		contactMap.fancybox({
			width: 700,
			height: 450
		});
	}
	new contactMapFancybox();

	function PromoPlayerOutIn(parms) {
		parms = parms || {};
		parms.elements = parms.elements || {};
		parms.animation = parms.animation || {};

		this.els = {};
		this.els.player = parms.elements.player || "#promo";
		this.els.slides = parms.elements.slides || "#promo .slides > li";
		this.els.nav = parms.elements.nav || "#promo .paging";
		this.els.tabs = parms.elements.tabs || "#promo .paging > li";

		this.animation = {};
		this.animation.duration = parms.animation.duration || 500;
		this.animation.interval = parms.animation.interval || 10000;

//		this.photos = [];

		this.currentSlide = 0;
		this.numSlides = $(this.els.slides).length;

		this.timer = false;

		this.init = function () {
			if (!$(this.els.player).length) {
				return false;
			}

			// prepare the slides for animation
			this.arrangeSlides();
			// post-load the photos in the slides
//			this.loadPhotos();

			// bind events
			var self = this;
/*
			$(this.els.player + " .paging li").each(function (id) {
				$(this).children("a").mousedown(function () {
					self.changeSlides(id);
					return false;
				});
			});
			$(this.els.player + " .paging li a").click(function () {
				return false;
			});
*/

			// Set a timer to automatically change slides
			// use window.setTimeout instead of window.setInterval - timer will have to be
			// reset during animation in case of manual invocation of slide changes anyway.
			this.timer = window.setTimeout(function () {
				self.advanceSlides();
			}, this.animation.interval);
		};

		// place slides in their initial positions, prepared for animation
		this.arrangeSlides = function () {
			// put the first slide in the appropriate position
			$(this.els.slides).eq(0).addClass("active").css({
				top : 0,
				left : 0
			});

			// place all other slides just off the bottom of the container
			$(this.els.slides).not(":eq(0)").removeClass("active").show().css({
				top : $(this.els.player).height()
			});
		};
/*
		this.loadPhotos = function () {
			// collect attributes of all the images
			var self = this;
			$(this.els.player).find(".slides li").each(function () {
				self.photos.push({
					attributes : {
						src : $(this).find("[name=\"src\"]").val(),
						alt : $(this).find("[name=\"alt\"]").val(),
						width : $(this).find("[name=\"width\"]").val(),
						height : $(this).find("[name=\"height\"]").val()
					}
				});
			});

			// start pre-loading all the images
			for (i in this.photos) {
				this.photos[i].image = new Image();
				this.photos[i].image.id = i;

				this.photos[i].image.onload = function () {
					var attributes = self.photos[this.id].attributes;
					$("#promo .slides li:eq(" + this.id + ") .photo").html("<img src=\"" + attributes.src + "\" alt=\"" + attributes.alt + "\" width=\"" + attributes.width + "\" height=\"" + attributes.height + "\"/>");
				};
				this.photos[i].image.src = this.photos[i].attributes.src;
			}
		};
*/

		// animate a slide change to the specified slide
		this.changeSlides = function (id) {
			// if slide specified is the slide currently being viewed, do nothing
			if (id == this.currentSlide) {
				return;
			}

			// stop any current animation on any slides and clear the animation queue
			$(this.els.slides).stop(true, true);
			// The slide down animation has a callback to slide up the next slide.
			// .stop() ends the slide down animation and triggers the callback.
			// Call stop again to stop this secondary animation.
			$(this.els.slides).stop(true, true);

			// destroy timer in case this change was manually invocated
			// Timer will be reset when animation is complete
			window.clearTimeout(this.timer);

			// set this slide as the current slide
			this.currentSlide = id;

			// move current slide out of view
			var self = this;
			$(this.els.slides + ".active").animate({
				top : 324
			}, this.animation.duration, function () {
				// remove the active flag from the slide
				$(this).removeClass("active");

				// change which navigation tab is highlighted
				$(self.els.tabs).eq(id).siblings().removeClass("active");
				$(self.els.tabs).eq(id).addClass("active");

				// move new slide into view and give it the active flag.
				$(self.els.slides).eq(id).addClass("active").animate({
					top : 0
				}, self.animation.duration, function () {
					// reset/recreate timer
					self.timer = window.setTimeout(function () {
						self.advanceSlides();
					}, self.animation.interval);
				});
			});
		};

		// proceed to the next slide
		this.advanceSlides = function () {
			// identify next slide
			var nextSlide = this.currentSlide + 1;
			if (nextSlide >= this.numSlides) {
				nextSlide = 0;
			}

			this.changeSlides(nextSlide);
		};

		// put things in motion
		this.init();
	}

	var promoPlayer = new PromoPlayerOutIn({
		elements : {
			player : "#promo > .content",
			slides : "#promo > .content .slides > li",
			nav : "#promo > .content .paging",
			tabs : ""
		},
		animation : {
			interval : 8000
		}
	});

	// create and show the paging links
	promoPlayer.initPaging = function () {
		// generate a list of paging links - one for each slide
		var pageList = [];
		var slideCount = 1;
		$(this.els.slides).each(function () {
			pageList.push("<li><a href=\"#\" title=\"" + slideCount + "\">" + slideCount + "</a></li>");
			slideCount ++;
		});
		pageList = pageList.join("");
		pageList = (
			"<ul class=\"paging\">" +
			pageList +
			"</ul>"
		);

		// add the links to the promo player
		$(this.els.player).append(pageList);

		// set the first paging link to active
		$(this.els.player + " .paging li:first-child").addClass("active");

		// bind events to the paging links
		var self = this;
		$(this.els.player + " .paging li").each(function (id) {
			$(this).children("a").mousedown(function () {
				self.changeSlides(id);
				return false;
			});
		});
		$(this.els.player + " .paging li a").click(function () {
			return false;
		});

		// fade the paging links into view
		$(this.els.player + " .paging").fadeIn("fast");
	};
	promoPlayer.initPaging();

	function PartnersLogoFader() {
		this.groups = 0;					// the number of groups that will be cycled through
		this.currentGroup = 0;				// the index of the current group being shown
		this.init = function () {
			var self = this;

			if (!$("#partners").length) {
				return;
			}

			// identify number of groups/sets of logos
			this.groups = $("#partners ul").length;

			// initialize timer and start animation
			window.setInterval(function () {
				self.swapLogos();
			}, 10000);
		};

		/*
		* Animate and change to the next group of logos
		*/
		this.swapLogos = function () {
			var self = this;
			// hide current group
			$("#partners ul").eq(this.currentGroup).fadeOut("slow", function () {
				// increment current group and reset if at end of cycle
				self.currentGroup ++;
				if (self.currentGroup >= self.groups) {
					self.currentGroup = 0;
				}

				// show next group
				$("#partners ul").eq(self.currentGroup).fadeIn("slow");
			});
		};

		this.init();
	}
	new PartnersLogoFader();

	function ModalDialog(parms) {
		parms = parms || {};

		this.init = function () {
			var self = this;

			// only proceed if the provided required element(s) exist
			if (!$(parms.requiredElement).length) {
				return;
			}

			// append popup window/overlay to end of body - IE7 can't handle z-index properly if added elsewhere
			if (!$("#modal-dialog").length) {
				$("body").append(
					"<div id=\"modal-dialog\">" +
					"<div class=\"controls\">" +
					"<a class=\"btn-close\" href=\"#\" title=\"Close\">Close X</a>" +
					"</div>" +
					"<div class=\"content\">" +
					"</div>" +
					"</div>" +
					"<div id=\"modal-dialog-overlay\"></div>"
				);
			}

			// click on dialog 'close' button
			$("#modal-dialog .btn-close, #modal-dialog-overlay").click(function () {
				self.hideDialog();
				return false;
			});

			// bind any application specific events
			this.bindControls();
		};

		/*
		*	overwrite this function, binding the desired events (typically the events that will show the dialog)
		*/
		this.bindControls = function () {
			return;
		};

		/*
		*	overwrite this function, generating the desired dialog content
		*/
		this.getDialogContent = function () {
			return "";
		};

		/*
		*	Show the dialog - identify/generate/insert dialog content
		*/
		this.showDialog = function () {
			var content = this.getDialogContent();
			this.populateDialog(content);
			$("#modal-dialog").fadeIn("fast");
			$("#modal-dialog-overlay").fadeIn("fast");
		};

		/*
		*	Hide the dialog
		*/
		this.hideDialog = function () {
			var self = this;
			$("#modal-dialog").fadeOut("fast", function () {
				$("#modal-dialog-overlay").fadeOut("fast");
				// clear contents of dialog
				self.clearDialog();
			});
		};

		/*
		*	Clear the contents of the dialog
		*/
		this.clearDialog = function () {
			$("#modal-dialog > .content").empty();
		}

		/*
		*	Insert the provided HTML into the dialog
		*/
		this.populateDialog = function (html) {
			$("#modal-dialog > .content").html(html);
		}
	}

	var TeamEmailDialogs = new ModalDialog({
		requiredElement : ".overview-list.team-foxnet > li > .content .more"
	});
	TeamEmailDialogs.member = {};
	TeamEmailDialogs.member.id = "";
	TeamEmailDialogs.member.name = "";

	TeamEmailDialogs.bindControls = function () {
		var self = this;
		$(".overview-list.team-foxnet > li > .content .more").click(function () {
			// identify parter ID and name
			self.member.id = $(this).parents(".content").find("[name=\"id\"]").val();
			self.member.name = $(this).parents(".content").find("[name=\"name\"]").val();
			self.member.title = $(this).parents(".content").find("[name=\"title\"]").val();
			self.member.avatar = $.parseJSON($(this).parents(".content").find("[name=\"avatar\"]").val()).avatar;

			self.showDialog();
			return false;
		});

		$("#modal-dialog .team-email form input[type=\"submit\"]").live("click", function () {
			self.validateForm();

			return false;
		});
	};

	TeamEmailDialogs.getDialogContent = function () {
		var html = (
			"<div class=\"team-email\">" +

			"<div class=\"detail cfx\">" +
			"\t<div class=\"avatar\">" +
			this.member.avatar +
			"\t</div>" +
			"\t<div class=\"content\">" +
			"\t\t<h1>Contact</h1>" +
			"\t\t<h2>" + this.member.name + "</h2>" +
			"\t\t<h3>" + this.member.title + "</h3>" +
			"\t</div>" +
			"</div>" +

			"<form action=\"/contact/\" method=\"post\">" +
			"\t<input type=\"hidden\" name=\"id\" value=\"" + this.member.id + "\"/>" +
			"\t<div class=\"group cfx\">" +
			"\t\t<div class=\"el\">" +
			"\t\t\t<label>Name</label>" +
			"\t\t\t<input type=\"text\" name=\"name\"/>" +
			"\t\t</div>" +
			"\t\t<div class=\"el\">" +
			"\t\t\t<label>Email</label>" +
			"\t\t\t<input type=\"text\" name=\"email\"/>" +
			"\t\t</div>" +
			"\t</div>" +
			"\t<div class=\"group cfx\">" +
			"\t\t<div class=\"el\">" +
			"\t\t\t<label>Phone</label>" +
			"\t\t\t<input type=\"text\" name=\"phone\"/>" +
			"\t\t</div>" +
			"\t\t<div class=\"el\">" +
			"\t\t\t<label>Subject</label>" +
			"\t\t\t<input type=\"text\" name=\"subject\"/>" +
			"\t\t</div>" +
			"\t</div>" +
			"\t<div class=\"el\">" +
			"\t\t<label>Your Message</label>" +
			"\t\t<textarea name=\"message\"></textarea>" +
			"\t</div>" +
			"\t<input type=\"submit\" value=\"Send Your Message »\"/>" +
			"</form>" +
			"</div>"
		);

		return html;
	};

	TeamEmailDialogs.validateForm = function () {
		// clear any previous error messages
		$("#modal-dialog .team-email .error").remove();

		// identify data
		var data = {};
		data.id = $("#modal-dialog .team-email form input[name=\"id\"]").val();
		data.fullname = $("#modal-dialog .team-email form input[name=\"name\"]").val();
		data.email = $("#modal-dialog .team-email form input[name=\"email\"]").val();
		data.phone = $("#modal-dialog .team-email form input[name=\"phone\"]").val();
		data.subject = $("#modal-dialog .team-email form input[name=\"subject\"]").val();
		data.message = $("#modal-dialog .team-email form [name=\"message\"]").val();

		$.get("/about-us/team-foxnet/ajax-contact/", data, function (json) {
			var i;

			if (json.errors) {
				for (i in json.errors) {
					if (!json.errors.hasOwnProperty(i)) {
						continue;
					}
					$("#modal-dialog .team-email form [name=\"" + i + "\"]").after("<div class=\"error\">" + json.errors[i] + "</div>");
				}
			} else {
				$("#modal-dialog .team-email form").hide();
				$("#modal-dialog .team-email form").after("<p>" + json.message + "</p>");
			}
		}, "JSON");
	};

	TeamEmailDialogs.init();

}());
