//var xdr_path 		= "";
//var fb_uid 			= 0;
var fb_friends		= 0;

jQuery.noConflict();
jQuery(document).ready(function($) {
try{            
/*			var fbuid = get_cookie_fbuid();
			var tmp1 = jQuery("#fb_data").html();
			tmp1 = tmp1.replace(/__fbuid__/ig, fbuid);
			jQuery("#fb_data").html(tmp1);*/
			FB.init(
				{
					appId:	api_key,
					status: true,
					cookie: true,
					xfbml: true,
					oauth : true,
					reloadIfSessionStateChanged : true,
					doNotUseCachedConnectState:true
				}
			);
			

			FB.XFBML.parse(document.getElementById("fb_data"));
			FB.Event.subscribe('auth.sessionChange', function(response) {
				if (response.session) {
				} else {
				// The user has logged out, and the cookie has been cleared
					//jQuery.cookie("irlogged", null);
					//jQuery.cookie("fbuid", null);
					fb_logout();
					window.location.href = logout_url;
				}
			});

		if(fb_uid) {   
					var raw_fb_cookie = jQuery.cookie("fbsr_" + api_key); 
					if(typeof(raw_fb_cookie) != "undefined") {  
						var re = /\&uid=([0-9]+)/i;
						var tmp = raw_fb_cookie.match(re);

						var re2 = /access_token=(.+?)\&/i;
						var at = raw_fb_cookie.match(re2);
                           
						jQuery.cookie("fan_cookie_id", tmp[1] + "&" + at[1], {"domain" : root_domain});
					}
					
					

            }

		 else {




		}

			// fb like trigger
			FB.Event.subscribe('edge.create', function(response) {
				if(!fb_uid) {
					$("#like_block").replaceWith("<small>" + loading_text + "</small><div id='ajax_bar'></div>");
					window.location.href = fb_login_url;
				} else {

					if($("#download_fan_btn").length > 0) {
						$("#download_fan_btn").attr("disabled", "");
						$("#download_fan_btn").addClass("download_fan_btn_active");
					}
				}
			});


			var _fan_cookie_id = 0;
			var _fancookie = jQuery.cookie("fan_cookie_id");
			_fancookie = _fancookie.split("&"); 
			if(_fancookie.length > 1) {
				_fan_cookie_id = _fancookie[0];
				_fan_cookie_access = _fancookie[1];
			}

			var checkfan_params = {method: 'pages.isfan', page_id : toggle_downloads_fb_pageid};
			if(_fan_cookie_id != 0 ) {
				checkfan_params = {method: 'pages.isfan', page_id : toggle_downloads_fb_pageid, uid : _fan_cookie_id, access_token : _fan_cookie_access};
			}
			FB.api(checkfan_params,  function(response) {
				if(response == true && $("#download_fan_btn").length > 0){
					$("#download_fan_btn").attr("disabled", "");
					$("#download_fan_btn").addClass("download_fan_btn_active");
				}
			});
/*
	function on_like() {



	}
*/
} catch(e){
}
/*
		if(!fb_uid) {
			FB.Event.subscribe('xfbml.render', function(response) {
				var ifr_w = $('iframe.fb_ltr').width();
				var ifr_h = $('iframe.fb_ltr').height();
				$("#ifr").css("position", "relative");
				$("#ifr").append("<div style='position:absolute;top:0;left:0;width:"+ifr_w+"px;height:"+ifr_h+"px;cursor:pointer;cursor:hand;z-index:1000;border:1px solid red;' id='ifr2'></div>");
				$("#ifr2").click(function(){
					window.location.href=fb_login_url;
				});
			});
		}
*/
});
    function handleSessionResponse(response) {
						// if we dont have a session 
						//(which means the user has been logged out, redirect the user)
						if (!response.session) { //alert("direct logout");
						window.location.href = logout_url;
						return;
						}
                         //alert("facebook logout");
						//if we do have a non-null response.session, call FB.logout(),
						//the JS method will log the user out
						//of Facebook and remove any authorization cookies
						FB.logout(handleSessionResponse);
						}
	function fb_logout(){ 
	
	   FB.getLoginStatus(handleSessionResponse); 
	  
	   
		/*try{
			FB.logout(handleSessionResponse);
			return false;
		}catch(e){ 
			window.location.href = logout_url;
		}*/
	
   }


