$(document).ready(function() {
	/*
	 * adds a javascript parameter to determine whether the to display part 
	 * of the page in a thick box
	 */
	$("option.thickbox").each( function(){
		$(this).attr("value", $(this).attr("value")+ "&javascript=true");
	});	
	
	/*
	 * adds a page to the print basket
	 */
	$("a#add-to-basket").click( function(){
		showBasketQty("a#add-to-basket");
		return false;
	});
	
	/*
	 * every time a page loads - get the print basket qty
	 */
	showBasketQty("a.print-basket-qty");
	
	$("ul#group-sites").hide();
	$("select#sites").show();
	
	/*
	 * use ui to create a sortable list and does an ajax request back to the page
	 * and passes the the the required params through and updates the the page with the new order.
	 */
	
	$(function sorted() {
		$("#sortable").sortable({
			update: function(event, ui) {
				var idUrl = $(ui.item).children("a[href*='id']").attr("href");	
				var position = ui.item.next().attr("id");
				if( position == undefined){
					position = ui.item.prev().attr("id");				
				}
				var id = idUrl.substring(idUrl.indexOf("id")+3, idUrl.length);
				//alert(id.split("&")[0]);
				var url = idUrl.substring(0, idUrl.indexOf("?"))
				var dataString = "?action=saveRank" + "&position=" + position + "&id=" + id.split("&")[0];
				$('#sortable-list').load(url + dataString + ' #sortable', '', function(){sorted()});
			}
		});
	});
	
});

/*
 * gets the print basket qty and from the print basket page and displays it
 * on the header 
 */
function showBasketQty(target){
	$('.print-basket-qty').load($(target).attr('href') + ' #print-qty ', function(){
		$('#print-qty').show();
		$('.print-basket-qty').show();
	});	
}

function showThickBox(selected){
 tb_show(document.getElementById('sites').options[selected.selectedIndex].text,document.getElementById('sites').value,'images/cart.gif');
 }
