/* Horizonatl Slider */
var Slider = function(sectionId, panelWidth, itemsPerPanel) {
	
	//private variables
	var _currentPanel = 0;
	var _totalPanels = 0;
	var _itemsPerPanel = itemsPerPanel;
	var _panelWidth = panelWidth;
	var _sectionId = sectionId;
	var _isAll = false;
	
	//private methods
	var showNextVideo = function() {
		$("."+_sectionId+" .episodeGroup ul").animate({"left": "-=" + _panelWidth + "px"}, "slow");
		$("."+_sectionId+" .Nav4 .arrowLeft").css("visibility", "visible");
		_currentPanel++;
		
		if (_currentPanel == _totalPanels - 1) {
			$("."+_sectionId+" .Nav4 .arrowRight").css("visibility", "hidden");

		}
		return false;
	}

	var showPreviousVideo = function() {
			$("."+_sectionId+" .episodeGroup ul").animate({"left": "+="+_panelWidth+"px"}, "slow");
			$("."+_sectionId+" .Nav4 .arrowRight").css("visibility", "visible");

		
		_currentPanel--;
		if (_currentPanel == 0) {
			$("."+_sectionId+" .Nav4 .arrowLeft").css("visibility", "hidden");
		}
		return false; 
	}

	var toggleShowAll = function() {
		if (_isAll) {
			//set currentPanel back to 0
			_currentPanel = 0;
			$("."+_sectionId+" .Nav4 .arrowLeft").css("visibility", "hidden")
			$("."+_sectionId+" .Nav4 .arrowRight").css("visibility", "visible")

			$(this).html("<a>See All</a>");
			$("."+_sectionId+" .episodeGroup").css("overflow", "hidden");
			$("."+_sectionId+" .episodeGroup ul")
				.css("width", "3000px")
				.css("position", "relative")
				.css("left", 0);
			$("."+_sectionId+" .Nav4 li:eq(1)").show();
			$("."+_sectionId+" .Nav4 li:eq(2)").show();
			_isAll = false;
		}
		else {
			$(this).html("<a>See 4</a>");
			$("."+_sectionId+" .episodeGroup").css("overflow", "visible");
			$("."+_sectionId+" .episodeGroup ul")
				.css("width", "100%")
				.css("position", "static");
			$("."+_sectionId+" .Nav4 li:eq(1)").hide();
			$("."+_sectionId+" .Nav4 li:eq(2)").hide();
			_isAll = true;

		}
	}

	//onload
	$(function() {
		/*
		//get total panels
		var items = $("."+_sectionId+" .episodeGroup ul li").length;
		_totalPanels = Math.ceil(items/_itemsPerPanel);
		if (_totalPanels == 1) {
			$("."+_sectionId+" .Nav4 .arrowRight").css("visibility", "hidden")			
		};
		//initialize click events	
		$("."+_sectionId+" .Nav4 .arrowLeft")
			.css("visibility", "hidden")
			.click(showPreviousVideo);

		$("."+_sectionId+" .Nav4 .arrowRight").click(showNextVideo);

		$("."+_sectionId+" .Nav4 li:eq(0)").click(toggleShowAll);
		*/
	});

};

/* Vertical Slider */
var verticalSlider = function(sectionId, panelHeight, itemsPerPanel) {
	
	//private variables
	var _currentPanel = 0;
	var _totalPanels = 0;
	var _itemsPerPanel = itemsPerPanel;
	var _panelHeight = panelHeight;
	var _sectionId = sectionId;
	var _isAll = false;
	
	//private methods
	var showNextVideo = function() {
		$("#"+_sectionId+" .episodeGroup ul").animate({"top": "-=" + _panelHeight + "px"}, "slow");
		$("#"+_sectionId+" .Nav4 .arrowUp").css("visibility", "visible");
		_currentPanel++;
		
		if (_currentPanel == _totalPanels - 1) {
			$("#"+_sectionId+" .Nav4 .arrowDown").css("visibility", "hidden");

		}
		return false;
	}

	var showPreviousVideo = function() {
			$("#"+_sectionId+" .episodeGroup ul").animate({"top": "+="+_panelHeight+"px"}, "slow");
			$("#"+_sectionId+" .Nav4 .arrowDown").css("visibility", "visible");

		
		_currentPanel--;
		if (_currentPanel == 0) {
			$("#"+_sectionId+" .Nav4 .arrowUp").css("visibility", "hidden");
		}
		return false;
	}

	//onload
	$(function() {
		//get total panels
		var items = $("#"+_sectionId+" .episodeGroup ul li").length;
		_totalPanels = Math.ceil(items/_itemsPerPanel);
		if (_totalPanels == 1) {
			$("#"+_sectionId+" .Nav4 .arrowDown").css("visibility", "hidden")			
		};
		//initialize click events	
		$("#"+_sectionId+" .Nav4 .arrowUp")
			.css("visibility", "hidden")
			.click(showPreviousVideo);

		$("#"+_sectionId+" .Nav4 .arrowDown").click(showNextVideo);

	});

};

var leaderBoardPanel = function (buttonId, containerId) {

	var leaderboard = $("#" +buttonId);
	var closeButton = $("#" +containerId+ " .close");
	
	$(leaderboard).bind("click", function () { 
		var offset = $(this).offset();
		$("#" +containerId)
			.css("left", offset.left -45)
			.css("top", offset.top + 30)
			.slideToggle();
			return false;
			
	});
	
	$(closeButton).bind("click", function() {
		$("#" +containerId)
			.slideToggle();
			return false;
	});
	
	$("#leaderboardCraptions").show();
	
	var _currentPanel = 0;
	var i = 0;
	
	$(".leaderboardSection").hide();
	$(".leaderboardSection:eq("+_currentPanel+")").show();
	$(".TextList4 li:eq("+i+")").addClass("selected");
	
	$(".TextList4 li").bind("click", function(){
		$(this).parent().find("li").removeClass("selected");
		i = $(".TextList4 li").index(this);
		_currentPanel = i;
		
		var leaderboardOverlayText = $(this).find("a").text();
		
		//place to store text
		var leaderboardText = $("#leaderboardButton .arrow");


		// replaced text from .... with placeholder text
		$(leaderboardText).html(leaderboardOverlayText + " Last 30 days");
		
		$(this).addClass("selected");
		$(".leaderboardSection").hide();
		$(".leaderboardSection:eq("+_currentPanel+")").show();
		$(closeButton).click();
		return false;
	});

}

var switcher = function() {
	
	var _currentPanel = 0;
	var _totalPanel = 0;
	var i = 0;	
	

	$(".categoryArticles").hide();
	$(".categoryArticles:eq("+_currentPanel+")").show();
	$(".Nav3 li:eq("+i+")").addClass("selected");
	
	$(".Nav3 li").hover(
		function () {
			$(this).parent().find("li").removeClass("selected");
			i = $(".Nav3 li").index(this);
			_currentPanel = i;
			
			// place where text is shown
			var linkText = $(this).text();
			
			// place to store text
			var catText = $("#catText");
			
			// replaces text from links with placeholder text
			$(catText).html(linkText);		
		
			$(this).addClass("selected");
			$(".categoryArticles").hide();
			$(".categoryArticles:eq("+_currentPanel+")").show();
	
		},
		function () {}
		
	);

		
}


var postComment = function() {
	$(".postComment").click(function () {
		$(".postCommentForm").show();
	});
	
	$(".postCommentForm form .Button1").click(function () {
		$(".submitted").fadeIn('slow', function() { 
			setTimeout(function() {
				$(".submitted").fadeOut('slow');
				$(".postCommentForm").fadeOut('slow');
			}, 1500);
		});
	});
}


var indexPage = function() {
	var newTodaySlider = new Slider('NewToday', '292' , '2');
	
	var popularVideoSlider = new verticalSlider('popular', '564', '6');
	var top50VideoSlider = new verticalSlider('top50', '564', '6');

	var popularVideoSlider = new Slider('PopularVideos', '474', '3');

	
	//onload
	$(function() {
		$(".VideoCharts .Nav2 li:eq(1)").click(function () {
			$(this).addClass("selected");
			$(".VideoCharts .Nav2 li:eq(0)").removeClass("selected");
			$("#popular").hide();
			$("#top50").fadeIn();
		});	
		
		$(".VideoCharts .Nav2 li:eq(0)").click(function () {
			$(this).addClass("selected");
			$(".VideoCharts .Nav2 li:eq(1)").removeClass("selected");
			$("#top50").hide();
			$("#popular").fadeIn();
		});	
		var leaderboardPanel = new leaderBoardPanel('leaderboardButton', 'leaderboardOverlay');
	
		switcher();
	});
	
}

var videoPage = function() {
	var episodeSlider = new Slider('episodeSection', '610', '4');
	var blooperSlider = new Slider('blooperSection', '610', '4');
	var hotCrackedSlider = new Slider('hotCracked', '610', '4');

	//onload
	$(function() {
		$("#aboutSection").hide();
		$(".postCommentForm").hide();

		$(".VideoAboutGroup .Nav2 li:eq(1)").click(function () {
			$(this).addClass("selected");
			$(".VideoAboutGroup .Nav2 li:eq(0)").removeClass("selected");
			$("#videoSection").hide();
			$("#aboutSection").fadeIn();
		});	
		
		$(".VideoAboutGroup .Nav2 li:eq(0)").click(function () {
			$(this).addClass("selected");
			$(".VideoAboutGroup .Nav2 li:eq(1)").removeClass("selected");
			$("#aboutSection").hide();
			$("#videoSection").fadeIn();
		});	

		postComment();
	});
}


var topicsPage = function() {
	var relatedCrackedSlider = new Slider('relatedCrackedContent', '300' , '4');
	
}

var craptionsOpen = function() {
	var pastCraptionsSlider = new Slider('PastCraptionsGroup', '139', '1');
}

function winopen(url, winname, width, height, top, left) {
	var popwin = window.open(url, winname, "width="+width+",height="+height+",menubar=no,status=no,location=no,toolbar=no,scrollbars=no,top=" + top + ",left=" + left);
	popwin.focus();
}

/* Arcannon was here. */
$(document).ready(function() {

	var _panelWidth = 617;
	var _currentPanel = 0;
	var _totalPanels = 0;

	$(".episodeNav .seeAll").click(function(){

		var list = $(this).parent().parent().parent().parent().parent().find(".ThumbnailList2");

		if(list.css("width") != "auto") {
			list.css("width", "auto");
			list.css("left", "0px");
			_currentPanel = 0;
			$(this).parent().parent().find(".arrowLeft").css("visibility", "hidden");
			$(this).parent().parent().find(".arrowRight").css("visibility", "hidden");
			this.innerHTML = "<a>Close</a>";
		} else {
			list.css("width", "10000px");
			$(this).parent().parent().find(".arrowLeft").css("visibility", "hidden");
			$(this).parent().parent().find(".arrowRight").css("visibility", "visible");
			this.innerHTML = "<a>See All</a>";
		}
	});

	$(".episodeNav .arrowLeft").click(function() {
		if(_currentPanel <= 0) {
			return;		
		}

		var list = $(this).parent().parent().parent().parent().parent().parent().find(".ThumbnailList2");
		$(list).animate({"left": "+="+_panelWidth+"px"}, "slow");			
		$(this).css("visibility", "visible");
		$(this).parent().parent().find(".arrowRight").css("visibility", "visible");
		_currentPanel--;

		if(_currentPanel <= 0) {
			$(this).css("visibility", "hidden");
		}
	});

	$(".episodeNav .arrowRight").click(function() {
		var list = $(this).parent().parent().parent().parent().parent().parent().find(".ThumbnailList2");
		$(list).animate({"left": "-="+_panelWidth+"px"}, "slow");	
		$(this).parent().parent().find(".arrowLeft").css("visibility", "visible");
		_currentPanel++;

		if(((_currentPanel+1)*4) >= $(list).find("li").length) {
			$(this).css("visibility", "hidden");
		}
	
	});
});

