// JavaScript Document
// JavaScript Document
$(document).ready(function() {
	/***************************
	NON FLASH NAVIGATION
	****************************/
	// display current sections navigation
	$("body.facts-about-hpv ul#main-nav > li#facts").css({top:'-10px'});
	$("body.learn-about-gardasil ul#main-nav > li#learn").css({top:'-10px'});
	$("body.make-an-impact ul#main-nav > li#impact").css({top:'-10px'});
	$("body.more-for-parents ul#main-nav > li#parents").css({top:'-10px'});
	
	// Hover nav is 18 px tall: 
	$("body ul#main-nav > li#facts").hoverIntent(
		function() {$(this).animate({top:'-190px'},600);},
		function() {$(this).animate({top:'0'},500);}
		);
		$("body.facts-about-hpv ul#main-nav > li#facts").hoverIntent(
		function() {$(this).animate({top:'-190px'},600);},
		function() {$(this).animate({top:'-10px'},500);}
		);
	$("body ul#main-nav > li#learn").hoverIntent(
		function() {$(this).animate({top:'-170px'},600);},
		function() {$(this).animate({top:'0'},500);}
		);
		$("body.learn-about-gardasil ul#main-nav > li#learn").hoverIntent(
		function() {$(this).animate({top:'-170px'},600);},
		function() {$(this).animate({top:'-10px'},500);}
		);
	$("body ul#main-nav > li#impact").hoverIntent(
		function() {$(this).animate({top:'-40px'},600);},
		function() {$(this).animate({top:'0'},500);}
		);
		$("body.make-an-impact ul#main-nav > li#impact").hoverIntent(
		function() {$(this).animate({top:'-40px'},600);},
		function() {$(this).animate({top:'-10px'},500);}
		);
	$("body ul#main-nav > li#parents").hoverIntent(
		function() {$(this).animate({top:'-100px'},600);},
		function() {$(this).animate({top:'0'},500);}
		);
		$("body.more-for-parents ul#main-nav > li#parents").hoverIntent(
		function() {$(this).animate({top:'-100px'},600);},
		function() {$(this).animate({top:'-10px'},500);}
		);
		
		// ADDING QUESTIONS TO ASK THE DOCTOR
		$("#add-question a#add").click(addQuestion);
		//$("#add-question a#add").click(function() { alert('addquestion'); addQuestion(); } );
		$(".your-questions").submit(addQuestion);
		
		// HAVE SOME FUN SECTION, EXPANDABLE ITEMS
		
		$('li.wallpapers a.expander,li.screensavers a.expander').toggle(
			function() {
				$(this).children('span').addClass("collapse").html("collapse");
				$(this).siblings("ul.expanded-options").show();
				return false;},
			function() {
				$(this).children('span').removeClass("collapse").html("Expand to download");				
				$(this).siblings("ul.expanded-options").hide();
				return false;
			});
			
		// EXPANDABLE BUDDY ICONS			
		$('li.buddy-icons a.expander,li.tshirts a.expander').toggle(
			function() {
				$(this).children('span').addClass("collapse").html("collapse");
				$(this).siblings("ul.expanded-icons").show();
				return false;},
			function() {
				$(this).children('span').removeClass("collapse").html("Expand to download");				
				$(this).siblings("ul.expanded-icons").hide();
				return false;
			});
		// EXPANDABLE BLOG BANNERS			
		$('li.banner a.expander').toggle(
			function() {
				$(this).children('span').addClass("collapse").html("collapse");
				$(this).siblings("ul.expanded-banners").show();
				return false;},
			function() {
				$(this).children('span').removeClass("collapse").html("Expand to download");				
				$(this).siblings("ul.expanded-banners").hide();
				return false;
			});
		// unclickable text in the center of the FUN links!!
		$("li.wallpapers a:first,li.screensavers a:first,li.buddy-icons a:first,li.tshirts a:first,li.banner a:first").hover(
			function () {
				$(this).css({cursor:"default"});
			},
			function () {
				$(this).css({cursor:"default"});
		});
		$("li.wallpapers a strong,li.wallpapers a span,li.screensavers a strong,li.screensavers a span,li.buddy-icons a strong,li.buddy-icons a span,li.tshirts a strong,li.tshirts a span,li.banner a strong,li.banner a span").hover(
			function () {
				$(this).css({cursor:"pointer"});
			},
			function () {
				$(this).css({cursor:"default"});
		});

		$("li.wallpapers a.expander div.noclick,li.screensavers a.expander div.noclick,li.buddy-icons a.expander div.noclick,li.tshirts a.expander div.noclick,li.banner a.expander div.noclick").click(function() {
			return false;
		});
		
	// FANCY TOOL TIPS, HOVER. see exploder.js for for IE6 additional rules
	$(".tip .tip-content,.tip .tip-bottom").hide();
	$(".tip").hoverIntent(
		function () {
			$(this).children(".tip-content,.tip-bottom").css({left:"-75px", cursor:"default"}).show();
			
		},
		function () {
			$(this).children(".tip-content,.tip-bottom").hide();
	});
	// DISABLE ANCHORS ON TOOL TIP LINKS
	$(".tip:not(.external)").click(function(){
		return false
	});
	// TURN CURSOR INTO POINTER ON "LINK" HOVER
	$(".tip em, .tipreversal em").hover(
		function () {
			$(this).css({cursor:"pointer"});
		},
		function () {
			$(this).css({cursor:"default"});
	});
	
	//RETURN ANCHOR OF TIP IF THEY CLICK ON 'SEE FULL GLOSSARY'
	$(".tip em.tip-more").click(function(){
		window.location.href = $(this).attr("title");
	});
	//RETURN ANCHOR OF TIP IF THEY CLICK ON 'SEE FULL GLOSSARY'
	$(".tip em:not(.tip-more,.external)").click(function(){
		window.location.href = $(this).parents("a").attr("href");
	});
	
	// TOOL TIPS ONCLICK FOR GENITAL WARTS PHOTOS
	$("a.click span.tip-content,a.click span.tip-bottom").hide();
	$("a.click").click(
		function () {
			$(this).children("span.tip-content,span.tip-bottom").toggle().css({left:"35px"});
			return false;
	});
	
	// adobe link on non flash pages, in the header
	$(".tipreversal .tip-content,.tipreversal .tip-bottom").hide();
	$(".tipreversal").hoverIntent(
		function () {
			$(this).children(".tip-content,.tip-bottom").css({cursor:"default"}).show();
			
		},
		function () {
			$(this).children(".tip-content,.tip-bottom").hide();
	});

	
	// SHARE THIS PAGE IN SOCIAL LINKS & BANNERS IN FUN SECTION
	$(".share_url,.expanded-banners textarea").click(
		function () {
			$(this).select();
			return false;
	});

	// ENABLE CLICKING IN THE SHARE PAGE POPUP
	$(".share-page").click(function () {
		return false;
	});
	
	$("#friend").val("Your Friend's E-mail");
	
	$("#friend").focus(function() {
		 if ($(this).val() == "Your Friend's E-mail") {
                $(this).val('');
          }
	});

	
/*******************
BOOMARK THIS PAGE
********************/
$("#bookmarksite").click(function () {
	var thisTitle = $('#bookmarksite').attr('title');
	var thisUrl = $('#bookmarksite').attr('rel');
	ua = navigator.userAgent.toLowerCase();
	//mac safari
	if ( ua.indexOf('webkit') > 0 ) {
		alert("Press Command D to bookmark Gardasil.com");
	} else if ( ua.indexOf('opera') > 0 ) {
		alert("opera");
	//IE
	} else if ( ua.indexOf('msie') > 0 ) {
		window.external.AddFavorite(thisUrl, thisTitle);
	//FF
	} else {
		window.sidebar.addPanel(thisTitle, thisUrl, "");
	}
	return false;
});

	// PRINT PAGE (BUTTONS ON PRINTER FRIENDLY PAGES)
	$('a.print').click(function() {
		window.print();
		return false;
	});

    $("a[@rel='new_window']").click(function(){
        window.open(this.href);
        return false;
    });	
	
	// VIEW PRINT FRIENDLY ON BUTTON CLICK
	 $("#learn-talking-print,#parents-daughter-print").click(function(){
 		thisUrl = $(this).attr("title");
 		window.open(thisUrl);
 	});
 	
     $("a.healthpro").click(function(){
		 window.open($(this).attr("href"),"healtpro","width=500,height=500,scrollbars=yes").focus();
		 return false;
	 });
	 
	 restoreCookie();

});

//////////////////////////////////////////////////////////////////////////////////
//BEGIN FUNCTIONS OUTSIDE OF DOCUMENT READY, WIDELY USED IN JQUERY SELCTORS ABOVE
//////////////////////////////////////////////////////////////////////////////////

function audioOff() { 
    thisMovie("mainnav").audioOff();
} 
function thisMovie(movieName) { 
 return document.getElementById(movieName);
}

/*************************************
VIDEO POPUPS, these are functions
specific to the flash popup video
player, found in video-popup.tpl
and video-tv-popup.tpl
**************************************/

function videoPop(){
	 popupwin = window.open("/gardasil-information/i-chose-shannon-v-pop/","Watch","width=727,height=742,scrollbars=yes,resizable=yes");
	 popupwin.focus();
	 popupwin.resizeTo(727,742);
	  return false;
}

// This link can be relative because It's only used in /hpv/human-papillomavirus/cervical-cancer/
function videoPopCervical(){
	 popupwin = window.open("/gardasil-information/i-chose-kristen-d-pop/","Watch","width=727,height=742,scrollbars=yes,resizable=yes");
         popupwin.focus();
	 popupwin.resizeTo(727,742);
	 return false;
}

// This link can be relative because It's only used in /vaccine-for-cervical-cancer/hpv-infection/
function videoPopMoms(){
	 popupwin =window.open("/gardasil-information/i-chose-elaina-pop/","Watch","width=727,height=742,scrollbars=yes,resizable=yes");
         popupwin.focus();
	 popupwin.resizeTo(727,742);
	 return false;
}

function videoPopTvOne(){
	 popupwin =window.open("/i-chose-tv-spot-1-pop/","Watch","width=625,height=470");
               popupwin .focus();
			   	 popupwin .resizeTo(625,550);
	 return false;
}
function videoPopTvTwo(){
	 popupwin =window.open("/i-chose-tv-spot-2-pop/","Watch","width=625,height=470");
               popupwin .focus();
			   popupwin .resizeTo(625,550);
	 return false;
}

function closeWindow() {
	window.close();
}

/*************************************
QUESTION Forms
**************************************/
// Store questions in array
var questions = [];

function addQuestion() {
	var thisText = $('#question').attr('value');
	if (thisText == null) {
		alert ("Veuillez entrez une question.");
		$("#question").focus();
	} else if (thisText.length <= 1) {
		alert("Est-ce vraiment une question ?");
		$("#question").focus();
	} else {
		questions.push(thisText);
		drawQuestions();
	}
	return false;
}
function drawQuestions() {
	// Empty questions
	$("#your-questions").html('');
	var html = "";
	for (var i in questions) {
		var q = new String(questions[i]);
		if (q == null || q.length == null || q.length == 1) {
			// Skip
		} else {
			html += "<li><p>" + questions[i] + "</p><span><a href=\"#\" class=\"delete-question\" onclick=\"return removeQuestion("+i+")\">Supprimer</a></span></li>";
		}
	}
	$("#your-questions").html(html);
	$("#question").focus();
	//alert("drawQuestions");
	$("form :input#question").val("");
	saveCookie();
}

function removeQuestion(id) {
	var oldQuestions = questions;
	questions = new Array();
	for (var i in oldQuestions) {
		if (i != id) {
			questions.push(oldQuestions[i]);
		}
	}
	drawQuestions();
	return false;
}
		
function saveCookie() {
	var content = questions.join("|*|");
	var encoded = escape(content);//encode64(content);
	createCookie(getCookieName(), encoded, 30);
}

function restoreCookie() {
	var encoded = readCookie(getCookieName());
	questions = new Array();
	if (encoded != null && encoded.length > 0) {
		var tmpQuestions = unescape(encoded).split("|*|");//decode64(encoded).split("|*|");
		for (var i in tmpQuestions) {
			if (tmpQuestions[i].length > 0) {
				questions.push(tmpQuestions[i]);
			}
		}
		drawQuestions();
	}
}

function getCookieName() {
	// Get cookie name depending on document URL.
	var cookieNum;
	if (/hpv-and-gardasil/.test(document.location)) {
		cookieNum = 1;
	} else if (/cervical-cancer-and-hpv/.test(document.location)) {
		cookieNum = 2;
	} else {
		cookieNum = 0;
	}
	return "gardasilQuestions" + cookieNum;
}

// Base64 encoding
// This code was written by Tyler Akins and has been placed in the
// public domain.  It would be nice if you left this header intact.
// Base64 code from Tyler Akins -- http://rumkin.com
var keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=";
function encode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   do {
      chr1 = input.charCodeAt(i++);
      chr2 = input.charCodeAt(i++);
      chr3 = input.charCodeAt(i++);

      enc1 = chr1 >> 2;
      enc2 = ((chr1 & 3) << 4) | (chr2 >> 4);
      enc3 = ((chr2 & 15) << 2) | (chr3 >> 6);
      enc4 = chr3 & 63;

      if (isNaN(chr2)) {
         enc3 = enc4 = 64;
      } else if (isNaN(chr3)) {
         enc4 = 64;
      }

      output = output + keyStr.charAt(enc1) + keyStr.charAt(enc2) + 
         keyStr.charAt(enc3) + keyStr.charAt(enc4);
   } while (i < input.length);
   
   return output;
}
function decode64(input) {
   var output = "";
   var chr1, chr2, chr3;
   var enc1, enc2, enc3, enc4;
   var i = 0;

   // remove all characters that are not A-Z, a-z, 0-9, +, /, or =
   input = input.replace(/[^A-Za-z0-9\+\/\=]/g, "");

   do {
      enc1 = keyStr.indexOf(input.charAt(i++));
      enc2 = keyStr.indexOf(input.charAt(i++));
      enc3 = keyStr.indexOf(input.charAt(i++));
      enc4 = keyStr.indexOf(input.charAt(i++));

      chr1 = (enc1 << 2) | (enc2 >> 4);
      chr2 = ((enc2 & 15) << 4) | (enc3 >> 2);
      chr3 = ((enc3 & 3) << 6) | enc4;

      output = output + String.fromCharCode(chr1);

      if (enc3 != 64) {
         output = output + String.fromCharCode(chr2);
      }
      if (enc4 != 64) {
         output = output + String.fromCharCode(chr3);
      }
   } while (i < input.length);

   return output;
}

// Cookie management
// from http://www.quirksmode.org/js/cookies.html
function createCookie(name,value,days) {
	/*
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	*/
	var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}