/* Create a namespace */
var FertilityLifeLinesV2 = {};

FertilityLifeLinesV2.enableTabs = function() {
    $("ul#inlineNavList").tabs();
};

FertilityLifeLinesV2.enableGlossary = function() {
	$("a.glossaryTrigger").mouseover(
      function () { 
		var triggers = this.id;
		var ele = "a.glossaryTrigger#" + triggers;
		var pos = $(ele).offset();
		var linkWidth = $(ele).width();
		var left = pos.left + linkWidth;
		//var left = pos.left - 360 + linkWidth;
		var posLeft = pos.left;
		var top = pos.top - 15;
		if (left < 0) {
			top = top + 15; //explorer needs 15 added to top.
		}
		//alert("posLeft=" + posLeft + "\nlinkWidth=" + linkWidth + "\ntop=" + top + "\nleft=" + left);
		$(".jqmAlert").css( { position: "absolute", marginLeft: 0, marginTop: 0, left: left, top: top } );
		var jqmAlertContent = ("/glossary/" + triggers + ".html");
		$("#glossaryModal").jqm({trigger: triggers, ajax: jqmAlertContent, target: 'div.jqmAlertContent div.contentbox', overlay: 0}).jqmShow();
		return false;
      }, 
      function () {
       	//$('#glossaryModal').jqmHide();
				return false;
      }
    );	
    
    $("a.glossaryTrigger").mouseout(
      function () { 
		var triggers = "";
		var ele = "";
		var pos = "";
		var linkWidth = "";
		var left = "";
		//var left = pos.left - 360 + linkWidth;
		var posLeft = "";
		var top = "";
		/*if (left < 0) {
			top = top + 15; //explorer needs 15 added to top.
		}*/
		//alert("posLeft=" + posLeft + "\nlinkWidth=" + linkWidth + "\ntop=" + top + "\nleft=" + left);
		//$(".jqmAlert").css( { position: "absolute", marginLeft: 0, marginTop: 0, left: left, top: top } );
		var jqmAlertContent = ("");
		$("#glossaryModal").jqm({trigger: triggers, ajax: jqmAlertContent, target: 'div.jqmAlertContent div.contentbox', overlay: 0}).jqmHide();
		return false;
      }, 
      function () {
       	//$('#glossaryModal').jqmHide();
				return false;
      }
    );	

	  // NOTE; we could have used document.getElementById(), or selected multiple elemets with $(..selector..) and passed the trigger
	  // as a jQuery object. OR, just include the string '#glossaryTrigger' as the trigger parameter (as typically demonstrated).

	  //  NOTE; we supply a target for the ajax return. This allows us to keep the structure of the alert window. An element can 
	  //  also be passed (see the documentation) as target.

	  //var triggers = $('a.glossaryTrigger')[0];
	  //$('#glossaryModal').jqm({ trigger: triggers, ajax: '/resources/glossary.jsp', target: 'div.jqmAlertContent', overlay: 0 });

	  // Close Button Highlighting. IE doesn't support :hover. Surprise?
	  if($.browser.msie) {
	  $('div.jqmAlert .jqmClose')
	  .hover(
	    function(){ $(this).addClass('jqmCloseHover'); }, 
	    function(){ $(this).removeClass('jqmCloseHover'); });
	  }
};

FertilityLifeLinesV2.addHideStyles = function() {
    /* Add a few CSS styles to hide certain page elements while the page loads */
    var hideStyle = '<style class="loading-styles">';
    hideStyle +=        'ul.collapsible-navigation-menu li.head ul,';
    hideStyle +=        'ul.accordion-menu li ul,';
    hideStyle +=        'ul.accordion-menu li div#top-news a.view-all,';
    hideStyle +=        'div.tabbed-content div.inner-tabbed-content,';
    hideStyle +=        'div.offering-tabbed-content div.inner-tabbed-content,';
    hideStyle +=        'div.research-tabbed-content div.inner-tabbed-content,';
    hideStyle +=        'div.faux-tabbed-content div.inner-tabbed-content div,';
    hideStyle +=        'div.hide-on-load';
    hideStyle +=        '{display:none;}';
    hideStyle +=    '</style>';

    var showStyle = '<style class="loading-styles">';
    showStyle +=        'ul.collapsible-navigation-menu li.current-head ul,';
    showStyle +=        'div.faux-tabbed-content div.inner-tabbed-content div#sortall,';
    showStyle +=        'div.faux-tabbed-content div.inner-tabbed-content div#sortall div,';
    showStyle +=        'div.faux-tabbed-content div.inner-tabbed-content div.featured-topic,';
    showStyle +=        'div.faux-tabbed-content div.inner-tabbed-content div.featured-topic div,';
    showStyle +=        '#tab1';
    showStyle +=        '{display:block;}';
    showStyle +=    '</style>';

    $("head").append(hideStyle);
    $("head").append(showStyle);
};

FertilityLifeLinesV2.removeHideStyles = function(id) {
    $('style.loading-styles').remove();
};


/* Add Mac stylesheet for certain font issues, where appropriate */
FertilityLifeLinesV2.enableMacStyles = function() {
	var isMac = false;
	var macStyle = '<link rel="stylesheet" type="text/css" href="/assets/styles/mac.css" media="all" />';
	if(navigator.userAgent.indexOf('Mac') != -1){
        isMac = true;	
	};
	if (isMac) {
		$("head").append(macStyle);
	};
};

/* These are called before the page loads */
FertilityLifeLinesV2.addHideStyles();
FertilityLifeLinesV2.enableMacStyles();

FertilityLifeLinesV2.checkLeftHeight = function() {
	if ($("#leftContentContainer").length > 0) {
		var rightHeight;
		var leftHeight;
		if (($("#tabs").length > 0) || ($("#step0").length > 0)) {
			$(".innerLeftNav").css("height", "auto");
		};
		rightHeight = $("#rightContentContainer").height();
		leftHeight = $("#leftContentContainer").height();
		if (leftHeight < rightHeight) {
			var newHeight = rightHeight - 13;
			$(".innerLeftNav").css("height", newHeight);
		};
	};
};

FertilityLifeLinesV2.PrintPreview = function() {
	// add close and print buttons to print preview screen
	if(FertilityLifeLinesV2.Helpers.IsPrint){	
		$('#contentContainer').prepend('<div id="print-controls-top"><button class="print">Print Page</button><a href="#" class="close">Close Window</a></div>');
		$('#center').append('<div id="print-controls-bottom"><button class="print">Print Page</button></div>');
		$("#rightMainContent a").attr('href', '#');
		$('#rightMainContent a').removeAttr('onClick'); 
		$("#rightMainContent a").unbind('click', null);
		$("#rightHeader").removeClass();
        $("#rightHeader h2+h1").css("color", "#666");
        $("#rightHeader h2+h1").css("font-size", "18px");
        $("#rightHeader h2+h1").css("font-family", "Arial, Helvetica, sans-serif");
        $("#rightHeader h2+h1").css("line-height", "21px");
        $("#rightHeader h2+h1").css("padding-top", "0px");
		$(".glossaryTrigger").removeClass('glossaryTrigger');
		$('#tabs ul').css({'display' : 'none'});
	}
	$('#print-controls-top a.close,#print-controls-bottom a.close').click(function() {
		if(window.close) {
			window.close();
		} else {
			alert('Your browser (or its current settings) does not support this close link. You may need to manually close this browser window or tab.');
		}
		return false;
	});
	$('#print-controls-top button.print,#print-controls-bottom button.print').click(function() {
		if(window.print) {
			FertilityLifeLinesV2.trackingPrint();
			window.print();
			return false;
		} else {
			alert('Your browser (or its current settings) does not support print button. You may need to manually print this page.');
		}
		return false;
	});
};

FertilityLifeLinesV2.trackingPrint = function() {
	var s=s_gi('co-fertilitylifelinescom,imcgonal-f');
	s.linkTrackVars='eVar10';
	s.linkTrackEvents='Event20';
	s.eVar10=s.pageName;
	s.events='Event20';
	s.tl(this,'o','Print this page');
};

FertilityLifeLinesV2.Helpers = {
	IsPrint:(location.search.indexOf('print=true')>-1)
};

FertilityLifeLinesV2.adjustleftNavPadding = function() {
	$("#leftNav").css("padding", "0 22px 0 13px");
};

FertilityLifeLinesV2.autoFill = function(id, v){
	$(id).css({ color: "#aaa" }).attr({ value: v }).focus(function(){
		if($(this).val()==v){
			$(this).val("").css({ color: "#000" });
		}
	}).blur(function(){
		if($(this).val()==""){
			$(this).css({ color: "#aaa" }).val(v);
		}
	});
};

FertilityLifeLinesV2.initHint = function() {
	$('input[title!=""]').hint();
};

/* Initialize everything */
$(document).ready(function() {
	/* Get rid of the styles that hide page elements now that we are ready to run the real scripts */
	FertilityLifeLinesV2.removeHideStyles();
	FertilityLifeLinesV2.PrintPreview();
	FertilityLifeLinesV2.enableTabs();
	FertilityLifeLinesV2.enableGlossary();
	if (!($("#tabs").length > 0)) {
		FertilityLifeLinesV2.checkLeftHeight();
	} 
	if (!($("#leftNavElements li").length > 0 )) {
		FertilityLifeLinesV2.adjustleftNavPadding();
	}
	FertilityLifeLinesV2.initHint();
});

