var zcom_id = "";

var sGSubscribe = "subscribe";
var sGZID		= "account";
var sGLastSubscribeTime = "lastsubscribetime";

var setuplink = '00010010';

var zcom_ver = null;
var oldClient = false;
var haveClient = false;

var magSource = "99990001";
var magChannel = "99990001";

var downloadMsg = {
		DOWNLOAD:'文件已存在您的下载列表中，不能重复下载',
		FAILED:'软件出错。',
		ERRDISK:'您的磁盘空间不足。'
		};

var ActiveXCreated		= false;
var ZCOMActiveX_v2	= null;
var ZCOMVer				= null;

//2007-6-29 创建控件
function createZcomObject(){
	if(!document.createElement){
		//alert("Your browser is not supported.");
		return;
	}
	if(ActiveXCreated == false){
		try{
			var oZcom		= document.createElement("object");
			oZcom.classid	= "clsid:3A7698F3-1BCC-4838-B3BF-EF4E3C5E209A";
			ZCOMVer = oZcom.GetInfo("zcomVersion");
			if(ZCOMVer){
				ZCOMActiveX_v2=oZcom;
			}
		}catch(e){}
		ActiveXCreated = true;
	}
}
createZcomObject();

//下载杂志函数，根据hash和下载类型
function mOpt(sHash, sType) {

	var temp = false;
	var need_iframe=true;
	if (!ZCOMActiveX_v2) {
		ActiveXCreated = false;
		createZcomObject();
	}

	if (ZCOMActiveX_v2) {

		xmlData = document.all.item("xmlData_" + sType + "_" + sHash + "_v2");
		if (xmlData[1]) { // 防止重复，如果重复（为数组）则去第一个。
			xmlData = xmlData[0];
		}
		xmlData = xmlData.innerHTML;
		xmlData = xmlData.replace(/&lt;/g, "<").replace(/&gt;/g, ">");

		magSource = magSource.replace(/[^\d]/g, '');
		if(magSource.length == 8){
			xmlData = xmlData.replace(" magSource=\"0\"", " magSource=\"" + magSource + "\"");
		}

		var downloadStatus = ZCOMActiveX_v2.send(xmlData);

		switch(downloadStatus.toLowerCase()){
			case 'downloading':	
				need_iframe=false;alert(downloadMsg.DOWNLOAD);break;
			case 'complete':	
				need_iframe=false;openComplete(sHash);break;
			case 'errdisksp':	
				need_iframe=false;alert(downloadMsg.ERRDISK);break;
			case 'failed':		
				need_iframe=false;alert(downloadMsg.FAILED);break;
		}

		if("TRUE"==ZCOMActiveX_v2.GetInfo("ZcomRunning"))need_iframe=false;

		temp = true;

	} else {
		noActiveXOpen();
	}

	if( sType == 'Download' && need_iframe){

		var d_mode = '0' ;
		if(temp == true){
			d_mode = '1' ;
		}

		try{
			document.getElementById("myHiddenIframe").src = "/download_later_x340b.html?" + sHash + "_" + d_mode ;
		}catch(exx){}
	}


}

//下载，没有安装客户端就http下载
function mOptHT(sHash, sType) {

	if(ZCOMActiveX_v2){
		mOpt(sHash, sType);
	} else {
		try{
			var mid = 0;
			var msz = 0;

			xmlData = document.getElementById("xmlData_" + sType + "_" + sHash);
			xmlData = xmlData.innerHTML;
			xmlData = xmlData.replace(/&lt;/g, "<").replace(/&gt;/g, ">");

			/\sid=\"([0-9]+)\"\s/.exec(xmlData);
			mid = RegExp.$1;

			/\sfileSize=\"([0-9]+)\"\s/.exec(xmlData);
			msz = RegExp.$1;

			location.href = "http://httpdown.zcominc.com/magazine/file/" + mid + ".exe";
			httpDown(mid, msz);
		
		}catch(e){}
	}

}

//取消magsource 2007-6-29 hqlulu
function noActiveXOpen() {
	confirm("体验高速杂志下载与管理       \n需先安装Zcom杂志订阅器 beta2007");
	//window.location="http://zcomcdl.zcominc.com/union/ZcomMagSubscribe-9999-1.exe";
	window.location="http://zcomcdl.zcominc.com/client/ZcomMagSubscribe_Setup-9999.exe";
	/*
	try{
		window.showModalDialog('http://mag.zcom.com/download_message/','','dialogHeight:385px;dialogWidth:436px;resizable:no;center:yes;help:no;status:no;');
	}catch(e){
		try{
			window.openDialog('http://mag.zcom.com/download_message/','ZCOM');
		}catch(e){}
	}*/
}

//打开杂志
function openComplete(sHash){

	if(confirm('此杂志已下载，是否立即阅读？') != true){
		return;
	}

	var sXML = '<'+'?xml version="1.0" encoding="utf-8"?'+'>'
		+ '<item action="OpenFile" hash="' + sHash + '" />';

	ZCOMActiveX_v2.Send(sXML);
}

///////////订阅 test中
var updateSublist = false;
var subscribeSendProc = false;

//订阅主方法,参数为杂志id.-0-
function subscribeMe( zzhID){

	if (ZCOMActiveX_v2) {
		sSubscribe = ZCOMActiveX_v2.ReadDataFile(sGSubscribe);
		if ((sSubscribe == "FALSE")||(sSubscribe == "")) {
			sSubscribe = ",";
		}
		if (sSubscribe.indexOf("," + zzhID + ",") == -1) {
			sSubscribe += zzhID + ",";
			sub_tmp = true;
			ZCOMActiveX_v2.WriteDataFile(sGSubscribe, "w", sSubscribe);
			alert("订阅成功，新发刊会发送给你！");
		}else{

			alert("谢谢，你已经订阅过了");
			/*
			if(confirm("您确定要退订本杂志么?") == false){
				return;
			}

			sSubscribe = str_replace("," + zzhID + ",", ",", sSubscribe);
			sub_tmp = false;
			ZCOMActiveX_v2.WriteDataFile(sGSubscribe, "w", sSubscribe);
			*/
		}
	} else {
		noActiveXOpen();
	}

	//switch subscribe image
	if(sub_tmp){
		//setSubscribeImg('no', zzhID);
	}else{
		//setSubscribeImg('yes', zzhID);
	}
}

//初始化 subscribe image
// 在img标签中用onload调用
function initSubscribeImg(zzhID){

	if (ZCOMActiveX_v2) {
		sSubscribe = ZCOMActiveX_v2.ReadDataFile(sGSubscribe);
		if ((sSubscribe == "FALSE")||(sSubscribe == "")) {
			sSubscribe = ",";
		}
	}

	if(sSubscribe == null || sSubscribe == "" || sSubscribe == ","){
		setSubscribeImg('yes', zzhID);
		return;
	}

	if(zzhID != '0'){
		if( sSubscribe.indexOf(','+zzhID+',') == -1 ){
			setSubscribeImg('yes', zzhID);
		}else{
			setSubscribeImg('no', zzhID);
		}

	}else{

		var subInfo = sSubscribe.split(",");

		for(var i=0;i<subInfo.length;i++){
			setSubscribeImg('no', subInfo[i]);
		}
	}

}

//获取订阅图片
function setSubscribeImg(img_stl, zzhID){

	if(img_stl=='') img_stl='yes';

	img = document.getElementById("img_subs");
	
	img_re = new RegExp("_(blank|yes|no|32)\\.png$");

	if(img != null){

		if(zzhID == '3200' && img_stl == 'yes'){
			// mk_070323
			img.src = img.src.replace(img_re, '_32.png');	
			return;
		}

		img.src = img.src.replace(img_re, '_'+ img_stl +'.png');
		return;
	}
	
	// category subscribe
	// 2006-6-1
	try{
		var imgc = document.all.item("subs_img_"+zzhID);
		
		if(typeof(imgc) != "undefined"){
		

			if(typeof(imgc.length) != "undefined"){
				for(var i=0;i<imgc.length;i++){
					imgc[i].src = document.getElementById("subs_" + img_stl + "_p").src ;
				}
			}else{
				imgc.src = document.getElementById("subs_" + img_stl + "_p").src ;
			}

		}

	}catch(ex){}


	try{

		var imgc2 = document.all.item("subs_img2_"+zzhID);
		
		if(typeof(imgc2) != "undefined"){
		

			if(typeof(imgc2.length) != "undefined"){
				for(var i=0;i<imgc2.length;i++){
					imgc2[i].src = document.getElementById("subs_" + img_stl + "_p2").src ;
				}
			}else{
				imgc2.src = document.getElementById("subs_" + img_stl + "_p2").src ;
			}

		}

	}catch(ex){}

}

/////////////////////////////
// http下载统计

function httpDown(mid, sz){

	var hd_zid = 0;
	var hd_channelID = '00000000';
	var hd_time = (new Date()).getTime();
	var hd_rand = parseInt( Math.random()* hd_time ) + 1;

	var hd_container = document.getElementById('clickTrackImg');
	var hd_stat_src = ''
			+'http://magstat.zcominc.com/clientaction/click/zcom/http/80'
			+'/'+hd_zid
			+'/'+hd_channelID
			+'/'+hd_rand
			+'/'+mid
			+'/5'
			+'/'+sz
			+'/0'
			+'/99990008'
			+'';

	try{
		hd_container.src = hd_stat_src;
	}catch(e){}

	return true;

}

function checkHttpDown(mid, sz){
	if(event.keyCode = 31){
		httpDown(mid, sz);
	}
	return true;
}

/////////////////////////////
// 读写 COOKIE

function set_cookie(sKey, sValue) {
	var tcookie_time = new Date();
	var tcookie_expiry = new Date(tcookie_time.getTime() + 1000 * 3600 * 24 * 31* 24);
	document.cookie = sKey + "=" + sValue + "; expires=" + tcookie_expiry.toGMTString()
		+ "; domain = .zcom.com; path = /";
}

function set_cookie_1day(sKey, sValue) {
	var tcookie_time = new Date();
	var tcookie_expiry = new Date(tcookie_time.getTime() + 1000 * 3600 * 24);
	document.cookie = sKey + "=" + sValue + "; expires=" + tcookie_expiry.toGMTString()
		+ "; domain = .zcom.com; path = /";
}

function set_cookie_thisDomain(sKey, sValue) {
	var tcookie_time = new Date();
	var tcookie_expiry = new Date(tcookie_time.getTime() + 1000 * 3600 * 24 * 31* 24);
	document.cookie = sKey + "=" + sValue + "; expires=" + tcookie_expiry.toGMTString()
		+ "; path = /";
}

function get_cookie(Name) {
	var search = Name + "="
	var returnvalue = "";
	if (document.cookie.length > 0) {
		offset = document.cookie.indexOf(search)
		if (offset != -1) {
			offset += search.length
			end = document.cookie.indexOf(";", offset);
			if (end == -1)
			end = document.cookie.length;
			returnvalue = unescape(document.cookie.substring(offset, end))
		}
	}
	return returnvalue;
}

///////////////////////////////////////////////////////////////////////////////
//调试用的键盘快捷键,留空

document.onkeydown = fkeydown;
function fkeydown() {
	if (ZCOMActiveX_v2 && (event.ctrlKey) && (event.altKey)) {
		if (event.keyCode == 76) { // Ctrl + Alt + L 调试信息
			return false;
		} else if (event.keyCode == 73) {   // 字母 I
			return false;
		} else if (event.keyCode == 79) {   // 字母 O
			return false;
		} else if (event.keyCode == 80) {   // 字母 P
			return false;
	} else if (event.keyCode == 67) {   // 字母 C
			return false;
		}
	}
}

