jQuery(document).ready(function () {
	
	var loggedstate = ms_isLoggedIn();
	if (loggedstate === true ) { 
		updateProfileBox(); 
		jQuery("#bianotlogged").hide();
		jQuery("#bianloggedin").show();
	} else { 
		jQuery("#bianotlogged").show();
		jQuery("#bianloggedin").hide();			
	}
	
	jQuery(".BIAsignlogin").click(function () { 
		showLoginOverlay();
	});
	
	jQuery("#bialogout").click(function () { 
		window.location.hash = '';		
		ms_doLogout();
	});
	
	jQuery("#biaprofile").click(function () { 
		showProfile();
	});
	
	jQuery(".cnnConnRSclose").click(function () { 
		jQuery("#cnnConnectOverlayBox").hide();
	});
	ms_initOverlay(); 
});
/*jQuery(function() {
	jQuery("input#inputfile").filestyle({ 
	 image: "http://i.cdn.turner.com/cnn/.element/img/2.0/sect/connect/btn_browse.gif",
	 imageheight : 21,
	 imagewidth : 46,
	 width : 152
	});
	jQuery(".inputdivcont div").css({ "background-position" : "0px -2px"});
    jQuery(".inputdivcont div").hover(
      function () {
        jQuery(this).css({ "background-position" : "0px -22px"});
      }, 
      function () {
        jQuery(this).css({ "background-position" : "0px -2px"});
      }
    );
});*/

function ms_isLoggedIn() {
	var authid = allCookies[ 'authid' ] || null;
	return (authid!=null);
}

function updateProfileBox() {
	if (!ms_isNull(allCookies['authid'])) {
	if (!ms_isNull(allCookies['displayname'])) {
					var displayname = allCookies.displayname;
		}
	}
	jQuery("#biausername").html(displayname);
}

function showLoginOverlay() {	// show login overlay
	var loggedstate = ms_isLoggedIn();
	if (loggedstate === false) { // show/hide profile/login box
		jQuery("#cnnConnectOverlayBox").show();
		centerDivOfPage('cnnConnectOverlayBox');
	}
}

function createLoginIframe() {
	samlPostback();
}

function samlPostback() {
	document.location = 'http://audience.cnn.com/services/cnn/gsso?assertionConsumerUrl=http://www.disqus.com/saml/cnn/postback/&target=' + encodeURIComponent(document.location);
}

function disqus_callback() {
	if(ms_isLoggedIn() && Dsq.jsonData.request.missing_perm == 'cnn-required') {
		samlPostback();
	} 
}
function centerDivOfPage(divId) {
	var divWidth = $(divId).offsetWidth;
	var divHeight = $(divId).offsetHeight;
	var winVisWidth = window.innerWidth || document.documentElement.clientWidth;
	var winVisHeight = window.innerHeight || document.documentElement.clientHeight;
	var winX = window.pageXOffset || document.documentElement.scrollLeft;
	var winY = window.pageYOffset || document.documentElement.scrollTop;
	var divLeft = Math.floor(((winVisWidth - divWidth) / 2) + winX);
	var divTop = Math.floor(((winVisHeight - divHeight) / 2) + winY);
	divTop = (divTop <= 110) ? 110 : divTop;
	divLeft = (divTop <= 9) ? 10 : divLeft;
	document.getElementById(divId).style.left = divLeft + 'px';
	document.getElementById(divId).style.top = divTop + 'px';
}

window.msReloadNewUser = function() {
	window.location.href = window.location.href+"?bia=subscribe";
}
window.msReloadCheck = function() {
	if(window.location.href.indexOf('?') > -1) {
		window.location.href = window.location.href.substr(0, window.location.href.indexOf('?'));
	} else {
		if (typeof sethrefstatus != 'undefined' ) { // checks if status was set
			window.location.reload();
		} else if (bianewuser === true) {
			window.location.href = window.location.href+"?bia=subscribe";
		} else { 
			window.location.reload();
		}
	}
}

function gup(name) {
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
	return "";
  else
	return results[1];
}

function updateName(string) {
 return string.split('+').join(' '); // changes string "+" to "space"
}

function stringTrim(strToTrim) {
	return(strToTrim.replace(/^\s+|\s+$/g, ''));
}		
		
function updateInput() {
	var inputview = jQuery("input#inputfile").val();
	if (inputview != '') {
		jQuery("#uploadbtnoff").hide();
		jQuery("#uploadbtnon").show();
	} else {
		jQuery("#uploadbtnoff").show();
		jQuery("#uploadbtnon").hide();
	}
}

function cancelAvatar() {
	jQuery("input#inputfile").val('');
	jQuery(".file.inputfileclass").val('');
	jQuery("#uploadbtnoff").show();
	jQuery("#uploadbtnon").hide();	
	jQuery('#cnnConnChangeMyPhoto').hide();
	jQuery('#cnnConnProfile-screenName-container').show();
}

function checkLoggedState() {
	if (!ms_isNull(allCookies['authid'])) {
		if (!ms_isNull(allCookies['displayname'])) {
			// nothing
			} else {
				jQuery("#cnn_INAOverlayH").show();
				centerDivOfPage('cnn_INAOverlayH');
		}
	}
}

function showAlmostThere() {
	jQuery("#cnn_INAOverlayH").show();
}

function enableAvatar() {
	jQuery("#avatardisabled").remove();
}

function screenNameAccepted() {
	var screenname = jQuery("input#screenName_bia").val();
	jQuery(".cnn_INAOvrL10_screenname").html('<p>Congratulations '+screenname+',your screen name has been added. You can now change your avatar.</p>');
}
function hideImage() {
	jQuery("#profile_avatar").hide();
}		
		
		
