// Create global object to "namespace" SC specific code
if ( ! window.sc) {

	window.sc = (function () {
	
		var localized_strings = {};
		
		return {
		
			// Function to ease localization of strings between PHP and JS
			// Takes an object with keys of string constants and 
			// values of localized values
			add_strings: function (strings) {
			
				for (var key in strings) {
				
					localized_strings[key] = strings[key];
				
				}
			},
			
			string: function (key) {
			
				if (localized_strings[key] !== undefined) {
				
					return localized_strings[key];
				
				} else {
				
					throw "No match for localized string: '" + key + "'";
					return false;
				
				}
			},
			
			promote_items: function (item_ids, category_ids, item_type) {
			
				var result = [];
			
				$.ajax({
					type: "POST",
					url: "/admin/promote_items",
					data: {
						'content_ids[]': item_ids,
						'category_ids[]': category_ids,
						'content_type': item_type
					},
					dataType: "json",
					error: function() {
						result = false;
					},
					success: function (json) {
			
						result = (json && json.status == 'SUCCESS')
							? json.ids
							: false;
		
					}
				});
				
				return result;	
			},
			
			promotions_for_item: function (item_id, item_type) {
			
				var promotion_ids = [];
			
				$.ajax({
					url: "/admin/promotions_for_item/" + item_id + "/" + item_type,
					async: false,
					data: {
						item_id: item_id,
						item_type: item_type
					},
					dataType: "json",
					type: "POST",
					success: function (json) {
					
						if (json && json.success) {
						
							promotion_ids = json.category_ids;
						
						}
					}
				});
			
				return promotion_ids;
			},
			
			dialog: {

				create_dialog: function (elm, buttons) {
				
					$(elm)
						.dialog({
							autoOpen: false,
							width: 300,
							modal: true,
							buttons: buttons
						});
						
					$(elm).default_html = $(elm).html();
					
					return $(elm);
				},

				cancel: function () {
					$(this).dialog("close");
					return false;
				},
				
				default_buttons: function () {
				
					var buttons = {};
					
					buttons[sc.string("SC_COMMON_TEXT_CANCEL")] = this.cancel;
					
					return buttons;
				}
								
			}
		};			
	}());

}
