var url = '';

function createUrl(type, values) {
	if (type == 'antique') {
		url = '/index.pl?isa=Metadot::SystemApp::CatalogSearch;op=selection_view;antique_search=Y;';
	} else if (type == 'artist') {
		url = '/index.pl?isa=Metadot::SystemApp::ArtistSearch;selections=Y;searching=1;op=show;paging=1;';
	} else if (type == 'school') {
		url = '/index.pl?isa=Metadot::SystemApp::AntiqueSearch;op=school_search;selections=Y;paging=1;modern=Y;';
	} else if (type == 'exhibition') {
		url = '/index.pl?isa=Metadot::SystemApp::Exhibitions;searching=1;';
	} else if (type == 'dealer') {
		url = '/index.pl?isa=Metadot::SystemApp::DealerSearch;searching=1;op=show;';
	}
         
	if (values.category) {
	        url = url + 'category=' + values.category + ';';
	}
	if (values.medium) {
        	url = url + 'medium=' + values.medium + ';';
	}
	if (values.style) {
        	url = url + 'style=' + values.style + ';';
	}
	if (values.style_cat) {
        	url = url + 'style_cat=' + values.style_cat + ';';
	}
	if (values.location) {
        	url = url + 'location=' + values.location + ';';
	}
	if (values.period) {
        	url = url + 'period=' + values.period + ';';
	}
	if (values.pagesize) {
        	url = url + 'pagesize=' + values.pagesize + ';';
	}
	if (values.order) {
        	url = url + 'order=' + values.order + ';';
	}
	if (values.artist) {
		url = url + 'artist=' + values.artist + ';';
	}
	if (values.online_gallery) {
		url = url + 'online_gallery=' + values.online_gallery + ';';
	}
}

function showRemovals() {
	$('#currently-showing').show('slow');
}
        
function hideNoMoreRefine() {
	$('#no-more-refine').hide();
}

function doSelections(selections) {  
	var first = 0;
	var last = 0;
	if (selections) {
		selections = selections.selections;
		if (selections.cats) {
			populate_categories(selections.cats);
			first = 1;
			last = 1;
		}
		if (selections.mediums) {
			populate_mediums(selections.mediums);
			if (first == 0) {
				first = 2;
			}
			if (last < 2) {
				last = 2;
			}
		}
		if (selections.styles) {
			populate_styles(selections.styles);
			if (first == 0) {
				first = 3;
			}
			if (last < 3) {
				last = 3;
			}
		}
		if (selections.locations) {
			populate_locations(selections.locations);
			if (first == 0) {
				first = 4;
			}
			if (last < 4) {
				last = 4;
			}
		}
		if (selections.periods) {  
			populate_periods(selections.periods);
			if (first == 0) {
				first = 5;
			}
			if (last < 5) {
				last = 5;
			}
		}
		if (selections.artists) {
			populate_artists(selections.artists);
			if (first == 0) {
				first = 6;
			}
			if (last < 6) {
				last = 6;
			}
		}
		shapeIt(first, last);
	}
}

function shapeIt(first, last) {
	var ids = new Array;
	ids[1] = 'cat-refinements';
	ids[2] = 'medium-refinements';
	ids[3] = 'style-refinements';
	ids[4] = 'location-refinements';
	ids[5] = 'period-refinements';
	ids[6] = 'artist-refinements';
	if (first == last) {
		makeRounded(ids[first]);
	} else {
		for (var i = 1; i < 7; i++) {
			if (i != first && i != last) {
				makeStraight(ids[i]);
			}
		}
		makeUpperRounded(ids[first]);
		makeLowerRounded(ids[last]);
	}
	
//	$('.lower-rounded').click(function() {
		//if ($(this).hasClass('straight')) {
//			$(this).removeClass('straight');
//		} else {
//			$(this).addClass('straight');
//		}
//	});
}

function makeRounded(id) {
	$('#' + id + ' .cat-sidebar-sub').addClass('rounded-all');
}

function makeStraight(id) {
	$('#' + id + ' .cat-sidebar-sub').addClass('straight');
}

function makeUpperRounded(id) {
	$('#' + id + ' .cat-sidebar-sub').addClass('upper-rounded');
}

function makeLowerRounded(id) {
	$('#' + id + ' .cat-sidebar-sub').addClass('lower-rounded');
}

function populate_categories(categories) {
	for (var x in categories) {  
		addCategory(categories[x]);
	}
	if (categories.length > 0) {
		hideNoMoreRefine();
		$('#cat-refinements').show('slow');
	}
}

function populate_mediums(mediums) {
	for (var x in mediums) {
		addMedium(mediums[x]);
	}
	if (mediums.length > 0) {   
		hideNoMoreRefine();
		$('#medium-refinements').show('slow');
	}
}

function populate_styles(styles) {
	for (var x in styles) {
		addStyle(styles[x]);
	}
	if (styles.length > 0) {
		hideNoMoreRefine();
		$('#style-refinements').show('slow');
	}
}

function populate_locations(locations) {
	for (var x in locations) {
		addLocation(locations[x]);
	}
	if (locations.length > 0) {
		hideNoMoreRefine();
		$('#location-refinements').show('slow');
	}
}
 
function populate_periods(periods) {
	for (var x in periods) {
		addPeriod(periods[x]);
	}
	if (periods.length > 0) {
		hideNoMoreRefine();
		$('#period-refinements').show('slow');
	}
}

function populate_artists(artists) {
	for (var x in artists) {
		addArtist(artists[x]);
	}
	if (artists.length > 0) {
		hideNoMoreRefine();
		$('#artist-refinements').show('slow');
	}
}

function addRemoval(name, type, id, parents) {
	var html = '';
	var count = 1;
	var last_parent = '';
	if (parents != null) {
		for (var x in parents) {
			if (parents[x].parent != 0) {
				var padding = 15 * count;
				var ehtml = '<li style="padding-left: ' + padding + 'px;"><div onclick=\'location.href="' + url.replace(type, 'na') + ";" + type + "=" + parents[x].uid + "\";'><a href='" + url.replace(type, 'na') + ";" + type + "=" + parents[x].uid + "'>" + parents[x].name + "</a></div></li>";
				html = html + ehtml;
				count++;
				last_parent = parents[x].uid;
			}
		}
	}	
	var myUrl = url.replace(type, 'na');
	if (last_parent != '') {
		myUrl += ";" + type + "=" + last_parent + ";";
	}
	var padding = 15 * count;
	html += "<li class='removal' style='padding-left: " + padding + "px;'><div onclick='location.href=\"" + myUrl + ";\";';><a href='" + myUrl + "'>" + name + "</a></div></li>";		
	$('#currently-showing ul').append(html);
	showRemovals();
}

function addCategory(category) {
	var myUrl = url.replace('category', 'na');
	var html = '<li><a href="' + myUrl + ';category=' + category.uid + '">' + category.name + '</a></li>';
	$('#cat-refinements ul').append(html);
}

function addMedium(medium) {
	var myUrl = url.replace('medium', 'na');
	var html = '<li><a href="' + myUrl + ';medium=' + medium.uid + ';">' + medium.name + '</a></li>';
	$('#medium-refinements ul').append(html);
}

function addStyle(style) {
	var myUrl = url.replace('style', 'na');
	var html = '<li><a href="' + myUrl + ';style=' + style.uid + ';">' + style.name + '</a></li>';
	$('#style-refinements ul').append(html);
}

function addLocation(location) {
	var myUrl = url.replace('location', 'na');
	var html = '<li><a href="' + myUrl + ';location=' + location.uid + ';">' + location.name + '</a></li>';
	$('#location-refinements ul').append(html);
}

function addPeriod(period) {
	var myUrl = url.replace('period', 'na');
	var html = '<li><a href="' + myUrl + ';period=' + period.uid + ';">' + period.name + '</a></li>';
	$('#period-refinements ul').append(html);
}

function addArtist(artist) {
	var myUrl = url.replace('artist', 'na');
	var html = '<li><a href="' + myUrl + ';artist=' + artist.uid + ';">' + artist.name + '</a></li>';
	$('#artist-refinements ul').append(html);
}
