//Constituent Homepage Background Cross-fading
var homebg = ["bg1", "bg2", "bg3"];
var fader;
var current = 0;
function xfade(){
	var next = current + 1;
	if(next == homebg.length){
		next = 0;
	}
	fader.attr("class",homebg[next]);
	fader.fadeIn(1500, function(){
		$('body').removeClass(homebg[current]);
		current = next;
		$('body').addClass(homebg[current]);
		fader.hide();
		setTimeout("xfade()",10000);
	});
}


$(document).ready(
	function() {

	  //Constituent Homepage Background Cross-fading
	  //Turned off for now...
	  //$("body.home").addClass('bg1');
	  //$("body.home .container").prepend('<div id="homefader"></div>');
	  //fader = $("#homefader");
	  //setTimeout("xfade()",10000);

	  // Instead...we'll just do a random number between 1 & 3...
	  var ranNum = Math.floor(Math.random() * 3) + 1;
	  $("body.home .container").addClass('bg' + ranNum);



	  //Portal Switcher
	  $('.portalbox:not(#Home)').hide();
	  $('#portalnav li a').click(function() {
	    $('.portalbox').hide();
	    if (!$(this).hasClass('active')) {
	      $('#portalnav li a').removeClass('active');
	      var currentSlide = $(this).attr('href');
	      $(this).addClass('active');
	    } else {
	      $('#portalnav li a').removeClass('active');
	      var currentSlide = "#Home";
	    }
	    var pbimgsrc = 'images/pbimg-' + currentSlide.toLowerCase().replace("#", "") + '.jpg'
	    $('#pbimg').attr('src', pbimgsrc);
	    $(currentSlide).fadeIn('slow');
	    return false;
	  });

	  // Animated mainnav backgrounds - uses jquery.bgpos plugin.
	  $('#header ul:not(.breadcrumbs) li a')
			.css({ backgroundPosition: "0px -40px" })
			.mouseover(function() {
			  $(this).stop().animate({ backgroundPosition: "(0px 0px)" }, { duration: 300 })
			})
			.mouseout(function() {
			  $(this).stop().animate({ backgroundPosition: "(0px -40px)" }, { duration: 200, complete: function() {
			    $(this).css({ backgroundPosition: "0px -40px" });
			  }
			  })
			});


	  // Open links with rel="external" in new window - like ye ole' target="_blank"
	  $('a[rel="external"]').click(function() { window.open($(this).attr('href')); return false; });

	  // Wrap internal-page images with a caption block if they start with "caption:"
	  $("#content img").each(function() {
	    var imgalt = $(this).attr('alt');
	    var captionsplit = imgalt.split(":");
	    if (jQuery.trim(captionsplit[0].toLowerCase()) == "caption") {
	      $(this).wrap('<div class="captionbox"></div>').after('<p>' + jQuery.trim(captionsplit[1]) + '</p>');
	    }
	  });



	  // Zebra-stripe data tables
	  $("table tbody").each(function() {
	    $(this).removeClass("odd even");
	    $("tr:odd", this).addClass("odd");
	    $("tr:even", this).addClass("even");
	  });

	  // Toggler used mainly for contact page to show and 
	  // hide correct contact info.
	  $('a.toggler').click(function() {
	    if (!$(this).hasClass('active')) {
	      $('ul.togglelist div.togglebox').slideUp();
	      $('a.toggler').removeClass('active');
	      $(this).next('div.togglebox').slideDown();
	      $(this).addClass('active');
	    } else {
	      $('ul.togglelist div.togglebox').slideUp();
	      $('a.toggler').removeClass('active');
	    }
	    return false;
	  });

	  // Searchbox show/hide.
	  //$("#header").append('<div id="searchbox"><form id="searchform" method="get" action="/search.aspx"><input type="text" name="query" class="textbox" /><a id="searchbutton" href="#">Search</a><br /><a id="closesearch" href="#">Close</a></form></div>');

	  InitSearchLink();

	  // "Usable" Popup
	  $('a[rel="popup"]').click(function() {
	    window.open($(this).attr('href'), "popup", 'toolbar=0,scrollbars=1,location=0,statusbar=0,menubar=0,resizable=1,width=750,height=550');
	    return false;
	  });

	  // "Fullscreen + location bar" Popup
	  $('a[rel="fullpopup"]').click(function() {
	    window.open($(this).attr('href'), "popup", 'toolbar=1,scrollbars=1,location=1,statusbar=1,menubar=1,resizable=1,statusbar=1');
	    return false;
	  });

	  // MBM Link
	  $('a[href="https://enrollment.bcbssc.com/enroll/Enroll.Logon.jsp"]').click(function() {
	    window.open("https://enrollment.bcbssc.com/enroll/Enroll.Logon.jsp", "mbm", 'width=775,height=550,resizable=no,scrollbars=yes,status=yes');
	    return false;
	  });

	  $('a[category]').click(function() {
	    if (pageTracker) {
	      pageTracker._trackEvent($(this).attr('category'), $(this).attr('event'));
	    }

	    return true;
	  });

	}
);


	//BLUE CROSS POPUP
	function launchSite(site, bIsMaximized) {
	    var iStartX = 0;
	    var iStartY = 0;
	    var iWinWidth = 0;
	    var iWinHeight = 0;
	    if (bIsMaximized == true) {
	        iWinWidth = screen.availWidth - 10;
	        iWinHeight = screen.availHeight - 48;
	        if ((navigator.appName.indexOf("Netscape") >= 0) && (navigator.appVersion.indexOf("5") >= 0)) {
	            iWinWidth = screen.availWidth - 6;
	            iWinHeight = screen.availHeight - 41;
	        }
	    }
	    else {
	        iStartX = parseInt((screen.availWidth - 691) / 2);
	        iStartY = parseInt((screen.availHeight - 500) / 2);
	        iWinWidth = 691;
	        iWinHeight = 500;
	    }
	    if (iStartX < 0) iStartX = 0;
	    if (iStartX < 0) iStartX = 0;
	    if (navigator.appName.indexOf("Microsoft") != -1) {
	        thewindow = window.open(site, "thewindow", "width=" + iWinWidth + ",height= " + iWinHeight + ",resizable=no,scrollbars=yes,status=yes,left=" + iStartX + ",top=" + iStartY);
	    }
	    else {
	        thewindow = window.open(site, "thewindow", "width=" + iWinWidth + ",height= " + iWinHeight + ",resizable=no,scrollbars=yes,status=yes,screenX=" + iStartX + ",screenY=" + iStartY);
	    }
	  }
	
	
	//BLUE CROSS POPUP
	function launchSiteWithBars(site, bIsMaximized) {
	    var iStartX = 0;
	    var iStartY = 0;
	    var iWinWidth = 0;
	    var iWinHeight = 0;
	    if (bIsMaximized == true) {
	        iWinWidth = screen.availWidth - 10;
	        iWinHeight = screen.availHeight - 150;
	        if ((navigator.appName.indexOf("Netscape") >= 0) && (navigator.appVersion.indexOf("5") >= 0)) {
	            iWinWidth = screen.availWidth - 6;
	            iWinHeight = screen.availHeight - 41;
	        }
	    }
	    else {
	        iStartX = parseInt((screen.availWidth - 691) / 2);
	        iStartY = parseInt((screen.availHeight - 500) / 2);
	        iWinWidth = 691;
	        iWinHeight = 500;
	    }
	    if (iStartX < 0) iStartX = 0;
	    if (iStartX < 0) iStartX = 0;
	    if (navigator.appName.indexOf("Microsoft") != -1) {
	        thewindow = window.open(site, "thewindow", "width=" + iWinWidth + ",height= " + iWinHeight + ",resizable=no,scrollbars=yes,toolbar=yes,location=yes,menubar=yes,status=yes,left=" + iStartX + ",top=" + iStartY);
	    }
	    else {
	        thewindow = window.open(site, "thewindow", "width=" + iWinWidth + ",height= " + iWinHeight + ",resizable=no,scrollbars=yes,toolbar=yes,location=yes,menubar=yes,status=yes,screenX=" + iStartX + ",screenY=" + iStartY);
	    }
	  }
	

function InitSearchLink() {
  $("#search a").click(function() {
    $("#search a").unbind('click');

    $("#searchbutton").unbind('click');
    $("#searchbutton").click(function() {
      $('#searchform').submit();
      return false;
    });

    $("#searchbox").slideDown();

    $("#closesearch").click(function() {
      $("#closesearch").unbind('click');
      $("#searchbox").slideUp();
      InitSearchLink();
      return false;
    });

    return false;
  });
}

