var searchUrl = "resource://livemargins/apps/search/search.html?";

var engines_map = {
    'wikipedia': {
        name: 'wikipedia',
        background: 'http://en.wikipedia.org/favicon.ico',
        checked: 'true',
        desc: 'Wikipedia',
    }, 
    'dict': {
        name: 'dict',
        background: 'http://shared.youdao.com/images/favicon.ico',
        checked: 'true',
        desc: '有道词典及翻译',
    }, 
    'baidu-baike': {
        name: 'baidu-baike',
        background: 'http://www.baidu.com/favicon.ico',
        checked: 'true',
        desc: '百度百科',
    }, 
    'local': {
        name: 'local',
        background: 'http://aibang.com/favicon.ico',
        checked: 'true',
        desc: '爱帮本地搜索',
    }, 
    
    'flickr': {
        name: 'flickr',
        background: 'http://www.flickr.com/favicon.ico',
        checked: 'true',
        desc: 'Flickr图片搜索',
    }, 
    'google-image': {
        name: 'google-image',
        background: 'http://www.google.cn/favicon.ico',
        checked: 'true',
        desc: '谷歌图片搜索',
    }, 
    
    'youku': {
        name: 'youku',
        background: 'http://www.youku.com/favicon.ico',
        checked: 'true',
        desc: '优酷视频搜索',
    }, 
    'tudou': {
        name: 'tudou',
        background: 'http://www.tudou.com/favicon.ico',
        checked: 'true',
        desc: '土豆视频搜索',
    }, 
    
    'google-news': {
        name: 'google-news',
        background: 'http://www.gstatic.cn/news/img/favicon.ico',
        checked: 'true',
        desc: '谷歌新闻搜索',
    }, 
    'stock': {
        name: 'stock',
        background: 'http://www.sina.com.cn/favicon.ico',
        checked: 'true',
        desc: '股票搜索',
    }, 
    'verycd': {
        name: 'verycd',
        background: 'http://www.verycd.com/favicon.ico',
        checked: 'true',
        desc: 'Verycd电驴搜索',
    }, 
    'douban-movie': {
        name: 'douban-movie',
        background: 'http://www.douban.com/favicon.ico',
        checked: 'true',
        desc: '豆瓣电影搜索',
    }, 
    'douban-book': {
        name: 'douban-book',
        background: 'http://www.douban.com/favicon.ico',
        checked: 'true',
        desc: '豆瓣书籍搜索',
    }, 
    
    'youdao-shopping': {
        name: 'youdao-shopping',
        background: 'http://shared.youdao.com/images/favicon.ico',
        checked: 'true',
        desc: '有道购物搜索',
    }, 
    'betty': {
        name: 'betty',
        background: 'chrome://livemargins/skin/images/bettykitchen.png',
        checked: 'true',
        desc: '贝太生活搜索',
    }, 
    'buy360': {
        name: 'buy360',
        background: 'http://www.360buy.com/favicon.ico',
        checked: 'true',
        desc: '京东购物搜索',
    }, 
};

var engines = ['wikipedia', 'dict', 'baidu-baike', 'local', '.', 
          'flickr', 'google-image', '.',
          'youku', 'tudou', '.',
          'google-news', 'stock', 'verycd', 'douban-movie', 'douban-book', '.',
          'youdao-shopping', 'betty', 'buy360'];
         
var SearchConfigPage = {
    searchString: '',
    
	init: function(){
		var searchString = "";
		try {
			var [, , searchInput] = window.location.href.match(/^(.*)\?(.*)$/);
			searchString = jQuery.trim(decodeURIComponent(searchInput));
		} catch (e) {
			searchString = "";
		}
		
		contentAPI.setBoolPref('config.message', false);
		
		this.searchString = searchString;
		
		var self = this;
		window.setTimeout(function() {
			self.loadConfigs();
		}, 0);
	},
	
	loadConfigs: function() {
	    var omitEngines = '';
	    try {
	        omitEngines = contentAPI.getCharPref('engines.omit');
	    } catch(e) {
	        omitEngines = '';
	    }
	    omit_engines = omitEngines.split(',');
	    for(var i = 0; i < omit_engines.length; i += 1) {
	        var engine = omit_engines[i];
	        if (engine && engine.length > 0 && engine in engines_map) {
	            engines_map[engine].checked = false;
	        }
	    }
	    
	    // show config checkbox
	    for (var i = 0; i < engines.length; i += 1) {
	        var engine = engines[i];
	        if (engine !== '.' && !(engine in engines_map)) {
	            continue;
	        }
	        if (engine !== '.') {
	            var template = unescape($('#engine-template').html());
    			var element = $(TrimPath.parseTemplate(template).process(engines_map[engine]));
    			$('#engines-content').append(element);
	        }
	        if (i != engines.length - 1) {
	            $('#engines-content').append($('<br>'));
	        }
	    }
	},
	
	saveConfigs: function() {
	    var omit_engines = [];
	    for (var i = 0; i < engines.length; i += 1) {
	        var engine = engines[i];
	        if (engine === '.' || !(engine in engines_map)) {
	            continue;
	        }
	        var element = $('input#' + engines_map[engine].name)[0];
	        if (!element.checked) {
	            omit_engines.push(engines_map[engine].name);
	        }
	    }
	    contentAPI.setCharPref('engines.omit', omit_engines.join(','));
	    window.location.href = searchUrl + encodeURIComponent(this.searchString);
	},
};

function saveConfigs() {
    SearchConfigPage.saveConfigs();
}

function init() {
	$("#config-button").click(function() {
		saveConfigs();
	});
	SearchConfigPage.init();
}
      