// kk checker
var kk_checker = new function() {
	
	var isIE = function() {
		//return false; // 暂时全部都当作不是IE来处理（不用判断控件）;
		return (document.all);
	}
	
	this.isIE = isIE;
	
	var install = function() {
		$.cookie('installed', 'true', {expires: new Date(2099, 12, 31), domain:DEFAULT_TOMAIN, path:'/'});			
	}
	
	this.install = install;
	
	var uninstall = function() {
		$.cookie('installed', 'false', {expires: new Date(2099, 12, 31), domain:DEFAULT_TOMAIN, path:'/'});			
	}
	
	var isInstalled = function() {
		var isInstalled;
		if (isIE()) {
			try {
				var kkproto = new ActiveXObject('kkproto.KKProtocol.1');
				isInstalled = true;
			} catch (e) {
				isInstalled = false;
			}
		} else {
			isInstalled = ($.cookie('installed') == 'true'?true:false);
		}
		return isInstalled;
	}
	
	dialog({
		name : 'download',
		on_open : function(d) {
			d.find('a.installed').click(function() {
				if(!isIE() && d.find('#rem_installed').attr('checked')){
					install();
				}
				run_protocol();
			});

			d.find('a.download').click(function() {
				if (isIE()) {
					dialog.state('download', 'downloading');
				}
			});
		},
		state : function(d, state) {
			var states = state.split(',');
			
			d.find('div.state').css('display', 'none');
			for (var i = 0; i < states.length; i++) {
				var s = states[i];
				d.find('div.' + s).css('display', 'block');
			}
		}
	});
	
	var cur_protocol = null;
	var run_protocol = function(protocol) {
		var pl = protocol || cur_protocol;
		cur_protocol = null;
		if (pl) {
			pl();
			dialog.close();
			//install();
			ga.track_event(EVENT_CATEGORY.ALL_ITEM,'run');
		}
	}
	
	this.run = function(protocol) {
		cur_protocol = protocol;
		if (isInstalled()) {
			run_protocol();
		} else {
			if (isIE()) {
				dialog.open('download', 'download');
				var interval_id = setInterval(function(){
					if(isInstalled()) {
						if (interval_id) {
							clearInterval(interval_id);
						}
						run_protocol();
					}
				}, 5000);				
			} else {
				dialog.open('download', 'installed');
			}				
		}
	}
	
	this.isInstalled = function() {
		return isInstalled();
	}
	
	this.prepareProtocol = function(protocol) {
		cur_protocol = protocol;
	}
	
	this.openDialog = function(state) {
		if (state) {
			dialog.open('download', state);
		} else {
			if (isIE()) {
				dialog.open('download', 'download');
				var interval_id = setInterval(function(){
					if(isInstalled()) {
						if (interval_id) {
							clearInterval(interval_id);
						}
						run_protocol();
					}
				}, 5000);				
			} else {
				dialog.open('download', 'installed');
			}
		}
	}
};

// kk protocol
var kk_protocol = (function() {
	var PROTOCOL_FRAME = '<iframe id="${NAME}" name="${NAME}" src="about:blank" width="0" height="0" frameborder="0" scroll="no"></iframe>';
	var PROTOCOL_FRAME_COUNT = 0;
	
	var _check = function(fn) {
		kk_checker.run(fn);		
	}
	
		
	var _get_protocol = function(p_protocol, args) {
		var p = p_protocol;
		for (var k in args) {
			var regex = new RegExp('\\$\\{' + k.toUpperCase() + '\\}', 'ig');
			p = p.replace(regex, args[k]);
		}
		return p;
	}
	
	var _run = function(protocol, isNewFrame) {
		if (isNewFrame) {
			var name = 'protocol_frame_' + PROTOCOL_FRAME_COUNT;
			var html = PROTOCOL_FRAME.replace(/\$\{NAME\}/ig, name);
			$(document.body).append(html);
			window[name].location.href = protocol;
			PROTOCOL_FRAME_COUNT++;
		} else {
			protocol=protocol.replace('{', '%7B');
			protocol=protocol.replace('}', '%7D');
			location.href = protocol;
		}
	}
	
	var _callback = function(callback) {
		if (callback) {
			callback();
		}
	}
	
	// protocols
	var PROTOCOL = new function() {
		// protocol for run app
		this.RUN = (function() {
			var PROTOCOL = 'kkrs://{appid=${APPID};run=${RUN};appname=${APPNAME}}/';
			
			return function(appid, run, appname, callback) {
				if (run) {
					run = run.replace('_desk', '_web');
				} else {
					run = appid + '_web';
				}
				var p = _get_protocol(PROTOCOL, {
					appid : appid,
					run : run,
					appname : encodeURI(appname || '')
				});
				if (document.all) {
					_run(p,true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for run app
		this.RUN_FLASH = (function() {
			var PROTOCOL = 'kkrs://{appid=9D076523-7C04-40F2-8A11-CB3EB6CD9B6A;run=kgp;runcmd=${RUNCMD};appname=${NAME_BASE64}}/';
			
			return function(runcmd, name_base64, callback) {
				var p = _get_protocol(PROTOCOL, {
					runcmd : runcmd,
					name_base64 : name_base64
				});
				if (document.all) {
					_run(p,true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for kkrs
		this.KKRS = (function() {			
			return function(p, callback) {
				if (document.all) {
					_run(p, true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for on/off plugin
		this.PLUGIN = (function() {
			var PROTOCOL = 'kkrs://{pluginid=${PLUGINID};option=${OPTION};pluginname=${PLUGINNAME}}/';
			
			return function(pluginid, option, pluginname, callback) {
				var opt;
				if (typeof option == 'string') {
					if (option == 'on') {
						opt = 1;
					} else {
						opt = 0;
					}
				} else if (typeof option == 'number' || typeof option == 'boolean') {
					if (option) {
						opt = 1;
					} else {
						opt = 0;
					}
				} else {
					opt = 0;
				}
				var p = _get_protocol(PROTOCOL, {
					pluginid : pluginid,
					option : opt,
					pluginname : encodeURI(pluginname)
				});
				if (document.all) {
					_run(p,true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for batch sync 
		this.BATCH_SYNC = (function() {
			var PROTOCOL = 'kkrs://{applist=${APPLIST};option=0;}/';
			
			return function(applist, callback) {
				if (applist instanceof Array) {
					applist = applist.join(',');
				}
				var p = _get_protocol(PROTOCOL, {
					applist : applist
				});
				if( document.all) {
					_run(p, true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for batch sync(tiny)
		this.BATCH_SYNC_TINY = (function() {
			var PROTOCOL = 'kkrs://{applistext=${TINYURL};option=0;}/';
			
			return function(tiny, callback) {
				if (tiny instanceof Array) {
					tiny = tiny.join(',');
				}
				var p = _get_protocol(PROTOCOL, {
					tinyurl : tiny
				});
				
				if (document.all) {
					_run(p,true);
				} else {
					_run(p);
				}
				_callback(callback);
			}
		})();
		
		// protocol for notification
		this.NOTIFY = (function() {
			var PROTOCOL = 'kkrs://{kknotify=${NOTIFY};kkid=${KKID}}';
			var NOTIFY_TYPE = {
				'sync' : 0
			}
			
			return function(notifyType, callback) {
				var kkid = kkc.getKKNumber();
				var p = _get_protocol(PROTOCOL, {
					notify : NOTIFY_TYPE[notifyType.toLowerCase()],
					kkid : kkid
				});
				_run(p);
				_callback(callback);
			}			
		})();
	}
	
	// wraper for protocol
	var _wrap = function(fn) {
		return function() {
			var args = arguments;
			_check(function() {
				fn.apply(window, args);
			});
		}
	}
	var PROTOCOL_WRAPER = {};
	for (var p in PROTOCOL) {
		PROTOCOL_WRAPER[p.toLowerCase()] = _wrap(PROTOCOL[p]);
	}
	return PROTOCOL_WRAPER;
})();

var kkp = kk_protocol;

function ie_not_installed(){
	if(kk_checker.isIE() && !kk_checker.install()){
		ga.track_event(EVENT_CATEGORY.IE_DETAILPAGE, 'page_view');
	}
}

function ie_not_installed_click_run(){
	if(kk_checker.isIE() && !kk_checker.install()){
		ga.track_event(EVENT_CATEGORY.IE_DETAILPAGE, 'click_run');
	}
}

function ie_not_installed_click_download(){
	if(kk_checker.isIE() && !kk_checker.install()){
		ga.track_event(EVENT_CATEGORY.IE_DETAILPAGE, 'click_download');
	}
}