///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Scope: This file controls the news articles withing the new bucket
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

$(function() {	
	/*var heightOfStory = 0;
	var currentMargin = 0;
	var marginIncrement = 100;
	////////////////// scroll bar ////////////////////
	
	$('#article').livequery(function() {
		currentMargin = 0;
		$('#article').append('<br><span id=\'endOfArticle\'>&nbsp;</span>');
		heightOfStory = $('#endOfArticle').position().top - $('#article').height();
		$('#article').css('height',$('#endOfArticle').position().top+'px');
	});
	
	function scrollNews(dir) {
		///////////// Set teh increment value ////
		var tempIncrement = marginIncrement;
		
		if(dir == "up" && (Math.abs(currentMargin) < heightOfStory)) {
			 // If there's less than 100px left to show
			//alert("CM:"+currentMargin+" HEIGHT:"+heightOfStory);
			if((Math.abs(currentMargin) + marginIncrement) > heightOfStory) {tempIncrement = heightOfStory - Math.abs(currentMargin);}
			
			/////////// Update the current margin //////
			currentMargin -= tempIncrement;
			
			///////////// Animate it /////////////////
			$('#article').animate({"marginTop": "-="+tempIncrement+"px"}, "1000","linear");
		} else if (dir == "down" && currentMargin < 0) {
			 // If there's less than 100px left to show
			if((currentMargin + marginIncrement) > 0) {tempIncrement = Math.abs(currentMargin);}
			
			/////////// Update the current margin //////
			currentMargin += tempIncrement;
			
			///////////// Animate it /////////////////
			$('#article').animate({"marginTop": "+="+tempIncrement+"px"}, "1000","linear");
		}
	}
	
	$('#up-news-arrow').livequery('click',function() {
		scrollNews('down');
		return false;
	});
	
	$('#down-news-arrow').livequery('click',function() {
		scrollNews('up');
		return false;
	});*/
	
	//////////////////  modal handling ////////////////
	function openNewsModal(id) {
		$('#single-news-article').load(includePath+"include/templateFiles/newsStory.php","sid="+Math.random()+"&newsID="+id+"&includePath="+includePath);
		$('#article-controls').load(includePath+"include/templateFiles/newsArticleControls.php","sid="+Math.random()+"&newsID="+id+"&includePath="+includePath);
		$('#footer-bucket2').css('visibility','hidden');
		$('#single-news-article, #article-controls').css('display','inline');
	}
	
	function closeNewsModal() {
		$('#single-news-article').html('');
		$('#single-news-article, #article-controls').css('display','none');
		$('#footer-bucket2').css('visibility','visible');
	}
	
	$('#initialNewsTitle p').livequery('click',function() {
		openNewsModal($(this).attr('id'));
		return false;
	});	
	
	//////// This is from the news colleague modal /////////
	$('#returnToArticle').livequery('click',function() {
		openNewsModal($(this).attr('rel'));
		Shadowbox.close();
		return false;
	});
	
	$('#newsbank p').livequery('click',function() {
		openNewsModal($(this).children('img').get(0).id);
		return false;
	});
	
	$('#article-controls ul li a').livequery('click',function() {
		openNewsModal($(this).attr('rel'));
		return false;
	});
	
	$('.curtain-close, #close-news, a.callOpenModal[rel=news], .occ').livequery('click',function() {
		closeNewsModal();
		return false;
	});
});
