$(window).load(function() {
	$("ol.testimonials").masonry({
		itemSelector: "li"
	});
});

$(document).ready(function() {

	/**
	 * Testimonials
	 *
	 */

	$("ol.contents.testimonial a").click(function() {
		var testimonial = $(this).attr("href");
		$("ol.testimonials li").removeClass("selected");
		$(testimonial).addClass("selected");
	});

	/**
	 * 'Back' link
	 *
	 */

	$("a.back").click(function() {
		history.back(1);
		return false;
	});

	/**
	 * Social media icons
	 *
	 */

	$("ul.social-icons img").reflect({
		height: 0.5,
		opacity: 0.25
	});

	$("ul.social-icons li").hover(function() {
		$(this).animate({ "margin-top": "-8px" }, 200);
	}, function() {
		$(this).animate({ "margin-top": "0" }, 200);
	});

	/**
	 * Placeholder text boxes
	 *
	 */

	$("#search-keywords, #search-location").placeholder({
		inactive: "#666",
		active: "#333",
		preventSubmit: false
	});

	/**
	 * Make a whole block-level element into a link
	 *
	 */

	$(".link-all").each(function() {

		var link = $(this).find("a:first"),
			url;

		if(link.length) {
			url = link.attr("href");
			$(this).css("cursor", "pointer")
				.click(function() {
					window.location = url;
				});
		}

	});

	/**
	 * Calculator slider
	 *
	 */

	var updatePrice = function(e, ui) {

		var cost = $("#slider-bg").slider("value");
		$("#last-recruitment-fee").val("£" + cost);

		if(cost < 2845) {
			cost /= 599;
		} else if(cost < 5390) {
			cost /= 569;
		} else if(cost < 10180) {
			cost /= 539;
		} else if(cost < 24750) {
			cost /= 509;
		} else {
			cost /= 479;
		}

		$("#roles").val(Math.floor(cost));

	}

	$("#slider-bg").slider({
		"min": 599,
		"max": 50000,
		"step": 100,
		"change": updatePrice,
		"slide": updatePrice,
		"create": updatePrice
	});

	/**
	 * 5-steps slider
	 *
	 */

	(function() {

		if($("div.slideshow").length) {

			function advanceSlideshow() {

				var current_tab   = $("div.slideshow ol.navigation li.here"),
					total_slides  = $("div.slideshow div.slide").length,
					current_slide = parseInt(current_tab.find("a").attr("href").replace(/^#slide_/, ""), 10),
					next_slide    = ((current_slide + 1) > total_slides) ? 1 : current_slide + 1,
					next_tab      = $("div.slideshow ol.navigation a[href='#slide_" + next_slide + "']").parent("li");

				current_tab.removeClass("here");
				next_tab.addClass("here");

				$("div.slides").stop(true).scrollTo("#slide_" + next_slide, 500, {
					onAfter: function() {
						timeout = window.setTimeout(advanceSlideshow, time);
					}
				});

			}

			var time = 10000,
				timeout = window.setTimeout(advanceSlideshow, time);

			$("div.slideshow").mouseover(function() {
				window.clearTimeout(timeout);
			});

			$(window).blur(function() {
				window.clearTimeout(timeout);
			});

			$("div.slideshow ol.navigation a[href!='/services/pricing/']").click(function() {
				window.clearTimeout(timeout);
				return false;
			});

			$("div.slideshow ol.navigation a[href!='/services/pricing/']").mouseover(function() {
				var slide = $(this).attr("href");
				$("div.slides").stop().scrollTo(slide, 500);
				$("div.slideshow ol.navigation li").removeClass("here");
				$(this).parent("li").addClass("here");
				return false;
			});

			$("div.slideshow ol.navigation a[href='/services/pricing/']").hover(function() {
				$(this).parent("li").addClass("here");
			}, function() {
				$(this).parent("li").removeClass("here");
			});

		}

	})();

	/**
	 * Turns a plain <input type="submit" ... /> element into a nice HTML
	 * button
	 *
	 */

	$("input.button").each(function() {

		var button = $("<a></a>"),
			span = $("<span></span>");

		span.text($(this).val());

		button.addClass("button")
			.html(span)
			.attr("href", "#")
			.click(function() {
				$(this).parents("form").submit();
				return false;
			});

		$(this).replaceWith(button);

	});

	/**
	 * Animated scrolling links
	 *
	 */

	$("ol.contents a, p.back a").click(function() {
		$.scrollTo($(this).attr("href"), 500);
		return false;
	});

	/**
	 * Home services
	 *
	 */

	(function() {

		var slides = [
				{
					title: 'Save essential time and money with low cost, Flat-Fee&nbsp;Recruitment',
					body:  'No hidden costs, no agency fees; just simple, straightforward, Flat-Fee Recruitment on over 1,800 online recruitment sites',
					url:   '/services/flat-fee-recruitment/',
					image: '/wp-content/themes/erecruit/images/slideshow/home/flat-fee-recruitment.png',
					video: true,
					small: {
						title: 'Flat-Fee Recruitment',
						body:  'No hidden costs or agency fees; just a simple flat fee',
						icon:  'flat-fee-recruitment.png'
					}
				},
				{
					title: 'We\'ll do all of the hard work sourcing a huge number of screened, relevant candidates',
					body:  'We have revolutionised the way that companies with a large, on-going recruitment need can recruit a high volume of staff',
					url:   '/services/high-volume-recruitment/',
					image: '/wp-content/themes/erecruit/images/slideshow/home/high-volume-recruitment.png',
					video: false,
					small: {
						title: 'High Volume Recruitment',
						body:  'Always recruiting? Need a large number of staff?',
						icon:  'high-volume-recruitment.png'
					}
				},
				{
					title: 'Our Research Team will find the best candidates for you',
					body:  'Millions of passive jobseekers are sat waiting on CV databases to be approached with the perfect role, maybe yours! Let us do the hard work for you, for one low-cost, flat fee',
					url:   '/services/search-and-selection/',
					image: '/wp-content/themes/erecruit/images/slideshow/home/search-and-selection.png',
					video: false,
					small: {
						title: 'Search & Selection',
						body:  'We hand-pick the best, most relevant candidates for you',
						icon:  'search-and-selection.png'
					}
				},
				{
					title: 'Keen to get your brand out there?',
					body:  'We\'ll source candidates by advertising your roles under your brand, letting you sit back and relax while we do the hard work. Candidate sourcing, branding and simply amazing results, all for one low-cost, flat fee',
					url:   '/services/client-branded-campaigns/',
					image: '/wp-content/themes/erecruit/images/slideshow/home/client-branded-campaigns.png',
					video: false,
					small: {
						title: 'Client Branded Campaigns',
						body:  'Recruitment campaigns carrying your company\'s branding',
						icon:  'client-branded-campaigns.png'
					}
				}
			],
			current_slide = 0,
			cache = [];
	
		for(var i = 0; i < slides.length; i++) {
			var cacheImage = document.createElement("img");
			cacheImage.src = slides[i].image;
			cache.push(cacheImage);
		}

		$("ul.services li")
			.hover(function() {
				$(this).addClass("over");
			}, function() {
				$(this).removeClass("over");
			});

		if(navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) || navigator.userAgent.match(/iPad/i)) {
			$("ul.services li").bind("mouseup", function() {
				$(this).removeClass("over");
			});
		}

		$("#home-services li")
			.css("cursor", "pointer")
			.click(function() {

				var id = $(this).attr("id").replace(/^services_/, ''),
					li = $(this);

				$("#intro-image").animate({ "margin-top": "-238px" }, "medium", function() {
					$(this).css({ "background-image": "url(" + slides[id].image + ")" });
					$(this).animate({ "margin-top": 0 }, "medium");
				});

				$("#intro-content").animate({ "margin-left": "532" }, "slow", function() {
					$("#intro-text h1").html(slides[id].title);
					$("#intro-text p").html(slides[id].body);
					$("#services-link").attr("href", slides[id].url);
					$(this).animate({ "margin-left": "0" }, "slow");
				});

				$(this).find("div.text").fadeOut("medium", function() {
					$(this).find("h2 span").html(slides[current_slide].small.title);
					$(this).find("p").html(slides[current_slide].small.body);
					$(this).find("img").attr("src", "/wp-content/themes/erecruit/images/services/" + slides[current_slide].small.icon);
					$(this).fadeIn("medium");
					li.attr("id", "services_" + current_slide);
					current_slide = id;
				});

				return false;

			});

	})();

	/**
	 * Make links in Tweets list open in a new window
	 *
	 */

	$("div.twitter a, ul.tweets a").attr("target", "_blank");

});

