//ADD Event
function bindEvent(method, Func){
		if(this.addEventListener){
			this.addEventListener(method, Func, false);	
		}else if(this.attachEvent){
			this.attachEvent("on" + method, Func);
		}else{
			this["on" + method] = Func;
		}
}
function BatchAddEvent(method, Func){
	for(var i=0, j=this.length; i<j; i++){
		bindEvent.call(this[i], method, Func)	
	}	
}
function setCookieForPopup(){
	if(getCookie("popupTimesInCookie") && getCookie("popupTimesInCookie") < 6) {
		document.cookie = "popupTimesInCookie=" + (Number(getCookie("popupTimesInCookie")) + 1) + "; expires=" + addDays() + "; path=/";
	}
	else if(getCookie("popupTimesInCookie") == null) {
		document.cookie = "popupTimesInCookie=1; expires=" + addDays() + "; path=/";
	}
}
function getCookie(sName){
	var sRegExp = new RegExp("(?:; )?" + sName + "=([^;]*);?");
	if(sRegExp.test(document.cookie)){
		return 	RegExp["$1"];
	}else{
		return null;	
	}
}
function addDays(){
	var now = new Date().getTime() + 365*100*24*60*60*1000;
	return 	new Date(now).toGMTString();
}
function isCookie(){
		document.cookie = "isCookie=true";
		if(document.cookie == "")return false;
		else return true;
}
//Start
//VAR Window is Open?
window.oneFB_isOpen = true;
//window.oneFB_leftTime = 45000;
//window.oneFB_SessionTimes = 1; // Get Client Coockie,if null, set Default 0;
function oneFB_BindOpenHandle(){
//Init Open
	if(!window.oneFB_leftTime)window.oneFB_leftTime = 0;
	//Past 45second
	DynamicSetLeftTime();
	//Batch bind links Event
	BatchAddEvent.call(document.links, "click", setLinkUnload);
	//Batch bind forms Event
	BatchAddEvent.call(document.forms, "submit", setFormUnload);
	//Batch bind Selects Event
	var Selects = document.getElementsByTagName("select");
	BatchAddEvent.call(Selects, "change", setFormUnload);
	//Bind window onunload Event
	bindEvent.call(window, "unload", oneFB_Unload);
}
function setLinkUnload(e){
	if(!e)e = window.event;
	var oObj = (e.target)?e.target:e.srcElement;
	window.oneFB_isOpen = PopRule.call(oObj);
	//window.oneFB_isOpen = false;
}
function setFormUnload(){
	if(!window.formSubmitPopup)
		window.oneFB_isOpen = false;
}
function PopRule(){
	try{
		if(this.href && this.href.indexOf("_print") > -1){
			return true;
		}
	}catch(e){
		return false;
	}
}
function DynamicSetLeftTime(){
	LeftTime = setInterval(function(){
		if(window.oneFB_leftTime > 0){
			window.oneFB_leftTime -= 1000;
			//window.status = window.oneFB_leftTime;
		}else{
			clearInterval(LeftTime);	
		}
	},1000)
}
function oneFB_Unload(){
		if(isCookie() && DealUnknowEvent() &&  window.oneFB_isOpen && window.oneFB_leftTime <= 0 && (
					 (getCookie("popupTimesInCookie") && getCookie("popupTimesInCookie") <= 6
						&& window.oneFB_SessionTimes == getCookie("popupTimesInCookie") )
					 || (getCookie("popupTimesInCookie") == null) )) {			
			if(getValueFromCookie('comeFromSurveyFormRefresh') == 'YES') {
				document.cookie="comeFromSurveyFormRefresh=;path=/";
			}
			else {
				var currentURL = document.location.href;
				if(window.SUPPRESS_ILG_POPUP && 
						(currentURL.indexOf("/odds/odds_home_tmpl.jhtml") != -1 || 
						currentURL.indexOf("/admission_survey/survey_form.jhtml") != -1)) return;
				if(currentURL.indexOf("/locker/connections.jhtml") != -1 || 
						currentURL.indexOf("/promo/fb_connect.jhtml") != -1) return;
				setCookieForPopup();
				window.open(window.oneFB_PopURL,"","left=100,top=100,width=630,height=440");
			}
		}
}
function DealUnknowEvent(){
	if(window.formSubmitPopup)
		return true;
	var isAction = true;
	try{
		if(window.opener){
			isAction = false;
			var SearchEngine = window.opener.location.toString();
			if(SearchEngine.indexOf("google") > -1){
				isAction = true;
			}
		}else if(window.parent.location != window.location){
			isAction = false;
		}else{
			isAction = true;
		}
	}catch(e){
		isAction = true;
		if(window.parent.location != window.location){
			isAction = false;
		}
	}
	return isAction;
}

function getValueFromCookie(c_name) {
	if (document.cookie.length>0)
	  {
	  c_start=document.cookie.indexOf(c_name + "=")
	  if (c_start!=-1)
	    { 
	    c_start=c_start + c_name.length+1 
	    c_end=document.cookie.indexOf(";",c_start)
	    if (c_end==-1) c_end=document.cookie.length
	    return unescape(document.cookie.substring(c_start,c_end))
	    } 
	  }
	return ""
}

//all the popup window objects which are produced by openPopupWin method
popupWins = {};
//a common mehod to open popup window
function openPopupWin(url, popupName, params, automaticallyClose, operation) {
	if(!popupWins[popupName]) popupWins[popupName] = {};
	if(params.replace == 'no' && popupWins[popupName] && popupWins[popupName].win && !popupWins[popupName].win.closed) {
		popupWins[popupName].win.focus();
		return;
	}
	var open = function() {
		var top = params.top ? params.top : 0;
		var left = params.left ? params.left : 0;
		var width = params.width ? params.width : '400';
		var height = params.height ? params.height : '400';
		if(params.center && params.center == 'yes') {
			width = (width + '').replace(/^([0-9]*).*/, '$1');
			height = (height + '').replace(/^([0-9]*).*/, '$1');
			top = (window.screen.height - height) / 2;
			left = (window.screen.width - width) / 2;
			if(top < 0) top = 0;
			if(left < 0) left = 0;
		}
		var parameters = 'left=' + left + 
				',top=' + top + 
				',height=' + height + 
				',width=' + width + 
				',status=' + (params.status ? params.status : 'no') + 
				',toolbar=' + (params.toolbar ? params.toolbar : 'no') + 
				',menubar=' + (params.menubar ? params.menubar : 'no') + 
				',location=' + (params.location ? params.location : 'no') + 
				',scrollbars=' + (params.scrollbars ? params.scrollbars : 'yes') + 
				',dependent=' + (params.dependent ? params.dependent : 'yes') + 
				',resizable=' + (params.resizable ? params.resizable : 'no');
		var win = params.window || window;
		popupWins[popupName].win = win.open(url, popupName, parameters);
		popupWins[popupName].win.focus();
		if(operation) operation();
	}
	closePopupWin(popupName, true);
	if(window.navigator.userAgent.indexOf('Safari') != -1) {
		open();	
	}
	else {
		popupWins[popupName].timer = setTimeout(open, 500);
	}
	//automatically close this window popup if required 
	if(automaticallyClose) {
		var closeAutomatically = function() {
			closePopupWin(popupName);
		}
		if(window.attachEvent) {
			window.attachEvent('onunload', closeAutomatically);
		}
		else {
			window.addEventListener('unload', closeAutomatically, false);
		}
	}
	return popupWins[popupName].win;
}
//a common method to close popup window
function closePopupWin(popupName, removeObj) {
	if(popupWins[popupName] && popupWins[popupName].timer) {
		clearTimeout(popupWins[popupName].timer);
	}
	if(popupWins[popupName] && popupWins[popupName].win && !popupWins[popupName].win.closed) {
		popupWins[popupName].win.close();
	}
	if(!removeObj) {
		delete popupWins[popupName];
	}
}
function openNewsfeedPopup(url, operation) {
	var open = function() {
		var top = 0;
		var left = 0;
		var width = 250;
		var height = 680 > window.screen.availHeight ? window.screen.availHeight : 680;
		window.newsfeedPopupHeight = height;
		top = (window.screen.height - height) / 2;
		left = (window.screen.width - width) / 2;
		if(top < 0) top = 0;
		if(left < 0) left = 0;
		var parameters = 'left=' + left + 
				',top=' + top + 
				',height=' + height + 
				',width=' + width + 
				',status=no' + 
				',toolbar=no' + 
				',menubar=no' + 
				',location=no' + 
				',scrollbars=yes' + 
				',dependent=yes' + 
				',resizable=yes';
		var popupName = 'NewsfeedPopupWin';
		setCookie('NEWSFEED_POPUP', 'YES');
		window.newsfeedPopup = window.open(url, popupName, parameters);
		window.newsfeedPopup.focus();
		if(operation) operation();
	}
	if(window.newsfeedPopupOpenerTimer) {
		clearTimeout(window.newsfeedPopupOpenerTimer);
	}
	if(window.newsfeedPopup && !window.newsfeedPopup.closed) {
		window.newsfeedPopup.focus();
	}
	else {
		if(window.navigator.userAgent.indexOf('Safari') != -1) {
			window.newsfeedPopupOpenerTimer = open();
		}
		else {
			window.newsfeedPopupOpenerTimer = setTimeout(open, 500);
		}
	}
}