var pageSize = 20;

function sortby(listType, id, title,itemCount,orderby) {
	var oldOrderBy = $("#drilldown").attr("orderby");
	var newOrderBy=orderby;
	if (Math.abs(oldOrderBy) == Math.abs(orderby)) {
		newOrderBy = -(oldOrderBy);
	} else {
		newOrderBy = orderby;
	}
	
	drillDown(listType, id, title, itemCount, newOrderBy);
	
}
function drillDown(listType, id, title,itemCount,orderby) {

	pageTracker._trackPageview("/drilldown/"+listType+"/"+id);

	if ($("#drilldown").attr("title") != title) {
		document.title = document.title + " - "+title;
	}
	$("[id$='title']").removeClass("box-header-featured");
	$("#list-content").hide(300);
	$("#list-zoom").show();
	$("#drilldown").show(300);
	$("#drilldown-content").html('<img class="centered-image" src="/images/loading.gif" border="0"></img>').show();
	
	$("#drilldown-title").addClass("box-header-featured").html('<span id="drilldown-zoom" class="maximize-box">[ + ]</span><span>'+title+'</span>');;
	$("#tape").hide(300);

	$("#drilldown-zoom").click(function() {
		document.title = "CTM - "+$("#list-title span:last").text()+" - "+title;
  		$("#drilldown-content").show(300);
  		$("#drilldown-zoom").hide(300);
  		$("[id$='title']").removeClass("box-header-featured");
  		$("#drilldown-title").addClass("box-header-featured");
  		
  		$("#tape").hide(300);
  		
  		 
  	});

	$("#drilldown").attr("drilldown-id", id);
	$("#drilldown").attr("drilldown-listType", listType);
	$("#drilldown").attr("orderby", orderby);
	$("#drilldown").attr("recs", itemCount);
	$("#drilldown").attr("title", title);
	$.ajax({
		type: "GET",
		url : "get-list.php",
		dataType : "html",
		data: {type: listType, id: id, page: 0, pageSize: pageSize, orderby: orderby, recs: itemCount, title: title},
		success: function(html){
    		$("#drilldown-content").html(html+'<div id="Pagination" class="pagination"></div><div style="clear:both"></div>');
    		$("#Pagination").pagination(itemCount, {
					num_edge_entries: 2,
					num_display_entries: 8,
					items_per_page: pageSize,
					next_show_always: false,
					prev_show_always: false,
					header: 'Page',
					next_text: '',
					prev_text: '',
                    callback: pageselectCallback
                });
  		}
	});
}

function showMessage(id, key) {

	pageTracker._trackPageview("/browse/"+key);
	
	$("[id$='title']").removeClass("box-header-featured");
	$("#drilldown-content").hide(300);
	$("#drilldown-zoom").show(300);
	showTape(id);
}
function pageselectCallback(page_id, jq){
	var id = $("#drilldown").attr("drilldown-id");
	var listType = $("#drilldown").attr("drilldown-listType");
	var orderby = $("#drilldown").attr("orderby");
	var recs = $("#drilldown").attr("recs");
	var offset = $("#drilldown").offset();
	var title = $("#drilldown").attr("title");
	
	pageTracker._trackPageview("/browse/"+listType+"/"+id+"/page/"+page_id);
	
	offset.top = offset.top+($("#drilldown").height() / 2);
	offset.left = offset.left+($("#drilldown").width() / 2);
	$("#drilldown-table").append('<img id="drilldown-busy" class="centered-float" src="/images/loading.gif" border="0"></img>');
	$("#drilldown-busy").css({top:offset.top+'px', left:offset.left+'px'}).show();
    $.ajax({
		type: "GET",
		url : "get-list.php",
		dataType : "html",
		data: {type: listType, id: id, page: page_id, pageSize: pageSize, orderby : orderby, recs: recs, title : title},
		success: function(html){
    		$("#drilldown-table").html(html);
  		}
	});
}