var interval;
var rows = 0;
var currentRow = 0;
var count = 2;
var thumbsVisible = false;
var infoStartPos;
var desc1Start;
var infoRange = 400;
var infoPos = 0;
var infoPositions;

$(document).ready(
	function()
	{
		var ie6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
		//find out how many 'rows' of thumbnails exist
		rows = Math.ceil($('#proj-thumbs-scroll').children().length / 5);
		//set the content to not display oddly in IE6 and Safari
		$('#content').css({overflow:'hidden'});
		//make the thumbnails scrollable
		$('#proj-thumbs').css({position:'relative',overflow:'hidden',width:"905px",height:"100px",display:'none'});
		$('#proj-thumbs-scroll').css({position:'relative',width:(rows *  905) + "px",height:"85px"});
		//$('#proj-thumbs-nav').css({position:'relative'});
		$('#proj-thumbs-container').css({position:'relative'});
		//hide the thumbnails
		$('#proj-thumbs-container').css({top:"-100px"});
		$('#proj-thumbs').css({position:'absolute'});
		//activate the thumb link
		$('#toggle-thumbs').click(toggleThumbs);
		//activate the thumb scrollers
		if(rows <= 1)
		{
			$('.scroll-right').hide();
		}
		$('.scroll-left').hide();
		$('.scroll-left').click(scrollThumbsRight);
		$('.scroll-right').click(scrollThumbsLeft);
		//make all the likns to thumbs not require a page refresh
		$('.thumb-link').click(onThumbClick);
		//link the prev and next asset buttons
		$('.next-asset').click(loadNextAsset);
		$('.prev-asset').click(loadPrevAsset);
		//add controls to move the text up and down
		$('.info-up').click(moveInfoUp);
		$('.info-down').click(moveInfoDown);
		$('body').mousedown(function(e){if(thumbsVisible && $(e.target).parent().children('#toggle-thumbs').length == 0 && !$(e.target).parent().hasClass('scroll-right') && !$(e.target).parent().hasClass('scroll-left') ){toggleThumbs();}});
		//set up the 'mask' for the info
		$('#news-info-container').css({top:"-431px",overflow:'hidden',width:'650px',height:'431px',marginLeft:'0px'});
		$('#news-info').css({marginTop:"401px",marginLeft:'10px'});
		$('#news-info-bg').css({marginTop:"401px",marginLeft:'0px'});
		//IE 6 fix
		if(ie6)
		{
			
		}
		$("#content").show();//we need to show to calculate height
		//find the starting position
		infoStartPos = Number($('#news-info').css('top').split("px").join(""));
		desc1Start = 10 + $('#desc1').height();
		infoPositions = [0, -desc1Start, -infoRange];
		//set the default position
		moveInfoUp();
		//find the actual height of the project text, if it requires, allow for scrollin
		var objHeight;
		var orgH = Number($('#news-info-content').css('height').split("px").join(""));
		$('#news-info-content').css({overflow:'',height:'auto'});
		objHeight = $('#news-info-content').height();
		$('#news-info-content').css({overflow:'hidden',height:orgH});
		if(objHeight > orgH)
		{
			$("#news-info-content").css({overflow:'hidden'});
			$(".scrollUp").mousedown(function(){moveUpStart();});
			$(".scrollUp").mouseup(function(){moveUpStop();});
			$(".scrollDown").mousedown(function(){moveDownStart();});
			$(".scrollDown").mouseup(function(){moveDownStop();});
		}
		else
		{
			$("#news-scrollers").hide();
		}
		//
		$('#news-headlines-container').css({overflow:'hidden'});
		var objHeight2;
		var orgH2 = Number($('#news-headlines-container').css('height').split("px").join("")) - $('#news-years').height() - 3;
		$('#nnews-headlines-container').css({overflow:'',height:'auto'});
		objHeight2 = $('#news-headlines-container').height();
		$('#news-headlines-container').css({overflow:'hidden',height:orgH2});
		if(objHeight2 > orgH2 - $('#news-years').height())
		{
			$("#news-headlines-container").css({overflow:'hidden'});
			$(".scrollUp2").mousedown(function(){moveUpStart2();});
			$(".scrollUp2").mouseup(function(){moveUpStop2();});
			$(".scrollDown2").mousedown(function(){moveDownStart2();});
			$(".scrollDown2").mouseup(function(){moveDownStop2();});
		}
		else
		{
			$("#headline-scrollers").hide();
		}
		
	}
);

function onThumbClick(e)
{
	var link;
	if(e.target.tagName.toLowerCase() == "img")
	{
		link = $(e.target).parent();
		//toggleThumbs();
	}
	else
	{
		link = $(e.target);
	}
	loadMainImage(getQueryVariable("assetID",link.attr("href")));
	return false;
}
function moveInfoUp()
{
	var currentPos = infoStartPos - Number($('#news-info').css('top').split("px").join(""));
	if(infoPos < infoPositions.length - 1)
	{
		infoPos ++;
		if(infoPositions[infoPos] + infoStartPos <= currentPos && currentPos + infoPositions[infoPos] <= infoRange && thumbsVisible)
		{
			toggleThumbs();
		}
		if(currentPos + infoPositions[infoPos]  <= infoRange)
		{
			$('#news-info').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
			$('#news-info-bg').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
		}
	}
}
function moveInfoDown()
{
	var currentPos = infoStartPos - Number($('#news-info').css('top').split("px").join(""));
	if(infoPos > 0)
	{
		infoPos --;
		if(currentPos - infoPositions[infoPos] >= 0)
		{
			$('#news-info').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
			$('#news-info-bg').animate( {top:infoPositions[infoPos]+'px' }, 750, 'easeInOutCubic');
		}
	}
}
function loadNextAsset()
{
	loadMainImage(Math.floor(nextAssetID));
	return false;
}
function loadPrevAsset()
{
	loadMainImage(Math.floor(prevAssetID));
	return false;
}
function loadMainImage(assetID)
{
	currentAssetID = assetID;
	//set the current image to selected
	$('.thumb-link').removeClass("selected");
	$(".thumb-link").each(
		function(i,e)
		{
			if(getQueryVariable("assetID",$(e).attr("href")) == assetID)
			{
				$(e).addClass("selected");
			}
		}
	);
	//do the load
	$(".thumb-index").each(
		function(i,e)
		{
			if(getQueryVariable("assetID",$(e).attr("href")) == assetID)
			{
				var pos = Number($(e).text()) - 1;
				var targetRow = Math.floor(pos / 5);
				if(currentRow > targetRow)
				{
					while(currentRow != targetRow)
					{
						scrollThumbsRight();
					}
				}
				else if(currentRow < targetRow)
				{
					while(currentRow != targetRow)
					{
						scrollThumbsLeft();
					}
				}
			}
		}
	);
	$.ajax({
	   type: "GET",
	   url: "/cfc/db/Assets.cfc",
	   data: "method=getMainAsset&assetID="+assetID+"&returnFormat=plain",
	   success: function(r, s){
			setMainImage(r);
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
		  // typically only one of textStatus or errorThrown 
		  // will have info
		  alert(textStatus); // the options for this ajax request
		}
	 });
	 $.ajax({
	   type: "GET",
	   url: "/cfc/db/News.cfc",
	   data: "method=getNextAsset&newsID="+newsID+"&assetID="+assetID+"&returnFormat=plain",
	   success: function(r, s){
			nextAssetID = r;
			if(r == -1)
			{
				$('.next-asset').hide();
			}
			else
			{
				$('.next-asset').show();
			}
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
		  // typically only one of textStatus or errorThrown 
		  // will have info
		  alert(textStatus); // the options for this ajax request
		}
	 });
	 $.ajax({
	   type: "GET",
	   url: "/cfc/db/News.cfc",
	   data: "method=getPrevAsset&newsID="+newsID+"&assetID="+assetID+"&returnFormat=plain",
	   success: function(r, s){
			prevAssetID = r;
			if(r == -1)
			{
				$('.prev-asset').hide();
			}
			else
			{
				$('.prev-asset').show();
			}
	   },
	   error: function (XMLHttpRequest, textStatus, errorThrown) {
		  // typically only one of textStatus or errorThrown 
		  // will have info
		  alert(textStatus); // the options for this ajax request
		}
	 });
}
function setMainImage(path)
{
	var i = new Image(); 
	var images;
	$(i).hide();//hide first - IE is too quick when the image is cached
	i.onload = function()
	{//once loaded, we fade. Once faded, we remove the image faded onto, and set the new image's position to 'relative' for IE (otherwise opening thumbs jogs display).
		$(i).fadeIn(1000, function(){$('#news-image').empty();$('#news-image').prepend(i);$('#news-image img').css({position:'relative'})});
	};
	i.src = path;
	$(i).css({position:'absolute',zIndex:3,verticalAlign:'bottom'});
	$('#news-image').prepend(i);
	$('#news-image img:last').css({zIndex:2});	
}
function toggleThumbs()
{
	if(thumbsVisible)
	{
		closeThumbs();
	}
	else
	{
		openThumbs();
	}
	thumbsVisible = !thumbsVisible;
}
function checkRow()
{
	if(currentRow == rows - 1)
	{
		$('.scroll-right').hide();
	}
	else
	{
		$('.scroll-right').show();
	}
	if(currentRow > 0)
	{
		$('.scroll-left').show();
	}
	else
	{
		$('.scroll-left').hide();
	}
}
function scrollThumbsRight()
{
	currentRow--;
	checkRow();
	if(Number($('#proj-thumbs-scroll').css('left').split("px").join("")) + 905 <= 0)
	{
		$('#proj-thumbs-scroll').animate( {left:'+=905px' }, 750, 'easeInOutCubic');
	}
}
function scrollThumbsLeft()
{
	currentRow++;
	checkRow();
	if(true)
	{
		$('#proj-thumbs-scroll').animate( {left:'-=905px' }, 750, 'easeInOutCubic');
	}
}
function openThumbs()
{
	$('#proj-thumbs').css({display:''});
	$('#proj-thumbs-container').stop();
	$('#proj-thumbs-container').animate({top:"0px"},750,"easeInOutCubic");
	//if the project info is at the top, moe it down
	var step = (infoRange) / 2;
	var currentPos = infoStartPos - Number($('#news-info').css('top').split("px").join(""));
	if(step < currentPos)
	{
		moveInfoDown();
	}
}
function closeThumbs()
{
	$('#proj-thumbs-container').stop();
	$('#proj-thumbs-container').animate({top:"-100px"},750,"easeInOutCubic",function(){$('#proj-thumbs').css({display:'none'});});
}
function moveDownStart()
{
	clearInterval(interval);
	interval = setInterval("moveDown()", 50);
}
function moveDownStop()
{
	clearInterval(interval);
	$('#news-info-content').stop();
	$('#news-info-content').scrollTo( {top:'+=60px' }, 500, { easing:'easeOutSine' } );
}
function moveDown()
{
	$('#news-info-content').scrollTo( {top:'+=10px' }, 50, { easing:'easeNone' } );
}
function moveUpStart()
{
	clearInterval(interval);
	interval = setInterval("moveUp()", 50);
}
function moveUpStop()
{
	clearInterval(interval);
	$('#news-info-content').stop();
	$('#news-info-content').scrollTo( {top:'-=60px' }, 500, { easing:'easeOutSine' } );
}
function moveUp()
{
	$('#news-info-content').scrollTo( {top:'-=10px' }, 50, { easing:'easeNone' } );
}


function moveDownStart2()
{
	clearInterval(interval);
	interval = setInterval("moveDown2()", 50);
}
function moveDownStop2()
{
	clearInterval(interval);
	$('#news-headlines-container').stop();
	$('#news-headlines-container').scrollTo( {top:'+=60px' }, 500, { easing:'easeOutSine' } );
}
function moveDown2()
{
	$('#news-headlines-container').scrollTo( {top:'+=10px' }, 50, { easing:'easeNone' } );
}
function moveUpStart2()
{
	clearInterval(interval);
	interval = setInterval("moveUp2()", 50);
}
function moveUpStop2()
{
	clearInterval(interval);
	$('#news-headlines-container').stop();
	$('#news-headlines-container').scrollTo( {top:'-=60px' }, 500, { easing:'easeOutSine' } );
}
function moveUp2()
{
	$('#news-headlines-container').scrollTo( {top:'-=10px' }, 50, { easing:'easeNone' } );
}
//$('#scrollable').scrollTo( {top:'-=50px'}, 800 );

/*/ JavaScript Document
var newsStartPos;
var newsPositions;
var newsPos = 0;
var newsRange = 375;
var front = 10;

$(document).ready(
	function()
	{
		var ie6 = (navigator.userAgent.indexOf("MSIE 6")>=0) ? true : false;
		if($('#news-image-header').length > 0)
		{
			//add controls to move the text up and down
			$('#news-image-header').click(toggleImage);
			$('#news-image').click(toggleImage);
			$('.news-down').hide();
			//set up the 'mask' for the info
			$('#news-image-container').css({position:'relative',top:"-393px",overflow:'hidden',width:'650px',height:'430px',zIndex:2});
			$('#news-image-bg').css({width:'650px',height:'448px',marginTop:"399px",zIndex:2});
			$('#news-image-header').css({cursor:'pointer',position:'relative',marginTop:"399px",zIndex:3});
			$('#news-image').css({cursor:'pointer',position:'relative',zIndex:4});
			//find the starting position
			newsStartPos = Number($('#news-image-header').css('top').split("px").join(""));
			newsPositions = [0, -newsRange];
			bringToFront("#news-scrollers");
			bringToFront("#news-file");
		}
		//scrollers for news-text
		$("#content").show();//we need to show to calculate height
		var objHeight;
		var orgH = Number($('#news-text').css('height').split("px").join(""));
		$('#news-text').css({overflow:'',height:'auto'});
		objHeight = $('#news-text').height();
		$('#news-text').css({overflow:'hidden',height:orgH});
		if(objHeight > orgH)
		{
			$(".scrollUp").mousedown(function(){moveUpStart();});
			$(".scrollUp").mouseup(function(){moveUpStop();});
			$(".scrollDown").mousedown(function(){moveDownStart();});
			$(".scrollDown").mouseup(function(){moveDownStop();});
		}
		else
		{
			$("#news-scrollers").hide();
		}

	}
);
function toggleImage()
{
	if(newsPos)
	{
		
		moveNewsDown();
	}
	else
	{
		bringImageToFront();
		moveNewsUp();
	}
}
function bringImageToFront()
{
	bringToFront("#news-image-container");
	bringToFront("#news-image-bg");
	bringToFront("#info");
	sendToBack("#news-scrollers");
	sendToBack("#news-file");
	sendToBack("#news-title");
	sendToBack("#news-text");
}
function bringTextToFront()
{
	bringToFront("#news-scrollers");
	bringToFront("#news-file");
	bringToFront("#news-title");
	bringToFront("#news-text");
}
function bringToFront(e)
{
	$(e).css({zIndex:front++});
}
function sendToBack(e)
{
	$(e).css({zIndex:0});
}
function stopAnims()
{
	$('#news-image-header').stop();
	$('#news-image-bg').stop();
	$('#news-image').stop();
}
function moveNewsUp()
{
	var currentPos = newsStartPos - Number($('#news-image').css('top').split("px").join(""));
	if(newsPos < newsPositions.length - 1)
	{
		newsPos ++;
		stopAnims();
		$('#news-image-header').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic');
		$('#news-image-bg').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic');
		$('#news-image').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic');
		$('.news-down').show();
		$('.news-up').hide();
	}
}
function moveNewsDown()
{
	var currentPos = newsStartPos - Number($('#news-image').css('top').split("px").join(""));
	if(newsPos > 0)
	{
		newsPos --;
		stopAnims();
		$('#news-image-header').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic');
		$('#news-image-bg').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic');
		$('#news-image').animate( {top:newsPositions[newsPos]+'px' }, 750, 'easeInOutCubic',bringTextToFront);
		$('.news-down').hide();
		$('.news-up').show();
	}
}
function moveDownStart()
{
	clearInterval(interval);
	interval = setInterval("moveDown()", 50);
}
function moveDownStop()
{
	clearInterval(interval);
	$('#news-text').stop();
	$('#news-text').scrollTo( {top:'+=60px' }, 500, { easing:'easeOutSine' } );
}
function moveDown()
{
	$('#news-text').scrollTo( {top:'+=10px' }, 50, { easing:'easeNone' } );
}
function moveUpStart()
{
	clearInterval(interval);
	interval = setInterval("moveUp()", 50);
}
function moveUpStop()
{
	clearInterval(interval);
	$('#news-text').stop();
	$('#news-text').scrollTo( {top:'-=60px' }, 500, { easing:'easeOutSine' } );
}
function moveUp()
{
	$('#news-text').scrollTo( {top:'-=10px' }, 50, { easing:'easeNone' } );
}*/