function ShopSort()
{
	var self;
	{
		self = this;
		
		self.Title = document.getElementById('shop_sort_title');
		self.Pop = document.getElementById('shop_sort_pop');
		self.Num = document.getElementById('shop_sort_num');
		self.SortType = 'title';
		self.SortId = self.Title;
		
		self.PosterList = document.getElementById('poster_list');
		self.ShopNav = document.getElementById('shop_nav');
		self.ShowAllTag = document.getElementById('shop_show_all');
		self.PageCount = 0;
		self.PageIndex = 0;
		self.ShowAll = false;
	}

	this.init = function()
	{
		// Shop Sorts
		$(self.Title).click(function() {
			self.selectSort('title', self.Title);
		});
		
		$(self.Pop).click(function() {
			self.selectSort('-quantity_purchased', self.Pop);
		});
		
		$(self.Num).click(function() {
			self.selectSort('swpl_number', self.Num);
		});
		
		// Shop Page Nav
		$('#shop_nav_dec').click(function() {
			self.decIndex();
		});
		
		$('#shop_nav_inc').click(function() {
			self.incIndex();
		});
		
		$(self.ShowAllTag).click(function() {
			self.toggleNav();
		});
		
		// Shop Page Index Info
		$.ajax({
			url: "/shop/getPageInfo/",
			success: self.countComplete
		});
	}

	this.countComplete = function(data, textStatus, jqXHR)
	{
		var val = data.split(",");
		self.PageCount = parseInt(val[0]);
		self.PageIndex = parseInt(val[1]);
		self.SortType = val[2];
		
		if(self.SortType == "title"){
			self.selectSort(self.SortType, self.Title);
		}
		else if(self.SortType == "-quantity_purchased"){
			self.selectSort(self.SortType, self.Pop);
		}
		else if(self.SortType == "swpl_number"){
			self.selectSort(self.SortType, self.Num);
		}
		else{
			self.SortType = "title";
			self.selectSort(self.SortType, self.Title);
		}
		
		self.buildNavIndex();
	}

	this.getNewSortPage = function()
	{
		$.ajax({
			url: "/shop/sort/",
			data: "type=" + self.SortType + "&index=" + self.PageIndex,
			success: self.sortComplete
		});
		
		try {
			pageTracker._trackPageview('/shop/sort');
		} catch(err) {}
	}
	
	this.sortComplete = function(data, textStatus, jqXHR)
	{
		$('#poster_list').html(data);
	}
	
	this.buildNavIndex = function()
	{
		var navindex = " | ";
		for(i = 0; i < self.PageCount; i++)
		{
			navindex += "<span id='nav_index" + i + "' class='unsel' onclick='shopsort.jumpIndex(" + (i) +")'>"+(i+1)+"</span> | ";
		}
		$("#shop_nav_index").html(navindex);

		if(self.PageIndex < 0)
		{
			self.ShowAll = true;
			self.ShopNav.style.display = "none";
			self.ShowAllTag.innerHTML = "Show in Pages";
		}
		else
		{
			self.selIndex("nav_index"+self.PageIndex);
			self.ShowAllTag.innerHTML = "Show All";
		}
	}

	this.selectSort = function(sortType, id)
	{
		// deselect old class
		$(self.SortId).removeClass("sel");
		$(self.SortId).addClass("unsel");
		
		// select and set new class
		$(id).removeClass("unsel");
		$(id).addClass("sel");
		self.SortId = id;
		
		self.SortType = sortType;
		if(self.PageIndex < 0)
			self.getNewSortPage();
		else
			self.jumpIndex(0);
	}

	this.incIndex = function()
	{
		if(self.PageIndex < self.PageCount-1)
		{
			self.unselIndex('nav_index'+self.PageIndex)
			self.PageIndex++;
			self.selIndex('nav_index'+self.PageIndex)
			self.getNewSortPage();
		}
	}

	this.decIndex = function()
	{
		if(self.PageIndex > 0)
		{
			self.unselIndex('nav_index'+self.PageIndex)
			self.PageIndex--;
			self.selIndex('nav_index'+self.PageIndex)
			self.getNewSortPage();
		}
	}

	this.jumpIndex = function(new_index)
	{
		self.unselIndex('nav_index'+self.PageIndex)
		self.PageIndex = new_index;
		self.selIndex('nav_index'+self.PageIndex)
		self.getNewSortPage();
	}
	
	this.unselIndex = function(id)
	{
		$('#'+id).removeClass("sel");
		$('#'+id).addClass("unsel");
	}
	
	this.selIndex = function(id)
	{
		$('#'+id).removeClass("unsel");
		$('#'+id).addClass("sel");
	}

	this.toggleNav = function()
	{
		if(self.ShowAll)
		{
			self.ShowAll = false;
			self.ShopNav.style.display = "inline";
			self.PageIndex = 0;
			self.selIndex('nav_index'+self.PageIndex);
			self.getNewSortPage();
			self.ShowAllTag.innerHTML = "Show All";
		}
		else
		{
			self.ShowAll = true;
			self.ShopNav.style.display = "none";
			self.unselIndex('nav_index'+self.PageIndex);
			self.PageIndex = -1;
			self.getNewSortPage();
			self.ShowAllTag.innerHTML = "Show in Pages";
		}
	}

	this.showOverlay = function(element, show)
	{
		if(show == 1)
		{
			element.getElementsByTagName("div")[0].style.display = "block";
		}
		else
		{
			element.getElementsByTagName("div")[0].style.display = "none";
		}
	}
}

