$(document).ready(function(){
	var scrollAllow,scrollDirection,scrollLength,currentLength,offsetVal,totalLength,screenWidth,currentLiLength,articleId,loadUrl,rand,nextArticle;
		scrollAllow = 1;
		scrollDirection = "-";
		screenWidth = $('html').width();
		scrollLength = ($('#thescroll li').width()+2) * $('#thescroll li').length;
		totalLength = scrollLength;
		currentLength = 0;
		$('.showoverlay:last').css('width',screenWidth);
		$('#thescroll').css('width',scrollLength);
		$('.scrolling').mouseover(function(e){
			mouseCheck(e)
		}).mouseout(function(e){
			mouseFinish(e)
		});
		$('#thescroll li').click(function(){
				if(allowonclick != 0){
					showArticle(this);
				}
		});
		$('#thescroll li').mouseover(function(){
			$(this).children('.clicktoview').fadeIn();
		}).mouseout(function(){
			$(this).children('.clicktoview').fadeOut();
			});
		$('.showoverlay').click(function(){
			hideArticle();
		});
		$('html').keydown(function(event){
			if(event.which == '27'){
				hideArticle();
			}
		});
		autoScroll();
	function getOffset()
	{
		offsetVal = $('#start').offset();
		currentLength = offsetVal.left;
		return currentLength;	
	}
	function mouseCheck(e) {
		if(scrollAllow == 1){
			currentLength = getOffset();
			if(currentLength < 0 && e.pageX < 400){
				scrollDirection = "+";
				scrollLength = 0 - currentLength;
				$('#thescroll').stop().animate({left:scrollDirection+"="+scrollLength+"px"},scrollLength*5);
			}
			if(e.pageX > screenWidth-400){
				scrollDirection = "-";
				scrollLength = (totalLength + currentLength) - screenWidth;
				$('#thescroll').stop().animate({left:scrollDirection+"="+scrollLength+"px"},scrollLength*5);
			}
		}
	}
	function mouseFinish(e) {
		if(scrollAllow == 1){
			currentLength = getOffset();
			if(currentLength < 0 && scrollDirection == "+"){
				$('#thescroll').stop().animate({left:scrollDirection+"=30px"},1000,"easeOutBack");
			}
			scrollLength = totalLength - screenWidth - 2;
			if(currentLength > "-"+scrollLength && scrollDirection == "-"){
				$('#thescroll').stop().animate({left:scrollDirection+"=30px"},1000,"easeOutBack");
			}
		}
	}
	function showArticle(elm) {
		scrollAllow = 0;
		currentLength = getOffset();
		currentLiLength = $(elm).offset();
		scrollLength = (currentLength - currentLiLength.left) + 420;
		articleId = $(elm).attr("class");
		nextArticle = $(elm).attr("id");
		if(nextArticle == "start"){
			nextArticle = 1;	
		}
		if(nextArticle == "end"){
			nextArticle = 0;	
		}
		nextArticle = parseInt(nextArticle);
		nextArticle = nextArticle+1;
		$('#thescroll').stop().animate({left:scrollLength+"px"},1000,"easeOutBack");
		$('.showoverlay').fadeOut(10).delay(1000).fadeIn(500);
		if(clientList == 1){
			$('.clearbox').fadeOut(10).delay(1000).fadeIn(500);		
		} else {
			$('.clearbox').show();		
		}
		$('.blackbox').css("height","0px").css("display","block").delay(800).animate({height:"272px"},500,"easeInOutExpo",function(){
			grabArticle(articleId,nextArticle);
			if(clientList == 1){
				grabGallery(articleId);
			}
		});
		$('.showoverlay').hide().delay(1000).show();
	}
	function hideArticle() {
		scrollAllow = 1;
		$('.showoverlay').fadeOut(500,function(){
			autoScroll();
		});
		$('.clearbox').hide();
		$('.clearbox').html("&nbsp;");
		$('.blackbox').html("&nbsp;");
		$('.blackbox').animate({height:"0px"},500,"easeInOutExpo");
	}
	function autoScroll() {
		if(scrollAllow == 1){
			currentLength = getOffset();
			if(scrollDirection == "+"){
				scrollLength = 0 - currentLength;
				$('#thescroll').stop().animate({left:"+="+scrollLength+"px"},scrollLength*25,function(){
					scrollDirection = "-";
					continueScroll();
				});
			}
			if(scrollDirection == "-"){
				scrollLength = (totalLength + currentLength) - screenWidth;
				$('#thescroll').stop().animate({left:"-="+scrollLength+"px"},scrollLength*25,function(){
					scrollDirection = "+";
					continueScroll();
				});
			}
		} else {
			$('#thescroll').stop();
		}
	}
	function continueScroll(){
		setTimeout(autoScroll,10);	
	}
	
	function grabArticle(articleId,nextArticle) {
		rand = Math.random();
		$('.blackbox').html("<img class=\"ajaxloader\" src=\"/ajaxinc/loading.gif?v="+rand+"\" />");
		setTimeout("$('.blackbox').load('/ajaxi_gethomeinfo.php', {p_id:'"+articleId+"',readMore:'"+showReadmore+"',nextArticle:'"+nextArticle+"'});",2400);
	}
	
	function grabGallery(articleId) {
		setTimeout("$('.clearbox').load('/ajaxi_getgallery.php', {p_id:'"+articleId+"'});",500);
	}
	
	// fonts.com required banner hack
	var count,newcount;
		count = $('div').length;
	function checkwebFontDiv() {
		newcount = $('div').length;
		if(newcount == count+1){
			$('div:last').css("display","none");
		} else {
			setTimeout(checkwebFontDiv,5);	
		}
	}
	checkwebFontDiv();
	
	$('.feedback').click(function(){
								  $('.feedbackbox').fadeIn(500);								  
								  });
	
	$('.feedback_submit').click(function(){
		var getComment = $('#feedback_comments').attr("value");
		if(getComment != ""){
			$('.feedbackbox').load('/ajaxi_feedback.php', {feedbackComment:getComment});
		} else {
			alert("Oops. Please enter a comment to submit."); 
		}
	});
	
});
function showNext(listId){
	if(listId == 1){
		$('#thescroll').stop().animate({left:"+420px"},1000,"easeOutBack");
		listId = "start";
	} else {
		$('#thescroll').stop().animate({left:"-=428px"},1000,"easeOutBack");
	}
	articleId = $('#'+listId).attr("class");
	nextArticle = listId;
	if(nextArticle == "start"){
		nextArticle = 1;	
	}
	if(nextArticle == "end"){
		nextArticle = 0;	
	}
	nextArticle = parseInt(nextArticle);
	nextArticle = nextArticle+1;
	if(nextArticle == "12"){
		nextArticle = "end";	
	}
	grabArticle2(articleId,nextArticle);
}
function grabArticle2(articleId,nextArticle) {
	rand = Math.random();
	$('.blackbox').html("<img class=\"ajaxloader\" src=\"/ajaxinc/loading.gif?v="+rand+"\" />");
	setTimeout("$('.blackbox').load('/ajaxi_gethomeinfo.php', {p_id:'"+articleId+"',readMore:'"+showReadmore+"',nextArticle:'"+nextArticle+"'});",2400);
}
	


