// news system

var news2 = (function() {
    var current2 = 0;
    var WIDTH = 780;
    var newsContainer2, scrollbox2, liCount2;
    
    
    function init2() {

    	newsContainer2 = $j("#news2");
    	scrollbox2 = $j("#news2-scrollbox");
    	liCount2 = newsContainer2.find("ul").eq(0).children().length;
    	//console.log(newsContainer2, liCount2, scrollbox2, WIDTH);
    
    	newsContainer2.find("#news2-navbox").css({ display: "block" });
    	
    	goToCurrent2();
    };
    
    function goTo2(what) {

    	switch (what) {
    		case "previous":
		    	current2--;
    		break;
    		case "next":
    			current2++;
    		break;
    		/*
    		case "archive":
    			current = liCount2 - 1;
    		break;
    		*/
    		default:
    			if (typeof what === "number") {
    				current2 = what;
    			}
    		break;
    	}
    	
    	current2 = Math.max(0, Math.min(liCount2 - 1, current2));
    	
    	goToCurrent2();
    };
    
    function goToCurrent2() {

		newsContainer2.find("#previous").css({ display: "none" });
		newsContainer2.find("#next").css({ display: "none" });

//    	console.log(current);
    	var visible = [];
    	if (!current2) {
			visible = ['archive-l', 'next'];
			newsContainer2.find("#next").css({ display: "block" });
    	}
    	else if (current2 == liCount2 - 2) {
			visible = ['previous', 'archive-r'];
			newsContainer2.find("#next").css({ display: "block" });
			newsContainer2.find("#previous").css({ display: "block" });			
    	}
    	else if (current2 >= liCount2 - 1) {
			visible = ['previous'];
			newsContainer2.find("#previous").css({ display: "block" });			
    	}
    	else {
	    	newsContainer2.find("#previous").css({ display: "block" });	
			visible = ['previous', 'next'];
    	}
//    	alert(current2);
  //  	$j("#news2-navbox ." + visible.join(', #news2-navbox .')).css({ display: "" });
//    	$j("#news2-navbox *").css({ display: "none" });
    	
    	if (current2 >= liCount2 - 1) {
    		//newsContainer2.find("h3").html("Archived News");
    //		newsContainer2.find("#previous").html("Archived News");
    	}
    	else {
    		//newsContainer2.find("h3").html("Latest News");
    	//	newsContainer2.find("#previous").html("Archived News");
    	}
    	
    	scrollbox2.animate({ left: (-WIDTH * current2) + "px" });
    };

    return {
    	init2: init2,
    	goTo2: goTo2
    }
})();

var news = (function() {
    var current = 0;
    var WIDTH = 468;
    var newsContainer, scrollbox, liCount;
    
    
    function init() {
    	newsContainer = $j("#news");
    	scrollbox = $j("#news-scrollbox");
    	liCount = newsContainer.find("ul").eq(0).children().length;
    	//console.log(newsContainer, liCount, scrollbox, WIDTH);
    	
    	newsContainer.find("#news-navbox").css({ display: "block" });
    	
    	goToCurrent();
    };
    
    function goTo(what) {
    	switch (what) {
    		case "previous":
		    	current--;
    		break;
    		case "next":
    			current++;
    		break;
    		/*
    		case "archive":
    			current = liCount - 1;
    		break;
    		*/
    		default:
    			if (typeof what === "number") {
    				current = what;
    			}
    		break;
    	}
    	
    	current = Math.max(0, Math.min(liCount - 1, current));
    	
    	goToCurrent();
    };
    
    function goToCurrent() {
//    	console.log(current);
    	var visible = [];
    	if (!current) {
			visible = ['archive-l', 'next'];
    	}
    	else if (current == liCount - 2) {
			visible = ['previous', 'archive-r'];
    	}
    	else if (current >= liCount - 1) {
			visible = ['previous'];
			
    	}
    	else {
			visible = ['previous', 'next'];
    	}

    	//$j("#news-navbox *").css({ display: "none" });
    	$j("#news-navbox ." + visible.join(', #news-navbox .')).css({ display: "" });
    	
    	if (current >= liCount - 1) {
    		newsContainer.find("h3").html("Archived News");
    	}
    	else {
    		newsContainer.find("h3").html("Latest News");
    	}
    	
    	scrollbox.animate({ left: (-WIDTH * current) + "px" },500, 'linear', closemovie());
    };

    return {
    	init: init,
    	goTo: goTo
    }
    
    function closemovie() {
		if (current == 1) {
    		$j("#movies").css({ display: "block" });
    	} else  {
	    	$j("#movies").css({ display: "none" });    	
    	}
    }
    
})();


$j(news.init);

$j(news2.init2);

