// facebook connect scripts

var likePageLoaded = false;
var shareScriptLoaded = false;
var fbFunc = null;
var today=new Date();
monthsOfYear=["January","February","March","April","May","June","July","August","September","October","November","December"];
var url=params(location.search,'url','');


function params(wut,qp,dflt){ dflt=(dflt==null)?'':dflt; try{r=unescape(wut.match(new RegExp(qp+"=+([^&;]*)"))[1]);}catch(qp){r=dflt;} return r; }
function gid(_id){ return document.getElementById(_id); }
function inner(_gid,_text){ gid(_gid).innerHTML=_text; }
function show(){ for (i=0; i<show.arguments.length; i++){ gid(show.arguments[i]).style.display='block'; }}
function hide(){ for (i=0; i<hide.arguments.length; i++){ gid(hide.arguments[i]).style.display='none'; }}


// cookie get / set
function setCookie( name, value, expires, path, domain, secure )
{
	// set time in milliseconds
	var today = new Date();
	today.setTime( today.getTime() );
	if ( expires )
	{
		// set expire time to minutes
		expires = expires * 1000 * 60;
	}
	var expires_date = new Date( today.getTime() + (expires) );
	document.cookie = name + "=" +escape( value ) +
	( ( expires ) ? ";expires=" + expires_date.toGMTString() : "" ) +
	( ( path ) ? ";path=" + path : "" ) +
	( ( domain ) ? ";domain=" + domain : "" ) +
	( ( secure ) ? ";secure" : "" );
}

function getCookie( check_name )
{
	var a_all_cookies = document.cookie.split( ';' );
	var a_temp_cookie = '';
	var cookie_name = '';
	var cookie_value = '';
	var b_cookie_found = false; // set boolean t/f default f
	for ( i = 0; i < a_all_cookies.length; i++ )
	{
		a_temp_cookie = a_all_cookies[i].split( '=' );
		cookie_name = a_temp_cookie[0].replace(/^\s+|\s+$/g, '');
		if ( cookie_name == check_name )
		{
			b_cookie_found = true;
			if ( a_temp_cookie.length > 1 )
			{
				cookie_value = unescape( a_temp_cookie[1].replace(/^\s+|\s+$/g, '') );
			}
			return cookie_value;
			break;
		}
		a_temp_cookie = null;
		cookie_name = '';
	}
	if ( !b_cookie_found )
	{
		return null;
	}
}

function deleteCookie( name, path, domain )
{
	if ( getCookie( name ) )
	{
		document.cookie = name + "=" +
		( ( path ) ? ";path=" + path : "") +
		( ( domain ) ? ";domain=" + domain : "" ) +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}

function checkBirthday () {
  birthday=new Date(gid('_month').value+' '+gid('_date').value+', '+gid('_year').value+' 00:00');
  dateToCheck=new Date(today.toGMTString()).setYear(today.getFullYear()-13); // 13 YEARS OLD CHECK
  if (birthday<dateToCheck){
	// OK - set 10-yr cookie
	setCookie('ageAllowed','true',(3650 * 60 * 24));
	document.getElementById("fb-agegate").style.display = "none";
	if (fbFunc == 'like') {
		//displayLike ();
		launchShare();
	} else if (fbFunc == 'share') {
		launchShare();
	} else if (fbFunc == 'follow') {
		displayFollow ();
	}
  } else {
	// TOO YOUNG
	setCookie('ageAllowed','gated',30);
	initAgeGate ();
  }
}

// called from loaded iframe
function initAgeGate (func) {
    if (navigator.userAgent.match(/iPad/i) == null) {
        $('#overlay').css('width', '100%');
    }
	// if cookie says gated, disable the form
	if (getCookie('ageAllowed') == 'gated') {
		// within 1/2 hour
		gid('_month').disabled=true; gid('_date').disabled=true; gid('_year').disabled=true; gid('SUBMIT').disabled=true; //show ('cancellink');
		document.getElementById("fbTitleBar").style.background = "none repeat scroll 0 0 #FF0000";
		document.getElementById("fbTitleBar").style.fontFamily = '"lucida grande",tahoma,verdana,arial,sans-serif';
		document.getElementById("fbTitleBar").style.fontSize = "10px";
		document.getElementById("fbTitleBar").style.height= "27px";
		document.getElementById("fb_dialog_header").style.paddingTop = "7px";
		
		document.getElementById("fb_dialog_header").innerHTML = "SORRY - You must be at least 13 years old to view this link";
	} else {
		gid('_month').disabled=false; gid('_date').disabled=false; gid('_year').disabled=false; gid('SUBMIT').disabled=false; //hide ('cancellink');
	}
	if (func) {
		fbFunc = func;
	}
}
function resetAgeGate(){
	gid('_month').disabled=false; gid('_date').disabled=false; gid('_year').disabled=false; gid('SUBMIT').disabled=false; hide ('cancellink');
}

// called on main html
function revealAgeGate (func)
{	
	/*
	var x = (document.width - 360)/2;
	if (navigator.appName.indexOf("Microsoft")!=-1) {
		x = (document.body.offsetWidth - 360)/2;
	}
	*/
	var x = ($(window).width() - 360)/2;
	$('#fb-agegate').css ('top','160px');
	$('#fb-agegate').css ('left',x+'px');
	$('#fb-agegate').css ('display','block');	
	initAgeGate (func);
}
// called on loaded iframe
function displayAgeGate (fbFunction) {
	fbFunc = fbFunction;
	resetAgeGate();
	$('#fb-agegate').css ('display','block');
	$('#fb-like').css ('display','none');
}

function closeAgeGate() {
	hideFacebook();
	hideLayer();
}

function hideFacebook() {
	$('#fb-agegate').css ('display','none');
}

function twFollow () {
	if (!getCookie('ageAllowed')) {
		revealAgeGate('follow');
	} else if (getCookie('ageAllowed').toString() == 'gated' || getCookie('ageAllowed').toString() == 'false' || getCookie('ageAllowed').toString() == 'null') {
		revealAgeGate('follow');
	} else if (getCookie('ageAllowed').toString() == 'true') {
		displayFollow ();
	}
}

function fbLike () {
	if (!getCookie('ageAllowed')) {		
		revealAgeGate('like');
		showLayer(); 
	} else if (getCookie('ageAllowed').toString() == 'gated' || getCookie('ageAllowed').toString() == 'false' || getCookie('ageAllowed').toString() == 'null') {
		revealAgeGate('like');
		showLayer(); 
	} else if (getCookie('ageAllowed').toString() == 'true') {
		launchShare();
		//displayLike ();
	}
}

function fbShare () {
	if (!getCookie('ageAllowed')) {
		revealAgeGate('share');
	} else if (getCookie('ageAllowed').toString() == 'gated' || getCookie('ageAllowed').toString() == 'false' || getCookie('ageAllowed').toString() == 'null') {
		revealAgeGate('share');
	} else if (getCookie('ageAllowed').toString() == 'true') {
		launchShare ();
	}
}

function displayFollow () {
	$('#fb-agegate').css ('display','none');
	newWindow ('http://twitter.com/DisneyAnimation', false);
}

function displayLike () {
	/*$('#fb-agegate').css ('display','none');
	popup ('facebook_like.html', 357, 310, false);*/
	var x = (document.width - 360)/2;
    if (navigator.appName.indexOf("Microsoft")!=-1) {
        x = (document.body.offsetWidth - 360)/2;
	}
	
	// don't display like popup
	//document.getElementById("RES_ID_fb_pop_dialog_table").style.left = x + "px";
    //document.getElementById("fb-like").style.display = "block";
}

function closeLike() {		
	hideFacebook();
	document.getElementById("fb-like").style.display = "none";	
}

function launchShare () {
	$('#fb-agegate').css ('display','none');
	window.open("http://www.facebook.com/DisneyEducation", "_blank");
	// hide the overlay
	hideLayer();
}

// activate / deactivate overlay for face book popup
function showLayer(){
	document.getElementById('overlay').style.display = 'block';
	document.getElementById('overlay').style.height = getWindowHeight() + 'px';
}

function hideLayer(){
	document.getElementById('overlay').style.display = 'none';
}

function getWindowHeight(){
	if (document.body.scrollHeight)
		return document.body.scrollHeight;
	return document.documentElement.offsetHeight;
}
