var flumist_swfs_loaded = 0;
var flumist_total_swf_count = 6;
var flumist_overlay_opacity = 40;
var flumist_overlay_width = 812;
var flumist_overlay_small_width = 601;
var flumist_overlay_small_height = 359;

$(document).ready(function(){
		$("#carousel_shareSection").show();
		$("#shareFooter").show();
		
		/* SHARE YOUR PICK */
		$("#share_your_pick .button").click(function() {
				var message = $("input[name=message]:checked").val();  
				var site = $(this).attr("name");
				
				// site == "twitter"
				var url = "http://twitter.com/home?status=" + message;
				if (site == "facebook") {
					FB.ui(
					   {
						 method: 'stream.publish',
						 message: message
					   }
					);

				} else {
					window.open(url);
				}
				return false;
		}); 
		
		/* SHARE ON FACEBOOK */
		$("#share_on_facebook").click(function() {
				// fire webtrends
				fireWebtrendsTag('facebook_share_click', '100');
				return false; 
		});
		
		/* INTERSTITIAL */
		$("a.external,a.externalHCP").click(function() {
				var type = "external";
				if($(this).attr("class").indexOf("HCP") != -1) {
					type = "hcp"
				}
				
				theURL = $(this).attr("href");
				var position = $(this).position();
				
				showInterstitial(type, theURL, position);
				return false;
				
		});
				
		$("#continueButton").click(function() {
			//close layer
			$.modal.close();
			//goto url
			window.open(theURL);
		});
				
		
		/* SEND TO A FRIEND */
		$('a.sendToAFriend').click( function() {
				// fire webtrends tag
				fireWebtrendsTag('email_conopen_share_click', '100');
				showSendToAFriend();
				
				return false;
		});
		
		if (build_nav == "true") {
			flumist_total_swf_count = 8;
		}
		
		// Registration page modal click handler
		$('.reg_link').click(function() {
			showRegistration();
		});
		
		// Media modal click handler
		$('.media_link').click(function() {
			showMedia();
		});
	}
);


function openModal(dialog) {
	dialog.overlay.fadeIn(function () {
		dialog.data.hide();
		$('#simplemodal-container a.modalCloseImg').hide();
		dialog.overlay.fadeIn('fast', function() {
			dialog.container.slideDown('fast', function() {
				dialog.data.fadeIn('fast');
				$('#simplemodal-container a.modalCloseImg').fadeIn('fast');
			});
		});
	});
}

function closeModal(dialog) {
	$('#simplemodal-container a.modalCloseImg').fadeOut(function() {
		dialog.data.fadeOut('fast', function () {
			dialog.container.slideUp(function () {
				dialog.overlay.fadeOut('fast', function () {
					$.modal.close(); // must call this!
				});
			});
		});	
	});
}

function showEmailThankYou() {
	// show thank you
	$("#stf_form").fadeOut("fast", function() {
		$("#stf_thanks").fadeIn();
		// close layer
		setTimeout(function() {
			$.modal.close();}, 
			2000);
	});
}

function sendToAFriendSubmit() {
	clearStfErrorState(".formRow");
	var actor_name = $("input[name=actor_name]").val();
	var actor_email = $("input[name=actor_email]").val();
	var recipient_name = $("input[name=recipient_name]").val();
	var recipient_email = $("input[name=recipient_email]").val();
	var errors = false;

	// error validation
	if (jQuery.trim(actor_name) == "") {
		addStfErrorState("#row_stf_actor_name");
		errors = true;
	}
	
	if (!validateEmail(actor_email)) {
		addStfErrorState("#row_stf_actor_email");
		errors = true;
	}
	
	if (jQuery.trim(recipient_name) == "") {
		addStfErrorState("#row_stf_friend_name");
		errors = true;
	}
	
	if (!validateEmail(recipient_email)) {
		addStfErrorState("#row_stf_friend_email");
		errors = true;
	}
	
	if (!errors) {
		var email_data = { 	actor_name: actor_name,
							actor_email: actor_email,
							recipient_name: recipient_name,
							recipient_email: recipient_email,
							organization: "FluMist",
							template: "Send to friend Trigger",
							url: $("input[name=url]").val()
		};
				
		// send email
		$.post($('input[name=post_url]').val(),
				  email_data,
				  function(data, status, request) {
					  showEmailThankYou()
				  });
	}
}

function addStfErrorState(rowSelector) {
	var row = $(rowSelector);
	row.find('.errorLbl').show();	
	row.find(':text').addClass('error');
}

function clearStfErrorState(rowSelector) {
	var row = $(rowSelector);
	row.find('.errorLbl').hide();
	row.find(':text').removeClass('error');
}

function showRegistration() {
	fireDoubleClickTag('registration_form_load');
	// load the registration form in an iframe
	$('#registration_modal_content').html('<iframe id="registration_frame" scrolling="no" width="770" height="381" frameborder="0" src="' + regFormUrl + '"></iframe>');
	
	$('#registration_container').modal({
		opacity: flumist_overlay_opacity,
		minWidth: flumist_overlay_width,
		maxWidth: flumist_overlay_width,
		minHeight: 437,
		maxHeight: 437,
		onOpen: function(dialog) {
			openModal(dialog);
		},
		onClose: function (dialog) {
			closeModal(dialog);
		}
	});
}	

function showMedia() {
	$('#media_container').modal({
		opacity: flumist_overlay_opacity,
		minWidth: flumist_overlay_small_width,
		maxWidth: flumist_overlay_small_width,
		minHeight: flumist_overlay_small_height,
		maxHeight: flumist_overlay_small_height,
		onOpen: function(dialog) {
			openModal(dialog);
		},
		onClose: function (dialog) {
			closeModal(dialog);
		}
	});
}

function lastChar(str) {
	return str.charAt(str.length - 1);
}

function getPickCount() {
	//console.log("get: " + pick_count);
	return pick_count; 
}

function incrementPickCount() {
	pick_count ++;
	$.ajax({
	  url: 'data-increment-pick-count',
	  success: function(data) {
		pick_count = data;
		
		// cookie the user so they can't vote again
		$.cookie('nosePicked', 'true', {expires:600} );

	  },
	  complete: function() {
		return pick_count;
	  }
	});
}

function getSelectedMenu() {
	return currentNavId + "," + currentPageId;
}

function setSelectedMenu(navId, pageId) {
	currentNavId = navId;
	currentPageId = pageId;
}

function swfLoaded(section) {
	flumist_swfs_loaded ++;
	// data and swf has to load for 4 modules on homepage, only 3 on subpages
	if ( flumist_swfs_loaded == flumist_total_swf_count ) {
		$("#flash_masthead")[0].buildIn();
		$("#left_side_nav")[0].buildIn();
		$("#right_side_nav")[0].buildIn();
		$("#megaphone")[0].buildIn();
	}
}

function dataLoaded(section) {
	flumist_swfs_loaded ++;
	// data and swf has to load for 4 modules on homepage, only 3 on subpages
	if ( flumist_swfs_loaded == flumist_total_swf_count ) {
		$("#flash_masthead")[0].buildIn();
		$("#left_side_nav")[0].buildIn();
		$("#right_side_nav")[0].buildIn();
		$("#megaphone")[0].buildIn();
	}
}

function showSendToAFriend() {
	$("#stf_thanks").hide();
	$(".errorLbl").hide();
	$("#stf_form").show();
	
	$('#send_to_a_friend_container').modal({
		opacity: flumist_overlay_opacity,
		minWidth: flumist_overlay_small_width,
		maxWidth: flumist_overlay_small_width,
		minHeight: flumist_overlay_small_height,
		maxHeight: flumist_overlay_small_height,
		onOpen: function(dialog) {
			openModal(dialog);
		},
		onClose: function (dialog) {
			closeModal(dialog);
		}
	});
	
	$("#send_to_a_friend_container .closeButton").click(function() {
		$.modal.close();
	});
	
	// cancel button
	$("#send_to_a_friend_buttons .smallGrey").click(function() {
		$.modal.close();
	});
	
	// submit button
	$("#send_to_a_friend_buttons .smallGreen").click(function() {
		sendToAFriendSubmit();
	});
}

function validateEmail(email) {
	 var re = /^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
	 return email.match(re);
}

function showInterstitialModal() {
	$('#interstitial_container').modal({
		opacity: flumist_overlay_opacity,
		minWidth: flumist_overlay_small_width,
		maxWidth: flumist_overlay_small_width,
		minHeight: flumist_overlay_small_height,
		maxHeight: flumist_overlay_small_height,
		onOpen: function(dialog) {
			openModal(dialog);
		},
		onClose: function (dialog) {
			closeModal(dialog);
		}
	});
}

function showInterstitial(type, url, position) {
	theURL = url;
	if (type == "hcp") {
		$("#message").hide(); 
		$("#message_hcp").show();
		
	} else {
		$("#message_hcp").hide();
		$("#message").show(); 
	}
				
	//Load DoubleClick tag
	var axel = Math.random() + ""; 
	var a = axel * 10000000000000; 
	var newIFrame=document.createElement('iframe'); 
	newIFrame.src='http://fls.doubleclick.net/activityi;src=2378231;type=pick2010;cat=leaving;ord=1;num=' + a + '?'; 
	newIFrame.width="1"; 
	newIFrame.frameBorder="0"; 
	newIFrame.height="1"; 
				
	$('#interstitial_dc').empty().append(newIFrame); 
	// end doubleclick
				
	showInterstitialModal();
				
}

function showFlashInterstitial(url) {
	//console.log("url: " + url);
	var position = $("#right_side_nav").position();
	
	position.top = position.top + 300;
	showInterstitial("hcp", url, position);
}



