﻿var _visibleContent = "Home";
var _currentPop = 1;
var _biosPositions = [1, 2, 3];
var _maxBios = 7;
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]);
			var el = $('.news-item-expander:eq('+anchorPair[1]+')').parents('.news-item').get(0);

			setTimeout(
				function(){
					$.scrollTo('#'+el.id, 800);
				},
				500
			);
			break;
		case 'services' :
			showService(anchorPair[1]);
			var w = parseInt(anchorPair[1],10);

			var el = $('#content_Services a[id^=serviceListing]:eq('+w+')').parents('table').get(0);

			if( el.id ){
				id = el.id;
			}
			else{
				var d = new Date();
				var m = d.getMilliseconds();
				var s = d.getSeconds();
				id = "id"+s+""+m+Math.round(Math.random()*9999)+Math.round(Math.random()*9999);
				
				$(el).attr('id', id );
			}

			setTimeout(
				function(){
					$.scrollTo('#'+id, 800);
				},
				500
			);
		
	}
}

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 showService ( article){
	
    if (_isInShow)
        return;
    
    var visibleContentId = "#content_" + _visibleContent;
    var contentId = "#content_Services";
    var currentTabId = "navigationBar_" + _visibleContent + "_Current";
    var nextTabId = "navigationBar_Services";

    
    $(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 = "Services";
	
	var id = $('#content_Services .expandableListing_Expand:eq('+article+')').get(0).id.split('_').pop();

	_isInShow = false; 
	toggleItem('serviceListing',id);
}

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";

	$('.news-item-expander:eq('+article+')').trigger('click');
    

    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;				
	}

$(
	function(){
		$(".btn_hideshow")
		.click(
			function(e){
				e.preventDefault();
				var el = $(this),
					ni = el.parents('.news-item'),
					ic = ni.find('.news-item-content');

				if( ni.hasClass('opened') ){
					ic.slideUp();
					ni.removeClass('opened');
				}
				else{
					ic.slideDown(function(){
						setTimeout(
							function(){
							if( ic.is(':hidden') ){
								ic.show();
							}
							},500
						);

					});
					ni.addClass('opened');
				}
			}
		);
	}
);



$(document).ready(function() {
	
    jQuery('#carousel').jcarousel({
    	wrap: 'circular',
		itemFallbackDimension: 103,
		size : 3,
		scroll : 1
    });

	//$('#content_Team').css({'position':null,'left':null});

	urlAction(jQuery.url);

	$('a').click(function() {
		var targetUrl = jQuery.url.setUrl($(this).attr('href'));
		
		if (targetUrl.attr('host') == jQuery.url.attr('host'))
		{
			urlAction(targetUrl);
		}
	});
});
