/***************
	OMN360 utilities JS
	developer:gkuhl@bluediesel.com

	++ avPlayer ++
	
	@aLinks = string -- the class name of live links to audio content
	the avPlayer looks for these links and opens the player
	
	@aPlayerUrl = string -- the url to the location of the podcast player
	
	Add this javascript to any pages that need the podcast player:
	
	YAHOO.util.Event.onAvailable("footer", OMN360.util.avPlayer, {aLinks:"audioLink",aPlayerUrl:"../../assets/player.html"});
	
*************************

	++ external link warning ++
	
	@oLinkClass = string -- the class name of links to external sites
	
	@thisSite = string -- the name of the current website as you'd like it to appear in the offsite warning window
	
	Add this javascript to any pages with offsite links:
	
	YAHOO.util.Event.onAvailable("footer", OMN360.util.offsiteLinks,{oLinkClass:"external", thisSite:"TOPAMAX360.com"});
	
************************

	++ migraine analyzer ++
	
	@jumpUrl = string -- the url of the "about topamax" page
	
	Add this javascript to the patient analyzer poge :

	YAHOO.util.Event.onAvailable("footer", OMN360.util.analyze, {jumpUrl:'at.html'});
	
************************/
var OMN360 = window.OMN360 || {};
OMN360.util = function(){
	var hitCount = 0; // used to turn off popup disclaimer
	var theDisclaimer;
	var warningText="You must agree to the terms before continuing.";
	return {
		avPlayer:function(obj){
			var disclaimer = document.createElement('DIV');
			var playerWindow;
			var playerUrl;
			var audioLinks = YAHOO.util.Dom.getElementsByClassName(obj.aLinks, 'a') || [];
			var aPlayer=obj.aPlayerUrl;
			disclaimer.innerHTML="";
			disclaimer.innerHTML+="<p><b>IMPORTANT SAFETY INFORMATION<b></p>\n";
			disclaimer.innerHTML+="<p>TOPAMAX has been associated with serious adverse events, including:</p>\n";
			disclaimer.innerHTML+="<p>* Hyperchloremic, non-anion gap metabolic acidosis - lowering of bicarbonate levels in the blood. Measurement of baseline and periodic serum bicarbonate is recommended.</p>\n";
			disclaimer.innerHTML+="<p>* Acute myopia and secondary angle-closure glaucoma - patients should be cautioned to seek medical attention if they experience blurred vision or ocular pain.</p>\n";
			disclaimer.innerHTML+="<p>* Oligohidrosis and hyperthermia - decreased sweating and increased body temperature, especially in hot weather. The majority of reports have been in children.</p>\n";
			disclaimer.innerHTML+="<p>* Cognitive/psychiatric side effects including cognitive dysfunction, psychiatric/behavioral disturbances including suicidal thoughts or behavior, and somnolence and fatigue.</p>\n";
			disclaimer.innerHTML+="<p>Most common adverse events associated with TOPAMAX 100 mg vs placebo were: paresthesia, 51% vs 6%; anorexia,* 15% vs 6%; fatigue, 15% vs 11%; nausea, 13% vs 8%; diarrhea, 11% vs 4%; weight decrease, 9% vs 1%; taste alteration, 8% vs 1%.</p>\n";
			disclaimer.innerHTML+="<p>The possibility of decreased contraceptive efficacy and increased breakthrough bleeding should be considered in patients taking combination oral contraceptive products with TOPAMAX.</p>\n";
			disclaimer.innerHTML+="<p>Patients should be instructed to maintain an adequate fluid intake in order to minimize the risk of renal stone formation.</p>\n";
			disclaimer.innerHTML+="<p>*Anorexia is defined as loss of appetite</p>\n";
			disclaimer.innerHTML+="<p>Please see full U.S. <a href='topamax-prescribing-information.html'>Prescribing Information</a></p>\n";
			disclaimer.innerHTML+="<form action='#' id='compliance' >\n<input type='checkbox' value='0' name='agree' class='checkbox' /> I have read and understand the <b>IMPORTANT SAFETY INFORMATION</b> above<br /><input type='button' value='Continue &gt;&gt;' class='button' id='disc_continue' /> <input type='button' value='Cancel' class='button' id='disc_cancel' /><p id='disc_warning'>"+warningText+"</p></form>";
			YAHOO.util.Dom.setStyle(disclaimer, 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle(disclaimer, 'height', '0');
			YAHOO.util.Dom.setStyle(disclaimer, 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle(disclaimer, 'fontSize', '11px');
			YAHOO.util.Dom.addClass(disclaimer, 'pod_disclaimer');
			disclaimer.id="disclaimer";
			document.body.appendChild(disclaimer);
			theDisclaimer = new YAHOO.widget.Panel("disclaimer", {draggable:false, modal:true, fixedcenter:true, visible:true, width:"500px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}});	
			function aPlayerInit(e){
				YAHOO.util.Event.stopEvent(e);
				//playerUrl = aPlayer+"?"+(parseInt(this.id,10));
				playerUrl = aPlayer+"?"+this.id;
				popWindow();
				return;
			}
			function popWindow(){
				var warningCount=0;
				var theForm = YAHOO.util.Dom.get("compliance");
				function showPlayer(){
					hitCount++;
					theDisclaimer.hide();
					playerWindow = window.open(playerUrl, "Window2", "width=400,height=440,scrollbars=no,status=no,top=200, left=200");
					playerWindow.focus();
				}
				function showWarning(e){
					var element;
					if(warningCount<1){
						element = YAHOO.util.Dom.get("disc_warning");
						YAHOO.util.Dom.setStyle(element, 'display', 'block');
					}
					warningCount++;	
				}
				function discCancel(){
					theDisclaimer.hide();
					var element = YAHOO.util.Dom.get("disc_warning");
					YAHOO.util.Dom.setStyle(element, 'display', 'none');
				}
				function popDisclaimer(){
					YAHOO.util.Dom.setStyle(disclaimer, 'height', 'auto');
					YAHOO.util.Dom.setStyle(disclaimer, 'overflow', 'auto');	
					theDisclaimer.render();	
					theDisclaimer.show();
				}
				if(hitCount===0){
					popDisclaimer();
					// set onsubmit and cancel
					YAHOO.util.Event.on(YAHOO.util.Dom.get('disc_cancel'), 'click', function(){discCancel();});
					YAHOO.util.Event.on(YAHOO.util.Dom.get('disc_continue'), 'click', function(){
						if(theForm.agree.checked===false) {
							showWarning();
						} else { 
							showPlayer();
						}
					});
				} else {
					showPlayer();
				}
			}
			for (var i=0; i < audioLinks.length; i++) {
				YAHOO.util.Event.on(audioLinks[i], 'click', aPlayerInit, audioLinks[i], true);
			}
		},
		offsiteLinks:function(obj){
			var offsiteUrl;
			var theWarning;
			var offsiteWarning = document.createElement('DIV');
			var offsiteLinks = YAHOO.util.Dom.getElementsByClassName(obj.oLinkClass, 'a') || [];
			offsiteWarning.id="offsiteWarning";
			YAHOO.util.Dom.setStyle(offsiteWarning, 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle(offsiteWarning, 'padding', '16px');
			YAHOO.util.Dom.setStyle(offsiteWarning, 'height', '0');
			YAHOO.util.Dom.setStyle(offsiteWarning, 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle(offsiteWarning, 'fontSize', '12px');
			YAHOO.util.Dom.addClass(offsiteWarning, 'pod_disclaimer');
			document.body.appendChild(offsiteWarning);
			theWarning = new YAHOO.widget.Panel("offsiteWarning", {draggable:false, modal:true, fixedcenter:true, visible:true, width:"400px", effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25}});	
			setLinks();
			//
			function warningInit(e){
				YAHOO.util.Event.stopEvent(e);
				offsiteUrl = this.href;
				offsiteWarning.innerHTML="<p><b>Thank you for visiting " + obj.thisSite + ".</b></p><p>By clicking 'Continue' you will be taken to: <br /><b>" + offsiteUrl +"</b><br /> This is a Web site to which our Privacy Policy does not apply. You are solely responsible for your interactions with such Web sites.</p>";
				offsiteWarning.innerHTML+="<input type='button' value='Continue &gt;&gt;' class='button' id='offsite_continue' /> <input type='button' value='Cancel' class='button' id='offsite_cancel' />";
				YAHOO.util.Event.on(YAHOO.util.Dom.get('offsite_cancel'), 'click', function(){warningCancel();});
				YAHOO.util.Event.on(YAHOO.util.Dom.get('offsite_continue'), 'click', function(){window.open(offsiteUrl,"_blank");theWarning.hide();});
				popWarning();
				return;
			}
			function setLinks(){
				for (var i=0; i < offsiteLinks.length; i++) {
					YAHOO.util.Event.on(offsiteLinks[i], 'click', warningInit, offsiteLinks[i], true);
				}
			}
			function popWarning(){
				YAHOO.util.Dom.setStyle(offsiteWarning, 'height', 'auto');
				YAHOO.util.Dom.setStyle(offsiteWarning, 'overflow', 'auto');	
				theWarning.render();	
				theWarning.show();
			}
			function warningCancel(){
				theWarning.hide();
			}
		},
		analyze:function(o) {
			var answerDiv = document.createElement('DIV');
			var anaylzerForm = YAHOO.util.Dom.get("il_analyzer_form");
			var analyzerDiv = YAHOO.util.Dom.get('il_analyzer');
			var learnMoreUrl = o.jumpUrl;
			YAHOO.util.Dom.setStyle(answerDiv, 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle(answerDiv, 'padding', '16px');
			YAHOO.util.Dom.setStyle(answerDiv, 'height', '0');
			YAHOO.util.Dom.setStyle(answerDiv, 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle(answerDiv, 'fontSize', '12px');
			answerDiv.id="analyze_panel";
			document.body.appendChild(answerDiv);
			//
			YAHOO.util.Event.on("il_analyzer_form", 'submit', formCheck, anaylzerForm, true);
			//
			var analyzePanel = new YAHOO.widget.Panel("analyze_panel", { fixedcenter:true, visible:false, width:"400px", modal:true, draggable:false,effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} } );
			//
			function showAnswer(answer){
				YAHOO.util.Dom.setStyle(answerDiv, 'height', 'auto');
				YAHOO.util.Dom.setStyle(answerDiv, 'overflow', 'auto');	
				switch(answer){
					case false :
					answerDiv.innerHTML="<p>This patient may not be a not candidate for migraine prevention therapy.<span><input type='button' value='Close' class='button' id='analyze_cancel' /></span</p>";
					YAHOO.util.Event.on("analyze_cancel", 'click', closeAnswer);
					break;
					case true :
					answerDiv.innerHTML="<p>This patient is a candidate for migraine prevention therapy. <a href='" + learnMoreUrl + "'>Click here</a> to get the latest information about TOPAMAX for migraine prevention.<span><input type='button' value='Close' class='button' id='analyze_cancel' /></span</p>";
					YAHOO.util.Event.on("analyze_cancel", 'click', closeAnswer);
					break;
					default :
					return;
				}
				analyzePanel.render();
				analyzePanel.show();
			}
			function closeAnswer(){
				analyzePanel.hide();
			}
			function formCheck(e) {
				YAHOO.util.Event.stopEvent(e);
				var myForm = this.elements;
				var question1 = +this.elements.question1.value;
				var question2 = +this.elements.question2.value;
				var question3 = +this.elements.question3.value;
				var question4 = this.elements.question4.value;
				if(question1 >= 2 || question2 >= 3 || question3 >= 3 || question4 == 'yes') {
					showAnswer(true);
				} else {
					showAnswer(false);
				}
			}
		},
		analyze_live:function(o) {
			var answerDiv = document.createElement('DIV');
			var anaylzerForm = YAHOO.util.Dom.get("il_analyzer_form");
			var analyzerDiv = YAHOO.util.Dom.get('il_analyzer');
			var learnMoreUrl = o.jumpUrl;
			YAHOO.util.Dom.setStyle(answerDiv, 'visibility', 'hidden');
			YAHOO.util.Dom.setStyle(answerDiv, 'padding', '16px');
			YAHOO.util.Dom.setStyle(answerDiv, 'height', '0');
			YAHOO.util.Dom.setStyle(answerDiv, 'overflow', 'hidden');
			YAHOO.util.Dom.setStyle(answerDiv, 'fontSize', '12px');
			answerDiv.id="analyze_panel";
			document.body.appendChild(answerDiv);
			//
			YAHOO.util.Event.on("il_analyzer_form", 'submit', formCheck, anaylzerForm, true);
			//
			var analyzePanel = new YAHOO.widget.Panel("analyze_panel", { fixedcenter:true, visible:false, width:"400px", modal:true, draggable:false,effect:{effect:YAHOO.widget.ContainerEffect.FADE,duration:0.25} } );
			//
			function showAnswer(answer){
				YAHOO.util.Dom.setStyle(answerDiv, 'height', 'auto');
				YAHOO.util.Dom.setStyle(answerDiv, 'overflow', 'auto');	
				switch(answer){
					case false :
					answerDiv.innerHTML="<p>This patient may not be a not candidate for migraine prevention therapy.<span><input type='button' value='Close' class='button' id='analyze_cancel' /></span</p>";
					YAHOO.util.Event.on("analyze_cancel", 'click', closeAnswer);
					break;
					case true :
					answerDiv.innerHTML="<p>This patient is a candidate for migraine prevention therapy.<span><input type='button' value='Close' class='button' id='analyze_cancel' /></span</p>";
					YAHOO.util.Event.on("analyze_cancel", 'click', closeAnswer);
					break;
					default :
					return;
				}
				analyzePanel.render();
				analyzePanel.show();
			}
			function closeAnswer(){
				analyzePanel.hide();
			}
			function formCheck(e) {
				YAHOO.util.Event.stopEvent(e);
				var myForm = this.elements;
				var question1 = +this.elements.question1.value;
				var question2 = +this.elements.question2.value;
				var question3 = +this.elements.question3.value;
				var question4 = this.elements.question4.value;
				if(question1 >= 2 || question2 >= 3 || question3 >= 3 || question4 == 'yes') {
					showAnswer(true);
				} else {
					showAnswer(false);
				}
			}
		}
	};
}();
