function getIcons() {
	$.ajax({
		type: "POST",
		url: "/tnt/inc/php/utils/icons-get.php",
		dataType: "html",
		success: handleResponseGetIcons
	});
}

function handleResponseGetIcons(data, status) {
	responseGetIcons(data);
}

var iconreq;
function getIcon(iconid, callback) {
	if (iconreq != undefined) iconreq.abort();
	iconreq = $.ajax({
		type: "POST",
		url: "/tnt/inc/php/utils/icon-get.php",
		data: "icon_id="+iconid+"&callback="+callback,
		dataType: "xml",
		success: handleResponseGetIcon,
		error: handleResponseGetIconError
	});
}

function handleResponseGetIconError(XMLHttpRequest, textStatus, errorThrown) {
}

function handleResponseGetIcon(data, status) {
	var callback = $("callback", data).text();
	
	var icondata = {
		id:					$("id", data).text(),
		title:				$("title", data).text(),
		year:				$("year", data).text(),
		introduction:		$("introduction", data).text()
		
	}

	icondata.properties = {
		brand:				$("brand", data).text(),
		material:			$("material", data).text(),
		technique:			$("technique", data).text(),
		width:				$("width", data).text(),
		height:				$("height", data).text(),
		depth:				$("depth", data).text(),
		type:				$("type", data).text(),
		printer:			$("printer", data).text(),
		manufacturer:		$("manufacturer", data).text(),
		location:			$("location", data).text(),
		value:				$("value", data).text(),
		inuse:				$("inuse", data).text(),
		circulation:		$("circulation", data).text(),
		sales:				$("sales", data).text()
	}
	
	icondata.mainimages = new Array();
	$("mainimages", data).find("mainimage").each(function(i, n) {	
		var mainimage = {
			path:			$("mainimage_path", n).text(),
			filename:		$("mainimage_filename", n).text(),
			width:			$("mainimage_width", n).text(),
			height:			$("mainimage_height", n).text()
			
		}
		icondata.mainimages.push(mainimage);
	});	
	
	
	
	/*
	icondata.mainimage = {
		path:				$("mainimage_path", data).text(),
		filename:			$("mainimage_filename", data).text()
	}
	*/
	
	
	icondata.relatedicons = new Array();
	$("relatedicons", data).find("relatedicon").each(function(i, n) {
		var relatedicon = {
			id:				$("related_id", n).text(),
			title:			$("related_title", n).text(),
			year:			$("related_year", n).text()
		}
		icondata.relatedicons.push(relatedicon);
	});
	
	icondata.designers = new Array();
	$("designers", data).find("designer").each(function(i, n) {
		var designer = {
			id:				$("designer_id", n).text(),
			name:			$("designer_name", n).text(),
			bio:			$("designer_bio", n).text(),
			website:		$("designer_website", n).text()
			
		}
		icondata.designers.push(designer);
	});
	
	icondata.sketches = new Array();
	$("sketches", data).find("sketch").each(function(i, n) {
		var sketch = {
			path:				$("sketch_path", n).text(),
			filename:			$("sketch_filename", n).text(),
			caption:			$("sketch_caption", n).text(),
			width:				$("sketch_width", n).text(),
			height:				$("sketch_height", n).text()

		}
		icondata.sketches.push(sketch);
	});
	
	icondata.storypages = new Array();
	$("story", data).find("page").each(function(i, n) {
		var page = {
			id:					$("page_id", n).text(),
			content:			$("page_content", n).text(),
			imgflv:				$("page_imgflv", n).text(),
			title:				$("page_title", n).text(),
			filmimage:			$("page_filmimage", n).text(),
			filmthumb:			$("page_filmthumb", n).text(),
			flashfilm:			$("page_flashfilm", n).text(),
			imagepath:			$("page_imgfilepath", n).text(),
			imagefilename:		$("page_imgfilename", n).text(),
			width:				$("page_width", n).text(),
			height:				$("page_height", n).text()

		}
		icondata.storypages.push(page);
	});
	
	if (window[callback]) window[callback](icondata);
}


function filterIcons(category, time, designer, sort, dir, search, callback) {
	$.ajax({
		type: "POST",
		url: "/tnt/inc/php/utils/icons-filter.php",
		data: "category="+category+"&time="+time+"&designer="+designer+"&sort="+sort+"&dir="+dir+"&search="+search+"&callback="+callback,
		dataType: "xml",
		success: handleResponseFilterIcons
	});
}

function handleResponseFilterIcons(data, status) {
	var callback = $("callback", data).text();
	
	var iconids = new Array();
	$("icons", data).find("id").each(function(i, n) {
			iconids.push($(this).text());
	});
	if (window[callback]) window[callback](iconids);
}

function sortIcons(sortOn, dir, callback) {
	$.ajax({
		type: "POST",
		url: "/tnt/inc/php/utils/icons-sort.php",
		data: "sort="+sortOn+"&dir="+dir+"&callback="+callback,
		dataType: "xml",
		success: handleResponseSortIcons
	});
}

function handleResponseSortIcons(data, status) {
	var callback = $("callback", data).text();
	
	var iconids = new Array();
	$("icons", data).find("id").each(function(i, n) {
			iconids.push($(this).text());
	});
	
	if (window[callback]) window[callback](iconids);
}

function searchIcons(search, callback) {
	$.ajax({
		type: "POST",
		url: "/tnt/inc/php/utils/icons-search.php",
		data: "search="+search+"&callback="+callback,
		dataType: "xml",
		success: handleResponseSearchIcons
	});
}

function handleResponseSearchIcons(data, status) {
	var callback = $("callback", data).text();
	
	var iconids = new Array();
	$("icons", data).find("id").each(function(i, n) {
			iconids.push($(this).text());
	});
	
	if (window[callback]) window[callback](iconids);
}
