
//smartsite site root: equivalent to ~/
var siteroot = (typeof(fullsiteroot) == "function") && (typeof(sitehost) == "function") ? fullsiteroot().replace(sitehost(), "") : "/";
//language
var lang = location.href.indexOf("/en/") > - 1 ? "en" : "fr";
//fixes double slash in URLs
var normalizePath = function(string) {return string.replace(/(\/+)|\\+/g, "/");}
//escapes <, > & and " into corresponding html entities
var escapeHTML = function(string) {return string.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");}
//rounds a number to two decimals places. Returns a string
var toCurrency = function(number) {return number.toFixed(2);}
//IE background image cache script
//@cc_on document.execCommand("BackgroundImageCache", false, true);

/****
	Setup The GA
****/
// get the current page that the user is viewing. The page url can be overrided by the page itself
var _pageTrackerPage = (typeof _overridePageTrackerPage === 'undefined'?document.location.pathname:_overridePageTrackerPage);
// this will override the name of a jquery hide/show button
var _buttonTracker = null;


var pageTracker = null;

var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
var kJsHost = (("https:" == document.location.protocol) ? "https://" : "http://");
document.write(unescape("%3Cscript src='" + kJsHost + "t.klick.com/t.js' type='text/javascript'%3E%3C/script%3E"));

function ButtonTracker(obj)
{
	var buttonTracker;
	
	if(_buttonTracker == null)
	{
		var pathName = document.location.pathname;
		// unfortunately because of the way the urls are designed, I had to have special cases to build the urls properly
		if(pathName.indexOf("sexual_health") >= 0)
			pathName = (pathName.substring(0, pathName.indexOf("sexual_health")) + "/sexual_health/").replace(/\x2F\x2F/g, "/");

		buttonTracker = obj.innerHTML;
		
		// Unfortunately, since we are using the inner HTML, this will destroy everything on the French version. So the fix for this
		// is to have a hard coded translator to translate the names. Alternatively, we could code a tag into each click and read from 
		// that.
		switch(escape(buttonTracker).toLowerCase()) {
			case "ALESSE%20ET%20L%u2019ACN%C9".toLowerCase():
				buttonTracker = "alesse and acne";
				break;
			case "ALESSE%20ET%20LA%20PRISE%20DE%20POIDS".toLowerCase():
				buttonTracker = "ALESSE AND WEIGHT GAIN";
				break;
			case "PARLER%20AVEC%20TON%20M%C9DECIN".toLowerCase():
				buttonTracker = "WITH YOUR DOCTOR";
				break;
			case "PARLER%20AVEC%20TES%20PARENTS".toLowerCase():
				buttonTracker = "WITH YOUR PARENTS";
				break;
			case "PARLER%20AVEC%20TON%20PARTENAIRE".toLowerCase():
				buttonTracker = "WITH YOUR PARTNER";
				break;
			case "%C0%20QUOI%20T%u2019ATTENDRE%20CHEZ%20LE%20M%C9DECIN".toLowerCase():
				buttonTracker = "WHAT TO EXPECT AT THE DOCTOR";
				break;
			case "SEXUALIT%C9%20SANS%20RISQUE".toLowerCase():
				buttonTracker = "SAFE SEX";
				break;
			case "TEST%20DE%20PAPANICOLAOU".toLowerCase():
				buttonTracker = "Pap Test";
				break;
			case "EXAMEN%20VAGINAL".toLowerCase():
				buttonTracker = "Internal Exam";
				break;
			case "EXAMEN%20DES%20SEINS".toLowerCase():
				buttonTracker = "Breast Exam";
				break;
			
		}
		
		// some pages require categories, but since the text does not allow categories to be provided, we need to convert then
		switch(buttonTracker.toLowerCase()) {
			case "Pap Test".toLowerCase():
			case "Internal Exam".toLowerCase():
			case "Breast Exam".toLowerCase():
				buttonTracker = "WHAT TO EXPECT AT THE DOCTOR/" + buttonTracker;
				break;
			
		}

		buttonTracker = pathName + buttonTracker.replace(/\x20/g, "_").toLowerCase();
	}
	else
		buttonTracker = _buttonTracker;
	
	return buttonTracker;
}
// end GA

jQuery.noConflict();
var $j = jQuery;

//modal functionality
var modal = {
	init: function() {
		$j('#modal .close').click(function (e) {
			e.preventDefault();
			$j('#modalmask').hide();
			$j('#modalbody').hide();
			$j('#modalcontent').html("");
		});		
		
		$j('#modalmask').click(function () {
			$j(this).hide();
			$j('#modalbody').hide();
			$j('#modalcontent').html("");
		});	

		$j(window).resize(function() {
			if (jQuery('#modalbody').css('display') == 'block') {
				jQuery('#modalmask').hide();
				jQuery('#modalbody').hide();
				modal.open({contentEl:'modalcontent'});
			}
		});
	},
	open : function(options){
		var url, contentEl, link;
		if (options){
			url = options.url || "";	
			contentEl = options.contentEl || "";
			link = options.link;			
		}		

		if (url != "") {
			$j('#modalcontent').load(url);
		} else {
			var content = $j('#'+contentEl).html();
			$j('#modalcontent').html(content);
		}
		
		//Set outbound link
		$j('#proceedButton').click(function() {
			window.open(link);
			modal.close();
		});
		
		//Get the screen height and width
		var maskHeight = $j(document).height();
		var maskWidth = $j(window).width();

		//Set heigth and width to mask to fill up the whole screen
		$j('#modalmask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$j('#modalmask').fadeIn(1000);	
		$j('#modalmask').fadeTo("slow",0.8);
		
		//Get the window height and width
		var winH = $j(window).height();
		var winW = $j(window).width();
		var toppos;
		
		if (typeof window.pageYOffset != 'undefined') {
			toppos = window.pageYOffset;
		} else {
			toppos = document.documentElement.scrollTop
		}	  
				
		//Set the popup window to center
		$j('#modalbody').css('top',  toppos+((winH/2)-($j('#modalbody').height()/2)));
		$j('#modalbody').css('left', (winW/2)-($j('#modalbody').width()/2));

		//transition effect
		$j('#modalbody').fadeIn(2000); 		
	},
	
	close : function() {
		$j('#modalmask').hide();
		$j('#modalbody').hide();
		$j('#modalcontent').html("");
	}
};

var externallinks = function(){
		var txtok = (lang === 'en' ? 'Proceed' : 'Avancer');
		var txtcancel = (lang === 'en' ? 'Cancel' : 'Annuler');
		var txtmsg = "";
		
		if (lang === 'en') {
			txtmsg = "<p><strong><span class='modalTitle'>YOU ARE NOW LEAVING THE ALESSE.CA WEB SITE.</span></strong><br /><br /> Links to other sites are provided as a convenience to the viewer.  Pfizer Canada accepts no responsibility for the content of linked sites.</p>";
		} else {
			txtmsg = "<p><strong><span class='modalTitle'>VOUS ÊTES EN TRAIN DE QUITTER LE SITE WEB ALESSE.CA.</span></strong><br /><br />Les liens avec d'autres sites ne sont offerts qu'à titre de service aux utilisateurs. Pfizer Canada n'assume aucune responsabilité relativement au contenu de ces sites.</p>";
		}
		
		var modalmsg = $j('<div>', {id: 'leavemodal', 'class': 'hidden'});
		var modaltxt = $j('<div>'+txtmsg+'</div>');
		var proceedbtn = $j('<button>', {'class': '', id: 'proceedButton'});
		var cancelbtn = $j('<button>',{'class': '', onclick: 'modal.close()'});
		
		proceedbtn.html(txtok);
		cancelbtn.html(txtcancel);
		
		//create the modal
		modalmsg.append(modaltxt);
		modalmsg.append(proceedbtn);
		modalmsg.append(cancelbtn);
		$j('.content').append(modalmsg);
		modal.init();
	
	$j('a[target="_blank"]').click(function(e) {
		var outboundLink = $j(this).attr('href');
		e.preventDefault();
		modal.open({contentEl: 'leavemodal', link: outboundLink});
		
	});
};

//Had to rebuild this function for the splash page, because the splash page has english AND french links
var externallinkssplash = function(){
	
		var txtok = {en : 'Proceed', fr : 'Avancer'};
		var txtcancel = {en : 'Cancel', fr : 'Annuler'};
		var	txtmsg = {
			en : "<p><strong><span class='modalTitle'>YOU ARE NOW LEAVING THE ALESSE.CA WEB SITE.</span></strong><br /><br /> Links to other sites are provided as a convenience to the viewer.  Pfizer Canada accepts no responsibility for the content of linked sites.</p>",
			fr: "<p><strong><span class='modalTitle'>VOUS ÊTES EN TRAIN DE QUITTER LE SITE WEB ALESSE.CA.</span></strong><br /><br />Les liens avec d'autres sites ne sont offerts qu'à titre de service aux utilisateurs. Pfizer Canada n'assume aucune responsabilité relativement au contenu de ces sites.</p>"
		};
		
		
		var modalmsg = {
			en: $j('<div>', {id: 'leavemodalEn', 'class': 'hidden'}), 
			fr: $j('<div>', {id: 'leavemodalFr', 'class': 'hidden'})
		};
		var modaltxt = {
			en: $j('<div>'+txtmsg.en+'</div>'),
			fr: $j('<div>'+txtmsg.fr+'</div>')
		};
		
		var proceedbtn = {
			en: $j('<button>', {'class': '', id: 'proceedButton'}),
			fr: $j('<button>', {'class': '', id: 'proceedButton'})	
		};
		
		var cancelbtn = {
			en: $j('<button>',{'class': '', onclick: 'modal.close()'}),
			fr: $j('<button>',{'class': '', onclick: 'modal.close()'})
		};
		
		var langArray = ['en', 'fr'];
		
		for (var x = 0; x < langArray.length; x++) {
			proceedbtn[langArray[x]].html(txtok[langArray[x]]);
			cancelbtn[langArray[x]].html(txtcancel[langArray[x]]);
			
			//create the modal
			modalmsg[langArray[x]].append(modaltxt[langArray[x]]);
			modalmsg[langArray[x]].append(proceedbtn[langArray[x]]);
			modalmsg[langArray[x]].append(cancelbtn[langArray[x]]);
			$j('.content').append(modalmsg[langArray[x]]);
			modal.init();	
		}
		
		
	
	$j('a[target="_blank"]').click(function(e) {

		var outboundLink = $j(this).attr('href');
		e.preventDefault();
			
		//break these up into cases: french links, english links and generic links which will default to english
		if ($j(this).attr('data-lang') === 'fr') {	
			modal.open({contentEl: 'leavemodalFr', link: outboundLink});
		} else {
			modal.open({contentEl: 'leavemodalEn', link: outboundLink});
		}
	});
};

var nav_pointer = {
	
	start : 165,
	
	init : function() {
		var position = this.getPosition();
		this.animateIt(position);
	},
	
	getPosition : function() {
			var position = $j('.leftnav .selected').offset();
			return position ? position.top : 165;
	},
		
	animateIt : function (position) {
		var movePointerBy = (position - this.start) - 3;
		$j('.leftnav-pointer').animate({
				top: '+='+movePointerBy
			}, 1600, 'easeInOutBack');
	}
};

function DisplayPanel(panelName)
{
	if (panelName) {
		
		switch(panelName) {
			case "alesse_and_acne":
				$j('.landing').hide();
				$j('.alesse_and_acne').removeClass('hidden');
				break;
			case "step2_21days":
				$j('.step1').hide();
				$j('.step2-21days').removeClass('hidden');
				break;
			case "step2_28days":
				$j('.step1').hide();
				$j('.step2-28days').removeClass('hidden');
				break;
			case "what_to_expect_at_the_doctor":
				$j('.landing').hide();
				$j('.what_to_expect').removeClass('hidden');
				break;
			case "pap_tests":
				$j('.landing').hide();
				$j('.inner_landing').addClass("hidden");
				$j('.what_to_expect').removeClass('hidden');
				$j('.pap_test').removeClass('hidden');
				break;
			case "the_internal_exam":
				$j('.landing').hide();
				$j('.inner_landing').addClass("hidden");
				$j('.what_to_expect').removeClass('hidden');
				$j('.internal_exam').removeClass('hidden');
				break;
			case "the_breast_exam":
				$j('.landing').hide();
				$j('.inner_landing').addClass("hidden");
				$j('.what_to_expect').removeClass('hidden');
				$j('.breast_exam').removeClass('hidden');
				break;
			case "safe_sex":
				$j('.landing').hide();
				$j('.safe_sex').removeClass('hidden');
				
				break;
		}
		
	}
}

//tab functionalitiy
var tabs = {
	
	init: function () {
		
		var tabs = $j('.tabs a');
		var panels = $j('.panels > div');
		var landingText = $j('.landing');
		var inner_nav = $j('.inner-nav a');
		var inner_panels = $j('.inner-panels > div');
		var innerLandingText = $j('.inner_landing');
		
		tabs.each(function(i) {
		
			$j(this).click(function() {
				// track the click
				pageTracker._trackPageview(ButtonTracker(this));
				
				landingText.addClass('hidden');
				
				panels.each(function(j) {
					$j(tabs[j]).removeClass('selected');
					$j(this).removeClass('selected');
					$j(this).addClass('hidden');
				});
				
				inner_panels.each(function(k) {
					$j(inner_panels[k]).removeClass('selected')
					$j(inner_panels[k]).addClass('hidden')
				});
				
				innerLandingText.addClass('selected')
				innerLandingText.removeClass('hidden')
				$j(this).addClass('selected');
				$j(panels[i]).addClass('selected');
				$j(panels[i]).removeClass('hidden');
			});
		});
		
		inner_nav.each(function(i) {

			$j(this).click(function() {
				pageTracker._trackPageview(ButtonTracker(this));
				innerLandingText.addClass('hidden');
		
				inner_panels.each(function(j) {
					$j(inner_nav[j]).removeClass('selected');
					$j(this).removeClass('selected');
					$j(this).addClass('hidden');
				});
				$j(this).addClass('selected');
				$j(inner_panels[i]).addClass('selected');
				$j(inner_panels[i]).removeClass('hidden');
			});
		});
	}
};


//check for existence of URL parameters
function gup(name)	{
	name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS  = "[\\?&]"+name+"=([^&#]*)";
	var regex   = new RegExp( regexS );
	var results = regex.exec( window.location.href );
	if( results == null )
		return "";
	else
		return results[1];
	
};

//Show specific section within a page, based on URL parameter

	var getSection = (typeof panelDisplay === 'undefined'?gup('showpage'):panelDisplay);
	DisplayPanel(getSection);
	

	//change body class
	var changebodyclass = function(from, to) {
		$j('body').removeClass(from).addClass(to);
	}
	
	//switch to french or english site
	var swapLanguage = function() {
		var oldurl = location.href, newurl;
		if (lang == "en") {
			newurl = oldurl.replace (/\/en\//, "/fr/");
		} else {
			newurl = oldurl.replace (/\/fr\//, "/en/");
		}
		
		return newurl;
	}

$j(document).ready(function() {

	//hide horizontal scroll bar if site is being display in 1024 resolution
	var hideScrollbar = function () {
		var wWidth = $j(window).width();
		var theHTML = $j('html');
		if (wWidth <= 1280 && wWidth >= 1000) {
			//theHTML.css('overflow-x', 'hidden');
		} else {
			theHTML.css('overflow-x', 'visible');
		}	
	};
	
	//launch hideScrollbar
	hideScrollbar();
	//attach hideScrollbar function to window.resize event
	$j(window).resize(hideScrollbar);
	
	//load external links with modal popup functionality
	if ($j('body.splash').length) {
		externallinkssplash();
	} else{
		externallinks();
	}
	
	//splash page animation
	var splashanimation = function() {
		//fade in homepage elements
		for (var i = 1; i <= 6; i++) {
			var elem = $j('#home_animation_'+i);
			if (i == 1) {
				elem.delay(i * 200).animate({left: '-14px', opacity: 1}, 600, 'easeOutCirc');
			} else if (i == 6) {
				elem.delay(i * 200).fadeIn(1500);
			} else{ 
				elem.delay(i * 200).fadeIn(650);
			}
		}
	}
	
	//splash page launcher
	if ($j('body.splash').length > 0) {
		modal.init();
		splashanimation();
	}

	//splash page validation animation
	if ($j('body.splash').length > 0) {
		
		//show error message
		var invalidText = $j('.invalid');
		if (invalidText.length > 0) {
			invalidText.slideDown();
		}
		
		//hide error message
		var inputBox = invalidText.parent().find('input[type="text"]'); //get the input box related to this message
		inputBox.focus(function() {
			invalidText.slideUp();
		});
	}
	
	
	//launch navigation pointer animation
	if ($j('.home').length == 0 && $j('.top5').length == 0 && $j('.splash').length == 0) {
		nav_pointer.init();
	}
	
	//launch Tab functionality
	if ($j('#tabs').length > 0) {
		tabs.init();
	}
	
	//left nav dropdown
	var slidingElement = $j(".leftnav .selected ul");
	slidingElement.slideDown("slow");

	//language swap
	$j('.languageSwap').attr('href', swapLanguage());
	
	//'Your body and the pill' link functionality
	var acneLink = $j('.no-ie6 .acnelink');
	if (acneLink.length > 0) {
		var weightLink = $j('.weightlink');
		var type = $j('.bottom-right-image-type1');
		var imageDiv = $j('.bottom-right-image-typealt');
		
		acneLink.click(function() {
			type.hide();
			imageDiv.show();
		});
		weightLink.click(function() {
			imageDiv.hide();
			type.show();
		});
	}
	
	//'How to take Alesse' page linking
	if ($j('.how_to_take_alesse').length > 0) {
		
		var mainLeftCol = $j('.how_to_take_alesse > .leftcol');
		
		$j('.goToStep2-21days').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/21_days").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.step2-21days').show();
		});
		
		$j('.goToStep2-28days').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/28_days").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.step2-28days').show();
		});
		
		$j('.goToStep1').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/back").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.step1').show();
		});
		
		$j('.goToStep3-21days-1stSunday').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/21_days/1st_sunday").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.21days-1stSunday').show();
		});
		
		$j('.goToStep3-21days-5thDay').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/21_days/5th_day").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.21days-5thDay').show();
		});
		
		$j('.goToStep3-21days-1stDay').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/21_days/1st_day").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.21days-1stDay').show();
		});
		
		$j('.goToStep3-28days-1stSunday').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/28_days/1st_sunday").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.28days-1stSunday').show();
		});
		
		$j('.goToStep3-28days-5thDay').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/28_days/5th_day").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.28days-5thDay').show();
		});
		
		$j('.goToStep3-28days-1stDay').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/28_days/1st_day").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			mainLeftCol.hide();
			$j('.28days-1stDay').show();
		});
		
		$j('.button-start-over').click(function() {
			pageTracker._trackPageview((document.location.pathname.replace("/step2_28days", "").replace("/step2_21days", "") + "/start_over").replace("//", "/"));
			$j(this).parents('.step3').hide();
			$j('.step1').show();
			$j('.how_to_take_alesse > .leftcol').show();
		});
	}
	
	//'Missed a Pill' page linking
	if ($j('.missed_pill').length > 0) {
		
		$j('.step2-sunday').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/sunday").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.sunday-start').show();
		});
		
		$j('.step2-nonsunday').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/nonsunday").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.nonsunday-start').show();
		});
		
		$j('.step3-sunday-1pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/sunday/1_pill").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.sunday-1pill').show();
		});
		
		$j('.step3-sunday-2pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/sunday/2_pills").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.sunday-2pill').show();
		});
		
		$j('.step3-sunday-3pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/sunday/3_pills").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.sunday-3pill').show();
		});
		
		$j('.step3-nonsunday-1pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/nonsunday/1_pill").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.nonsunday-1pill').show();
		});
		
		$j('.step3-nonsunday-2pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/nonsunday/2_pills").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.nonsunday-2pill').show();
		});
		
		$j('.step3-nonsunday-3pill').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/nonsunday/3_pills").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.nonsunday-3pill').show();
			$j('#rollover2').show();
		});
		
		$j('.backbutton').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/back").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('.step1').show();
		});
		
		$j('.startover').click(function() {
			pageTracker._trackPageview((document.location.pathname + "/start_over").replace("//", "/"));
			$j(this).parents('.rightcol').hide();
			$j('#rollover2').hide();
			$j('.step1').show();
		});
	}
	
	/**
		Analytics Tracking
	**/
	pageTracker = _gat._getTracker((typeof trackingCode === 'undefined'?"UA-4418557-22":trackingCode));
	pageTracker._initData();
	pageTracker._trackPageview(_pageTrackerPage);
});
