var categories_output;
var mediums_output;
var styles_output;
var locations_output;
var periods_output;
var mappings_output;
var description_output;

var category_detail_output;
var medium_detail_output;
var style_detail_output;
var location_detail_output;
var period_detail_output;
var artist_detail_output;

var can_fetch_categories;
var can_fetch_mediums;
var can_fetch_styles;
var can_fetch_locations;
var can_fetch_periods;
var can_fetch_mappings;
var can_fetch_description;

var can_detail_category;
var can_detail_medium;
var can_detail_style;
var can_detail_location;
var can_detail_period;
var can_detail_artist;

var visible = 1;

function set_visible(val) {
	visible = val;
}

function fetch_description(category, medium, style, location, date, period) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_description;";
	if (category != "" && category != undefined) {
		url += "category=" + escape(category) + ";";
	}
	if (medium != "" && medium != undefined) {
		url += "medium=" + escape(medium) + ";";
	}
	if (style != "" && style != undefined) {
		url += "style=" + escape(style) + ";";
	}
	if (location != "" && location != undefined) {
		url += "location=" + escape(location) + ";";
	}
	if (date != "" && date != undefined) {
		url += "date=" + escape(date) + ";";
	}
	if (period != "" && period != undefined) {
		url += "period=" + escape(period) + ";";
	}
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_description = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				description_output = $.evalJSON(message);
			}
			can_fetch_description = true;
		}
	});
}

function fetch_category(category) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_category;category=" + category + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_category = false;
		},
		success: function(message) {
			if (message.length > 5) {
				category_detail_output = $.evalJSON(message);
			}
			can_detail_category = true;
		}
	});
}

function fetch_artist(artist) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_artist;artist=" + artist + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_artist = false;
		},
		success: function(message) {
			if (message.length > 5) {
				artist_detail_output = $.evalJSON(message);
				artist_detail_output = artist_detail_output[0];
			}
			can_detail_artist = true;
		}
	});
}

function fetch_medium(medium) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_medium;medium=" + medium + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_medium = false;
		},
		success: function(message) {
			if (message.length > 5) {
				medium_detail_output = $.evalJSON(message);
			}
			can_detail_medium = true;
		}
	});
}

function fetch_style(style) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_style;style=" + style + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_style = false;
		},
		success: function(message) {
			if (message.length > 5) {
				style_detail_output = $.evalJSON(message);
			}
			can_detail_style = true;
		}
	});
}

function fetch_location(location) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_location;location=" + location + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_location = false;
		},
		success: function(message) {
			if (message.length > 5) {
				location_detail_output = $.evalJSON(message);
			}
			can_detail_location = true;
		}
	});
}

function fetch_period(period) {
	var url = "isa=Metadot::SystemApp::Catalog;op=detail_period;period=" + period + ";";

	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_detail_period = false;
		},
		success: function(message) {
			if (message.length > 5) {
				period_detail_output = $.evalJSON(message);
			}
			can_detail_period = true;
		}
	});
}

function fetch_categories(parent, keywords) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_categories;visible=" + visible + ";";
	if (parent != "" && parent != "undefined") {
		url += "parent=" + escape(parent) + ";";
	}
	if (keywords != "" && keywords != "undefined") {
		url += "keywords=" + escape(keywords) + ";";
	}
	url += "has_children=Y;";
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_categories = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				categories_output = $.evalJSON(message);
				categories_output = categories_output.categories;
			}
			can_fetch_categories = true;
		}
	});
}

function fetch_mediums(parent, keywords) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_mediums;visible=" + visible + ";";
	if (parent != "" && parent != "undefined") {
		url += "parent=" + escape(parent) + ";";
	}
	if (keywords != "" && keywords != "undefined") {
		url += "keywords=" + escape(keywords) + ";";
	}
	url += "has_children=Y;";
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_mediums = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				mediums_output = $.evalJSON(message);
				mediums_output = mediums_output.mediums;
			}
			can_fetch_mediums = true;
		}
	});
}

function fetch_styles(parent, keywords, categories) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_styles;visible=" + visible + ";";
	if (parent != "" && parent != "undefined") {
		url += "parent=" + escape(parent) + ";";
	}
	if (keywords != "" && keywords != "undefined") {
		url += "keywords=" + escape(keywords) + ";";
	}
	if (categories != undefined) {
		for (var x in categories) {
			url += "category=" + escape(categories[x]) + ";";
		} 
		url += "restrict=Y;";
	}
	url += "has_children=Y;";
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_styles = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				styles_output = $.evalJSON(message);
				styles_output = styles_output.styles;
			}
			can_fetch_styles = true;
		}
	});
}

function fetch_locations(parent, keywords) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_locations;";
	if (parent != "" && parent != "undefined") {
		url += "parent=" + escape(parent) + ";";
	}
	if (keywords != "" && keywords != "undefined") {
		url += "keywords=" + escape(keywords) + ";";
	}
	url += "has_children=Y;";
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_locations = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				locations_output = $.evalJSON(message);
				locations_output = locations_output.locations;
			}
			can_fetch_locations = true;
		}
	});
}

function fetch_periods(location, date, keywords) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_periods;";
	if (location != "" && location != "undefined") {
		url += "location=" + escape(location) + ";";
	}
	if (date != "" && date != "undefined") {
		url += "date=" + escape(date) + ";";
	}
	if (keywords != "" && keywords != "undefined") {
		url += "keywords=" + escape(keywords) + ";";
	}
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_periods = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				periods_output = $.evalJSON(message);
				periods_output = periods_output.periods;
			}
			can_fetch_periods = true;
		}
	});
}

function fetch_mappings(lang, list, id) {
	var url = "isa=Metadot::SystemApp::Catalog;op=fetch_mappings;";
	if (lang != "" && lang != "undefined") {
		url += "lang=" + lang + ";";
	}
	if (list != "" && list != "undefined") {
		url += "list=" + list + ";";
	}
	if (id != "" && id != undefined) {
		url += "parent=" + id + ";";
	}
	url += "has_children=Y;";
	$.ajax({
		type: "POST",
		url: "/index.pl",
		data: url,
		beforeSend: function(item) {
			can_fetch_mappings = false;
		},
		success: function(message) {	
  			if (message.length > 5) {							
				mappings_output = $.evalJSON(message);
				mappings_output = mappings_output.mappings;
			}
			can_fetch_mappings = true;
		}
	});
}

function can_fetch_categories_output() {
	return can_fetch_categories;
}

function can_fetch_category() {
	return can_detail_category;
}

function can_fetch_artist() {
	return can_detail_artist;
}

function can_fetch_medium() {
	return can_detail_medium;
}

function can_fetch_style() {
	return can_detail_style;
}

function can_fetch_location() {
	return can_detail_location;
}

function can_fetch_period() {
	return can_detail_period;
}

function can_fetch_description_output() {
	return can_fetch_description;
}

function fetch_categories_output() {
	return categories_output;
}

function fetch_description_output() {
	return description_output;
}

function can_fetch_mediums_output() {
	return can_fetch_mediums;
}

function fetch_mediums_output() {
	return mediums_output;
}

function can_fetch_styles_output() {
	return can_fetch_styles;
}

function fetch_styles_output() {
	return styles_output;
}

function can_fetch_locations_output() {
	return can_fetch_locations;
}

function fetch_locations_output() {
	return locations_output;
}

function can_fetch_periods_output() {
	return can_fetch_periods;
}

function fetch_periods_output() {
	return periods_output;
}

function can_fetch_mappings_output() {
	return can_fetch_mappings;
}

function fetch_mappings_output() {
	return mappings_output;
}

function fetch_category_detail() {
	return category_detail_output;
}

function fetch_medium_detail() {
	return medium_detail_output;
}

function fetch_style_detail() {
	return style_detail_output;
}

function fetch_location_detail() {
	return location_detail_output;
}

function fetch_period_detail() {
	return period_detail_output;
}

function fetch_artist_detail() {
	return artist_detail_output;
}
