
/* open links in new window */
$(function(){
    $('#newsright li a').live("click",function(){
        window.open(this.href);
        return false;
    });
});
$(function(){
    $('#whatsnewsidebar p a').live("click",function(){
        window.open(this.href);
        return false;
    });
});



/* flash resize functie */
function resizeNavigationHeight(newheight) {
	var _nav = document.getElementById("jobnav");
	_nav.style.height = (newheight)+"px";
}

function validate() {
    
    var name = $('#name').val();
    var email = $('#email').val()
    
    if((name == 'Your Name') || (name = '')) {
        alert('Please fill out your name.');
        $('#name').focus();
        return false;
    }
    if((email == 'Your E-mail') || (email = '')) {
        alert('Please fill out your e-mail.');
        $('#email').focus();
        return false;
    }
    return true;

}
/*
	Closures are used for each function so we lessen the memory footprint and increase speed by not polluting the global namespace.
	
	To create a new closure:
	(function() {
		// code here, variables won't pollute the global namespace
		var privateVariable = "private!";
		var privateFunction = function() { return "private"; };
		
		this.publicVariable = "public!";
		this.publicFunction = function() { return "public"; };
	});
	
	Adding () before the semi-colon will call it immediately. You can save it to a variable and call the function closure later.
	Adding $(); around the closure will call it on jQuery ready. Useful for making sure DOM is loaded before proceeding with script
*/

/*
	Immediate execution code
*/
(function() {
	/*
		Strings for Localization
			- these are strings added through JS
			- this should provide a centralized place for modifying those strings to provide for translation
			- all other strings, unless noted, will be present in the HTML
	*/
	
	(function($) {
		$.localize = {
			turnEffectsOn: "Turn Effects On", // link in footer, when effects have been turned off
			turnEffectsOff: "Turn Effects Off"
		};

		$.thinbox.defaults.closeText = "Click anywhere to close"; // "close" text in work overlays
	})(jQuery);
})();

/*
	This is the main function
		- called on jQuery's ready event
*/
$((function() {
	
	$.log('Hello from Draftfcb');
	
	/*
		Browser warning for IE6
			- Javascript warning is handled through CSS
	*/
	$.log($.browser, $.browser.version, "browser warning:", $.cookie("noBrowserWarning"));
	if ($.browser.msie && $.browser.version < 7 && !$.cookie("noBrowserWarning")) {
		var $browserWarning = $(".warning span.browser").css('display', 'block');
		$("a", $browserWarning).click(function() {
			$.cookie("noBrowserWarning", true, {expires:14}); //don't warn again for 2 weeks
			$(this).parent().parent().animate({height:0}, 400, "swing", function() {
				$(this).hide();
			});
			return false;
		});
	}
	
	/*
		Turn Effects Off
			- Link in footer, for people whose browsers are running slowly
	*/
	$.log("effects cookie:", $.cookie("effects"));
	var $effectsLink = $("div#footer li.effects a");
	
	function turnEffectsOff() {
		// $.log("Turning effects off");
		$.fx.off = true;
		$effectsLink.text($.localize.turnEffectsOn);
		
	}
	
	function turnEffectsOn() {
		// $.log("Turning effects on");
		$.fx.off = false;
		$effectsLink.text($.localize.turnEffectsOff);
	}
	
	$effectsLink.click(function() {
		if ($.fx.off) {
			turnEffectsOn();
			$.cookie("effects", true, {expires:14});
		}
		else {
			turnEffectsOff();
			$.cookie("effects", false, {expires:14});
		}
		return false;
	});
	
	if ($.cookie("effects") != null && $.cookie("effects") == "false") turnEffectsOff();
	else turnEffectsOn();
		
	/*
		Navigation
	*/
	var $dropdown = $("#navigation .dropdown").dropdown({scrollEnabled:false}); // navigation dropdown
	$("#navigation .matters").click(function() {
		$dropdown.trigger("toggle"); // engage dropdown when user clicks "what matters text"
		return false;
	});
	
	/*
		What's New at Draftfcb section
	*/
	if (document.body.className.indexOf("whatsnew") != -1) {
		(function() {
			$(".thumb-viewer").thumbviewer({
				exclude: 2
			});
			
			var $img = $("div.sidebar div.work img");
			$img.hover(function() {
				$img.stop().fadeTo(250, 0.5);
			}, function() {
				$img.stop().fadeTo(250, 1, function() {
					$img.css('filter','');
				});
			});

			return false;
		})();
	}
	
	/*
		About section
	*/
	if (document.body.className.indexOf("about") != -1) {
		(function() {
			// HOME
			if (document.body.className.indexOf("about home") != -1) {
				(function() {
					/*
						Bottom slider
					*/
					var $gallery = $("div.slider-gallery");
					$("div.header-wrapper", $gallery).corners("20px top");
					var $scroll = $("div.scroll", $gallery).css('overflow', 'hidden');
					var $container = $(".scrollContainer", $scroll);
					var $panels = $("div.panel", $container).css({'float': 'left', position:'relative'});
					var $links = $(".slider a", $gallery);
					var panelWidth = $(":first", $panels).outerWidth(); // should be 922px
					
					$container.css('width', panelWidth * $panels.length);
										
					$(".slider", $gallery).slider({
						item: $panels,
						onChange: function(value, oldValue) {
							// check for whether effects are enabled, colors screw up if we don't
							if ($.fx.off) {
								$links.css('color', '#8D8D8D').removeClass("active");
								$($links[value]).css('color', '#FFFFFF').addClass("active");
							}
							else {
								$($links[oldValue]).stop().animate({color:"#8D8D8D"}, 500, "swing", function() { // animate the old span to grey
									$links.css('color', '#8D8D8D').removeClass("active");
								});
								$($links[value]).stop().animate({color:"#FFFFFF"}, 500, "swing", function() { // animate the new span to white
									$(this).css('color', '#FFFFFF').addClass("active");
								});
							}
						},
						target: $scroll
					});
					
					return false;
				})();
			}

			// BIOS
			if (document.body.className.indexOf("about bios") != -1) {
				(function() {
					var $items = $("div.main ul li.profile").css('cursor', 'pointer').animatedRoll();
					$("ul li", $items).css('cursor', 'pointer');
					
					$items.hover(function() {
						$items.stop().fadeTo(300, 0.5);
						$("img", this).stop().css({opacity:1});
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$items.stop().fadeTo(300, 1, function() {
							// $(this).css("filter", "");
						});
					});
					
					$items.click(function() {
						window.location = $(this).find("a:first").attr("href");
					});
					return false;
				})();
			}

			// CLIENTS
			if (document.body.className.indexOf("about clients") != -1) {
				(function() {
				$logos = $(".main ul:first li");
                                       $logos.hover(function() {
						$logos.stop().fadeTo(300, 0.5);
						$("img", this).stop().css({opacity:1});
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$logos.stop().fadeTo(300, 1, function() {
							// $(this).css("filter", "");
						});
					});
					return false;
				})();
			}

			// FAQS
			if (document.body.className.indexOf("about faqs") != -1) {
				(function() {
					var $list = $("div.main dl");
					var $titles = $("dt", $list);
					var $items = $("dd", $list);
					
					// get rid of widows
					var itemtext = "";
					$items.each(function() {
						itemtext = $(this).html().replace(/(\S+)$/, "$1");
						$(this).html(itemtext);
					});
					
					$items.each(function() {
						$(this).data("fullHeight", $(this).trueHeight({
							appendTo:"div.main dl"
						}) + 12);
					});
					
					$items.css({height:0, opacity:0, display:"none"});

					$titles.singleclick(function() {
						var $item = $(this).next();
						if ($item.is(":visible")) {
							$item.stop().animate({height:0, opacity:0}, 500, "swing", function() {
								$titles.css('padding-left', '30px');
								$(this).hide();
							});
							return false;
						}
						
						var $visible = $("dd:visible", $list);
						
						if ($visible.length == 0) {
							$item.stop().show().animate({
								height: $item.data('fullHeight'),
								opacity:1
							}, 500, "swing", function() {
								$titles.css('padding-left', '30px');
							});
						}
						else {
							$visible.each(function() {
								$(this).stop().animate({height:0, opacity:0}, 500, "swing", function() {
									$(this).hide();
									$item.stop().show().animate({
										height: $item.data('fullHeight'),
										opacity:1
									}, 500, "swing", function() {
										$titles.css('padding-left', '30px');
									});
									
								});
							});
						}
						return false;
					});

					return false;
				})();
			}

			return false;
		})();
	}
	
	
	/*
		WORK SECTION
	*/
	if (document.body.className.indexOf("work") != -1) {
		(function() {
			
			if (document.body.className.indexOf("work home") != -1) {
				(function() {
					$("#work-filter .filters-wrapper").workfilter();					
					var $items = $("div.main ul.works li.work").css('cursor', 'pointer');
					$("ul li", $items).css('cursor', 'pointer');
					
					$.animatedRollLive("div.main ul.works li.work", {
						text: {
							selector: "ul li",
							off: {
								color:"#666666"
							}
						}
					});
					$("div.main ul.works li.work").live("click", function() {
						window.location = $(this).find("a:first").attr("href");
					});
					return false;
				})();
			}
			
			// handle work detail pages
			if (document.body.className.indexOf("work detail") != -1) {
				(function() {
					var $share = $(".share");
					var $list = $("ul:first", $share);
					$share.singleclick(function() {
						if ($list.is(":visible")) {
							$list.fadeOut();
						}
						else {
							$list.fadeIn();
						}
						return false;
					});
					
					$("body.work.detail a[rel*='thinbox']").click(function() {
						$.thinbox($(this).attr('href'));
						return false;
					});

					$("body.work.detail div.sidebar .thumb-viewer").thumbviewer({
						exclude: 1
					});

					$("body.work.detail div.related-content .thumb-viewer").thumbviewer({
						exclude: 4
					});
					
					var $loader = $(new Image()).attr("src", "images/global/black/ui_loader.gif").addClass("loader");
					var $item = $(".item", $wrapper);			
					var $wrapper = $("body.work.detail .main .itemwrapper");
					var $pagination = $("body.work.detail ul.pagination");
					var $items = $("body.work.detail ul.pagination li");
					
					$pagination.pagination({
						onChange: function() {
							var url = $(this).attr("href");
							$item.animate({opacity:0}, 500, "swing", function() { // fade current image out
								$loader.hide();
								$wrapper.append($loader);
								$loader.fadeIn(200, function() { // fade loader in
									$item.load(function() {
										$loader.stop().fadeOut(200, function() { // fade loader out
											$loader.remove();
											$item.animate({opacity:1}, 500, "swing"); // fade new image in
										});
									});
									$item.attr('src', url);
								});
							});
						},
						reset: true
					});
					
					return false;
				})();
			}
			
			return false;
		})();
	}
	
	/*
		FIND US SECTION
	*/
	if (document.body.className.indexOf("findus") != -1) {
		(function() {
			// detail page
			if (document.body.className.indexOf("findus detail") != -1) {
				(function() {
					$("body.findus.detail .thumb-viewer").thumbviewer({
						exclude: 1
					});
					
					return false;
				})();
			}
			
			return false;
		})();
	}
	
	/*
		PRESS CENTER
	*/
	if (document.body.className.indexOf("press") != -1) {
		(function() {
			$("div#content .dropdown").dropdown({group:"sidebar"});

			// press releases
			if (document.body.className.indexOf("press news releases") != -1) {
				(function() {
					var $content = $("div.main");
					var $loader = $("<div class='loader'></div>");
					$content.page({
						contentSelector:">div.posts",
						onBeforeLoad:function() {
							$loader.hide().appendTo($content).fadeIn(250);
						},
						onAfterLoad: function(data) {
							$(".loader").stop().fadeOut(250, function() {
								$(this).remove();
							});
						}
					});

					return false;
				})();
			}

			// in the news
			if (document.body.className.indexOf("press news inthenews") != -1) {
				(function() {
					var $content = $("div.main");
					var $loader = $("<div class='loader'></div>");
					$content.page({
						contentSelector:">div.posts",
						onBeforeLoad:function() {
							$loader.hide().appendTo($content).fadeIn(250);
						},
						onAfterLoad: function(data) {
							$(".loader").stop().fadeOut(250, function() {
								$(this).remove();
							});
						},
						url: "press-news.html"
					});

					return false;
				})();
			}

			// bios home
			if (document.body.className.indexOf("press kit bios biographies") != -1) {
				(function() {
					var $items = $("div.main ul li.profile").css('cursor', 'pointer');
					$("ul li", $items).css('cursor', 'pointer');
					
					$items.hover(function() {
						$items.stop().fadeTo(300, 0.5);
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$items.stop().fadeTo(300, 1, function() {
							this.style.removeAttribute("filter");
						});
					});
					
					$items.animatedRoll({
						image:{
							hover:{
								opacity:1
							}
						},
						text: {
							off:{
								color: "#333333"
							}
						}
					});
					$items.click(function() {
						window.location = $(this).find("a:first").attr("href");
					});
					return false;
				})();
			}

			// engage
			if (document.body.className.indexOf("press engage") != -1) {
				(function() {
					var $content = $("div.main");
					var $loader = $("<div class='loader'></div>");
					$content.page({
						contentSelector:">div.posts",
						onBeforeLoad:function() {
							$loader.hide().appendTo($content).fadeIn(250);
						},
						onAfterLoad: function(data) {
							$(".loader").stop().fadeOut(250, function() {
								$(this).remove();
							});
						}
					});

					return false;
				})();
			}			

			// speaking events
			if (document.body.className.indexOf("press speakingevents") != -1) {
				(function() {
					var $content = $("div.main");
					var $loader = $("<div class='loader'></div>");
					$content.page({
						contentSelector:">table",
						onBeforeLoad:function() {
							$loader.hide().appendTo($content).fadeIn(250);
						},
						onAfterLoad: function(data) {
							$(".loader").stop().fadeOut(250, function() {
								$(this).remove();
							});
						}
					});

					return false;
				})();
			}			
			
			return false;
		})();
	}
	
	
	/*
		JOIN SECTION
	*/
	if (document.body.className.indexOf("join") != -1) {
		(function() {
			
			if (document.body.className.indexOf("join home") != -1) {
				(function() {
					$(".thumb-viewer").thumbviewer({
						exclude: 2
					});
					
					return false;
				})();
			}
			
			if (document.body.className.indexOf("join apply") != -1) {
				(function() {
					var $locations = $("div.main div.location");
					var $expanders = $("a[rel='expand']", $locations);
					var $details = $("div.detail", $locations);
										
					$details.each(function() {
						$(this).data("fullHeight", $(this).trueHeight({
							appendTo:"div.main div.location"
						}) + 126);
					});
					
					$details.css({height:0, opacity:0, display:"none"});

					$expanders.click(function() {
						// $.log("here");
						var $detail = $(this).next();
						if ($detail.is(":visible")) {
							$detail.stop().animate({height:0, opacity:0}, 500, "swing", function() {
								$(this).hide();
							});
						}
						else {
							$detail.stop().show().animate({
								height: $detail.data('fullHeight'),
								opacity:1
							}, 500, "swing");
						}
						return false;
					});
					
					return false;
				})();
			}
			
			return false;
		})();
	}

	/*
		FOLLOW section
	*/
	if (document.body.className.indexOf("follow") != -1) {
		(function() {

			if (document.body.className.indexOf("follow home") != -1) {
				(function() {
					var $items = $("li.section").css('cursor', 'pointer');

					$items.hover(function() {
						$items.stop().fadeTo(300, 0.3);
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$items.stop().fadeTo(300, 1);
					});
				})();
			}

		})();
	}
	/*
		Work section
	*/
	if (document.body.className.indexOf("work home") != -1) {
		(function() {

			if (document.body.className.indexOf("work home") != -1) {
				(function() {
					var $items = $("li.workitem").css('cursor', 'pointer');
                                       
					$items.hover(function() {
						$items.stop().fadeTo(300, 0.3);
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$items.stop().fadeTo(300, 1);
					});
				})();
			}

		})();
	}

}));