atv.bucket = {};

// Initialize
atv.init.bucket = function(){
	// Cufon
	Cufon.replace('#bucket .tabs .tab a');

	// Proper Clearing
	$("#bucket .pages .page.featured .pod:nth-child(3n+1)").addClass("first");
	$("#bucket .pages .page.all .pod:nth-child(3n+1)").addClass("first");

	// Tabs
	$("#bucket .tabs .tab a").click(function(){
		var newTab = $(this);
		var oldTab = $("#bucket .tabs .tab.selected a");
		oldTab.parent().removeClass("selected");
		newTab.parent().addClass("selected");

		$("#bucket .scroller .panel.active").removeClass("active");
		$("#bucket .scroller .panel."+newTab.attr("href").substr(1)).addClass("active");

		if($("#bucket .scroller .panel.active ul").length > 1){
			$("#bucket .scroller > a").show();
		} else {
			$("#bucket .scroller > a").hide();
		}

		if($(this).attr("href") == "#featured"){
			$("#bucket .pages .page.all").removeClass("active");
			$("#bucket .pages .page.featured").addClass("active");
		} else {
			$("#bucket .pages .page.featured").removeClass("active");
			$("#bucket .pages .page.all").addClass("active");

			// Sort
			var sortType = $(this).attr("href");
			var podlist = $("#bucket .pages .page.all");
			var listitems = podlist.children('.pod').get();
			var compA;
			var compB;
			listitems.sort(function(a, b){
				switch(sortType){
					case "#az":
						compA = $(a).find("h3").text().toUpperCase();
						compB = $(b).find("h3").text().toUpperCase();
					break;
					case "#viewed":
						compA = parseInt($(a).attr("views"),10);
						compB = parseInt($(b).attr("views"),10);
					break;
					case "#rated":
						compA = parseFloat($(a).attr("rating"),10);
						compB = parseFloat($(b).attr("rating"),10);
					break;
					case "#added":
						compA = parseInt($(b).attr("added"),10); // These are reversed for DESC order
						compB = parseInt($(a).attr("added"),10);
					break;
				}
				return (compA < compB) ? -1 : (compA > compB) ? 1 : 0;
			});
			$.each(listitems, function(idx, itm) { podlist.append(itm); });

			// Fix clearing
			$("#bucket .pages .page.all .pod").removeClass("first");
			$("#bucket .pages .page.all .pod:nth-child(3n+1)").addClass("first");


			// Tracking Calls
			switch(sortType){
				case "#featured":
					s.pageName += ":Featured";
				break;
				case "#az":
					s.pageName += ":Alphabetical";
				break;
				case "#viewed":
					s.pageName += ":Most Viewed";
				break;
				case "#rated":
					s.pageName += ":Highest Rated";
				break;
				case "#added":
					s.pageName += ":Recently Updated";
				break;
			}
			s.channel="Adobe TV 3.0";
			s.t();
		}

		// Update tab style
		Cufon.replace('#bucket .tabs .tab a');

		// TODO: Notify Omniture

		//Refresh Pagination
		atv.bucket.pagination.refresh();

		return false;
	});

	// List/Grid Buttons
	$("#bucket .filter .tool.view a").click(function(){
		var thisButton = $(this);
		if(thisButton.attr("href") == "#grid"){
			$("#bucket").removeClass("list").addClass("grid");
		} else {
			$("#bucket").removeClass("grid").addClass("list");
		}
		return false;
	});

	// Pod Meta Data
	$("#bucket .pages .page.all .pod").each(function(){
		var data = $(this).attr("id").replace("meta","").split('-');
		$(this).attr("views",data[0]);
		$(this).attr("rating",data[1]);
		$(this).attr("added",data[2]);
	});

	//Refresh Pagination
	atv.bucket.pagination.refresh();
};

atv.bucket.pagination = {};
atv.bucket.pagination.itemsperpage = 9;

// Initialize Back/Forward and View All buttons
atv.init.pagination = function(){
	$("#bucket .filter .pagination a.back").click(function(){
		if($("#bucket .filter .pagination .numbers a.active").length > 0){
			$("#bucket .filter .pagination .numbers a.active").prev().click();
		} else {
			$("#bucket .filter .pagination .numbers a:first").click();
		}
		return false;
	});
	$("#bucket .filter .pagination a.next").click(function(){
		if($("#bucket .filter .pagination .numbers a.active").length > 0){
			$("#bucket .filter .pagination .numbers a.active + a").click();
		} else {
			$("#bucket .filter .pagination .numbers a:first").click();
		}
		return false;
	});
	$("#bucket .filter .pagination a.all").click(function(){
		$("#bucket .pages .page.active .pod").show();
		$("#bucket .filter .pagination .numbers a.active").removeClass("active");
		return false;
	});
};

atv.bucket.pagination.refresh = function(){ // Execute every time a page changes
	var page = $("#bucket .pages .page.featured"); // Cache page
	if(!page.hasClass("active")){ // If current page is not popular, cache all
		page = $("#bucket .pages .page.all");
	}

	var numItems = page.find(".pod").length; // Cache number of pods in page
	
	if(numItems > atv.bucket.pagination.itemsperpage){ // If there are more than the items per page, proceed
		var numPages = Math.ceil(numItems/atv.bucket.pagination.itemsperpage); // Calculate number of pages
		var numberHTML = []; // Set temporary array for string concatenation
		for(var i=0; i < numPages; i++){ // Build # links for pagination
			numberHTML.push('<a href="'+i+'"');
			if(i===0){
				numberHTML.push(' class="active"');
			}
			numberHTML.push('>'+(i+1)+'</a>');
		}
		$("#bucket .filter .pagination .numbers").html(numberHTML.join('')); // Insert numbers
		$("#bucket .filter .pagination").show(); // Show pagination
		page.find(".pod").hide().slice(0,atv.bucket.pagination.itemsperpage).show(); // Show/hide for page 1 of pods
		
		$("#bucket .filter .pagination .numbers a").click(function(){ // Click functions for pagination numbers
			var toPage = $(this).attr("href"); // Cache the index we're going to
			$("#bucket .filter .pagination .numbers a.active").removeClass("active"); // Unhighlight old number, remember the second set at the bottom
			$("#bucket .filter .pagination .numbers a[href="+toPage+"]").addClass("active"); // Highight new number, remember the second set at the bottom
			page.find(".pod").hide().slice((toPage*atv.bucket.pagination.itemsperpage),((toPage*atv.bucket.pagination.itemsperpage)+atv.bucket.pagination.itemsperpage)).show(); // Show/hide for current page
			return false;
		});
	} else { // Fewer pods than itemsperpage, show all, hide pagination
		$("#bucket .filter .pagination").hide();
		page.find(".pod").show();
	}
};
