﻿var _swfname = 'flashcontent';
var _isLoggedIn = false;
var _api;
var _debugmode = true;
var _FBMLPDialog;
//
function fbInit(swfname, api_key, receiver){
	_swfname = swfname;
	//
	FB.init(api_key, receiver);
}
//
function fbLogin(){
	trace("fbdojslogin");
	FB.ensureInit(function(){
		trace("get sessionstate");
		FB.Facebook.get_sessionState().waitUntilReady(function(){
			trace("waituntilready");
			var uid = FB.Facebook.apiClient.get_session().uid;
			//
			if(uid){
				loggedIn();
			}
		});
		//
		if(!_isLoggedIn){
			FB.Connect.requireSession(function(){
				trace("FB CONNECT REQUIRESESSION");
				loggedIn();
			}, false);
		}
	});
}
function loggedIn(){
	trace("LOGGED IN");
	//
	api = FB.Facebook.apiClient;
	//
	if(!_isLoggedIn){
		_isLoggedIn = true;
		flashCallBack('fblogin');
	}
}
//
function fbLogout(){
	FB.ensureInit(function(){
		FB.Connect.logout(function(){
			trace("FB LOGOUT");
			_isLoggedIn = false;
			flashCallBack('fblogout');
		})
	});
}
//
function showFBMLTemplate(title, fbmlcontents, contentW, contentH){
	trace("SHOWFBMLTEMPLATE ----------------");
	trace(fbmlcontents);
	trace("----------------");
	//
	FB.ensureInit(function(){
		//
		_FBMLPDialog = new FB.UI.FBMLPopupDialog(title, "");
		//
		_FBMLPDialog.setFBMLContent(fbmlcontents);
		if(contentW) _FBMLPDialog.setContentWidth(contentW);
		if(contentH) _FBMLPDialog.setContentHeight(contentH);
		//
		_FBMLPDialog.show();
		//
		//hideFlash();
	});
}
//
function flashCallBack (func) {
	trace("FLASHCALLBACK: " + func);
	//
	if( arguments.length > 1 ){
		document[_swfname][func](Array.prototype.slice.call(arguments).slice(1)[0]);
	}else{
		document[_swfname][func]();
	}
}
//
function postToWall(template_id, template_data, title){
	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! DEPRECATED !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
	FB.ensureInit(function(){
		trace("POSTTOWALL: " + template_id);
		trace(template_data);
		//
		FB.Connect.showFeedDialog(template_id, template_data, null, null, null, FB.RequireConnect.promptConnect, null, title);
	});
}
//
function streamPublish(user_message, attachment, action_links, target_id, user_message_prompt){
	FB.ensureInit(function(){
		trace("STREAMPUBLISH: " + user_message);
		//
		FB.Connect.streamPublish(user_message, attachment, action_links, target_id, user_message_prompt);
	});
}
//
function shareLink(link){
	FB.ensureInit(function(){
		FB.Connect.showShareDialog(link);
	});
}
//
function openPopup(link){
	newwindow = window.open(link,'popup','resizable=0,height=611,width=777,toolbar=no,scrollbars=yes,location=no,status=no');
	if(window.focus) newwindow.focus();
}
//
var timerID;
function hideFlash(){
	$('#flashcontent').css('margin-left','-4000px');
	timerID = setInterval("tick()", 500);
}
function tick(){
	disable = false;
	if($('.fb_popupContainer iframe').position()){
		if(!$('.fb_popupContainer iframe').position().top){
			disable = true;
		}	
	}else{ disable = true; }
	if(disable){
		clearInterval(timerID);
		//showFlash();	
	}
}
function showFlash(){
	$('#flashcontent').css('margin-left','0');
}
//
function trace(msg){
	//if(typeof console !== 'undefined' && console != null && _debugmode) console.debug(msg);
}