
var Local = {

    onCommentReply: function(id) {
		Comments.openReply(id);
	},

    onCancelReply: function(id) {
		Comments.closeReply();
	}

};

if(!window['ftf']) {
	window['ftf'] = {};
	var ftf = window['ftf'];
}

(function() {
	var $ = jQuery;
	
	$(document).ready(function() {
		ftf.headlineShadows();
		ftf.hero();
		ftf.formDefaults();
		ftf.bocx_setup();
		ftf.img_bocx_setup();
		ftf.podcast_player_setup();
		$('#swap_image').click(function(c) {
			c.preventDefault();
			if($(this).attr('name') == 'swap_on') {
				$(this).html('<span>Turn Off Image &times;</span>').attr({'name':'swap_off','class':'swap_off'});
				$('body').css({'background-image': 'url(/page/home-background)'});
			} else {
				$(this).html('<span>Turn On Image +</span>').attr({'name':'swap_on','class':'swap_on'});
				$('body').css({'background-image': 'none'});
			}
		});
	});
	
	$(window).load(function() {
		if($(window).height() > $('body').height() && $('html').is('#home')) {
			$('body').height($(window).height());
		}
	});
	
	$(window).resize(function() {
		if($(window).height() > $('body').height() && $('html').is('#home')) {
			$('body').height($(window).height());
		}
	});
	
	ftf.headlineShadows = function() {
		$('.add_shadow,.confirm').each(function()
			{
				$(this).css({'color': '#777'});
				var headline = $(this).html();
				$(this).html(headline + '<span class="shadow" style="z-index: 0">' + headline + '</span>');
			}
		);
	}
	
	ftf.hero = function() { ftf.hero.init(); };

	ftf.hero.init = function() {
		if($('#imageFadeContainer').size()) {
			ftf.hero.dur = parseInt($('#imageFadeContainer').find('input[name=showvalue]').val(),10);
			ftf.hero.fade = parseInt($('#imageFadeContainer').find('input[name=fadevalue]').val(),10);
			var count = 0;
			ftf.hero.heroes = $('#imageFadeContainer img');
			$(ftf.hero.heroes[0]).addClass('visible');
			ftf.hero.container = $(jQuery('<div id="container"></div>'));
			ftf.hero.controls = $(jQuery('<div class="heroshot_navigation"></div>'));
			var list = $(jQuery('<ul />'));
			list.appendTo(ftf.hero.controls);
			ftf.hero.heroes.each(
				function(count) {
					var img = $(this);
					var item = $(jQuery('<li />'));
					if(count == 0) {
						item.addClass('selected');
					}
					item.html(count + 1);
					item.css({'cursor':'pointer'});
					item.click(
						function() {
							ftf.hero.showImg(item, 'jump');
						}
					);
					list.append(item);
					if(img.parent('a')) {
						ftf.hero.container.append(img.parent('a'));
					} else {
						ftf.hero.container.append(img);
					}
				}
			);
			ftf.hero.container.appendTo('#imageFadeContainer');
			$(jQuery('<div class="heroshot_shadow"></div>')).appendTo('#imageFadeContainer');

			if(ftf.hero.heroes.size() < 2) { return; }
			
			ftf.hero.controls.appendTo($('#imageFadeContainer').parent().next());

			$(window).load(function() {
				ftf.hero.rotation = setInterval(function() { ftf.hero.rotate(); },(ftf.hero.dur + ftf.hero.fade) * 1000);
			});
		}
	}
	
	ftf.hero.rotate = function() {
		ftf.hero.showImg(ftf.hero.controls.find('li.selected').next().size() ? ftf.hero.controls.find('li.selected').next() : ftf.hero.controls.find('li:first-child'), 'rotate');
	}
	
	ftf.hero.showImg = function(item, action) {
		if(ftf.hero.container.not(':animated')) {
			var controls = ftf.hero.controls.find('li');
			var from_img = ftf.hero.heroes.filter('.visible');
			from_img.removeClass('visible');
			var to_img = null;
			
			if(action == 'jump') {
				clearInterval(ftf.hero.rotation);
				ftf.hero.rotation = setInterval(function() { ftf.hero.rotate(); },(ftf.hero.dur * 2) * 1000);
			}
			
			var img_no = parseInt(item.html(),10) -1;

			if(action == 'rotate' && item.is(':first-child')) {
				if($(ftf.hero.heroes[0]).parent('a')) {
					var first = $(ftf.hero.heroes[0]).parent('a').clone(true);
				} else {
					var first = $(ftf.hero.heroes[0]).clone(true);
				}
				ftf.hero.container.append(first);
				to_img = first;
				setTimeout(
					function() { 
						ftf.hero.container.css({'left': 0}); 
						first.remove();
					},
					(ftf.hero.fade * 1000) + 500
				);
			} else {
				to_img = $(ftf.hero.heroes[img_no]);
				to_img.addClass('visible');
			}
			var current = controls.filter('.selected');
			current.removeClass('selected');
			var change = 0 - (!(item.is(':first-child') && action == 'rotate') ? (to_img.width() * img_no) : (to_img.width() * ftf.hero.heroes.size()));
			$(controls[img_no]).addClass('selected');
			ftf.hero.container.animate({
				'left': change + 'px'
			}, ftf.hero.fade * 1000);
		}
	}
	
	ftf.formDefaults = function() {
		$('input').each(function() {
			var defaultVal = false;
			switch($(this).attr('name')) {
				case 'questionnaire-submission[answers][12]' || 'footer_questionnaire-submission[answers][12]':
					defaultVal = 'Enter your email address';
					$(this).focus(function() {
						$(this).siblings(':hidden').slideDown();
					});
					break;
				case 'questionnaire-submission[answers][17]' || 'footer_questionnaire-submission[answers][17]':
					defaultVal = 'Your name';
					break;
				case 'questionnaire-submission[answers][18]' || 'footer_questionnaire-submission[answers][18]':
					defaultVal = 'Your street address';
					break;
				case 'questionnaire-submission[answers][19]' || 'footer_questionnaire-submission[answers][19]':
					defaultVal = 'Your city';
					break;
				case 'questionnaire-submission[answers][21]' || 'footer_questionnaire-submission[answers][21]':
					defaultVal = 'Your ZIP code';
					break;
				default:
					defaultVal = false;
			}
			if(defaultVal) {
				$(this).focus(function() {
					if($(this).val() == defaultVal) { $(this).val(''); }
				});
				$(this).blur(function() {
					if($(this).val() == '') { $(this).val(defaultVal); }
				});
				$(this).val(defaultVal);
			}
		});
	}
	
	ftf.bocx_setup = function() {
		$('#cxarton').css({
			'width': typeof window.scrollX != 'undefined' ? window.clientWidth + 'px' : document.body.clientWidth + 'px',
			'height': typeof window.scrollX != 'undefined' ? window.clientHeight + 'px' : document.body.clientHeight + 'px'
		});
		$('.close_bocx').each(
			function() {
				$(this).click(function() {
					$('#cxarton').fadeOut('fast');
				});
			}
		);
		$('.email_link').each(
			function(obj) {
				$(this).click(function(c) {
					c.preventDefault();
					ftf.set_cxarton_position();
					if($.browser.msie && $.browser.version < 7) { window.scrollTo(0,0); }
					$('#cxarton').fadeIn('fast');
				});
			}
		);
	}
	
	ftf.set_cxarton_position = function() {
		$('#cxarton').css({
			'top': typeof window.scrollX != 'undefined' ? window.pageYOffset + 'px' : document.body.scrollTop + 'px'
		});
		$('#img_cxarton').css({
			'top': typeof window.scrollX != 'undefined' ? window.pageYOffset + 'px' : document.body.scrollTop + 'px'
		});
	}
	
	ftf.show_img_bocx = function(link) {
		ftf.set_cxarton_position();
		if($.browser.msie && $.browser.version < 7) { window.scrollTo(0,0); }
		if(link.previous()) {
			$('#img_bocx_thumb_left').html(link);
		}
		$('#img_bocx_main_image').html('<img src="' + link.attr('href') + '" />');
		$('#img_cxarton').fadeIn('fast');
	}
	
	ftf.img_bocx_setup = function() {
		$('#img_cxarton').css({
			'width': typeof window.scrollX != 'undefined' ? window.clientWidth + 'px' : document.body.clientWidth + 'px',
			'height': typeof window.scrollX != 'undefined' ? window.clientHeight + 'px' : document.body.clientHeight + 'px'
		});
		$('.close_img_bocx').each(
			function() {
				$(this).click(function() {
					$('#img_cxarton').fadeIn('fast');
				});
			}
		);
		$('a[rel=lightbox]').each(
			function() {
				$(this).click(function(c) {
					c.preventDefault();
					ftf.show_img_bocx($(this));
				});
			}
		);
	}
	
	ftf.podcast_player_setup = function() {
		$('#audio_player a').filter('.attachment').each(
			function(i) {
				var src		=	$(this).attr('href');
				var player	=	document.createElement('object');
				var player = $(jQuery('<object type="application/x-shockwave-flash" data="/player.swf" height="24" width="290" id="audioplayer' + i + '"></object>'));
				player.append($(jQuery('<param name="movie" value="/player.swf" />')));
				player.append($(jQuery('<param name="FlashVars" value="playerID=1&soundFile=' + src + '&bg=0xf3ca75&text=0x513005&lefticon=0x513005&leftbg=0xe4ba67&rightbg=0xe4ba67&righticon=0x513005&righticonhover=0xe4ba67&rightbghover=0x513005&track=0xf3ca75&border=0x513005&slider=0x513005&loader=0xe4ba67" />')));
				player.append($(jQuery('<param name="quality" value="high" />')));
				player.append($(jQuery('<param name="menu" value="false" />')));
				player.append($(jQuery('<param name="wmode" value="transparent" />')));
				$(this).html('Download This Episode');
				$('#audio_player').append(player);
				$('#audio_player').append($(jQuery('<br/>')));
				$('#audio_player').append($(this));
			}
		);
	}

})();