var FareAlert;

(function($) {
    FareAlert = {
	app_url: "",
	app_id: "",
	init: function(callback){
	    FareAlert.loadScript("http://connect.facebook.net/en_GB/all.js", function(){
	      FB.init({
		appId  : FareAlert.app_id,
		status : true, // check login status
		cookie : true, // enable cookies to allow the server to access the session\
		xfbml  : true,  // parse XFBM
		debug  : true,
		logging: true
	      });
	      callback()
	   })

	},
	checkLogin: function(callback) {
	    FareAlert.init(function(){
		FB.getLoginStatus(function(response) {
		    if (response.status === 'connected') {
			callback()
		    } else {
			FB.login(function(response) {
			    if (response.authResponse) {
				callback()
			    }
			},
			{
			    perms: 'email'
			});
		    }
		});
	    });

	    return false
	},

	showOverlay: function(path) {
	    jQuery(document).ready(function() {
	    jQuery.fancybox(FareAlert.app_url + path, {
		type: 'iframe',
		padding: 20,
		width: 480,
		height: 450
	    })
	    });
	},

	addHotel: function(params){
	    FareAlert.checkLogin(function(){
		FareAlert.showOverlay("/hotels/new?mini=true&" + $.param(params))
	    });
	},

	addAlert: function(params) {
	    FareAlert.checkLogin(function() {
		FareAlert.showOverlay("/fare_alerts/new?mini=true&" + $.param(params))
	    });
	    return false;
	},

	editAlerts: function() {
	    FareAlert.checkLogin(function() {
		FareAlert.showOverlay("?mini=true")
	    })
	    return false;
	},

	loadScript: function(sScriptSrc,callbackfunction){
	    var oHead = document.getElementsByTagName('head')[0];
	    if(oHead){
		var oScript = document.createElement('script');

		oScript.setAttribute('src',sScriptSrc);
		oScript.setAttribute('type','text/javascript');

		var loadFunction = function(){
		    if (this.readyState == 'complete' || this.readyState == 'loaded'){
			callbackfunction();
		    }
		};
		oScript.onreadystatechange = loadFunction;
		oScript.onload = callbackfunction;
		oHead.appendChild(oScript);
	    }
	}
    };
}) (jQuery);



