//for carousel functions
		//I've renamed the global variables to include the bc_crsl_ prefix to make sure there wont be any conflict
var bc_crsl_depth = 100;
var bc_crsl_i = 0;
var bc_crsl_y = 1;
var bc_crsl_currNews = 0;
var scrollInterval;

//for loading and parsing xml functions
var bc_crsl_curNewsStories = 0;
var bc_crsl_xmlhttp;

$(document).ready(function()
{
	
	//loading the xml
	myHTMLOutput = '';
	navHTMLOutput = '';
	bc_crsl_xmlhttp=null;
	
	if(window.XMLHttpRequest)
	{
		// code for IE7, Firefox, Opera, etc.
		bc_crsl_xmlhttp=new XMLHttpRequest();
	}
	else if(window.ActiveXObject)
	{
		// code for IE6, IE5
		bc_crsl_xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	if(bc_crsl_xmlhttp!=null)
	{
		bc_crsl_xmlhttp.onreadystatechange=state_Change;
		bc_crsl_xmlhttp.open("GET",bc_crsl_xmlurl,true);
		bc_crsl_xmlhttp.send(null);
	}
	else
	{
		//alert("Your browser does not support XMLHTTP.");
	}
});

function state_Change()
{
	// 4 = "loaded"
	if (bc_crsl_xmlhttp.readyState==4)
	{
		// 200 = "OK"
		if (bc_crsl_xmlhttp.status==200)
		{
			$(bc_crsl_xmlhttp.responseXML).find("tease").each(function(i)
			{
				if(bc_crsl_curNewsStories < bc_crsl_maxNewsStories)
				{
					imgURL = $(this).attr("image");
					linkURL = $(this).find("storyURL").text();
					linkTarget = $(this).find("storyURL").attr("target");
					titleOne = $(this).find("photoTitle").text();
					descriptionLong = $(this).find("descriptionPhoto").text();
					photocredit = $(this).find("photocredit").text();
					studentPost = $(this).find("name").attr("post");

					//nav
					titleTwo = $(this).find("title").text();
					descriptionShort = $(this).find("descriptionShort").text();
					mydata = buildHTML(imgURL,linkURL,linkTarget,titleOne,photocredit,studentPost);
					mynav = buildNav(titleTwo);

					myHTMLOutput = myHTMLOutput + mydata;
					navHTMLOutput = navHTMLOutput + mynav;
				}
				bc_crsl_curNewsStories++;
			});
		}
		else
		{
			//alert("Problem retrieving XML data:" + bc_crsl_xmlhttp.statusText);
		}
		$("#bc_crsl_newsnav").append(navHTMLOutput);
		$("#bc_crsl_news").prepend(myHTMLOutput);
		$("#bc_crsl_news").append('<div class="bc_crsl_newsLoaded" style="visibility:hidden;"><script>startLoad();</script></div>');
	}
}

function startLoad()
{
	$("image[name='imageClass1']").ready(function()
	{
	$("#bc_crsl_news").css({'display':'block'});
	addMouseDownListeners();
	autoScroll();
	});
}

/*******************on auto scroll*********************************/
function autoScroll()
{
	var storiesNum = $('.bc_crsl_mover').length;
	for(x=1;x<storiesNum+1;x++)
	{
		bc_crsl_depth--;
		$('.bc_crsl_mover'+x).css({'z-index': bc_crsl_depth});
	}

	//makes the first story active
	if($.browser.msie && $.browser.version < 7)
			{
				$('#bc_crsl_news').find('*').css({'backgroundImage':'none'});
			}
			$('.bc_crsl_newsnav').addClass("bc_crsl_inactivenews");
	$(".bc_crsl_newsnav"+1).addClass("bc_crsl_activenews");
	
	
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });

	scrollInterval = window.setInterval(
		function()
		{
			onAutoscroll(storiesNum);
		}
		,bc_crsl_newsSwitchInterval
	);
}

function onAutoscroll(storiesNum)
{
	//scrolls through the image
	if(bc_crsl_i<storiesNum-1)
	{
		bc_crsl_i++;
	}
	else
	{
		$('.bc_crsl_mover1').fadeIn();
		bc_crsl_i=0;
		window.setTimeout(
			function()
			{
				reShow(storiesNum)
			}
			,350
		);
	}
	
	//scrolls the blue arrow
	if(bc_crsl_y<storiesNum)
	{
		bc_crsl_y++;
	}
	else
	{
		bc_crsl_y=1;
		for(z=1;z<storiesNum+1;z++)
		{
			if($.browser.msie && $.browser.version < 7)
			{
				$('#bc_crsl_news').find('*').css({'backgroundImage':'none'});
			}
			$('.bc_crsl_newsnav').removeClass("bc_crsl_activenews");
			$('.bc_crsl_newsnav'+z).addClass("bc_crsl_inactivenews");
			
			
			
			
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });
		}
	}

	$('#bc_crsl_news').children('.bc_crsl_mover'+bc_crsl_i).fadeOut(300);
	var minusOne = bc_crsl_y-1;
	$('.bc_crsl_newsnav'+minusOne).removeClass("bc_crsl_activenews");
	
	
	
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });
	$('.bc_crsl_newsnav'+bc_crsl_y).addClass("bc_crsl_activenews");
	

	
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });
		
}

// brings back the images once the widget has scrolled through them all
function reShow(storiesNum)
{
	for(x=2;x<storiesNum;x++)
	{
		$('.bc_crsl_mover'+x).fadeIn();
	}
}
// end on auto scroll

/******************************on stop scroll *************************/
function addMouseDownListeners()
{
	var storiesNum = $('.bc_crsl_mover').length;
	for(eachNav=1;eachNav<storiesNum+1;eachNav++)
	{
		$('.bc_crsl_newsnav'+eachNav).mouseover(
			function()
			{
				var thisNum = parseInt($(this).attr('class').substr(15,1));
				onStopscroll(thisNum,storiesNum,$(this));
			}
		);
	}
}

function onStopscroll(thisNum,storiesNum, pressedClass)
{
	bc_crsl_i = thisNum - 1;
	bc_crsl_y = thisNum;
	endInterval();

	if(pressedClass.hasClass("bc_crsl_activenews")==false)
	{
		endInterval()
		for(z=1;z<=storiesNum;z++)
		{
			$('.bc_crsl_newsnav'+z).removeClass("bc_crsl_activenews");
			$('.bc_crsl_newsnav'+z).addClass("bc_crsl_inactivenews");
			
			
			
			
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });
		}
		//hides all the images above the current image, so you can see the right image
		for(m=1;m<thisNum;m++)
		{
			$('.bc_crsl_mover'+m).fadeOut();
		}
		$('.bc_crsl_mover'+thisNum).fadeIn();
		pressedClass.addClass("bc_crsl_activenews");
		pressedClass.removeClass("bc_crsl_inactivenews");
		
		
		
	
	$('#bc_crsl_news').find(".bc_crsl_inactivenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg_inactive.png')" });$('#bc_crsl_news').find(".bc_crsl_activenews").css({"filter":"progid:DXImageTransform.Microsoft.AlphaImageLoader(src='../../images/carousel/newsnavbg.png')" });
	}
}

function endInterval()
{
	window.clearInterval(scrollInterval);
}

//output story
function buildHTML(imgURL,linkURL,linkTarget,titleOne,photocredit,studentSE)
{
	output = '';
	output += '<div class="bc_crsl_mover'+parseInt(bc_crsl_curNewsStories+1)+' bc_crsl_mover">';
	output += '<a href="' + linkURL + '" target="' + linkTarget + '"><img name="imageClass'+parseInt(bc_crsl_curNewsStories+1)+'" src="' + imgURL + '" alt="Photo Credit: ' + photocredit + '" /></a>';
	output += '<h3><a href="' + linkURL + '" target="' + linkTarget + '">' + titleOne + '</a></h3>';
	output += '<p>' + descriptionLong + '</p></div>';
	return output;
}

//output nav
function buildNav(titleTwo)
{
	navOutput ='';
	if (bc_crsl_curNewsStories == 0)
	    navOutput += '<li class="bc_crsl_newsnav'+parseInt(bc_crsl_curNewsStories+1)+' bc_crsl_newsnav bc_crsl_activenews" onclick="location=\'' + linkURL + '\'"><h4>' + titleTwo + '</h4>';
	else	
	    navOutput += '<li class="bc_crsl_newsnav'+parseInt(bc_crsl_curNewsStories+1)+' bc_crsl_newsnav bc_crsl_inactivenews" onclick="location=\'' + linkURL + '\'"><h4>' + titleTwo + '</h4>';
	navOutput +='<p>' + descriptionShort + '</p></li>';
	return navOutput;
}
