
function checkObject (name) {
	var object = null;
	eval('if (' + name + ') object = ' + name + ';');
	return object;
}

$(function() {
	$('.actionShowGadgetThemes').click(function() {
		var anchor = $(this).attr('href').split('#')[1];
		var obj = checkObject(anchor);
		if (obj) {
			var themesNode = $('#' + obj.type + 'ThemesContainer');
			if(!$(themesNode).attr('filled')) {
				for (themeGroup in obj.themeGroups) {
					var themes = obj.themeGroups[themeGroup].elements.split(',');
					var groupDiv = $('<div class="sfGadgetThemes simpleList ' + obj.type + '" />')
						.appendTo(themesNode);
					$('<h3 class="sfHeading">' + obj.themeGroups[themeGroup].name + '</h3>')
						.appendTo(groupDiv);
					for (theme=0; theme < themes.length; theme++) {
						var themeNode = $('<span class="item" />')
							.appendTo(groupDiv);
						var link = $('<a href="#" class="sfItem"><span class="title">' + i18n.iface.button.zoom + '</span></a>')
							.attr({
								'preview-image': obj.baseUrl + '/' + themes[theme] + '/' + 'preview.jpg',
								'title': i18n.gadget.theme.zoomTitle
							})
							.appendTo(themeNode)
							.click(function() {
								$('#themeGalleryPreview').parent().dialog('destroy');
								$('#themeGalleryPreview').remove();
								$('<img src="' + $(this).attr('preview-image') + '" title="' + i18n.gadget.theme.zoomWindowTitle + '" id="themeGalleryPreview" />')
									.appendTo(document.body)
									.hide()
									.load(function() {
										$.sfWindow.open('#themeGalleryPreview');
									})
								return false;
							});
						$('<img>', {
							src: obj.baseUrl + '/' + themes[theme] + '/' + obj.thumbnail + '.jpg'
						}).prependTo(link);
						$('<a href="#" class="link">' + i18n.iface.button.order + '</a>')
							.appendTo(themeNode)
							.attr({
								form: 'sfGadgetCreate_' + obj.type,
								theme: themes[theme]
							})
							.click(function() {
								$('#' + $(this).attr('form') + '_theme').val($(this).attr('theme'));
								$('#' + $(this).attr('form')).submit();
								return false;
							});
					}
				}
				$(themesNode).attr('filled', 'true');
			}
			$('html,body').animate({
				scrollTop: $('#' + anchor).offset().top
			});
		}
		return false;
	});
});
