﻿var _visibleContent = "Home";
var _currentPop = 1;
var _biosPositions = [1, 2, 3];
var _maxBios = 6;
var _maxVisibleBios = 3;
var _biosWidth = 103;
var _isInShow = false;
var _isIE = false;

var ieVersion = getInternetExplorerVersion();

if (ieVersion > 0)
{
    _isIE = true;
}

function urlAction(url)
{
	var anchor = url.attr("anchor");
	
	if (anchor == null)
	{
		return;
	}
	
	anchor = anchor.toLowerCase();
	
	var anchorPair = anchor.split('/');
	
	if (anchorPair.length < 2)
	{
		return;
	}
	
	switch (anchorPair[0])
	{
		case 'news':
			showNews(anchorPair[1]);
	}
}

$(document).ready(function() {
	urlAction(jQuery.url);

	$('a').click(function() {
		var targetUrl = jQuery.url.setUrl($(this).attr('href'));
		
		if (targetUrl.attr('host') == jQuery.url.attr('host'))
		{
			urlAction(targetUrl);
		}
	});
});

function getInternetExplorerVersion()
    // Returns the version of Internet Explorer or a -1
    // (indicating the use of another browser).
    {
      var rv = -1; // Return value assumes failure.
      if (navigator.appName == 'Microsoft Internet Explorer')
      {
        var ua = navigator.userAgent;
        var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
        if (re.exec(ua) != null)
          rv = parseFloat( RegExp.$1 );
      }
      return rv;
}

function showNews(article)
{
    if (_isInShow)
        return;
    
    var visibleContentId = "#content_" + _visibleContent;
    var contentId = "#content_News";
    var currentTabId = "navigationBar_" + _visibleContent + "_Current";
    var nextTabId = "navigationBar_News";
    
    $(visibleContentId).hide();
    $("a." + currentTabId).removeClass(currentTabId).addClass("navigationBar_" + _visibleContent);            
    
    _isInShow = true;
    
    $(contentId).show('fast');
    $("a." + nextTabId).removeClass(nextTabId).addClass(nextTabId + "_Current");
    
    $("#content_Bottom").hide();
    
    _visibleContent = "News";
    
    var expandId = "#newsListing_Expand_" + article;
    var contentId = "#newsListing_Content_" + article;    
    var separatorId = "#newsListing_Separator_" + article;
    
    if ($(expandId).hasClass("expandableListing_Expand"))
    {
        $(expandId).removeClass("expandableListing_Expand").addClass("expandableListing_Collapse");
		$(contentId).show('fast', function() { _isInShow = false; $.scrollTo("#newsListing_Expand_" + article, 800);});		
	} else
	{
		_isInShow = false; 
		$.scrollTo("#newsListing_Expand_" + article, 800);
	}
    $(separatorId).hide();    
}

function toggleItem(type, article)
{
    if (_isInShow)
        return;
        
    var expandId = "#" + type + "_Expand_" + article;
    var contentId = "#" + type + "_Content_" + article;    
    var separatorId = "#" + type + "_Separator_" + article;
    
    _isInShow = true;
    
    if ($(expandId).hasClass("expandableListing_Expand"))
    {
        $(expandId).removeClass("expandableListing_Expand").addClass("expandableListing_Collapse");
        $(contentId).show('slow', function() { _isInShow = false; });
        $(separatorId).hide();
    }
    else
    {
        $(contentId).hide('fast', function() { _isInShow = false; });
        $(expandId).removeClass("expandableListing_Collapse").addClass("expandableListing_Expand");
        $(separatorId).show();
    }
}

function showContent(content)
{
    if (_isInShow)
        return;
    
    var visibleContentId = "#content_" + _visibleContent;
    var contentId = "#content_" + content;
    var currentTabId = "navigationBar_" + _visibleContent + "_Current";
    var nextTabId = "navigationBar_" + content;                   
    
    $(visibleContentId).hide();
    $("a." + currentTabId).removeClass(currentTabId).addClass("navigationBar_" + _visibleContent);            
    
    _isInShow = true;
    
    $(contentId).show('slow', function() { _isInShow = false; });
    $("a." + nextTabId).removeClass(nextTabId).addClass(nextTabId + "_Current");
    
    if (content == "News" ||
        content == "Services")
    {
        $("#content_Bottom").hide();
    }
    else
    {
        $("#content_Bottom").show();
    }
    
    _visibleContent = content;
}

function scrollBios(offset)
{
    for (var i = 1; i <= _maxVisibleBios; i++)
    {
        $("a#bios_" + i).removeClass("bios_" + _biosPositions[i - 1]);
    }
    
    for (var i = 0; i < _maxVisibleBios; i++)
    {
        var newPosition = _biosPositions[i] + offset;
        
        if (newPosition > _maxBios)
        {
            newPosition = newPosition - _maxBios;
        }
        else if (newPosition < 1)
        {
            newPosition = _maxBios - newPosition;
        }
        
        _biosPositions[i] = newPosition;
    }
    
    for (var i = 1; i <= _maxVisibleBios; i++)
    {
        $("a#bios_" + i).addClass("bios_" + _biosPositions[i - 1]);
    }
}

function showTag(position)
{
    $("#bios_Tag_" + _biosPositions[position - 1]).show();
}

function hideTag(position)
{
    $("#bios_Tag_" + _biosPositions[position - 1]).hide();
}

function showBio(position)
{
    var width = _isIE ? '440px' : '470px';
    var height = _isIE ? '680px' : '710px';
    
    $("#AllianceBioSciences").hide();
    $("#OurProvenTeam").hide();
    
    $("#bioDialogContent_" + _currentPop).hide();
    $("#bioDialogPhoto").removeClass("bioDialogPhoto_" + _currentPop);
    
    _currentPop = _biosPositions[position - 1];
                
    $("#bioDialogContent_" + _currentPop).show();
    $("#bioDialogPhoto").addClass("bioDialogPhoto_" + _currentPop);

    $("#bioDialog").show().dialog(
        {
            modal: true, 
            width: width,
            height: height,
            overlay: {
                opacity: 0.6, 
                background: "#001e4a"
            },
            resizable: false,
            draggable: false,
            hide: "drop",
            show: "drop",
            close: function() { $("#AllianceBioSciences").show(); $("#OurProvenTeam").show(); }
        }
    );            
}               

function closeBio()
{
    $("#bioDialog").dialog("close");
}

function scrollPop(offset)
{
    $("#bioDialogContent_" + _currentPop).hide();
    $("#bioDialogPhoto").removeClass("bioDialogPhoto_" + _currentPop);
    
    _currentPop += offset;
    
    if (_currentPop > _maxBios)
    {
        _currentPop = 1;
    }
    else if (_currentPop < 1)
    {
        _currentPop = _maxBios;
    }
    
    $("#bioDialogContent_" + _currentPop).show();
    $("#bioDialogPhoto").addClass("bioDialogPhoto_" + _currentPop);
}

function sendEmail()
{
    var emailAddress = $("#emailText").val();
    
    if (!echeck(emailAddress))
        return;
       
    $.post("Contact.ashx", { email: emailAddress });
    $("#emailConfirm").fadeIn("slow");
}

function echeck(str) {

		var at="@";
		var dot=".";
		var lat=str.indexOf(at);
		var lstr=str.length;
		var ldot=str.indexOf(dot);
		
		var invalidMessage = "Invalid e-mail address specified.  Please correct and try again.";
		
		if (str.indexOf(at)==-1){
		   alert(invalidMessage);
		   return false;
		}

		if (str.indexOf(at)==-1 || str.indexOf(at)==0 || str.indexOf(at)==lstr){
		   alert(invalidMessage);
		   return false;
		}

		if (str.indexOf(dot)==-1 || str.indexOf(dot)==0 || str.indexOf(dot)==lstr){
		    alert(invalidMessage);
		   return false;
		}

		 if (str.indexOf(at,(lat+1))!=-1){
            alert(invalidMessage);
		    return false;		 
		  }

		 if (str.substring(lat-1,lat)==dot || str.substring(lat+1,lat+2)==dot){
		    alert(invalidMessage);
		   return false;
		 }

		 if (str.indexOf(dot,(lat+2))==-1){
		    alert(invalidMessage);
		   return false;
		 }
		
		 if (str.indexOf(" ")!=-1){
		    alert(invalidMessage);
		   return false;
		 }

 		 return true;				
	}

