function ExtensionSettings(){
}

ExtensionSettings.prototype.initialize = function(jq){
  this.$ = jq;
}

ExtensionSettings.prototype.returnExtensionsData_ = function(extensionsData){
	$(".ext-item").remove();
	GE.initExtData = true;
	if (extensionsData.extensions.length > 0) {
		// Enforce order specified in the data or (if equal) then sort by
				// extension name (case-insensitive).
		extensionsData.extensions.sort(function(a, b) {
			if (a.order == b.order) {
				a = a.name.toLowerCase();
				b = b.name.toLowerCase();
				return a < b ? -1 : (a > b ? 1 : 0);
			} else {
				return a.order < b.order ? -1 : 1;
			}
		});
	}
	
	//开发者模式开关      
	extensionsData.extensions.forEach(function(extension, index, array1){
		var node = $("#tmpl_extension_item").tmpl({
				ico_url : extension.icon.replace(/chrome/g, "kwe"),
				ext_name : extension.name,
				ext_ver : extension.version,
				ext_detail : extension.description,
				ext_id : extension.id,
				has_toolbar_btn : extension.hasOwnProperty("enable_show_button")});
			$.tmpl.complete();
				
			(function(){
				//初始化启用禁用按钮
				var eable_node = $(".ext-enable", node);
				if(extension.enabled){
					eable_node.removeClass("blueBg");
					eable_node.text("停用");
					$(".exp-font",node).removeClass("fontColor");
				}else{
					eable_node.addClass("blueBg");
					eable_node.text("启用");
					$(".exp-font",node).addClass("fontColor");
				}
			})();
	
			//if (!extension.enabled || extension.terminated)
			//node.classList.add('inactive-extension');
	
			if(extension.id == GE.defaultExtid){
				$(node).addClass("def-sel");
			}
			
			if(extension.enable_show_button){
				$(".ext-show-on-tb",node).click(function(){
					var ext_id =$(this).parent().parent().parent().attr("id");
					chrome.send('extensionSettingsShowButton', [ext_id]);
				});
				$(".ext-show-on-tb",node).show();
			}

		
		if(extension.enabled && extension.optionsUrl){
			$(".options-link", node).click(function(e){
								var ext_id =$(this).parent().parent().parent().parent().attr("id");
				chrome.send('extensionSettingsOptions', [ext_id]);
				e.preventDefault();
			});
			$(".options-link", node).show();
		}else{
				$(".options-link", node).hide();
		}
		
		$(".ext-delete",node).click(function(){
			var ext_id =$(this).parent().parent().parent().attr("id");
			chrome.send('extensionSettingsUninstall', [ext_id]);
		});
		
		$(".ext-enable",node).click(function(){
			var ext_id =$(this).parent().parent().parent().attr("id");
			var canEnable = $(this).hasClass("blueBg");
			if(canEnable){
				$(this).removeClass("blueBg");
				$(this).text("停用");
			}else{
				$(this).addClass("blueBg");
				$(this).text("启用");
			}
			
			chrome.send('extensionSettingsEnable',[ext_id, canEnable ? 'true' : 'false']);
		});
						
		$(".ext-check-view",node).click(function(){
			extension.views.forEach(function(view, i) {
					chrome.send('extensionSettingsInspect', [
						String(extension.id),
						String(view.renderProcessId),
						String(view.renderViewId),
						view.incognito
					]);
			});
		});
		
		$(".ext-privilege", node).click(function(){
			var ext_id =$(this).parent().parent().parent().parent().attr("id");
			chrome.send('extensionSettingsPermissions', [ext_id]);
		});
		
		$(".ext-content-list").append(node);
	});
	
	if($(".ext-content-list").children().length == 0){
		$(".no-results").show();
	}else{
		$(".no-results").hide();
	}
 
	var developerMode = extensionsData.developerMode;
	$("#dev_mode_chbox").attr("checked", developerMode);
	developerMode ? $(".ext-dev-mode").show() : $(".ext-dev-mode").hide();
}

ExtensionSettings.returnExtensionsData = function(extensionsData){
	ExtensionSettings.getInstance().returnExtensionsData_(extensionsData);
};

// |pem_path| files.  Ask if user wants override the warning.  Send
// |overrideFlags| to repeated 'pack' call to accomplish the override.
ExtensionSettings.askToOverrideWarning = function(message, crx_path, pem_path, overrideFlags) {
	var oParam = {};
	oParam.caption = "打包扩展程序警告";
	oParam.content = message;
	oParam.cancel_text = "取消";
	oParam.ok_text = "任然继续";
	oParam.fnOkCallback = function(){
		try{
			chrome.send('pack', [crx_path, pem_path, overrideFlags]);
		}catch(e){}
	};
	oParam.fnCancelCallback = function(){
	};   
	showCommonModalDlg(oParam);
}

ExtensionSettings.prototype.showDragDropOverlay = function(bShow){
  if(bShow){
    var node = $("#tmpl_drag_drop_overlay").tmpl({});
    $.tmpl.complete();
    $("body").append(node);
  }else{
    $("#dropTargetOverlay").remove();
  }
}
