/* Last updated: 9/24 */
//Baynote prefix and suffix for custom window.name
var bn_window_param_start = "unb~ti~[";
var bn_window_param_end = "]~";
var bn_location_href = window.location.href;
var window_name = window.name;

//Check if a variable is null or is an empty string
function bn_isNotEmpty(name) {
   return (name != null) && (name != "");
}

//Get the value of cookie c_name
function bn_getCookie(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 "";
}

//Set value for cookie c_name
function bn_setCookie(c_name,value,expiredays) {
   var exdate=new Date();
   exdate.setDate(exdate.getDate()+expiredays);
   document.cookie=c_name+ "=" +escape(value)+ ";path=/" +
      ((expiredays==null) ? "" : ";expires="+exdate.toGMTString());
}

//Set window.name
function bn_setWU(bn_iter) {
   var bn_iter_limit = 5;
   if(typeof(bn_iter) != 'undefined' && bn_iter > bn_iter_limit) { return; }
   var bn_u_val = bn_getCookie('bn_u');
   if(bn_isNotEmpty(bn_u_val)) {
      window.name = bn_window_param_start + bn_u_val + bn_window_param_end;
   } else {
      if(typeof(bn_iter) != 'undefined' && bn_iter > 0)
         setTimeout("bn_setWU("+(bn_iter+1)+");",500);
      else
         setTimeout("bn_setWU("+1+");",500);
   }
}

//Get the window name and set bn_u cookie
function bn_getWU() {

   
   var bn_u_cookieval = bn_getCookie('bn_u');
   var start_idx = bn_window_param_start.length;
   var end_idx = window_name.indexOf(bn_window_param_end);
   
   if(end_idx > start_idx && end_idx < window_name.length) {
      var bn_u_val = window_name.substring(start_idx, end_idx);
      if(bn_isNotEmpty(bn_u_val)) {
         bn_setCookie('bn_u',bn_u_val,365*3);
      } else {
         bn_setCookie('bn_u',4,365*3);
      }
   }
}
function baynote_getUrlParam(name) {
	  var regexS = "[\\?&]"+name+"=([^&#]*)";
	  var regex = new RegExp( regexS, "i" );
	  var pageUrl = baynote_getPageUrl();
	  var results = regex.exec( pageUrl );
	  if( results == null )
	    return "";
	  else
	    return results[1];
	}
	function baynote_getPageUrl() {
	   var pageUrl = bn_location_href;
	   if ( (typeof(pageUrl) == "undefined") || (pageUrl == null) || (pageUrl == ""))
	     pageUrl = window.location.href;
	   return pageUrl;
	}
function bn_setAttrs(){
	if("^https?://www\.mail.com\/article.aspx\/.*","i"){
		var pageUrl = baynote_getPageUrl() + " ";
		var articlePath = pageUrl.substring(33);
		var slash1 = articlePath.indexOf("/");
		var slash2 = articlePath.indexOf("/",slash1+1);
		var slash3 = articlePath.indexOf("/",slash2 +1);
		var slash4 = articlePath.indexOf("/",slash3 +1);
		//var category = articlePath.substring(0,slash1);
		//var subcat = articlePath.substring(slash1+1, slash2);
		//var feed = articlePath.substring(slash2+1, slash3);
		//var subfeed = articlePath.substring(slash3+1, slash4);
		var date = articlePath.substring(slash4+1, slash4+9);
		date = date.substring(0,4) + "-" + date.substring(4,6) + "-" + date.substring(6);
		//baynote_tag.docAttrs.category = category;
		//baynote_tag.docAttrs.subcat = subcat;
		//baynote_tag.docAttrs.feed = feed;
		//baynote_tag.docAttrs.subfeed = subfeed;
		baynote_tag.docAttrs.date = date;
	}
	var metas = document.getElementsByTagName("meta");
	if (!metas) return;
	
	for (var i = 0; i < metas.length; i++) {
		if (!metas[i]) return;
			if (metas[i].name == "article-category") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleCat = tempMetaContent;
				}
			} else if (metas[i].name == "article-title") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleTitle = tempMetaContent;
				}
			}else if (metas[i].name == "article-description") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleDesc = tempMetaContent;
				}
			}else if (metas[i].name == "article-publishdate") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articlePubDate = tempMetaContent;
				}
			}else if (metas[i].name == "article-credit") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleCredit = tempMetaContent;
				}
			}else if (metas[i].name == "article-subcategory") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleSubCat = tempMetaContent;
				}
			}else if (metas[i].name == "article-defaultimage") {
				var tempMetaContent = metas[i].content;
				if(bn_isNotEmpty(tempMetaContent)) {
					baynote_tag.docAttrs.articleDefaultImage = tempMetaContent;
				}
			}
	
	}
	
}


function bn_showObserver() {
   //Determine appropriate tag server
   
   if (bn_location_href.indexOf("https://") == 0) {
      baynote_tag.server = "https://mail-www.baynote.net";
   } else {
      baynote_tag.server = "http://mail-www.baynote.net";
   }

   baynote_tag.customerId = "mail";
   baynote_tag.code = "www";
   baynote_tag.type = "baynoteObserver";
   //baynote_globals.cookieDomain         = "ti.com";
   
   
   bn_setAttrs();

   baynote_tag.show(); 
}

/*

If window.name has been initialized with the baynote pattern, then call getWU to set the bn_u cookie. 
Otherwise, call bn_showObserver(), then bn_setWU()
*/

   if (bn_isNotEmpty(window_name) && window_name.indexOf(bn_window_param_start) == 0) {
      bn_getWU();
      bn_showObserver();
   } 
   else {
      bn_showObserver();
      bn_setWU();
   }


