$(function() {
	// Functions and mini-plugins
		jQuery.fn.exists = function() { return (jQuery(this).length > 0); }
		jQuery.preloadImages = function(urls) {
			var i=0,x,y,z=[];
			while(x = arguments[i++]) {
				y = document.createElement('img');
				y.src = x;
				z.push(y);
			}
		}
		jQuery.fn.flicker = function(callback) {
			var t = jQuery(this); t.fadeTo(125, 0.25, function() { t.fadeTo(125, 1, function() { t.fadeTo(125, 0.25, function() { t.fadeTo(125, 1, callback) }); } ); } );
		};
	// Globals
		var spfx = '/wp-content/themes/lifebyme/';
		var cpfx = '/';
		var _degrade = ($.browser.msie && $.browser.version < 8);
		var _mobile = (navigator.userAgent.search(/(iOS|iPhone|iPad|Android)/) != -1);
	// Graphical Titles
		if (_degrade) {
			$('#layout_explore .features .title').removeClass('cimg_sm_posttitle');
		}
		// Site-related
			$('.simg_special').zzt({limit: 4, type: 'gif', prefix: spfx + 'site/images/special_'});
			$('.simg_interviewheading').zzt({type: 'gif', prefix: spfx + 'site/images/interviewheading_', height: 20});
			$('.simg_footerheading').zzt({type: 'gif', prefix: spfx + 'site/images/footerheading_', height: 20});
			$('.simg_lg_heading').zzt({type: 'gif', prefix: spfx + 'site/images/lg_heading_', height: 35});
			$('.simg_heading').zzt({type: 'gif', prefix: spfx + 'site/images/heading_', height: 35});
			$('.simg_sm_heading').zzt({type: 'gif', prefix: spfx + 'site/images/sm_heading_', height: 25});
			$('.simg_staffname').zzt({type: 'gif', prefix: spfx + 'site/images/staffname_', height: 20});
		// Content-related
			$('.cimg_author').zzt({type: 'gif', prefix: cpfx + 'content_images/titles/author_', height: 20});
			$('.cimg_sm_author').zzt({type: 'gif', prefix: cpfx + 'content_images/titles/sm_author_', height: 20});
			$('.cimg_posttitle').zzt({type: 'gif', prefix: cpfx + 'content_images/titles/posttitle_', width: 400, height: 100});
			$('.cimg_sm_posttitle').zzt({type: 'gif', prefix: cpfx + 'content_images/titles/sm_posttitle_', height: 30});
			$('.cimg_lg_posttitle').zzt({type: 'gif', prefix: cpfx + 'content_images/titles/lg_posttitle_', width: 575, height: 160});
			$('.cimg_worddef').zzt({limit: 4, type: 'gif', prefix: cpfx + 'content_images/titles/worddef_'});
	// Forms
		$('form').formerize();
	// Preloads
		$.preloadImages(
			spfx + 'site/images/tooltip_arrow.gif',
			spfx + 'site/images/transparentbg1_arrow1.png',
			spfx + 'site/images/transparentbg1_arrow2.png',
			spfx + 'site/images/cxyz_bl.png',
			spfx + 'site/images/cxyz_br.png',
			spfx + 'site/images/cxyz_bc.png',
			spfx + 'site/images/cxyz_tl.png',
			spfx + 'site/images/cxyz_tr.png',
			spfx + 'site/images/cxyz_tc.png',
			spfx + 'site/images/cxyz2_bl.png',
			spfx + 'site/images/cxyz2_br.png',
			spfx + 'site/images/cxyz2_bc.png',
			spfx + 'site/images/cxyz2_tl.png',
			spfx + 'site/images/cxyz2_tr.png',
			spfx + 'site/images/cxyz2_tc.png'
		);
	// Popups
		// Contributors
			if (_degrade) {
				$('.contributorbox .details').remove();
			}
			else {
				$('.contributorbox').each(function() {
					var t = $(this), stub = t.find('.stub'), postURL = t.find('.postURL').attr('href');

					stub.click(function() {
						window.location = postURL;
					});

					stub.xyz({
						contentSelector: '.details',
						follow: true,
						xpos: 'center',
						ypos: 'top',
						xpad: -55,
						ypad: 10,
						delay: 400,
						bottomLeftClass: 'details_bottom_left',
						bottomRightClass: 'details_bottom_right',
						bottomCenterClass: 'details_bottom_center',
						topLeftClass: 'details_top_left',
						topRightClass: 'details_top_right',
						topCenterClass: 'details_top_center'
					});
				});
			}
		// Everyday Meaning
			var em_popup = $('<div class="lbm_popup" id="popup_everydaymeaning"><span class="closer">Close</span><h2></h2><p></p><a href="#" class="close">Close</a></div>');
			em_popup
				.appendTo($('body'))
				.hide()
				.ppp();
			em_popup
				.bind('setMessage', function(e, title, message) {
					var t = $(this), p = t.find('p'), h2 = t.find('h2');
					p.html(message);
					h2.html(title);
					h2.zzt({type: 'gif', prefix: spfx + 'site/images/sm_heading_', height: 25});
				});
			em_popup.find('a.close')
				.click(function(e) {
					e.preventDefault();
					em_popup.trigger('pppclose');
				});
			$('.em_form')
				.each(function() {
					var t = $(this), f = t.find('form'), b = f.find('input.submit'), email=f.find('input[name$="email"]');
					var em_popup_title = em_popup.find('h2'), em_popup_message = em_popup.find('p');
					if (_degrade) {
						$('.em_sample').remove();
					}
					else
					{
						t.xyz({
							contentSelector: '.em_sample',
							follow: true,
							xpos: 'left',
							ypos: 'top',
							bottomLeftClass: 'em_sample_bottom_left',
							bottomRightClass: 'em_sample_bottom_right',
							bottomCenterClass: 'em_sample_bottom_center',
							topLeftClass: 'em_sample_top_left',
							topRightClass: 'em_sample_top_right',
							topCenterClass: 'em_sample_top_center',
							xpad: -60,
							ypad: 40,
							delay: 1000
						});
					}
					email
						.bind('turnoff', function() {
							email.css('cursor', 'default').attr('disabled', 'disabled').fadeTo(200, 0.50);
						})
						.bind('turnon', function() {
							email.css('cursor', 'text').attr('disabled', '').fadeTo(200, 1);
						})
						.focus(function() {
							t.trigger('turnoff');
						});
					b
						.bind('turnoff', function() {
							b.css('cursor', 'default').attr('disabled', 'disabled').fadeTo(200, 0.50);
						})
						.bind('turnon', function() {
							b.css('cursor', 'pointer').attr('disabled', '').fadeTo(200, 1);
						});
					f.submit(function(event) {
						event.preventDefault();
						
						em_popup
							.bind('ppp_onclose.tmp', function() {
								b.trigger('turnon');
								email
									.val('')
									.trigger('turnon');
								$(this).unbind('ppp_onclose.tmp');
							});
						
						if (email.val() == '') {
							return false;
						}
						// Disable some stuff
							b.trigger('turnoff');
							email.trigger('turnoff');
						// Build query
							$.post(
								'/aw_subscribe_echo.php',
								{
									meta_web_form_id: '304391479',
									meta_split_id: '',
									listname: 'lifebymedotcom',
									redirect: '',
									meta_adtracking: 'My_Web_Form_2',
									meta_message: '1',
									meta_required: 'email',
									meta_tooltip: '',
									email: email.val()
								},
								function (data) {
									var response = parseInt(data);

									switch (response) {
										case 501:
											em_popup
												.trigger('setMessage', ['Thank You!', 'Your email has been submitted. Thank you!'])
												.trigger('pppopen');
											break;
										case 602:
											em_popup
												.trigger('setMessage', ['There was a problem', 'Your email address is already subscribed. Please try again.'])
												.trigger('pppopen');
											break;
										case 703:
											em_popup
												.trigger('setMessage', ['There was a problem', 'The email address you specified is invalid. Please try again.'])
												.trigger('pppopen');
											break;
										default:
											em_popup
												.trigger('setMessage', ['There was a problem', 'Something went wrong! Please try again.'])
												.trigger('pppopen');
											break;
									}
								
								}
							);
						});
				});
	// Misc
		// Audio
			if (_mobile == false)
			{
				$('audio').each(function() {
					var t = $(this), src = t.attr('src');
					t.replaceWith('<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="400" height="27" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="bgcolor" value="#ffffff" /><param name="flashvars" value="audioUrl=' + src + '" /><param name="src" value="http://www.lifebyme.com/content_images/audio-player.swf" /><param name="wmode" value="window" /><param name="quality" value="best" /><embed type="application/x-shockwave-flash" width="400" height="27" src="http://www.lifebyme.com/content_images/audio-player.swf" quality="best" wmode="window" flashvars="audioUrl=' + src + '" bgcolor="#ffffff"></embed></object>');
				});
			}
		// Tooltips
			if (_degrade)
			{
				$('.social li').each(function() {
					var t = $(this), img = t.find('img'), a = t.find('a'), d = t.find('.label');
					d.hide();
					img.attr('alt', d.text());
				});

				$('#layout_interview .recommendations ul li div').each(function() {
					var t = $(this), d = t.parent().find('.label');
					d.hide();
					t.click(function() {
						var url = $(this).parent().find('a').attr('href');
						if (url != '') {
							window.open(url);
							return false;
						}
					});
				});
			}
			else
			{
				$('.social li a').xyz({
					contentSelector: '.label',
					follow: true,
					xpos: 'center',
					ypos: 'top',
					xpad: 0,
					ypad: 30,
					delay: 0,
					bottomLeftClass: 'label_bottom_left',
					bottomRightClass: 'label_bottom_right',
					bottomCenterClass: 'label_bottom_center',
					topLeftClass: 'label_top_left',
					topRightClass: 'label_top_right',
					topCenterClass: 'label_top_center'
				});

				$('#layout_interview .recommendations ul li div')
					.click(function() {
						var url = $(this).parent().find('a').attr('href');
						if (url != '') {
							window.open(url);
							return false;
						}
					})
					.xyz({
						contentSelector: '.label',
						follow: true,
						xpos: 'center',
						ypos: 'top',
						xpad: 0,
						ypad: 30,
						delay: 0,
						bottomLeftClass: 'label_bottom_left',
						bottomRightClass: 'label_bottom_right',
						bottomCenterClass: 'label_bottom_center',
						topLeftClass: 'label_top_left',
						topRightClass: 'label_top_right',
						topCenterClass: 'label_top_center'
					});
			}
		// Set offsite links
			$('.recommendations a').addClass('offsite');
			$('#layout_interview .author .bio a').addClass('offsite');
			//$('#footer1 a').addClass('offsite');
			$('a.offsite').attr('target', '_blank');
});

