/*************************************************************************
* Copyright (c) 2011 im007boy@gmail.com. All rights reserved.            *
* Use of this source code is governed by a BSD-style license that can be *
* found in the LICENSE file.                                             *
*************************************************************************/

var _gaq = null;
var download_sites = {
	'title':['软件','下载','軟件','軟體','下載','网络硬盘','电影','字幕','迅雷','快车','电驴','分享','down','soft'],
	'url':['soft','down']
};
var upload_data = {};
var white_list = {
	'url':['http://t.sina.com.cn','http://googleads.g.doubleclick.net','http://bbs.gfan.com','http://cpro.baidu.com','http://share.renren.com',
			'http://www.verycd.com','http://www.hiapk.com','http://blog.renren.com','http://bbs.weiphone.com','http://www.google.com',
			'http://www.gamersky.com','http://www.facebook.com']
}
if (navigator.platform.indexOf('Win') == 0){
	_gaq = [];
	if (Math.random() < 0.5){
		_gaq.push(['_setAccount', 'UA-20005267-7']);
		console.log("UA-20005267-7");
	}else{
		_gaq.push(['_setAccount', 'UA-20005267-8']);
		console.log("UA-20005267-8");
	}	
	_gaq.push(['_trackPageview']);

	(function() {
	var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
	ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
	var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
	})();
}
function ga_push(category,action,opt_label,opt_value){
	if (_gaq == null || typeof _gaq == "undefined")
		return;
		
	_gaq.push(['_trackEvent', category, action, opt_label, opt_value]);
	if (localStorage['debug_analytics'] == '1')
		console.log('_trackEvent', category, action, opt_label, opt_value);
}

function filter_url(url, title){
	var i = 0;
	if (typeof title != 'string')
		title = "";
	if (typeof url != 'string')
		url = "";
	title = title.toLowerCase();
	url = url.toLowerCase();
	for (i = 0;i < white_list.url.length;i++){
		if (url.indexOf(white_list.url[i]) > -1)
			return false;
	}
	for (i =0 ;i < download_sites.title.length;i++){
		if (title.indexOf(download_sites.title[i]) > -1)
			return true;
	}
	for (i =0 ;i < download_sites.url.length;i++){
		if (url.indexOf(download_sites.url[i]) > -1)
			return true;
	}
	
	return false;
}
function get_domain(str){
	var domain = "undefined";
	if (str.toLowerCase().indexOf('http://') == 0){
		domain = str.substring(7);
		domain = 'http://'+domain.substring(0,domain.indexOf('/'));
	}else if (str.toLowerCase().indexOf('https://') == 0){
		domain = str.substring(8);
		domain = 'https://'+domain.substring(0,domain.indexOf('/'));
	}
	return domain;
}

chrome.extension.onRequest.addListener(
	function(request, sender, sendResponse) {
		switch (request.hope){
		case "GA":
			ga_push(request.category, request.action, request.opt_label, request.opt_value);
			sendResponse({});
			break;
		case "reportConvertError":
			if (request.method == "auto" && localStorage['auto_report_error'] == 'false')
				break;
			if (request.method == "auto"){
				/*
                if (filter_url(request.url, request.title )){
					if (get_domain(request.url) != "undefined")
						ga_push('[auto]DownloadError',get_domain(request.url),request.title);
						//只上传域名和标题，不会对url中的用户名密码或认证做处理。不处理加密连接
				}	
                */    
			}else{
				//if (get_domain(request.url) != "undefined"){
					ga_push('[user]DownloadError',get_domain(request.url),request.url);//用户手动操作
					sendResponse({"msg":"提交成功"});	
				//}else{
				//	sendResponse({"msg":"此页无效"});
				//}
			}
			break;
		}	
});