/*************************************************************************
* 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.                                             *
*************************************************************************/
video_downloader = JSON.parse('{"http://www.tudou.com":"flvcd","http://www.youtube.com":"flvcd","http://v.youku.com":"flvcd","http://www.youku.com":"flvcd","http://www.56.com":"flvcd","http://mm.56.com":"flvcd","http://tieba.56.com":"flvcd","http://tv.56.com":"flvcd","http://v.ku6.com":"flvcd","http://hd.ku6.com":"flvcd","http://6.cn":"flvcd","http://you.video.sina.com.cn":"flvcd","http://v.sina.com.cn":"flvcd","http://video.sina.com.cn":"flvcd","http://sports.sina.com.cn":"flvcd","http://ent.sina.com.cn":"flvcd","http://eladies.sina.com.cn":"flvcd","http://blog.sina.com.cn":"flvcd","http://tieba.baidu.com":"flvcd","http://mv.baidu.com":"flvcd","http://video.qq.com":"flvcd","http://cgi.video.qq.com":"flvcd","http://live.qq.com":"flvcd","http://bb.news.qq.com":"flvcd","http://sports.qq.com":"flvcd","http://finance.qq.com":"flvcd","http://space.tv.cctv.com":"flvcd","http://www.cctv.com":"flvcd","http://v.cctv.com":"flvcd","http://video.cctv.com":"flvcd","http://sports.cctv.com":"flvcd","http://tv.sohu.com":"flvcd","http://s.sohu.com":"flvcd","http://v.sohu.com":"flvcd","http://v.blog.sohu.com":"flvcd","http://v.wangyou.com":"flvcd","http://tv.mofile.com":"flvcd","http://v.mofile.com":"flvcd","http://www.espnstar.com.cn":"flvcd","http://video.espnstar.com.cn":"flvcd","http://www.espnstar.com.tw":"flvcd","http://vlog.17173.com":"flvcd","http://media.17173.com":"flvcd","http://v.game.sohu.com":"flvcd","http://www.metacafe.com":"flvcd","http://www.veoh.com":"flvcd","http://you.joy.cn":"flvcd","http://tvplay.joy.cn":"flvcd","http://movie.joy.cn":"flvcd","http://db.joy.cn":"flvcd","http://auto.joy.cn":"flvcd","http://v.joy.cn":"flvcd","http://home.pomoho.com":"flvcd","http://www.megavideo.com":"flvcd","http://www.stage6.com":"flvcd","http://movie.gougou.com":"flvcd","http://www.im.tv":"flvcd","http://hk.im.tv":"flvcd","http://play.hupo.tv":"flvcd","http://ktv.hupo.tv":"flvcd","http://www.openv.com":"flvcd","http://vsearch.cctv.com":"flvcd","http://ent.openv.com":"flvcd","http://hd.openv.com":"flvcd","http://t.openv.com":"flvcd","http://v.ifeng.com":"flvcd","http://v.vodone.com":"flvcd","http://msn.vodone.com":"flvcd","http://zhangmen.baidu.com":"flvcd","http://news.replays.net":"flvcd","http://www.wfbrood.com":"flvcd","http://news.163.com":"flvcd","http://ent.163.com":"flvcd","http://v.163.com":"flvcd","http://www.yiyi.cc":"flvcd","http://www.ouou.com":"flvcd","http://www.boosj.com":"flvcd","http://movie.boosj.com":"flvcd","http://v.pcgames.com.cn":"flvcd","http://j.pcgames.com.cn":"flvcd","http://www.sk-gaming.com":"flvcd","http://v.zol.com.cn":"flvcd","http://game.zol.com.cn":"flvcd","http://info.smgbb.cn":"flvcd","http://tvshow.smgbb.cn":"flvcd","http://msn.smgbb.cn":"flvcd","http://youku.soufun.com":"flvcd","http://home.sogua.com":"flvcd","http://www.acfun.cn":"flvcd","http://124.228.254.229":"flvcd","http://vblog.hunantv.com":"flvcd","http://ent.hunantv.com":"flvcd","http://www.top100.cn":"flvcd","http://www.aipai.com":"flvcd","http://art.china.cn":"flvcd","http://www.letv.com":"flvcd","http://news.mtime.com":"flvcd","http://i.mtime.com":"flvcd","http://movie.mtime.com":"flvcd","http://www.break.com":"flvcd","http://www.stupidvideos.com":"flvcd","http://www.collegehumor.com":"flvcd","http://www.imgo.tv":"flvcd","http://news.xinhuanet.com":"flvcd","http://www.google.cn":"flvcd","http://www.1ting.com":"flvcd","http://www.wat.tv":"flvcd","http://tv.esl.eu":"flvcd","http://news.cntv.cn":"flvcd","http://www.jsbc.com":"flvcd","http://www.jstv.com":"flvcd","http://tv.btv.com.cn":"flvcd","http://space.btv.com.cn":"flvcd","http://www.gztv.com":"flvcd","http://taobao.joy.cn":"flvcd","http://www.umiwi.com":"flvcd","http://www.qiyi.com":"flvcd","http://www.m1905.com":"flvcd","http://www.yinyuetai.com":"flvcd","http://www.11688.net":"flvcd","http://v.bokecc.com":"flvcd","http://union.bokecc.com":"flvcd","http://dv.ce.cn":"flvcd","http://bilibili.us":"flvcd","http://live.jingtime.com":"flvcd","http://download.5721.net":"flvcd","http://tv.cztv.com":"flvcd","http://n.cztv.com":"flvcd","http://tv.tom.com":"flvcd","http://www.xiami.com":"flvcd","http://www.5min.com":"flvcd","http://winout.net":"flvcd","http://www.wzipad.com":"flvcd","http://www.plu.cn":"flvcd"}')
var background = chrome.extension.getBackgroundPage();
var page_url = '';
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;
}
function go(url){
	chrome.tabs.getSelected(null,function(thisTab){
		chrome.tabs.create({
			index:thisTab.index+1,
			url: url
		});	
	});
}
function replaceLinks(mode){
	switch(mode){
	case 'fromlabel':
		background.ga_push("[PopupEvent]","Click_简单破解",get_domain(page_url),0);
		background.ga_push("[PopupEvent]","Start_简单破解",get_domain(page_url),0);
		chrome.tabs.getSelected(null, function(tab) {	
			chrome.tabs.sendRequest(tab.id,{"hope":"simple"},function(){
				background.ga_push("[PopupEvent]","End_简单破解",get_domain(page_url),0);
				window.close();
			});
		});
		break;
	case 'fromsource':
		background.ga_push("[PopupEvent]","Click_终极探测",get_domain(page_url),0);
		//嵌入层
		//console.log("v");
		background.ga_push("[PopupEvent]","Start_终极探测",get_domain(page_url),0);
		chrome.tabs.getSelected(null, function(tab) {	
			chrome.tabs.sendRequest(tab.id,{"hope":"fromsource"},function(){
				background.ga_push("[PopupEvent]","End_终极探测",get_domain(page_url),0);
				window.close();
			});
		});	
	}
}
function reporterror(){
	background.ga_push("[PopupEvent]","Click_报告问题",get_domain(page_url),1);
	background.ga_push("[user][DownloadError]",get_domain(page_url),page_url,1);
	document.getElementById("report_this_page").innerHTML = "<span>提交成功</span>";
}
function helpMe(){
	background.ga_push("[PopupEvent]","Click_打开反馈页面",get_domain(page_url),0);
	go(chrome.extension.getURL("./submit.html"));
}
function getHelp(){
	background.ga_push("[PopupEvent]","Click_打开帮助页面",get_domain(page_url),0);
	go(chrome.extension.getURL("./help.html"));
}
function tools(){
	background.ga_push("[PopupEvent]","Click_打开工具箱",location.pathname,0);
	go(chrome.extension.getURL("./tools.html"));
}
function loadset(){
	chrome.tabs.getSelected(null, function(tab) {	
		page_url = tab.url;
		if (video_downloader[get_domain(page_url)] == 'flvcd'){
			document.getElementById('video').style.display = 'block';
			document.getElementById('video').onclick = function(){
				background.ga_push("[PopupEvent]","Click_视频解析_Flvcd",get_domain(page_url),0);
				go('http://www.flvcd.com/parse.php?kw='+page_url);
			};
		}else{
			;//
		}		
	});
	background.ga_push("[PageLoad]", location.hostname, location.pathname, 1);
}
