function rollNavOn(el)
{

}


function submitEmail()
{
	if (isValidEmail(document.forms[0].emailAddress.value))
	{
		new Ajax.Request('register.php',
			{
				method: 'post',
				parameters: { emailAddress: document.forms[0].emailAddress.value },
				onSuccess: function(transport) {
					if (transport.responseText.indexOf('x') != -1)
						showEmailError();
					else
						showEmailSuccess();
				},
				onFailure: function() {
					showEmailError();
				}
			});
	}


	return false;
}

function isValidEmail(s)
{
	var ret = false;
	if (s == '')
		return ret;
	else
		ret = true;
	return ret;
}

function showEmailError()
{
	Effect.Appear('emailerror', { duration: 0.5, queue: 'end' });
	$('emailAddress').style.backgroundColor = '#feeec5';
}

function showEmailSuccess()
{
	Effect.Fade('emailform', { duration: 0.5, queue: 'end' });
	Effect.Appear('emailsuccess', { duration: 0.5, queue: 'end' });
}

function monitorEmailEntry(e)
{
    var key;
    if (window.event)
        key = window.event.keyCode;
    else
        key = e.which;

    if (key == 13)
    {
        submitEmail();
        return false;
    }
}

var recentDownloadsLink = 'utilbar-downloads';
var recentDownloadsPopup = 'utilbar-downloads-popup';
var recentDownloadsOpen = false;

function showRecentDownloads()
{
	if (recentDownloadsOpen)
		hideRecentDownloads();
	else
	{
		enableRecentDownloadsLink();
		$(recentDownloadsPopup).style.display = 'block';
		recentDownloadsOpen = true;
	}
}

function hideRecentDownloads()
{
	$(recentDownloadsPopup).style.display = 'none';
	recentDownloadsOpen = false;
	unhighlightRecentDownloadsLink();
}

function enableRecentDownloadsLink()
{
	changeClass($(recentDownloadsLink), 'utilbar-downloads-enabled');
}

function highlightRecentDownloadsLink()
{
	if (!recentDownloadsOpen)
		changeClass($(recentDownloadsLink), 'utilbar-downloads-active');
}

function unhighlightRecentDownloadsLink()
{
	if (!recentDownloadsOpen)
		changeClass($(recentDownloadsLink), 'utilbar-downloads-inactive');
}

function changeClass(el, toClass)
{
    el.setAttribute('class', toClass);
    el.setAttribute('className', toClass);
}

function adjustUtilBar()
{
	w = document.body.offsetWidth;
	if (w <= 1070)
	{
		$('utilbar').style.width = (w + 2) + 'px';
		$('utilbar').style.margin = '0px auto 0px auto';
		$('utilbar').style.borderLeft = '0px';
		$('utilbar').style.borderRight = '0px';

		$('utilbar-downloads-popup').style.width = w + 'px';
		$('utilbar-downloads-popup').style.margin = '0px auto 25px auto';
	}
	else
	{
		$('utilbar').style.width = '96%';
		$('utilbar').style.margin = '0px 2% 0px 2%';
		$('utilbar').style.borderLeft = '1px solid #0399D6';
		$('utilbar').style.borderRight = '1px solid #0399D6';

		$('utilbar-downloads-popup').style.width = '96%';
		$('utilbar-downloads-popup').style.margin = '0px 2% 25px 2%';
	}
}

function loadKBIGPanel()
{
	$('kbig-panel').appear({queue: 'end', duration: 0.5});
}

function loadKBIGNext()
{
	$('kbig-next').appear({queue: 'end', duration: 0.5});
}

function showKBIGContent(animfunc)
{
	loadKBIGPanel();
	if (animfunc != null)
		animfunc();
	loadKBIGNext();
}

function loadCCGPanel()
{
	$('ccg-panel').appear({queue: 'end', duration: 0.5});
}

function loadCCGNext()
{
	$('ccg-next').appear({queue: 'end', duration: 0.5});
}

function showCCGContent(animfunc)
{
	loadCCGPanel();
	if (animfunc != null)
		animfunc();
}

function loadSample()
{
	var qs = location.search.substring(1);
	if (qs != null && qs > 0)
		sampleid = qs;
	else
		sampleid = 1;

	$('sample-large').style.backgroundImage = 'url(/images/ccsample_' + sampleid + '_large.jpg)';
	$('sample-large').appear({queue: 'end'});
}

function showSample(sampleid)
{
	setTimeout('changeSample(' + sampleid + ')', 650);
	$('sample-large').fade({queue: 'end', duration: 0.5});

}

function changeSample(sampleid)
{
	$('sample-large').style.backgroundImage = 'url(/images/ccsample_' + sampleid + '_large.jpg)';
	$('sample-large').appear({queue: 'end', duration: 0.5});
}

var numSamples = 3;
var currentSample = 0;
function rotateSamples()
{
	showSample((currentSample++ % numSamples) + 1);
	setTimeout('rotateSamples()', 6000);
}

function downloadSample()
{

}

var rotateGalleryTimer = null;
function showGalleryItemManual(galleryitemid)
{
	if (!manualMode)
	{
		resetGalleryNav();
		manualMode = true;
		previousGalleryItem = 0;
	}
	else
		resetCurrentGalleryNav();
	
	clearTimeout(rotateGalleryTimer);
	currentGalleryItem = galleryitemid + 1;
	showGalleryItem(galleryitemid);
}

function resetGalleryNav()
{
	for (var i = 0; i < galleryItems.length; i++)
	{
		document.images['nav_' + galleryItems[i]].src = '/images/leftnav_' + galleryItems[i] + '_off.gif';
	}
}

function resetCurrentGalleryNav()
{
	document.images['nav_' + galleryItems[currentGalleryItem - 1]].src = '/images/leftnav_' + galleryItems[currentGalleryItem - 1] + '_off.gif';
}

function showGalleryItem(galleryitemid)
{
	setTimeout('changeGalleryItem(' + galleryitemid + ')', 650);
	$('ccg-panel').fade({queue: 'end', duration: 0.5});

}

function changeGalleryItem(galleryitemid)
{
	document.images['ccg-image'].src = '/images/gallery_' + galleryItems[galleryitemid] + '.png';

	if (galleryItems.length > 1)
	{
		if (!manualMode)
			document.images['nav_' + galleryItems[(previousGalleryItem - 1) % galleryItems.length]].src = '/images/leftnav_' + galleryItems[(previousGalleryItem - 1) % galleryItems.length] + '_off.gif';

		document.images['nav_' + galleryItems[galleryitemid]].src = '/images/leftnav_' + galleryItems[galleryitemid] + '_on.gif';
		document.images['nav_' + galleryItems[galleryitemid]].onmouseout = function () {};
		document.images['nav_' + galleryItems[galleryitemid]].onmouseover = function () {};
	}

	$('ccg-panel').appear({queue: 'end', duration: 0.5});
}

var currentGalleryItem = 1;
var previousGalleryItem = 0;
var manualMode = false;

function rotateGalleryItems()
{
	previousGalleryItem = currentGalleryItem;
	showGalleryItem((currentGalleryItem++ % galleryItems.length));
	rotateGalleryTimer = setTimeout('rotateGalleryItems()', 6000);
}

function initRotateGalleryItems()
{
	rotateGalleryTimer = setTimeout('rotateGalleryItems()', 5000);

	if (galleryItems.length > 1)
	{
		document.images['nav_' + pageid].src = '/images/leftnav_' + pageid + '_off.gif';
		document.images['nav_' + galleryItems[0]].src = '/images/leftnav_' + galleryItems[0] + '_on.gif';
	}
}

function displayStaticMessage(messageContent,cssClass)
{
	messageObj.setHtmlContent(messageContent);
	var h = 180;
	if (cssClass.indexOf(',') != -1)
	{
		h = 195;
		cssClass = cssClass.split(',')[0];
	}

	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7)
		messageObj.setSize(400, h);
	else
		messageObj.setSize(400, h);
	messageObj.setCssClassMessageBox(cssClass);
	messageObj.setSource(false);	// no html source since we want to use a static message here.
	messageObj.setShadowDivVisible(false);	// Disable shadow for these boxes	
	messageObj.display();
}

function displayRegionDisclaimer(messageContent, adjust)
{
	if (adjust)
		h = 195;
	else
		h = 180;
	messageObj.setHtmlContent(messageContent);
	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7)
		messageObj.setSize(432,h);
	else
		messageObj.setSize(400,h);
	messageObj.setCssClassMessageBox('popup-default');
	messageObj.setSource(false);	// no html source since we want to use a static message here.
	messageObj.setShadowDivVisible(false);	// Disable shadow for these boxes
	messageObj.display();
}

function closeMessage()
{
	messageObj.close();	
}


function externalSiteWarning(url)
{
	if (!showextwarnings)
	{
		pageTracker._trackPageview('/outbound/' + url);
		window.open(url);
		return false;
	}
	else
	{
		displayStaticMessage('<form name="extwarningform" id="extwarningform"><div class="header">Warning: Leaving HemophiliaSource</div><div class="body">You are leaving HemophiliaSource and going to an external website which is not affiliated with HemophiliaSource or Bayer. Bayer provides these links as a service and does not endorse or accept responsibility or liability for any information presented on external websites.<div style="margin: 6px 0px 6px -4px"><table cellpadding="0" cellspacing="0" border="0"><tr><td><input type="checkbox" name="hideextwarning" id="hideextwarning"/></td><td>&nbsp;Do not show this message again during this visit</td></tr></table></div><div><a class="cancelbutton" href="javascript:closeExternalSiteWarning()">Cancel</a> &nbsp; <a class="button" href="javascript:pageTracker._trackPageview(\'/outbound/\' + \'' + url + '\'); window.open(\'' + url + '\'); closeExternalSiteWarning()">Continue</a></div></div></form>', 'popup-default');
		return false;
	}
}

function closeExternalSiteWarning()
{
	if (document.extwarningform.hideextwarning.checked)
	{
		jQuery.get('/util/hide-external-warnings.php', null, function () {
			showextwarnings = false;
			closeMessage();
		});
	}
	else
	{
		closeMessage();
	}
}

function calendarWarning(url)
{
	if (!showcalwarnings)
	{
		return true;
	}
	else
	{
		displayStaticMessage('<form name="calwarningform" id="calwarningform"><div class="header">Note: Adding an Event to Your Calendar</div><div class="body">This action may behave differently based on your system\'s default calendar program. If you use Microsoft Outlook, clicking Continue below will open a new Outlook calendar entry. If you use Lotus Notes, you will be prompted to save the file, after which you may drag and drop it into Lotus Notes to add it to your calendar.<div style="margin: 6px 0px 6px -4px"><table cellpadding="0" cellspacing="0" border="0"><tr><td><input type="checkbox" name="hidecalwarning" id="hidecalwarning"/></td><td>&nbsp;Do not show this message again during this visit</td></tr></table></div><div><a class="cancelbutton" href="javascript:closeCalendarWarning()">Cancel</a> &nbsp; <a class="button" href="javascript:window.open(\'' + url + '\'); closeCalendarWarning()">Continue</a></div></div></form>', 'popup-default,calwarn');
		return false;
	}
}

function closeCalendarWarning()
{
	if (document.calwarningform.hidecalwarning.checked)
	{
		jQuery.get('/util/hide-calendar-warnings.php', null, function () {
			showcalwarnings = false;
			closeMessage();
		});
	}
	else
	{
		closeMessage();
	}
}

function showSiteTour()
{
	messageObj.setHtmlContent('<div id="tourcontent"></div>');
	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7)
		messageObj.setSize(978,760);
	else
		messageObj.setSize(978,760);
	messageObj.setCssClassMessageBox(false);
	messageObj.setSource(false);
	messageObj.setShadowDivVisible(false);
	messageObj.display();
	swfobject.embedSWF("/tour/hsinfo_tour.swf", "tourcontent", "978", "760", "9.0.0", "", "", { base: "/tour/", wmode: "transparent" }, "");
}

var sortselected = null;
var sortcontrollabel = 'View by ';
var sortcontrolprefix = 'sort-control-';

function initHSInfo()
{
	messageObj = new DHTML_modalMessage();
	messageObj.setShadowOffset(0);

	BrowserDetect.init();

	$(document).ready(function() {
		$('.nav-primary-item').mouseover(function() {
			$(this).css('background-color', '#6da73c').css('cursor', 'pointer');
		}).mouseout(function() {
			$(this).css('background-color', '#609F2A').css('cursor', 'default');
		}).click(function() {
			location.href = $(this).children()[0];
		});

		$('.nav-secondary-item').mouseover(function() {
			$(this).toggleClass('nav-secondary-item-hover').css('cursor', 'pointer');
		}).mouseout(function() {
			$(this).toggleClass('nav-secondary-item-hover').css('cursor', 'default');
		}).click(function() {
			location.href = $(this).children()[0];
		});

		sortselected = $('.sort-control:first').attr('id');

		// Sort control init
		$('.sort-control').prepend(sortcontrollabel);
		$('.sort-control').mouseover(function() {
			if (this.id != sortselected)
				$(this).toggleClass('sort-control-selected').css('cursor', 'pointer');
		}).mouseout(function() {
			if (this.id != sortselected)
				$(this).toggleClass('sort-control-selected').css('cursor', 'default');
		}).click(function() {
			if (sortselected != this.id)
			{
				$('#' + sortselected).toggleClass('sort-control-selected');
				sortselected = this.id;
				sortContents(this.id);
			}
		});

		$('.sort-control:first').toggleClass('sort-control-selected');

		$('.external').click(function() {
			return externalSiteWarning(this);
		});

		$('.calendaradd').click(function() {
			return calendarWarning(this);
		});
	});

	jQuery.fn.sort = function() {  
		return this.pushStack( [].sort.apply( this, arguments ), []);  
	};
}

    	var mgopen = false;
    	var featurect = 0;
    	featureidx = 0;
	var cfeature = 0;
    	var featurew = 567;
    	var mult = 0;
   	var scrolltimeout;
	var scrollspeed = 7; // Seconds

    	$(document).ready(function() {
        	$('.home-feature').each(function() {
            		if (featurect == 0)
                		$(this).css('left', '0px');
			featurect++;

			$(this).click(function() {
				eval($(this).attr('act'));
			});

			$(this).mouseover(function() {
				$(this).css('cursor', 'pointer');
			});

			$(this).mouseout(function() {
				$(this).css('cursor', 'default');
			});

			var i = ($(this).attr('id').split('-')).pop();
       		    	$('#home-feature-indicator').append('<div class="home-feature-menu-item">' + i + '</div>');
		});

		feature = 0;
		$('.home-feature-menu-item').each(function() {
			$(this).attr('featureid', feature++);
		});

		adjust = Math.floor((567-(featurect*27-10)) / 2);
		$('#home-feature-indicator').css('width', (567-adjust) + 'px');
		$('#home-feature-indicator').css('padding-left', adjust + 'px');
		$('.home-feature-menu-item:first').addClass('home-feature-menu-item-selected');

    		reacButtons();
    		scrolltimeout = setTimeout('showFeature(1,true)', scrollspeed*1000);
        });


    function showFeature(x, auto) {
        clearTimeout(scrolltimeout);
        deacButtons();

        featureidx += x;

       mult += x;
        if (featureidx < 0)
            featureidx = featurect - 1;
        featureidx = featureidx % featurect;

         // Sliding right
        if (x < 0) {
            $('.home-feature:eq(' + featureidx + ')').css('left', (featurew*mult) + 'px');
            $('#feature-slider').animate({ left: (parseInt($('#feature-slider').css('left')) + featurew) + 'px' }, 500, 'linear', function() {reacButtons()});
        }
        // Sliding left
        else {
            $('.home-feature:eq(' + featureidx + ')').css('left', (featurew*mult) + 'px');
            $('#feature-slider').animate({ left: (parseInt($('#feature-slider').css('left')) - featurew) + 'px' }, 500, 'linear', function() { reacButtons() });
        }

 	$('.home-feature-menu-item').each(function() {
		if ($(this).attr('featureid') != featureidx)
			$(this).removeClass('home-feature-menu-item-selected');
	});			

	$('.home-feature-menu-item:eq(' + featureidx + ')').addClass('home-feature-menu-item-selected');

        if (auto)
            scrolltimeout = setTimeout('showFeature(1,true)', scrollspeed*1000);
        return false;
    }

    function deacButtons() {
        $('.home-feature-scroll-button a').unbind('click');
        $('.home-feature-scroll-button a').click(function() {
            return false;
        });
    }

    function reacButtons() {
        $('#home-feature-scroll-button-left a').click(function() {
            return showFeature(-1, false);
        });

        $('#home-feature-scroll-button-right a').click(function() {
            return showFeature(1, false);
        });
    }


function sortContents(el)
{
	$(el).toggleClass('sort-control-selected');
	var sortType = el.substr(sortcontrolprefix.length);
	$('#results').children().sort(eval('sort' + sortType)).appendTo('#results');
}

function displayWFHVideo()
{

	messageObj.setHtmlContent('<div id="videocontentclose" style="width:924px;height:27px;text-align:right"><a href="javascript:closeMessage()"><img src="/images/close.png" width="65" height="27" style="border:0px"/></a></div><div id="videocontent"></div><div id="videocontentfooter" style="height:30px;background-color:#BAEBA9"></div>');
	if (BrowserDetect.browser == 'Explorer' && BrowserDetect.version < 7)
		messageObj.setSize(924, 681);
	else
		messageObj.setSize(924, 681);

	messageObj.setCssClassMessageBox(false);
	messageObj.setSource(false);
	messageObj.setShadowDivVisible(false);
	messageObj.display();
    $('#videocontent').html('<div style="height:300px">&nbsp;</div>You must have Adobe&reg; Flash&reg; installed to view this content. Please download and install <a href="http://get.adobe.com/flashplayer" class="external" onclick="return externalSiteWarning(this.href)">Adobe&reg; Flash&reg; Player</a>.');
	swfobject.embedSWF("/video/videos.swf", "videocontent", "924", "624", "9.0.0", "", "", { base: "/video/", wmode: "opaque" }, "");
}

var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

