// CORE JS FILE
jQuery.fn.clickableBlocks = function ()
{
	return this.each (function () {
		var $this = jQuery(this);
		var $anchor = $this.find("a");
		if ($anchor.length)
		{
			$this.css("cursor", "pointer").click (function ()
			{
				location.href = $anchor[0].href;
			});
		}
	});
}


CAREER_INNOVATION = (function ($) {
	var init = function () {
			// runs straigh away
			$("html").removeClass ("no-js").addClass ("has-js");
			
	
			// runs like jQuery normally would when everything is ready
			//run all functions for the site inside the domReady function
			$(domReady); // same as $(document).ready (function () {...});
			
		},
		
		frontPage = function () {
			// banner slider
			if($("#banners").length)
			{
				$("#banner-slider").jshowoff( {
					controls : false,
					speed : 10000,
					changeSpeed : 1000
				}); 
			}
			
			$(".tool-box.single, .tool-box.double ul li").hover( function () {
				$(this).find("h2 a").css({"color":"rgb(158,0,93)"});
			}, function () {
				$(this).find("h2 a").css({"color":"rgb(4,135,135)"});
			});
			
		}, 
		
		aboutUs = function () {
						
			if($("#team-list").length)
			{
				$(".team-member").css({"overflow":"hidden"}).clickableBlocks();
				
				$(".team-member .team-info").css({ "bottom": -80 });
				
				$(".team-member").hover( function () {
					$(this).find(".team-info").stop().animate({"bottom":0},450);
				}, function () {
					$(this).find(".team-info").stop().animate({"bottom": -80 },450);	
				});
			}
			
			if($(".team-related").length)
			{
				$(".team-related ul").slideUp(0);
				
				$(".team-related h3").click ( function () {
					
					if( $(this).siblings("ul").is(":visible") )
					{
						$(this).siblings("ul").slideUp();
					}
					else
					{
						$(this).siblings("ul").slideDown();
					}
					
				});
			}
			
		},
		
		ciTVPage = function () {
			if($(".video").length)
			{
				$container = $('#citv-video-wall');
				
				$container.imagesLoaded( function(){
					$container.masonry({
						itemSelector : '.video',
						columnWidth : 340,
	    				gutterWidth : 20
  					});
				});
				
				$(".video a").fancybox({
					'hideOnContentClick': false,
					'transitionIn': 'elastic',
					'transitionOut': 'elastic',
					'easingIn' : 'swing', 
					'easingOut' : 'swing'	
				});
				
				$(".video").click( function () {
					$(this).find("a").trigger("click");
				});
				
			}
		},
		
		innovationHub = function () {
			if($("#publications-carousel").length)
			{
				$("#publications-carousel").jcarousel( {
					"scroll" : 1,
					"visible" : 5,
					"wrap" : "circular",
					"buttonNextHTML" : '<div id="publications-next" class="nav-button">Next</div>',
					"buttonPrevHTML" : '<div id="publications-prev" class="nav-button">Prev</div>'
				});

				// this is the bit that stops the tooltip appearing! (not awful for accessibility)
				$("#publications-carousel li a img").hover( function() {
					$(this).data("img-title", $(this).attr("title"));
					$(this).attr("title","");
				}, function () {
					$(this).attr("title",$(this).data("img-title"));	
				});
				
			}		
			if($("#projects-carousel").length)
			{
				$("#projects-carousel").jcarousel( {
					"scroll" : 1,
					"visible" : 3,
					"wrap" : "circular",
					"buttonNextHTML" : '<div id="projects-next" class="nav-button">Next</div>',
					"buttonPrevHTML" : '<div id="projects-prev" class="nav-button">Prev</div>'
				});
			}
		},
		
		checkOutPage = function () {
			if($(".form-container").length)
			{
				$(".form-container.close").slideUp(0);
				
				$("h3.option-header").click ( function(e) {
					e.preventDefault();
					
					var formName = $(this).find("a").attr("href");
					
					if( $(formName).is(":visible") )
					{
						$(formName).slideUp();
					}
					else
					{
						$(formName).slideDown();
					}
				});
			
			}
		},
		
		registrationForm = function () {
		
			$("#jobrole").change( function () {
				if( $(this).find("option:selected").text() == "Manager/Team Leader" || $(this).find("option:selected").text() == "HR Professional" )
				{
					$("#extra-options").slideDown();
				}
				else if( $(this).find("option:selected").text() == "Other" )
				{
					$("#other-jobrole").fadeIn('fast');
					$("#extra-options").slideUp();
				}
				else
				{
					$("#other-jobrole").fadeOut();
					$("#extra-options").slideUp();
				}
			});
			
			$("#other.chkbox").click( function () {
				if( $(this).is(":checked") )
					$("#other-interest").fadeIn(); 					
				else
					$("#other-interest").fadeOut(); 				
			});
		
			$("#other-jobrole,#other-interest").focus( function () {
				if($(this).val() == "Please specify")
					$(this).val("");
			});
		
			$("#other-jobrole,#other-interest").blur( function () {
				if($(this).val() == "")
					$(this).val("Please specify");
			});
		
		},
		
		toolsSliders = function () {
		
			if( $("#content-slider").length )
			{
				var $container = $(".main #content-slider");
				var $h3s = $("h3", $container);
				$(".answer", $container).slideUp(1).width(430).hide(); // CM: setting width here to stop the dreaded slidedown jump :)
				$h3s.css("cursor", "pointer").append(' <span>(click to expand)</span>');
				$h3s.click (function () {
					$(".answer").slideUp();
					$h3s.removeClass("active");
					if ($(this).next().is(":hidden"))
					{
						$(this).addClass("active");
						$(this).next().stop().slideDown(1000);
					}
					else
					{
						$(this).removeClass("active");
						$(this).next().stop().slideUp(1000);
					}
				});
			}
		
		},
		
		// this runs only when we know the whole DOM is ready
		domReady = function () {
			frontPage();
			aboutUs();
			ciTVPage();
			innovationHub(); 
			checkOutPage();	
			registrationForm();
			toolsSliders();
			
			var linksHeight = 50 + ( ( $("nav#quicks-site").height() < $("nav#quicks-themes").height() ) ? $("nav#quicks-themes").height() : $("nav#quicks-site").height() );
			
			$("a[href=#quick-links]").click( function (e) {
				e.preventDefault();

				if($("#quick-links").height() > 0)
				{
					$(this).removeClass("active");
					$("#quick-links").stop().animate({"height": 0},800,"easeInOutCirc");			
				}
				else
				{	
					$(this).addClass("active");
					$("#quick-links").stop().animate({"height": linksHeight},1000,"easeOutBounce");			
				}
			});
			
			if( $("#tool-info").length )
			{
				$('#tool-info').portamento( {
					wrapper: $('#content') // set #content as the bottom boundary
				});	
			}
			
			$('.tool-list ul li, .aside-box, .publication-list li, .tool-box.double.hub').clickableBlocks();
			
			
			function scrollUpAfterFancy() {
				$("html,body").animate({ scrollTop: 0 }, 100);
			}
			
			$("a[href=#register]").fancybox({
				'hideOnContentClick': false,
				'transitionIn': 'elastic',
				'transitionOut': 'elastic',
				'easingIn' : 'swing', 
				'easingOut' : 'swing',
				'onComplete' : scrollUpAfterFancy
			});
			
			$("a[href=#contact]").fancybox({
				'hideOnContentClick': false,
				'transitionIn': 'elastic',
				'transitionOut': 'elastic',
				'easingIn' : 'swing', 
				'easingOut' : 'swing'	
			});
			
			if(document.location.search == "?thanks")
			{
				$("a[href=#contact]").trigger("click");
			}
			if(document.location.search == "?done")
			{
				$("a[href=#register]").trigger("click");
			}
		};

	return {
		go : init,
		
		functionNotNeededInside : function () {
			// this will only be accessible from outside
		}
	};
	
})(jQuery);


CAREER_INNOVATION.go ();
