// toggle form elements
function toggleElement(el) {
	var theTarget = document.getElementById(el);
	if (theTarget.style.display=='block') {
		theTarget.style.display='none';
	} else {
		theTarget.style.display='block';
		theTarget.style.position="absolute";
		setFocus();
	}
	dockSignIn();
	return false;
}


//set focus
function setFocus()
{
     document.getElementById("username").focus();
}


// toggle faq
function faqToggle(x1,x2) {
	var dd = document.getElementById('faq-dd-'+x1+'-'+x2);
	var dt = document.getElementById('faq-dt-'+x1+'-'+x2);
	if (dd.style.display=='block') {
		dd.style.display='none';
		dt.style.backgroundImage='url(../imagesglobal/faq_plus.gif)';
	} else {
		dd.style.display='block';
		dt.style.border = 'none';
		dt.style.backgroundImage='url(../imagesglobal/faq_minus.gif)';
	}
}


// Read a cookie
function getTextSize(name)
{
  var cookieValue = "";
  var search = name + "=";
  if(document.cookie.length > 0)
  { 
    offset = document.cookie.indexOf(search);
    if (offset != -1)
    { 
      offset += search.length;
      end = document.cookie.indexOf(";", offset);
      if (end == -1) end = document.cookie.length;
      cookieValue = unescape(document.cookie.substring(offset, end))
    }
  }
}
//getTextSize('exp_css_skin');

var windowHeight = 0;
var windowScrollTop = 0;
	
function dockSignIn() {
  	var boxPanel = document.getElementById("sign-in");
	if(boxPanel.style.display!=='none') {
		if( typeof( window.innerWidth) == 'number' ) {
			windowWidth = window.innerWidth;//Non-IE or netscape compliant
			//alert('Non-IE or netscape compliant');
		} else if( document.documentElement && ( document.documentElement.clientWidth ) ) {
			windowWidth = document.documentElement.clientWidth;//IE 6+ in 'standards compliant mode
			//alert('IE 6+ in standards compliant mode');
		} else if( document.body && ( document.body.clientWidth ) ) {
			windowWidth = document.body.clientWidth;//IE 4 compatible
			//alert('IE 4 compatible');
		}
		if( typeof( window.pageYOffset ) == 'number' ) {
			windowScrollLeft = window.pageXOffset;//Netscape compliant
		} else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
			windowScrollLeft = document.body.scrollLeft;//DOM compliant
		} else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
			windowScrollLeft = document.documentElement.scrollLeft;//IE6 standards compliant mode
		}
		if(windowWidth <= 1060) {
			//var finalLeftOffset = windowWidth - (boxPanel.clientWidth + 30);
			boxPanel.style.right = null;
			boxPanel.style.left = '820px';
		} else if (windowWidth > 1060) {
			boxPanel.style.left = null;
			boxPanel.style.right = '30px';
		}
	}
}

function toggleKeyPoints() {
	$("h2.kp-toggler").each(function(i){
																								
		$(this).disableTextSelect();
		
		if($("span#kp-header").html() == "(hide)") {
			$("span#kp-header").html("(show)");
			$("div#kp-togglee").hide("slow");
			$("span#kp-toggler-icon").removeClass("up");
			$("span#kp-toggler-icon").addClass("down");
		} else {
			$("span#kp-header").html("(hide)");
			$("div#kp-togglee").show("slow");
			$("span#kp-toggler-icon").removeClass("down");
			$("span#kp-toggler-icon").addClass("up");
		}
		
		//$(this).children().html();
										
	});	
}

function setGlossaryHoverState() {
	
	$(".glossary-term").each(function(i){
		$(this).hover(function(){
							   
							   
			$("#glossary-panel dt").each(function() {
				$(this).hide();				  
			});
			$("#glossary-panel dd").each(function() {
				$(this).hide();				  
			});				  
							   
			var getPanelOffSetLeft = $(this).offset().left + $(this).width() + 1;	
			var getPanelOffSetTop = $(this).offset().top - 15;
			
			var theTerm = $(this).html();
			theTerm = theTerm.toLowerCase();
			$("#glossary-panel dt").each(function() {
				var tmpTerm = $(this).html();
				tmpTerm = tmpTerm.toLowerCase();
				if(theTerm !== tmpTerm) {
					$(this).hide();
					$(this).next().hide();
				} else {
					
					$("#glossary-holder").css({ left: getPanelOffSetLeft, top: getPanelOffSetTop });
					
					
					$("dl#glossary-panel").show();
					$("div#glossary-holder").show();
					$(this).show('fast');
					$(this).next().show('fast');
					
				}
			});
			   
		$(this).addClass("over");},function(){
		$(this).addClass("out");
			$("dl#glossary-panel").hide();
			$("div#glossary-holder").hide();
			$("#glossary-panel dt").each(function() {
				$(this).hide();				  
			});
			$("#glossary-panel dd").each(function() {
				$(this).hide();				  
			});
		});
	});
}

function getGlossaryTerm() {
	//alert('called getGlossaryTerm');
	$.ajax({ url: 'http://www.healthcube.com.au/includes/glossary_xml/', type: 'GET', dataType: 'xml', timeout: 30000, error: function(){
		//alert('Error loading XML document');
		},
		success: function(xml){
			$("dl#glossary-panel").empty();
			$(xml).find('title').each(function(){
			var title_text = $(this).text();
			
			var desc_text = $(this).next().text();
			//alert(desc_text);
			
			$('<dt></dt>').html(title_text).appendTo('dl#glossary-panel');
			$('<dd></dd>').html(desc_text).appendTo('dl#glossary-panel');
			});
			
			//alert( $("dl#glossary-panel").html());
			
			$("dl#glossary-panel dt").each(function() {
				var theTerm = $(this).html();
				setTheGlossaryTerm (theTerm);
			});
		}
	});

}

function setTheGlossaryTerm (theTerm) {
	
	//alert('theTerm: ' + theTerm);
		var re = new RegExp();
		re.compile('(' + theTerm + ')', 'gi');
		$("div#main p:not(.intro-text, .post-link-members, .post-link, .post-cat-com, .post-tags, .label)").each(function() {
			//if ($(this).is("h1")) return; 
			
			var thehtml = $(this).html();
			//alert(re);
			var newhtml = thehtml.replace(re, '<span class="glossary-term">$1</span>');
			//alert(newhtml);
			$(this).html(newhtml);
		});
		
		setGlossaryHoverState();
}

function setGlossaryPanelPos(panelOffSetLeft, panelOffSetTop) {
	$("#glossary-holder").css({ left: panelOffSetLeft, top: panelOffSetTop });
}
