subscribeInit();

function subscribeInit(){

/*	if(!$("ppvUpsell")){
		var child;
		var titles = getElementsByClassName($("chewy"),"div","upsell")[0].getElementsByTagName("H3");
		for(x=0;titles.length>x;x++){
			addEvent(titles[x],"click",revealFeature);
			titles[x].style.cursor = (d.all) ? "hand" : "pointer";
		}
	}
*/	
	if($("loginAccess")) { addEvent($("loginAccess"),"click",createLoginFrame); }
	
	function revealFeature(){
		var child = (this.nextSibling.nodeType=="1") ? this.nextSibling : this.nextSibling.nextSibling;
		if (child.id == "insiderTour") {
			// remove movie from DOM -- IE only
			if($("giTourMov")) { Element.remove($("giTourMov")); }
			var swf = new SWFObject("http://images.theglobeandmail.com/services/site/help/tour.swf", "giTourMov", 912, 525, 7, "#FFF");
			swf.write('giTour'); 
			Element.toggle(child, "blind");
		} else {
			new Effect.toggle(child, "blind");
		}
	}
	
	customLogin(); 
}

	
function customLogin() {
	if(getCookie("ERIGHTS")!=null && LoginRI.user!=null){
		if($("loginNow")){ Element.hide($("loginNow")); }
		chkUrlSub();
	} else {
		setTimeout("customLogin()",100);
	}
}

function chkUrlSub(){
	var subscription = LoginRI.user.subscription; // no value
	if(subscription=="empty"){
		setTimeout("chkUrlSub()",200);
	} else {
		var url = "/servlet/Page/document/v5/includes/global/login/url_subscription?user_URL="+UrlSub.url;
		var xhr = hector();
		xhr.open("POST", url, true);
		xhr.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
		xhr.onreadystatechange = function() {
			if (xhr.readyState == 4) {
				var url_subscription = xhr.responseText;
				url_subscription = url_subscription.split("|");
				for(x=0;url_subscription.length>x;x++){
					switch(url_subscription[x]){
						case "key":
							UrlSub.key = "true";
							break;
						case "newspaper":
							UrlSub.newspaper = "true";
							break;
						case "ppv":
							UrlSub.ppv = "true";
							break;
					}
				}
				
				/* NB: upgrade msg */
				var upgrade_msg,jumpTo,upsellObj;
				var product_arr = [
					/* register */ "You have a free member account with globeandmail.com. ",
					/* newspaper */ "You have FREE Globe Insider access to columns and editorials. ",
					/* key */ "You have full Globe Insider access. "
				]
				var upgrade_msg_arr = [
					/*register key*/ "Upgrade your account to GlobePlus to access this item. ", 
					/*register ppv*/ "Upgrading your account to GlobePlus will also allow you to access this item. ",
					/*newspaper key*/ "To access all the features of GlobePlus including the personalized homepage, Globe Edge customized e-mails and interactive puzzles you will need to upgrade your Globe Insider newspaper subscription. See the ",
					/*newspaper ppv*/ "Upgrading your account to GlobePlus will also allow you to access this item. "
				];
				if($("insiderUpsell")){
					upsellObj = $("insiderUpsell");
					if (subscription==""){ upgrade_msg = product_arr[0]+upgrade_msg_arr[0]; }
					if(subscription.indexOf("newspaper")!=-1 && UrlSub.key == "true"){ 
						upgrade_msg = product_arr[1]+upgrade_msg_arr[2]; 
						jumpTo == true;
					}
				} else if($("ppvUpsell")){
					upsellObj = $("ppvUpsell");
					if (subscription==""){ upgrade_msg = product_arr[0]+upgrade_msg_arr[1]; } 
					if(subscription.indexOf("newspaper")!=-1 && UrlSub.key == "true"){
						upgrade_msg = product_arr[1]+upgrade_msg_arr[2]; 
						jumpTo == true;
					} else if (subscription.indexOf("newspaper")!=-1 && UrlSub.ppv == "true") {  /* free newspaper user */
						upgrade_msg = product_arr[1]+upgrade_msg_arr[3]; 
						jumpTo == true;
					}
				}
				if(typeof upsellObj == "object" && (typeof upgrade_msg=="string")) {
					var upgrade = createElement("p","upgradeMsg");
					upgrade.appendChild(d.createTextNode(upgrade_msg));
					if(jumpTo) {
						jumpTo = createElement("a","","","#jumpToFeatureChart","","","","Jump to the feature chart");
						jumpTo.appendChild(d.createTextNode("chart below for more details"));
						upgrade.appendChild(jumpTo);
						upgrade.appendChild(d.createTextNode("."));
					}
					upsellObj.parentNode.insertBefore(upgrade,upsellObj);
					new Effect.Highlight(upgrade);
				}
				
					
				/* NB: go to URL */
				if(UrlSub.key=="true"){
					if(subscription.indexOf("key")!=-1){
						location.replace(UrlSub.url);			
					} else if(subscription.indexOf("newspaper")!=-1 && $("keyOption")){
						Element.hide($("keyOption"));
					} else if($("newspaperOption")) {
						Element.hide($("newspaperOption"));
					}
				} 
				if(UrlSub.newspaper=="true"){
					if(subscription.indexOf("key")!=-1 || subscription.indexOf("newspaper")!=-1){
						location.replace(UrlSub.url);			
					} else {
						Element.hide($("newspaperOption"));
					}
				} 
				if(UrlSub.ppv=="true"){ 
					if(subscription.indexOf("ppv")!=-1){
						location.replace(UrlSub.url);	
					} else if(subscription.indexOf("key")!=-1){
						if($("keyOption")){ $("keyOption").hide(); }
						if($("comboOption")){ $("comboOption").hide(); }
					} else if($("keyOption")){
						var obj = $("keyOption");
						var url = obj.getElementsByTagName("A")[0].href;
						url = url.replace(/(type=key)/,"type=newspaper");
						url = url.replace(/(INSIDE2)/,"COMBO");
						obj.getElementsByTagName("IMG")[0].src = "/v5/content/Subscription/images/button-insider_combo.png";
						obj.id = "comboOption";
					} else if($("comboOption")){
						var obj = $("comboOption");
						var url = obj.getElementsByTagName("A")[0].href;
						url = url.replace(/(type=newspaper)/,"type=key");
						url = url.replace(/(COMBO)/,"INSIDE2");
						obj.getElementsByTagName("IMG")[0].src = "/v5/content/Subscription/images/button-insider.png";
						obj.id = "keyOption";
					} 
				}
				if((UrlSub.key==null && UrlSub.ppv==null && UrlSub.newspaper==null) && LoginRI.user.username!=""){
					location.replace(UrlSub.url);			
				} 
				
			}
		}
		xhr.send(null);
	}
}