/*
	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($) {
		$.thinbox.defaults.closeText = $.localize.clickToClose;
		// "Click anywhere to close"; // "close" text in work overlays
	})(jQuery);
})();

/*
 * This is the main function - called on jQuery's ready event
 */
$(( function() {

	/* Markus Kobligk - centering the pulldown menu on home page*/
	var containerWidth = $("body.home #navigation").width();
	var navigationWidth = $("body.home #navigation div.matters").width()
			+ $("body.home #navigation div.dropdown").width();
	$("body.index #navigation div.matters").css('marginLeft',
			((containerWidth - navigationWidth) / 2) + 'px');
	/** ********************************************************* */

	/** Daniel Busch - content background image */

	if (typeof bgposition != "undefined" && typeof bgimage != "undefined") {
		$.log("INFO ### Draftfcb Hintergrundbild");
		$.log("Datei: " + bgimage + " Position: " + bgposition);

		var brackgroundposition = bgposition;
		if (/top/.test(bgposition)) {
			var horizontal = bgposition.replace(/top/, "");
			var bgtop = 0;
			if ($("#page-navigation ul.sub").size() > 0) {
				$.log('Submenu vorhanden');
				bgtop = $("#page-navigation ul.sub").offset().top + 1; // +
				// border
			} else {
				$.log('Submenu nicht vorhanden');
				bgtop = $("#page-navigation").offset().top
						+ $("#page-navigation").outerHeight(false);
			}

			brackgroundposition = horizontal + ' ' + bgtop + 'px';
			$.log('background-position: ' + brackgroundposition);
			$("div.container").css('background',
					'url(' + bgimage + ') no-repeat ' + brackgroundposition);
			$("#footer").css('background', $('body').css('background-color'));
		} else {
			$("#content").css('background',
					'url(' + bgimage + ') no-repeat ' + brackgroundposition);
		}

	}
	/** ********************************************************* */

	/*
	 * 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.each( function() {
						var $this = $(this);
						var $vw = $(".viewwork", $this);
						if ($vw.length > 0) {
							$this.hover( function() { // over
										$this.css("z-index", 60);

										var left = 140;
										if ($this.hasClass("last")) {
											$vw.css( {
												left : -84
											});
											left = -104;
										}

										$vw.stop().css('z-index', 100).show()
												.animate( {
													left : left,
													opacity : 1
												}, 300, 'swing');
									}, function() { // out
										$this.css("z-index", 50);

										var left = 120;
										if ($this.hasClass("last")) {
											left = -84;
										}

										$vw.stop().css('z-index', 80).animate( {
											left : left,
											opacity : 0
										}, 300, 'swing', function() {
											$this.css('z-index', 1);
											$vw.hide().css('z-index', 1);
										});
									});
						}
					});

					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+)$/, "&nbsp;$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 = $("div.main ul li.section").css('cursor',
							'pointer').animatedRoll();

					$items.hover( function() {
						$items.stop().fadeTo(300, 0.3);
						$(this).stop().fadeTo(300, 1);
					}, function() {
						$items.stop().fadeTo(300, 1);
					});
				})();
			}

		})();
	}
}));

function changePageNavImage(obj, image) {
	var img = $(obj).children()[0];
	$(img).attr('src', image);
}


function podcastPopUp(){
  podcastPopUpWindow = window.open("http://www.draftfcb.com/podcasts.html","Podcast","scrollbars=yes,left=20,top=20,screenX=20,screenY=20,width=800,height=800");
}

/** IE-Funktion fuer Social-Media Bookmarks **/
$(document).ready(function() {
	if ($.browser.msie) {
		$('#socialbookmarks').find('li').css('cursor', 'pointer').click(function() {
			window.open($(this).find('a').attr('href'));
		});
	}
});
/** IE-Funktion fuer Social-Media Bookmarks **/