﻿function SNDABackPop() {
 this.PopUrl = '';
 this.PopTimeSpan = 1;
 this.GlobalePopTimeSpan = 15;
	this.GlobalPopKey = 'syad';
 this.PopWindowWidth = '0';
 this.PopWindowHeight = '0';
 this.PopKeyName = '';
 this.PopType = 1;
 this.JsString = 'toolbar=yes,menubar=yes,scrollbars=yes,resizable=yes,location=yes,status=yes';
 this.exp = new Date();
	this.GloblePopSpanTime = new Date();
};
SNDABackPop.prototype.Initialize = function(PopUrl, PopTimeSpan, PopWindowWidth, PopWindowHeight, PopKeyName, PopType, JsString) {
 this.PopUrl = PopUrl;
 this.PopTimeSpan = PopTimeSpan;
 this.PopWindowWidth = PopWindowWidth;
 this.PopWindowHeight = PopWindowHeight;
 this.PopKeyName = PopKeyName;
 this.PopType = PopType;
 this.JsString = JsString;
 this.exp.setTime(this.exp.getTime() + (this.PopTimeSpan * 60 * 1000));
	this.GloblePopSpanTime.setTime(this.GloblePopSpanTime.getTime()+ this.GlobalePopTimeSpan * 60 * 1000);
};
SNDABackPop.prototype.GetCookie = function(name) {
 var arg = name + '=';
 var alen = arg.length;
 var clen = document.cookie.length,
 i = 0;
 while (i < clen) {
 var j = i + alen;
 if (document.cookie.substring(i, j) == arg) return this.getCookieVal(j);
 i = document.cookie.indexOf(' ', i) + 1;
 if (i == 0) break;
 };
 return null;
};
SNDABackPop.prototype.getCookieVal = function(offset) {
 var endstr = document.cookie.indexOf(';', offset);
 if (endstr == -1) endstr = document.cookie.length;
 return unescape(document.cookie.substring(offset, endstr));
};
SNDABackPop.prototype.SetCookie = function(name, value) {
 var argv = this.SetCookie.arguments;
 var argc = this.SetCookie.arguments.length;
 var expires = (argc > 2) ? argv[2] : null;
 var path = (argc > 3) ? argv[3] : null;
 var domain = (argc > 4) ? argv[4] : null;
 var secure = (argc > 5) ? argv[5] : false;
 document.cookie = name + '=' + escape(value) + ((expires == null) ? '': ('; expires=' + expires.toGMTString())) + ((path == null) ? '': ('; path=' + path)) + ((domain == null) ? '': ('; domain=' + domain)) + ((secure == true) ? '; secure': '');
};
SNDABackPop.prototype.DeleteCookie = function(name) {
 var exp = new Date(),
 cval = 0;
 exp.setTime(exp.getTime() - 1);
 document.cookie = name + '=' + cval + '; expires=' + exp.toGMTString();
};
SNDABackPop.prototype.Pop = function() {
 
 if (document.referrer.indexOf('http://hao.360.cn') > -1) {
 return;
 }
 if (document.referrer.indexOf('http://i.maxthon.cn/index.v3.htm') > -1) {
 return;
 }

 if(this.Check('gb',15) && this.Check(this.PopKeyName,this.PopTimeSpan)){
	 this.ResetCookie(this.PopKeyName);
	 this.paypopup();
	}
};
SNDABackPop.prototype.paypopup = function() {
 popwin = window.open(this.PopUrl, 'newWindow', (this.PopWindowWidth > 0 ? 'width=' + this.PopWindowWidth: '') + ',' + (this.PopWindowHeight > 0 ? 'height=' + this.PopWindowHeight: '') + ',' + this.JsString);
 if (popwin) poped = true;
 if (this.PopType == 1) self.focus();
}; 

SNDABackPop.prototype.Check=function(pos,span){
	var ts = span * 60 * 1000;
	var ret = this.GetCookie(this.GlobalPopKey);
	var curDate = new Date();
	if(ret == null){
		return true;
	}
	else{
		var locs = ret.split('|')
		for(var i=0;i < locs.length;i++){
			 var item = locs[i].split('$');
			 if(pos == item[0]) {
				 var minsec = curDate - (Date.parse(item[1]));
				 if(minsec > ts){
					return true;
				 }
				 else{
					return false;
				 }
			 }
		}
		return true;
	}
};

SNDABackPop.prototype.ResetCookie=function(pos){
		var cvalue=[];
		var curDate = this.GetDateString(new Date());
		var ret = this.GetCookie(this.GlobalPopKey);
		var locs = ret ? ret.split('|') :['gb$'+curDate,pos+'$'+curDate];	
		var falg = false;			
		for(var i=0;i < locs.length;i++){
			 var item = locs[i].split('$');
			 if(pos == item[0] ) {
				item[1] =curDate;
				falg = true;
			 }
			 if( 'gb' == item[0]) {
				item[1] =curDate;
			 }
			 cvalue.push(item.join('$'));
		}
		if(!falg)
		 cvalue.push(pos+'$'+curDate);
		 
		SetCookie(this.GlobalPopKey,cvalue.join('|'),(new Date()).AddDays(365),'/', document.domain);
};

SNDABackPop.prototype.GetDateString = function(v){
 if(v instanceof Date){ 
 var y = v.getFullYear(); 
 var m = v.getMonth() + 1; 
 var d = v.getDate(); 
 var h = v.getHours(); 
 var i = v.getMinutes(); 
 var s = v.getSeconds(); 
 return y + '/' + m + '/' + d + ' ' + h + ':' + i + ':' + s; 
 } 
 return ''; 
};

