/**
 * Global variables
 */

var disableHashChangeEvent = false;
var disableCloseVideo = false;

/**
 * Functions
 */

/**
 * @description: Checks if a variable is empty
 * @params: mixed
 * @return: boolean
 */
function isEmpty(val) {
	return (typeof val == 'undefined') || (val == null) || ((typeof val == 'string') && (val.length == 0));
}

/**
 * @description: Remove all extra whitespaces and remove spaces at the beginning and ending of a string
 * @params: string 
 * @return: string
 */
function trimAll(str){
	return str.replace(/^\s+|\s+$/g,'').replace(/\s+/g,' ');
}

function trim(str, chr) {
	if (typeof chr == 'undefined' || chr == null || chr.length == 0) {
		chr = ' ';
	}
	var reg = new RegExp('^[' + chr + ']+|[' + chr + ']+$', 'g');
	return str.replace(reg, '');
}

/**
 * @description: Checks if the param string or array is empty
 * @params: string / array
 * @return: true or false
 */
function isEmail(str) {
	return /\w{1,}[@][\w\-]{1,}([.]([\w\-]{1,})){1,3}$/.test(trimAll(str));
}

function isURL(str) {
	return /^https?:\/\/([-\w\.]+)+(:\d+)?(\/([\w\/_\.]*(\?\S+)?)?)?/.test(str);
}

function capitalize(str) {
	return str.replace(/^\w/, function($0) {return $0.toUpperCase();});
}

/**
 * @description: Detect mobile devices
 * @params: string 
 * @return: boolean
 */
function isMobileClient(userAgent) {
	var mobileClients = [
		"midp",
		"240x320",
		"blackberry",
		"netfront",
		"nokia",
		"panasonic",
		"portalmmm",
		"sharp",
		"sie-",
		"sonyericsson",
		"symbian",
		"windows ce",
		"benq",
		"mda",
		"mot-",
		"opera mini",
		"philips",
		"pocket pc",
		"sagem",
		"samsung",
		"sda",
		"sgh-",
		"vodafone",
		"xda",
		"iphone",
		"android",
		"ipad"
	];
	userAgent = userAgent.toLowerCase();
	for (var i in mobileClients) {
		if (userAgent.indexOf(mobileClients[i]) != -1) {
			return true;
		}
	}
    return false;
};

function isMobile() {
	return isMobileClient(navigator.userAgent);
}

function hasEventListener($target, eventName) {
	var ret = false;
	for (var i in $target.data('events')) {
		if (i == eventName) {
			ret = true;
			break;
		}
	}
	return ret;
}

/**
 * @description: Gets the ipod/iphone/ipad window orientation
 * @params: none 
 * @return: string
 */
function getWindowOrientation() {
	var orientation = 'landscape';
	var supportsOrientation = (typeof window.orientation == 'number' && typeof window.onorientationchange == 'object');
	if(supportsOrientation) {
		orientation = window.orientation;
		switch(orientation) {
			case 90: case -90:
				orientation = "landscape";
			break;
			default:
				orientation = "portrait";
			break;
		}
	}
	return orientation;
}

function fillVideoInfo(credit) {
	var title = $('#video-title');
	(typeof credit.title == 'undefined' || credit.title == null)? title.hide(): title.show();
	title.text(credit.title);
	
	var subtitle = $('#video-subtitle');
	(typeof credit.subtitle == 'undefined' || credit.subtitle == null)? subtitle.hide(): subtitle.show();
	subtitle.text(credit.subtitle);
	
	var agency = $('#video-agency');
	(typeof credit.agency == 'undefined' || credit.agency == null)? agency.hide(): agency.show();
	$('p', agency).text(credit.agency);
	
	var artist = $('#video-artist');
	(typeof credit.artist == 'undefined' || credit.artist == null)? artist.hide(): artist.show();
	$('p', artist).text(credit.artist);
	
	var client = $('#video-client');
	(typeof credit.client == 'undefined' || credit.client == null)? client.hide(): client.show();
	$('p', client).text(credit.client);
	
	var director = $('#video-director');
	(typeof credit.director == 'undefined' || credit.director == null)? director.hide(): director.show();
	$('p', director).text(credit.director);
	
	$('#video-description').html(credit.description);
	
	// change hash
	var hash = trim(window.location.hash, '#');
	if (!isEmpty(hash)) {
		var pos = hash.search('-');
		if (pos >= 0) {
			hash = hash.substr(0, pos);
		}
	}
	if (isEmpty(hash)) {
		hash = 'home';
	}
	
	var videoName = credit.video;
	pos = videoName.lastIndexOf('/');
	videoName = videoName.substr(pos + 1);
	
	hash += '-' + videoName;
	
	window.location.hash = hash;
}

function searchCreditById(id) {
	var ret = null;
	var len = credits.length;
	for (var i = 0; i < len; i++) {
		if (id == credits[i].id) {
			ret = credits[i];
			break;
		}
	}
	return ret;
}

function searchCreditByVideo(video) {
	var ret = null;
	var trimVideo = trim(video, '/');
	var len = credits.length;
	for (var i = 0; i < len; i++) {
		if (trimVideo == credits[i].video) {
			ret = credits[i];
			break;
		}
	}
	return ret;
}

function hideScroller() {
	$('.scroller').hide();
	$('html').css('overflow-y', 'auto');
	$('body').css({'position': 'static', 'overflow-y': 'auto'});
}

function showScroller() {
	$('.scroller').show();
	$('html').css('overflow-y', 'hidden');
	$('body').css({'overflow-y': 'hidden'});	
}

function showVideo(credit) {
	if ((credit == null) || isEmpty(credit.video) || isEmpty(credit.image)) {
		return;
	}

	fillVideoInfo(credit);

	// show the video
	$('#video').css('visibility', 'visible');
	$('.video-container').height($(document).height());
	if (isMobile()) {
		disableCloseVideo = true;
		$('.video-container').css({'display' : 'block', 'visibility' : 'visible'});
		$('#video').html(
			'<video controls="" width="743" height="395" poster="' + credit.image + '" >\n' +
			'	<source src="' + credit.video + '"  />\n' +
			'</video>'
		);
		$('.video-container .content').css('top', 0);
		var timer = $.timer(1000, function() {
			disableCloseVideo = false;
			timer.stop();
		});
	} else {
		$('.video-container').css({'display' : 'none', 'visibility' : 'visible'});
		$('.video-container').fadeIn(function() {
			disableCloseVideo = true;
			var timer = $.timer(100, function() {
				var player = $('*[name=\'VideoPlayer\']')[0];
				if (typeof player.load != 'undefined') {
					timer.stop();
					
					player.load(credit.video, credit.image);
					/*
					var topPadding = Math.min(200, $(window).height() - $('.video-container .wrapper').height() - 15);
					$('.video-container .content').css('top', topPadding + 'px');*/
					disableCloseVideo = false;
				}
			});
		});
	}
}

/**
 * COMMON
 */

Cufon.replace('div.container div.header ul.main-menu a', {printable: false, fontFamily: 'Helvetica_300', hover: true});
Cufon.replace('div.container div.header ul.right-menu a', {printable: false, fontFamily: 'Helvetica_300', hover: true});
Cufon.replace('div.container div.header p.quote', {printable: false, fontFamily: 'MrsEaves_italic_500'});
Cufon.replace('div.container div.header div.banner div.current-date', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.container div.header div.banner ul.weather', {printable: false, fontFamily: 'MrsEavesRoman_400'});
Cufon.replace('div.container div.footer', {printable: false, fontFamily: 'Times_400', hover: true});

/**
 * MAIN
 */

$(document).ready(function() {
	var speed = 500;
	var timer;
	
	var isMobile = isMobileClient(navigator.userAgent);
	if (isMobile) {
		$('.scroller').remove();
	}
	
	function updateVerticalScrollbar($page) {
		var win = $('.page-slider');
		var target = $('.page-slider-container');
		var scroller = $('.scroller');
		//scroller.height(document.body.parentNode.clientHeight);
		var height = (scroller.height() / win.height()) * $page.height();
		var scrollerPage = $('.scroller-page');
		scrollerPage.height(height);
		var y0 = target.offset().top;
		var h0 = $page.height() - win.height();
		var h1 = scrollerPage.height() - scroller.height();
		var a = h0 / h1;
		scroller.scrollTop(0);
		scroller.unbind('scroll').scroll(function() {
			var y = $page.offset().top - target.offset().top + a * (scroller.offset().top - scrollerPage.offset().top);
			/*if($('.bulletin-overlay').css('visibility') == 'visible') {
				console.log(scroller.offset().top);
				$('.bulletin-overlay').css({top: -y});
			}*/
			target.translate({y: -y});
		});
	}

	function scrollToPage(page, speed, callback) {
		if (typeof speed == 'undefined') {
			speed = 0;
		}
		var container = $('.page-slider-container');
		var y = page.offset().top - container.offset().top;
		container.translate({y: -y}, speed, callback);
	}

	function gotoPage(page, speed, callback) {
		// var isMobile = isMobileClient(navigator.userAgent);
		if (!isEmpty(page)) {
			var pageId = '#' + page + '-class';
			page = $(pageId);
		}

		if (typeof page == 'undefined' || page.length == 0) {
			page = $('.content:first');
		}

		if (typeof timer != 'undefined') {
			timer.stop();
		}

		var oldTop;
		scrollToPage(page, speed, function() {
			var pageId = page.attr('id');
			var pos = pageId.search(/-class$/);
			if (pos >= 0) {
				pageId = pageId.substring(0, pos);
			}
			// window.location.hash = '#' + pageId;
			updateVerticalScrollbar(page);
		});
		/*
		if (!isMobile) {
			var slider = $('.page-slider');
			slider.height(page.height());
		}*/
		timer = $.timer(100, function() {
			var newTop = page.offset().top;
			if (typeof oldTop == 'undefined' || oldTop != newTop) {
				scrollToPage(page, 0, function() {
					updateVerticalScrollbar(page);
				});
				/*
				if (!isMobile) {
					var slider = $('.page-slider');
					slider.height(page.height());
				}*/
			} else {
				timer.stop();
			}
			oldTop = newTop;
		});
	}
	
	$(window).hashchange(function() {
		// force video close
		$('#close_video').click();
		
		var page = trim(window.location.hash, '#');
		if (page != 'op-ed') {
			var pos = page.search('-');
			if (pos >= 0) {
				page = page.substr(0, pos);
			}	
		}
		
		//
		$('.main-menu a, .right-menu a').each(function() {
			$(this).attr('class', '');
		});
		
		$('.main-menu a[href="/'+ window.location.hash +'"]').attr('class', 'selected');
		$('.right-menu a[href="/'+ window.location.hash +'"]').attr('class', 'selected');
		
		Cufon.refresh('div.container div.header ul.main-menu a');
		Cufon.refresh('div.container div.header ul.right-menu a');
		
		if (!disableHashChangeEvent) {
			setTimeout(function() {
				gotoPage(page, speed);
			}, 100);
		}
		
		disableHashChangeEvent = false;
	});

	var slider = $('.page-slider');
	var container = $('.page-slider-container');
	container.css('margin-top', 0);
	
	var page = trim(window.location.pathname, '/');
	var pos = page.search('/');
	if (pos >= 0) {
		page = page.substr(0, pos);
	}
	if (isEmpty(page)) {
		page = trim(window.location.hash, '#');
	}
	
	var video = null;
	if (page != 'op-ed') {
		pos = page.search('-');
		if (pos >= 0) {
			video = page.substring(pos + 1);
			page = page.substr(0, pos);
		}
		if (!isEmpty(video)) {
			var credit = searchCreditByVideo('data/video/' + video);
			showVideo(credit);
		}	
	}
	
	gotoPage(page);
	
	$('.right-menu a, .footer .menu a, .title .up , .title .down').click(function() {
		var target = this;
		target.blur();
		
		if ($(target).parent().attr('class') == 'disclaimer') {
			return false;
		}
		
		if (target.target == '_blank') {
			return true;
		}
		/*
		timer.stop();
		// window.location.hash = target.hash;
		var section = trim(target.hash, '#');
		if (isEmpty(section)) {
			section = trim($(target).attr('href'), '/');
		}
		gotoPage(section, speed);
		
		return false;*/
	});
	
	slider.pageSlider(
		{complete: function(page) {
			var pageId = page.attr('id');
			var pos = pageId.search(/-class$/);
			if (pos >= 0) {
				pageId = pageId.substring(0, pos);
			}
			
			disableHashChangeEvent = true;
			window.location.hash = '#' + pageId;
		}
	});
	
	// Update top menu the firt time the user reaches the page
	$('.main-menu a, .right-menu a').each(function() {
		$(this).attr('class', '');
	});

	$('.main-menu a[href="/'+ window.location.hash +'"]').attr('class', 'selected');
	$('.right-menu a[href="/'+ window.location.hash +'"]').attr('class', 'selected');

	Cufon.refresh('div.container div.header ul.main-menu a');
	Cufon.refresh('div.container div.header ul.right-menu a');
});

/**
 * HOME
 */

Cufon.replace('div.content#home-class div.the-team h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#home-class div.the-team div.info', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.content#home-class div.slideshow div.controls a.play', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#home-class div.slideshow h3 span.title', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#home-class div.slideshow h3 span.subtitle', {printable: false, fontFamily: 'Times_400'});

$(document).ready(function() {
	var overlay_index = 0;
	
	// Adjust height of hidden layers
	$('.home-team-overlay').height($(document).height());
	
	// Team overlay
	$('#home-class .the-team .people li a').click(function() {
		overlay_index = $('#home-class .the-team .people li a').index(this);
		$('#home_team_overlay_' + overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
		$('#home_team_overlay_' + overlay_index).fadeIn();
		hideScroller();
		return false;
	});
	$('.home-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#home_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		$('#home_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.home-team-overlay a.closer').click(function() {
		$('#home_team_overlay_' + overlay_index).fadeOut(function() {
			$('#home_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
});

/**
 * WORK
 */

Cufon.replace('div.content#work-class div.the-team h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#work-class div.the-team div.info', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.content#work-class p.custom-reels', {printable: false, fontFamily: 'Times_italic_400'});

function adjustImageSize(target, w0, h0, w1, h1) {
	var a0 = h0 / w0;
	var a1 = h1 / w1;
	var w2 = 0;
	var h2 = 0;
	var x2 = 0;
	var y2 = 0;
	if (a0 < a1) {
		w2 = h1 / a0;
		h2 = h1;
		x2 = (w1 - w2) / 2;
		y2 = 0;
		target.css({
			'width' : w2,
			'height' : h2,
			'left' : x2,
			'top' : 0,
			'background-size' : w2+'px '+h2+'px'
		});
	} else {
		w2 = w1;
		h2 = w1 * a0;
		x2 = 0;
		y2 = (h1 - h2) / 2;
		target.css({
			'width' : w2,
			'height' : h2,
			'left' : 0,
			'top' : y2,
			'background-size' : w2+'px '+h2+'px'
		});
	}
	target.css('visibility', 'visible');
}

$(document).ready(function() {
	var overlay_index = 0;
	var page = $('div.content#work-class');
	var grid = $('div.grid', page);
	var overlay = $('#overlay', grid);
	var inner = $('div.inner', grid);
	var mobile = isMobile();
	
	function showImageOverlay(obj) {
		var p = 1.24;
		var target = $(obj).parents('div:first');
		var index = target.data('index');
		var credit = searchCreditById(workCredits[index].dataVideoCreditId);
		var agency = workCredits[index].title;
		var title = workCredits[index].subtitle;
		var image = $('img', obj).attr('src');
		var video = $(obj).attr('href');
		var offset = target.offset();
		var innerOffset = inner.offset();
		var x0 = innerOffset.left;
		var y0 = innerOffset.top;
		var w0 = inner.width();
		var h0 = inner.height();
		var x1 = offset.left;
		var y1 = offset.top;
		var w1 = target.width();
		var h1 = target.height();
		var w2 = Math.round(p * w1);
		var h2 = Math.round(p * h1);
		var x2 = Math.round(x1 - (w2 - w1) / 2);
		var y2 = Math.round(y1 - (h2 - h1) / 2);

		x2 = Math.min(x0 + w0 - w2, Math.max(x0, x2)) - 3;
		y2 = Math.min(y0 + h0 - h2, Math.max(y0, y2)) - 3;

		overlay.data('credit', credit);

		$('a#link1 .img', overlay).remove();
		$('a#link1', overlay).prepend('<div class="img" style="background-image:url(' + image + ');"></div>');

		//var source = $('img', target);
		var img = $('.img', overlay);
		var img_width = $(obj).attr('img_width');
		var img_height = $(obj).attr('img_height');
		$(img).ready(function() {
			overlay.show();
			overlay.offset({left: x2, top: y2});
			overlay.width(w2);
			overlay.height(h2);
			adjustImageSize(img, img_width, img_height, w2, h2);
		});

		$('.bottom', overlay).css('display', credit == null? 'none': 'block');
		$('#link1', overlay).css('cursor', credit == null? 'default': 'pointer');
		if (!isEmpty(agency) && !isEmpty(title)) {
			$('#agency', overlay).show();
			$('#agency', overlay).text(agency);
			$('#title', overlay).text(title);
		} else {
			$('#agency', overlay).hide();
			var a = isEmpty(agency)? '': agency;
			var t = isEmpty(title)? '': title;
			$('#title', overlay).text(a + t);
		}
		$('a', overlay).attr('href', video);

		// remove background preloaded
		$('div.content#work-class div.grid div.inner').css('background', 'none');
	}
	
	function hideImageOverlay() {
		var innerOffset = inner.offset();
		overlay.offset(innerOffset);
		overlay.hide();
		
		var innerContent = $('.inner-content', grid);
		innerContent.stop();
		$('.row', grid).each(function() {
			//if($(this).css('opacity') != 1) {
				$(this).stop().animate({
					'opacity' : 1,
					'filter' : 'alpha(opacity=100)'
				}, 'fast');
			//}
		});
	}
	
	// Adjust height of hidden layers
	$('.work-team-overlay').height($(document).height());
	
	// Team overlay
	$('#work-class .the-team .people li a').click(function() {
		overlay_index = $('#work-class .the-team .people li a').index(this);
		$('#work_team_overlay_' + overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
		$('#work_team_overlay_' + overlay_index).fadeIn();
		hideScroller();
		return false;
	});
	$('.work-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#work_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		$('#work_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.work-team-overlay a.closer').click(function() {
		$('#work_team_overlay_' + overlay_index).fadeOut(function() {
			$('#work_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
	
	var videoContainer = $('.video-container');
	$(window).resize(function() {
		/*
		if (!isMobile() && videoContainer.css('visibility') == 'visible') {
			var topPadding = Math.min(200, $(window).height() - $('.video-container .wrapper').height() - 15);
			$('.video-container .content').css('top', topPadding + 'px');
		}*/
	});
	
	// hide video
	$('#close_video').click(function() {
		if (disableCloseVideo) {
			return false;
		}
		
		// change hash
		var hash = trim(window.location.hash, '#');
		if (hash != 'op-ed') {
			if (!isEmpty(hash)) {
				var pos = hash.search('-');
				if (pos >= 0) {
					hash = hash.substr(0, pos);
				}
			}
			if (isEmpty(hash)) {
				hash = 'home';
			}
			window.location.hash = hash;	
		}
	
		if (mobile) {
			$('#video').empty();
			$('#video').css('visibility', 'hidden');
			$('.video-container').css({'display' : 'block', 'visibility' : 'hidden'});
		} else {
			var player = $('*[name=\'VideoPlayer\']')[0];
			if (typeof player != 'undefined') {
				try {
					player.close(null);
				} catch(e) {
					console.log(e);
				}	
			}
			
			$('#video').css('visibility', 'hidden');
			$('.video-container').fadeOut(function() {
				$('.video-container').css({'display' : 'block', 'visibility' : 'hidden'});
			});
		}
		showScroller();
		
		this.blur();
		return false;
	});
	
	$('#overlay a', grid).click(function() {
		this.blur();
		
		var credit = overlay.data('credit');
		var video = credit != null? credit.video: null;
		var image = credit != null? credit.image: null;
		if ((typeof credit == 'undefined') || credit == null || isEmpty(credit.video) || isEmpty(credit.image)) {
			return false;
		}
		
		hideImageOverlay();
		fillVideoInfo(credit);
		
		// show video
		$('#video').css('visibility', 'visible');
		$('.video-container').height($(document).height());
		hideScroller();
		if (isMobile()) {
			disableCloseVideo = true;
			$('.video-container').css({'display' : 'block', 'visibility' : 'visible'});
			$('#video').html(
				'<video controls="" width="743" height="395" poster="' + credit.image + '" >\n' +
				'	<source src="' + credit.video + '"  />\n' +
				'</video>'
			);
			$('.video-container .content').css('top', 0);
			var timer = $.timer(1000, function() {
				disableCloseVideo = false;
				timer.stop();
			});
		} else {
			disableCloseVideo = true;
			$('.video-container').css({'display' : 'none', 'visibility' : 'visible'});
			$('.video-container').fadeIn(function() {
				var timer = $.timer(100, function() {
					var player = $('*[name=\'VideoPlayer\']')[0];
					if (typeof player.load != 'undefined') {
						timer.stop();

						player.load(video, image);
						/*
						var topPadding = Math.min(200, $(window).height() - $('.video-container .wrapper').height() - 15);
						$('.video-container .content').css('top', topPadding + 'px');*/
						disableCloseVideo = false;
					}
				});
			});
		}
		
		return false;
	});
	
	$('a.cell', grid).click(function() {
		showImageOverlay(this);
		this.blur();
		return false;
	});
	
	$('a.cell', grid).mouseenter(function() {
		if (mobile) {
			return;
		}
		showImageOverlay(this);
	});
	
	grid.mouseenter(function() {
		if (mobile) {
			return;
		}
		$('.row', grid).each(function() {
			var target = $(this);
			if ($('.row', target).length > 0) {
				return;
			}
			//if($(this).css('opacity') == 1) {
				target.stop().animate({
					'opacity' : 0.50,
					'filter' : 'alpha(opacity=50)'
				}, 'fast');
			//}
		});
	});
	
	grid.mouseleave(function() {
		if (mobile) {
			return;
		}
		hideImageOverlay();
	});
	
	$('a.cell img', grid).each(function() {
		var target = $(this);
		var parent = target.parents('div:first');
		if (target.width() > 0) {
			adjustImageSize(target, target.width(), target.height(), parent.width(), parent.height());
		} else {
			target.one('load', function() {
				adjustImageSize(target, target.width(), target.height(), parent.width(), parent.height());
			});
		}
	});
	
	$('a.cell', grid).each(function(i) {
		$(this).parents('div:first').data('index', i);
	});
});

/**
 * BULLETIN
 */
Cufon.replace('div.content#bulletin-class div.the-team h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#bulletin-class div.the-team div.info', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.content#bulletin-class div.header-story', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.content#bulletin-class div.news div.item h4', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#bulletin-class div.main-story h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#bulletin-class div.main-story div.main-story-content h4', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#bulletin-class div.main-story div.main-story-content div.text a.full-story', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.content#bulletin-class div.main-story div.video a.play', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#bulletin-class div.secondary-story h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#bulletin-class div.secondary-story div.video a.play', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.content#bulletin-class div.secondary-story div.secondary-story-content h4', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.bulletin-overlay h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.bulletin-overlay div.main-story-content h4', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('div.bulletin-overlay div.main-story-content div.text a.full-story', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('div.bulletin-overlay div.video a.play', {printable: false, fontFamily: 'Helvetica_300'});

$(document).ready(function() {
	var overlay_index = 0;
	
	// Adjust height of hidden layers
	$('.bulletin-team-overlay').height($(document).height());
	
	// Team overlay
	$('#bulletin-class .the-team .people li a').click(function() {
		overlay_index = $('#bulletin-class .the-team .people li a').index(this);
		$('#bulletin_team_overlay_' + overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
		$('#bulletin_team_overlay_' + overlay_index).fadeIn();
		hideScroller();
		return false;
	});
	$('.bulletin-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#bulletin_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		$('#bulletin_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.bulletin-team-overlay a.closer').click(function() {
		$('#bulletin_team_overlay_' + overlay_index).fadeOut(function() {
			$('#bulletin_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
	
	$('#close_bulletin').click(function() {
		window.location.href = '/#bulletin';
		return false;
	});
});

/**
 * ABOUT
 */

Cufon.replace('#about-class .header-story', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('#about-class h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('#about-class h4', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('#about-class .info', {printable: false, fontFamily: 'Times_italic_400'});

Cufon.replace('.team-overlay h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('.team-overlay h4', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('.team-overlay h5', {printable: false, fontFamily: 'Dakota'});
Cufon.replace('.the-team .people li a', {printable: false, fontFamily: 'Dakota', hover: true});

var _About = new About();
function About() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};

About.prototype.build = function() {
	var _l0 = this;
	
	// Adjust height of hidden layers
	$('.about-team-overlay').height($(document).height());
	$('.about-team-overlay .content').height($('.about-team-overlay').height()-270);

	var hash = trim(window.location.hash, '#');
	var person = null;
	if (!isEmpty(hash)) {
		var pos = hash.search('-');
		if (pos >= 0) {
			person = hash.substring(pos + 1);
		}
	}
	if (person != null) {
		var overlay = $('.about-team-overlay[name="' + person + '"]');
		overlay_index = $('.about-team-overlay').index(overlay);
		
		overlay.css({'display' : 'none', 'visibility' : 'visible'});
		overlay.fadeIn();
		hideScroller();
	}
	
	// Team overlay
	$('#about-class .the-team .people li a').click(function() {
		overlay_index = $('#about-class .the-team .people li a').index(this);
		var overlay = $('#about_team_overlay_' + overlay_index);

		disableCloseVideo = true;
		window.location.hash = '#about-' + overlay.attr('name');
		var timer = $.timer(1000, function() {
			disableCloseVideo = false;
			timer.stop();
		});
		
		overlay.css({'display' : 'none', 'visibility' : 'visible'});
		overlay.fadeIn();
		hideScroller();
		return false;
	});
	$('.about-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#about_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		var overlay = $('#about_team_overlay_' + overlay_index);
		
		disableCloseVideo = true;
		window.location.hash = '#about-' + overlay.attr('name');
		var timer = $.timer(1000, function() {
			disableCloseVideo = false;
			timer.stop();
		});

		overlay.css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.about-team-overlay a.closer').click(function() {
		$('#about_team_overlay_' + overlay_index).fadeOut(function() {
			disableCloseVideo = true;
			window.location.hash = 'about';
			var timer = $.timer(1000, function() {
				disableCloseVideo = false;
				timer.stop();
			});
			
			$('#about_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
	
	$('.mailto').click(function() {
		$(this).attr('href', 'mailto:?subject=I\'d like to share this link with you&body=' + window.location.href);
	});
	
	// Archive
	var isOldIE = $.browser.msie && $.browser.version.substr(0, 1) < 9;
	var hideShowArchive = $('#hide-show-archive');
	var archiveList = $('#archive-list');
	hideShowArchive.click(function() {
		if ((archiveList).css('opacity') == 0) {
			/*
			if (isOldIE) {
				archiveList.css({'opacity': 1, 'filter': ''});
			} else {
				archiveList.animate({'opacity': 1});
			}*/
			archiveList.css({'opacity': 1, 'filter': ''});
			hideShowArchive.addClass('selected');
		} else {
			/*
			if (isOldIE) {
				archiveList.css('opacity', 0);
			} else {
				archiveList.animate({'opacity': 0});
			}*/
			archiveList.css('opacity', 0);
			hideShowArchive.removeClass('selected');
		}
		
		this.blur();
		return false;
	});
	archiveList.css('opacity', 0);
	hideShowArchive.trigger('click');
};

About.prototype.showTeamOverlay = function() {
	var _l0 = this;
	$('#about_team_overlay_' + _l0.overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
	$('#about_team_overlay_' + _l0.overlay_index).fadeIn();
};

About.prototype.hideTeamOverlay = function() {
	var _l0 = this;
	$('#about_team_overlay_' + _l0.overlay_index).fadeOut(function() {
		$('#about_team_overlay_' + _l0.overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
	});
};

/**
 * CONTACT
 */

Cufon.replace('div.content#contact-class div.the-team h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#contact-class div.the-team div.info', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('#contact-class_section h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('#contact-class_section div.content#contact-class div.items div.item h3', {printable: false, fontFamily: 'Helvetica_300'});

$(document).ready(function() {
	var overlay_index = 0;
	
	// Adjust height of hidden layers
	$('.contact-team-overlay').height($(document).height());
	
	// Team overlay
	$('#contact-class .the-team .people li a').click(function() {
		overlay_index = $('#contact-class .the-team .people li a').index(this);
		$('#contact_team_overlay_' + overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
		$('#contact_team_overlay_' + overlay_index).fadeIn();
		hideScroller();
		return false;
	});
	$('.contact-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#contact_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		$('#contact_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.contact-team-overlay a.closer').click(function() {
		$('#contact_team_overlay_' + overlay_index).fadeOut(function() {
			$('#contact_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
});

/**
 * Op/Ed
 */

Cufon.replace('div.content#op-ed-class div.the-team h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('div.content#op-ed-class div.the-team div.info', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('#op-ed-class .header-story', {printable: false, fontFamily: 'Times_italic_400'});
Cufon.replace('#op-ed-class h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('#op-ed-class h4', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('#op-ed-class h5 .left', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('#op-ed-class h5 .right', {printable: false, fontFamily: 'Times_italic_400'});

var _OpEd = new OpEd();
function OpEd() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};

OpEd.prototype.build = function() {
	var overlay_index = 0;
	
	// Adjust height of hidden layers
	$('.oped-team-overlay').height($(document).height());
	
	// Team overlay
	$('#op-ed-class .the-team .people li a').click(function() {
		overlay_index = $('#op-ed-class .the-team .people li a').index(this);
		$('#oped_team_overlay_' + overlay_index).css({'display' : 'none', 'visibility' : 'visible'});
		$('#oped_team_overlay_' + overlay_index).fadeIn();
		hideScroller();
		return false;
	});
	$('.oped-team-overlay .top-nav li a').click(function() {
		$('.team-overlay').css({'display' : 'block', 'visibility' : 'hidden'});
		overlay_index = $('#oped_team_overlay_' + overlay_index + ' .top-nav li a').index(this);
		$('#oped_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'visible'});
		return false;
	});
	$('.oped-team-overlay a.closer').click(function() {
		$('#oped_team_overlay_' + overlay_index).fadeOut(function() {
			$('#oped_team_overlay_' + overlay_index).css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
	
	// hide video
	$('#close_register').click(function() {
		$('.register-container').fadeOut(function() {
			$('.register-container').css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
	
	// show register form
	$('.click-here').click(function() {
		$('.register-container').height($(document).height());
		$('.register-container').css({'display' : 'none', 'visibility' : 'visible'});
		$('.register-container').fadeIn();
		hideScroller();
		this.blur();
		return false;
	});
	
	$(':checkbox').each(function() {
		var target = $(this);
		var div = $('<div class="checkbox" />');
		if (target.is(':checked')) {
			div.addClass('checked');
		}
		target.wrap(div);
	});
	
	$('div.checkbox').click(function() {
		var target = $(this);
		var checkbox = $('input', target);
		checkbox.attr('checked', !checkbox.is(':checked'));
		if (checkbox.is(':checked')) {
			target.addClass('checked');
		} else {
			target.removeClass('checked');
		}
		checkbox.change();
	});
	
	$(':checkbox').change(function() {
		var target = $(this);
		
		if (target.attr('id') == "checkall") {
			if (target.is(':checked')) {
				var checkboxes = $(':checkbox:not(#checkall)');
				checkboxes.parent().removeClass('checked');
				checkboxes.attr('checked', false);
			}
		} else {
			if (target.is(':checked')) {
				var checkall = $('#checkall');
				checkall.parent().removeClass('checked');
				checkall.attr('checked', false);
			}
		}
	});
	
	$('#submit').click(function() {
		this.blur();
		var target = $(this);
		
		if (target.parent().hasClass('disabled')) {
			return false;
		}
		
		// parameters
		var name = $.trim($('#name').val());
		var email = $.trim($('#email').val());
		var company = $.trim($('#company').val());
		var subscriptions = [];
		if ($('#checkall').is(':checked')) {
			$('input[name=subscriptions]:not(#checkall)').each(function() {
				var t = $(this);
				subscriptions.push(t.val());
			});
		} else {
			$('input[name=subscriptions]').each(function() {
				var t = $(this);
				if (t.is(':checked')) {
					subscriptions.push(t.val());
				}
			});	
		}
		
		// required fields
		if (isEmpty(name) || isEmpty(email) || isEmpty(company)) {
			alert('Please fill out the required fields.');
			return false;
		}
		
		if (subscriptions.length == 0) {
			alert('Please select at least one option');
			return false;
		}
		
		target.parent().addClass('disabled');
		$.post('/xml/op-ed.xml.php', {name: name, email: email, company: company, subscriptions: subscriptions}, function(data) {
			target.parent().removeClass('disabled');
			
			var status = parseInt($('status', data).text());
			var message = $('message', data).text();
			if (status > 0) {
				alert(message);
				return;
			}
			
			alert('Thank you.\nYour request has been sent successfully');
			
			// reset the form
			$('#name').val(null);
			$('#email').val(null);
			$('#company').val(null);
			
			var checkboxes = $('input[name=subscriptions]:not(#op_ed)');
			checkboxes.parent().removeClass('checked');
			checkboxes.attr('checked', false);
			
			$('#op_ed').parent().addClass('checked');
			$('#op_ed').attr('checked', true);
			
			// close the form
			$('#close_register').click();
		});
		
		return false;
	});
};

/**
 * DIRECTIONS
 */

Cufon.replace('.directions-container h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('.directions-container h5', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('.directions-container .header', {printable: false, fontFamily: 'Helvetica_300'});
Cufon.replace('.directions-container .download-directions', {printable: false, fontFamily: 'Helvetica_300', hover: true});

var _Directions = new Directions();
function Directions() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};

Directions.prototype.build = function() {
	var _l0 = this;
	// Show directions
	$('a[href*="#directions"]').click(function() {
		// Adjust height of hidden layers
		$('.directions-container').height($(document).height());
		$('.directions-container .content').height($('.directions-container').height()-270);
		$('.directions-container').css({'display' : 'none', 'visibility' : 'visible'});
		$('.directions-container').fadeIn();
		hideScroller();
		return false;
	});
	$('a[href="#detailed-directions"]').click(function() {
		// Adjust height of hidden layers
		$('.directions-container').height($(document).height());
		$('.directions-container .content').height($('.directions-container').height()-270);
		$('.directions-container').css({'display' : 'none', 'visibility' : 'visible'});
		$('.directions-container').fadeIn();
		hideScroller();
		return false;
	});
	// Hide directions
	$('#close_directions').click(function() {
		$('.directions-container').fadeOut(function() {
			$('.directions-container').css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
};

/**
 * DISCLAIMER
 */

Cufon.replace('.disclaimer-container h3', {printable: false, fontFamily: 'Times_400'});
Cufon.replace('.disclaimer-container .header', {printable: false, fontFamily: 'Helvetica_300'});

var _Disclaimer = new Disclaimer();
function Disclaimer() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};

Disclaimer.prototype.build = function() {
	var _l0 = this;
	// Adjust height of hidden layers
	$('.disclaimer-container').height($(document).height());
	$('.disclaimer-container .content').height($('.disclaimer-container').height()-270);
	// Show directions
	$('.disclaimer a').click(function() {
		$('.disclaimer-container').height($(document).height());
		$('.disclaimer-container .content').height($('.disclaimer-container').height()-270);
		$('.disclaimer-container').css({'display' : 'none', 'visibility' : 'visible'});
		$('.disclaimer-container').fadeIn();
		hideScroller();
		return false;
	});
	// Hide directions
	$('#close_disclaimer').click(function() {
		$('.disclaimer-container').fadeOut(function() {
			$('.disclaimer-container').css({'display' : 'block', 'visibility' : 'hidden'});
			showScroller();
		});
		return false;
	});
};
