var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
var currID = '';
var currExtendedID = '';
var currTab = 'public_coalition';

function getPage() {
	var url = window.location.pathname;
	var array = url.split('/');
	var lastsegment = array[array.length-1];
	
	switch (lastsegment) {
		case '6-12-months-prior':
		{
			returnurl = 'plan_6_to_12';
			break;	
		}

		case '3-6-months-prior':
		{
			returnurl = 'plan_3_to_6';
			break;	
		}
		
		case '1-3-months-prior':
		{
			returnurl = 'plan_1_to_3';
			break;	
		}

		case 'weeks-leading-up-to-sliv-clinic':
		{
			returnurl = 'plan_weeks_leading_up';
			break;	
		}		

	}
	
	return returnurl;
}

$(document).ready(function() {
			
		$('.ie6 nav ul li').mouseover(function() {
			if(!$(this).hasClass("sel")) { 
				$(this).css("background", "0 -163px url('/_assets/shell/desktop/images/sprite-horiz.png')"); 
			}
		})
		
		$('.ie6 nav ul li').mouseout(function() {
			if(!$(this).hasClass("sel")) { 
				$(this).css("background", "right -563px url('/_assets/shell/desktop/images/sprite-horiz.png') no-repeat"); 
			}
		});
		
		$('.ie6 nav ul ul li a').mouseover(function() {
			if ( !$(this).parent().parent().parent().hasClass("sel") ) {
				$(this).parent().parent().parent().children('a').attr("style", "color: #fff !important");
			}
		});

		$('.ie6 nav ul ul li a').mouseout(function() {
			if ( !$(this).parent().parent().parent().hasClass("sel") ) {
				$(this).parent().parent().parent().children('a').attr("style", "color: #004d42 !important");
			}
		});

		
		$('#tabs .contentLink').hide();
		$('#tabs #tab-1').show();
		$('#tabs ul li:first').addClass('active');
		$('#tabs ul li a').click(function(){ 
			$('#tabs ul li').removeClass('active');
			$(this).parent().addClass('active'); 
			var currentTab = $(this).attr('href'); 
			$('#tabs div').hide();
			$(currentTab).show();
			return false;
		});	

		$('.refsuper').live("mouseenter", function() {
			var refpos = $(this).position();
			refID = $(this).attr("id").substr(3);			
			if ($.browser.msie && $.browser.version <= 7){
				refTop = refpos.top + 13; refLeft = refpos.left + -640;
			}	
			else {
				refTop = refpos.top + 13; refLeft = refpos.left + -175;
			}
			$('body').prepend("<div class='refwrap' style='margin-top: " + refTop + "px; margin-left: " + refLeft + "px;'><div class='refhead'></div><div class='refcontent'></div><div class='reffoot'></div></div>");
			$.post('/forms/get_references', {ref_id: refID}, function(data) {
				$('.refwrap .refcontent').html(data);
				$('.refwrap').fadeIn(150);
			});			
		});
		
		$('.refsuper').live('mouseout', function() {
			$('.refwrap').fadeOut(150, function() {
				$(this).remove();
			});
		});
		
		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		//PLEASE USE FOR REFERENCES THAT ARE NOT SINGLE DIGIT OR ARE SINGLE DIGITS COMBINED WITH ANOTHER CHARACTER
		$('.refsuper_more').live("mouseenter", function() {
			var refpos = $(this).position();
			refID = $(this).attr("id").substr(3);			
			if ($.browser.msie && $.browser.version < 7){
				refTop = refpos.top + 13; refLeft = refpos.left + -640;
			}
			else if ($.browser.msie && $.browser.version >= 7){
				refTop = refpos.top + 13; refLeft = refpos.left + -475;
			}			
			else {
				refTop = refpos.top + 13; refLeft = refpos.left + -10;
			}
			$('body').prepend("<div class='refwrap' style='margin-top: " + refTop + "px; margin-left: " + refLeft + "px;'><div class='refhead'></div><div class='refcontent'></div><div class='reffoot'></div></div>");
			$.post('/forms/get_references', {ref_id: refID}, function(data) {
				$('.refwrap .refcontent').html(data);
				$('.refwrap').fadeIn(150);
			});			
		});
				
		$('.refsuper_more').live('mouseout', function() {
			$('.refwrap').fadeOut(150, function() {
				$(this).remove();
			});
		});
		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		//////////////////////////////////////////////////////////////////////////////////////////////////////////
		
		$('.pdfpreview').click(function() {
			$('.pdfpreviewWrapper').remove();			
			var pageID = $(this).attr("id").substr(4);
			$('body').prepend("<div class='pdfpreviewWrapper'></div>");
			$('.pdfpreviewWrapper').load('/_assets/shell/desktop/bits/pdfpreview.php', function() {
				if ($.browser.msie && $.browser.version <= 7){
					$(this).fadeIn(0);
				}
				else {
					$(this).fadeIn(250);
				}	
			});
			$.post('/forms/get_page', {page_id: pageID}, function(data) {
				pageID = data.page_id;			
				$('.pdfpreviewWrapper .pageTitle').html(data.page_title);
				linksfound = false;
				if(data.page_standalone == 0) { pageLinks = '<a id="ga_view_content" href="/' + data.page_url + '" class="button white-green"><span class="title"><span class="icon arrow"></span>View Content</span></a>';} else { pageLinks = " ";  } 
				var imgFileID = pageID;
				var pagedocs = data.docs;	
				
				for (var i = 0; i < pagedocs.length; i++) {
					linksfound = true;
					var docFileExt = '';
					var object = pagedocs[i];
                                        
					if ( object['doc_filename'].search("__pdf__") > 1 ) { docFileExt = "pdf"; }
					else if ( object['doc_filename'].search("__docx__") > 1 ) { docFileExt = "docx"; }
					pageLinks = pageLinks + '<a id="' + object['doc_filename'] + '" href="/_assets/shell/desktop/media/' + object['doc_filename'] + '" class="button white-green disclaimer"><span class="title"><span class="icon ' + docFileExt + '"></span>' + object['doc_title'] + '</span></a>';
				}
				
				if (pageLinks == " ") { pageLinks = '<a href="#" class="button gray-gray"><span class="title">Coming Soon...</span></a>'; }
				if (linksfound == false) { imgFileID = 'default';  }
				
				$('.pdfpreviewWrapper .thumb img').attr("src", "/_assets/shell/desktop/docs/thumbs/" + imgFileID + ".jpg");
				
				$('.pdfpreviewWrapper .close').attr("id", currID);
				$('.pdfpreviewWrapper .links').html(pageLinks);
				$('.pdfpreviewWrapper .info').html(data.page_info);
				
				//alert(data);
			}, "json");
			return false;
		});		
		
		$('.pdfpreviewWrapper .close').live("click", function() {
			google_track('_trackEvent', 'resources_overlay', 'close', $(this).attr("id"));
			if ($.browser.msie && $.browser.version <= 7){
				$('.pdfpreviewWrapper').fadeOut(0, function() {
					$(this).remove();
				});
			}
			else {
				$('.pdfpreviewWrapper').fadeOut(250, function() {
					$(this).remove();
				});
			}
		});
		
		$('.disclaimer').live("click", function() {
			currExtendedID = $(this).attr("id");
			google_track('_trackEvent', 'resources_overlay', 'download', $(this).attr("id"));
			$('body').append('<div class="backdrop"></div><div class="disclaimerWrap"></div>');

			docURL = $(this).attr("href");
			$('.backdrop').css("opacity", .5).fadeIn(250);
			$('.disclaimerWrap').load('/_assets/shell/desktop/bits/disclaimer.php', function() {
				if ($.browser.msie && $.browser.version <= 7){
					$(this).fadeIn(0);
					$('.disclaimerWrap .download').attr("href", docURL);
				}
				else{
					$(this).fadeIn(250);
					$('.disclaimerWrap .download').attr("href", docURL);	
				}
				
			});
			return false;
		});
		
		$('.disclaimerWrap .close').live("click", function() {
			if ($(this).attr("id") != 'email_cancel') {
				google_track('_trackEvent', 'resources_disclaimer', 'cancel', currExtendedID);
			}
			$('.backdrop').fadeOut(250, function() {
				$(this).remove();
			});
			if ($.browser.msie && $.browser.version <= 7){
				$('.disclaimerWrap').fadeOut(0, function() {
					$(this).remove();
				});
			}
			else {
				$('.disclaimerWrap').fadeOut(250, function() {
					$(this).remove();
				});
			}
			return false;
		});
		
		$('.disclaimerWrap .download').live("click", function() {		
                  google_track('_trackEvent', 'resources_disclaimer', 
'proceed', currExtendedID);		
	$('.backdrop').fadeOut(250,function(){
				$(this).remove();
			});
			if ($.browser.msie && $.browser.version <= 7){
				$('.disclaimerWrap').fadeOut(0, function() {
					$(this).remove();
				});
			}
			else {
				$('.disclaimerWrap').fadeOut(250, function() {
					$(this).remove();
				});
			}
			return true;
		});
						
		/*
		$('.disclaimerWrap .download').live("click", function() {
			google_track('_trackEvent', 'resources_disclaimer', 'proceed', currExtendedID);
			docFilename = $(this).attr("href");
			$('.disclaimerWrap').fadeOut(250, function() {
				docPath = 'doc.php?file=' + docFilename;
				//alert(docPath);
				downloadURL(docPath);
				$(this).remove();
				$('.backdrop').remove();	
				
			});
			return false;
		});
		*/
		
		$('.contactus a').click(function() {
			$('body').prepend("<div class='backdrop'></div><div class='disclaimerWrap contactuswrap'></div>");
			$('.backdrop').css("opacity", .5).fadeIn(250);
			$('.contactuswrap').load("/_assets/shell/desktop/bits/contactus.php", function() {
				$(this).fadeIn(250);
			});
		});
		
		
		$('a[href=#isi]').click(function() {
			$('html,body').animate({scrollTop: $("#isi").offset().top-20},1000);
		});		

		function validateFields() {
			$('.error').removeClass('error'); 
			var errorPrefix = "Please enter ";
			errorfound = false;
			$('.form input[type=text].req').each(function() {
				var errorMsg = errorPrefix + $(this).parent().children('label').html().toLowerCase().replace(":", "").replace("*", "");
				if (!$(this).val() || $(this).val() == errorMsg) { 
					$(this).val(errorMsg).parent().addClass("error");
					errorfound = true;
				}
			});
		}
		
		$('.form .submit').live("click", function() {
			validateFields();
			if (errorfound == true) { return false; } else { $('#frmContactUs').submit(); }
		});
		
		$('.form').submit(function() {
			validateFields();
			if (errorfound == true) { return false; }
		});	
		
		$('.form .error input[type=text].req').live("click", function() {
			$(this).val("").parent().removeClass('error');
		});
		
		$('.form textarea').live('click', function() {
			if ($(this).val() == "Message...") { $(this).val(""); }
		});
		
		$('.form textarea').live('blur', function() {
			if ($(this).val() == "") { $(this).val("Message..."); }
		});

	//  ===========================================================================
	// 	GOOGLE TRACKING
	//  ===========================================================================

	/* google tracking -- pdfs */
	$(".pdf").click(function() {
		var url = $(this).attr("href");
		var array = url.split('/');
		var lastsegment = array[array.length-1];
		
		//_gaq.push(['_trackPageview', lastsegment]);
		google_track('_trackPageview', lastsegment);
	});

	/* google tracking -- internal/external links */
	$(".internal_link, .external_link").click(function() {
		google_track('_trackEvent', ($(this).hasClass('internal_link') ? 'internal_link' : 'external_link'), 'click', $(this).attr("href"));	
	});
	
	/* google tracking -- ISI */
	$(".isi").click(function() {
		google_track('_trackEvent', 'ISI', 'click');	
	});

	/* google tracking -- Flumist Logo */
	$("#flumistlogo").click(function() {
		google_track('_trackEvent', 'external_link', 'click', 'flumist.com_logo');	
	});

	/* google tracking -- Flumist Share with a Colleague */
	$("#flumistlink").click(function() {
		google_track('_trackEvent', 'external_link', 'click', 'flumist.com');	
	});

	/* google tracking */
	$("#sharecoll").click(function() {
		google_track('_trackEvent', 'internal_link', 'click', 'Share_with_a_colleague');	
	});

	/* google callouts */
	$(".hcallout").click(function() {
		google_track('_trackEvent', 'homepage_callouts', 'click', $(this).attr("id"));	
	});


	/* google callouts */
	$(".why_vaccinate_left_nav, .lessons_learned_left_nav, .plan_left_nav").click(function() {
		var cat = $(this).attr("class");
		var val = $(this).attr("id");
		
		google_track('_trackEvent', cat, 'click', val);	
	});
	
	/* google tabs */
	$("#public_coalition, #buy_and_bill, #community_immunizers-mediated").click(function() {
		currTab = $(this).attr("id");
		google_track('_trackEvent', currTab, 'click', getPage());
	});
	
	/* google buttons" */
	$("#next_stage, #previous_stage").click(function() {
		var page = getPage();
		google_track('_trackEvent', page, 'click', currTab + '_' + $(this).attr("id"));
	});
	
	$(".why_vaccinate_callouts").click(function() {
		var val = $(this).attr("id").replace("call_", "");
		google_track('_trackEvent', 'why_vaccinate_callouts', 'click', val);
	});

	$(".lessons_learned_callouts").click(function() {
		var val = $(this).attr("id").replace("call_", "");
		google_track('_trackEvent', 'lessons_learned_callouts', 'click', val);
	});

	$(".plan_callouts").click(function() {
		var val = $(this).attr("id").replace("call_", "");
		google_track('_trackEvent', 'plan_callouts', 'click', val);
	});

	$("#email_cancel, #email_send").live("click", function() {
		google_track('_trackEvent', 'email_overlay', 'click', ($(this).attr("id") == "email_send" ? 'submit' : 'cancel'));	
	});

	$(".ga_pdfpreview").click(function() {
		currID = $(this).attr("id");
		google_track('_trackEvent', 'resources_overlay', 'open', $(this).attr("id"));
	});

	$(".download-all .title").click(function() {
		google_track('_trackEvent', 'resources_callouts', 'click', 'download_all');	
	});

	$("#ga_view_content").live("click", function() {
		google_track('_trackEvent', 'resources_overlay', 'view_content', currID);	
	});
	
	//  ===========================================================================
	// 	HOVER FIX FOR IE6
	//  ===========================================================================
	
	sfHover = function() {
		var sfEls = document.getElementById("toplevel").getElementsByTagName("LI");
		for (var i=0; i<sfEls.length; i++) {
			sfEls[i].onmouseover=function() {
				this.className+=" sfhover";
			}
			sfEls[i].onmouseout=function() {
				this.className=this.className.replace(new RegExp(" sfhover\\b"), "");
			}
		}
	}
	if (window.attachEvent) window.attachEvent("onload", sfHover);

});



		function downloadURL(url)
		{
			var iframe;
			iframe = document.getElementById("hiddenDownloader");
			if (iframe === null)
			{
				iframe = document.createElement('iframe');  
				iframe.id = "hiddenDownloader";
				iframe.style.visibility = 'hidden';
				document.body.appendChild(iframe);

				iframePI = document.createElement('iframe');  
				iframePI.id = "hiddenDownloaderPI";
				iframePI.style.visibility = 'hidden';
				document.body.appendChild(iframePI);

			}
			iframe.src = url;  
			iframePI.src = "doc.php?file=PI.pdf";
		}

/**
 * function created for google tracking, just in case we need to make changes to
 * the google func call and/or we want to track these items ourselves
 */
function google_track(trackEvent, trackTitle, trackType, trackValue) {
	_gaq.push([trackEvent, trackTitle, trackType, trackValue]);
	
	//alert('Track Event: \t' + trackEvent + '\n\nTrack Title: \t' + trackTitle + '\n\nTrack Type: \t' + trackType + '\n\nTrack Value: \t' + trackValue + '');
	//console.log('[Track Event: ' + trackEvent + '] [Track Title: ' + trackTitle + '] [Track Type: ' + trackType + '] [Track Value: ' + trackValue + ']');
}

