curType = 2;
curGroupSort = 0;
curAsc = true;
curFirstChar = "a";
curState = 0;

function refreshUI()
{
	$("#a_typeall").css( "background-color", curType == 0 ? "#666666" : "transparent" );
	$("#a_typelp").css( "background-color", curType == 1 ? "#666666" : "transparent" );
	$("#a_typecd").css( "background-color", curType == 2 ? "#666666" : "transparent" );
	$("#a_typedvd").css( "background-color", curType == 3 ? "#666666" : "transparent" );
	$("#a_typebook").css( "background-color", curType == 4 ? "#666666" : "transparent" );
	
	$("#a_groupsortartist").css( "background-color", curGroupSort == 0 ? "#666666" : "transparent" );
	$("#a_groupsortname").css( "background-color", curGroupSort == 1 ? "#666666" : "transparent" );
	$("#a_groupsortlabel").css( "background-color", curGroupSort == 2 ? "#666666" : "transparent" );
	
	$("#a_stateboth").css( "background-color", curState == 0 ? "#666666" : "transparent" );
	$("#a_statenew").css( "background-color", curState == 1 ? "#666666" : "transparent" );
	$("#a_stateused").css( "background-color", curState == 2 ? "#666666" : "transparent" );
	
	$("#a_sortup").css( "background-color", curAsc ? "#666666" : "transparent" );
	$("#a_sortdown").css( "background-color", ! curAsc ? "#666666" : "transparent" );
	
	$("#a_alphaall").css( "background-color", curFirstChar == "" ? "#666666" : "transparent" );
	
	lowers = "abcdefghijklmnopqrstuvwxyz";
	for( i = 0; i < 26; i++ )
	{
		$("#a_alpha" + lowers[ i ] ).css( "background-color", curFirstChar == lowers[ i ] ? "#666666" : "transparent" );
	}
}

function addToCart( pk )
{
	$.getJSON( "/ajax/addtocart/", { pk : pk }, function( json )
	{
		
	} );
	
	refreshCartUI();
}

function removeFromCart( pk )
{
	$.getJSON( "/ajax/removefromcart/", { pk : pk }, function( json )
	{
		
	} );
	
	refreshCartUI();
}

function refreshCartUI()
{
	$.getJSON( "/ajax/cart/getinnerhtml/", { }, function( json )
	{
		$("#sp_cart").html( json[ 'innerHtml' ] ); 
	});
}

function setType( type )
{
	curType = type;
	refreshStockItems();
}

function setGroupSort( groupSort )
{
	curGroupSort = groupSort;
	refreshStockItems();
}

function setAsc( asc )
{
	curAsc = asc;
	refreshStockItems();
}

function setFirstChar( firstChar )
{
	curFirstChar = firstChar;
	refreshStockItems();
}

function setState( state )
{
	curState = state;
	refreshStockItems();
}

function refreshStockItems()
{
	$("#stockitems").fadeOut( 100 );
	$.getJSON( "/ajax/stockitems/getinnerhtml/", 
	           { type : curType, groupSort : curGroupSort, asc : curAsc, firstChar : curFirstChar, state : curState }, 
	           function( json )
	{
		// alert( json[ 'success' ] );
		
		$("#stockitems").html( json[ 'innerHtml' ] ); 
		$("#stockitems").fadeIn( 100 );
	});
	
	refreshUI();
}
