﻿// JScript File
function showmen(menuName) {
	var evt = getEvent(window.event);
	var target,newmenu;

	if(!document.getElementById) {
		throw('ERROR: missing getElementById');
		return false;
	}
	
	if(evt.srcElement)
		target = evt.srcElement;
	else if(evt.target)
		target = evt.target;
	else {
		throw('ERROR: missing event target');
		return false;
	}
	
	newmenu = document.getElementById(menuName);

	if(window.themenu && window.themenu.id!=newmenu.id)
		hidemen();

	window.themenu = newmenu;
	if(!window.themenu.style) {
		throw('ERROR: missing style');
		return false;
	}

	if(themenu.style.visibility == "hidden") {
		var x = gleft(target);
		// Make sure the menu stays inside the page
		// offsetWidth or clientWidth?!?
		if(document.documentElement) {
			if(x + themenu.offsetWidth>document.documentElement.offsetWidth - 20)
				x += target.offsetWidth - themenu.offsetWidth;
		}
		themenu.style.left = (x-(themenu.offsetWidth-target.offsetWidth)) + "px";
		themenu.style.top = (gtop(target)) + "px";
		themenu.style.visibility = "visible";
		themenu.style.zIndex = 100;
	} else {
		hidemen();
	}

	return false;
}

function hidemen() {
	if(window.themenu) {
		window.themenu.style.visibility = "hidden";
		window.themenu = null;
	}
}

function getEvent(eventobj) {
	if(eventobj.stopPropagation) {
		eventobj.stopPropagation();
		eventobj.preventDefault();
		return eventobj;
	} else {
		window.event.returnValue = false;
		window.event.cancelBubble = true;
		return window.event;
	}
}

function gleft(obj) {
	var x = 0;
	while(obj) {
		x += obj.offsetLeft;
		obj = obj.offsetParent;
	}
	
	return x;
}

function gtop(obj) {
	var y = obj.offsetHeight;
	while(obj) {
		y += obj.offsetTop;
		obj = obj.offsetParent;
	}
	return y;
}

function mouseHover(cell,hover) {
	if(hover) {
		cell.className = "postfooter";
		try {
			cell.style.cursor = "pointer";
		}
		catch(e) {
			cell.style.cursor = "hand";
		}
	} else {
		cell.className = "post";
	}
}

document.onclick = hidemen;
if(document.addEventListener) document.addEventListener("click",function(e){window.event=e;},true);
if (document.addEventListener) document.addEventListener("mouseover", function (e) { window.event = e; }, true);


/////////////////////////////////////////////////////////////FACEBOOK
function fbconnected() {
    //alert("FB LOGIN DONE");
    document.location.href = "/FBLogin.aspx?RURL=" + document.location.href;
}


/*
* The facebook_onload statement is printed out in the PHP. If the user's logged in
* status has changed since the last page load, then refresh the page to pick up
* the change.
*
* This helps enforce the concept of "single sign on", so that if a user is signed into
* Facebook when they visit your site, they will be automatically logged in -
* without any need to click the login button.
*
* @param already_logged_into_facebook  reports whether the server thinks the user
*                                      is logged in, based on their cookies
*
*/

var loggedintofb;

function facebook_onload(already_logged_into_facebook) {
    //alert("ALR: " + already_logged_into_facebook);
    // user state is either: has a session, or does not.
    // if the state has changed, detect that and reload.
    FB.ensureInit(function () {
        FB.Facebook.get_sessionState().waitUntilReady(function (session) {
            var is_now_logged_into_facebook = session ? true : false;
            
            //alert("NOW : " + is_now_logged_into_facebook);

            // if the new state is the same as the old (i.e., nothing changed)
            // then do nothing
            if (is_now_logged_into_facebook == already_logged_into_facebook) {
                loggedintofb = true;
                return;
            }

            // otherwise, refresh to pick up the state change
            refresh_page();
        });
    });
}

/*
* Our <fb:login-button> specifies this function in its onlogin attribute,
* which is triggered after the user authenticates the app in the Connect
* dialog and the Facebook session has been set in the cookies.
*/
function facebook_onlogin_ready() {
    // In this app, we redirect the user back to index.php. The server will read
    // the cookie and see that the user is logged in, and will deliver a new page
    // with content appropriate for a logged-in user.
    //
    // However, a more complex app could use this function to do AJAX calls
    // and/or in-place replacement of page contents to avoid a full page refresh.
    refresh_page();
}

function refresh_page() {
    //alert("DOING REFRESH");
    document.location.reload();
    //document.location.href = document.location.href;
}

/*
* Prompts the user to grant a permission to the application.
*/
function facebook_prompt_permission(permission) {
    FB.ensureInit(function () {
        FB.Connect.showPermissionDialog(permission);
    });
}

/*
* Show the feed form. This would be typically called in response to the
* onclick handler of a "Publish" button, or in the onload event after
* the user submits a form with info that should be published.
*
*/
function facebook_publish_feed_story(form_bundle_id, template_data) {
    // Load the feed form
    FB.ensureInit(function () {
        FB.Connect.showFeedDialog(form_bundle_id, template_data);
        //FB.Connect.showFeedDialog(form_bundle_id, template_data, null, null, FB.FeedStorySize.shortStory, FB.RequireConnect.promptConnect);

        // hide the "Loading feed story ..." div
        ge('feed_loading').style.visibility = "hidden";
    });
}

/*
* If a user is not connected, then the checkbox that says "Publish To Facebook"
* is hidden in the "add run" form.
*
* This function detects whether the user is logged into facebook but just
* not connected, and shows the checkbox if that's true.
*/
function facebook_show_feed_checkbox() {
    FB.ensureInit(function () {
        FB.Connect.get_status().waitUntilReady(function (status) {
            if (status != FB.ConnectState.userNotLoggedIn) {
                // If the user is currently logged into Facebook, but has not
                // authorized the app, then go ahead and show them the feed dialog + upsell
                checkbox = ge('publish_fb_checkbox');
                if (checkbox) {
                    checkbox.style.visibility = "visible";
                }
            }
        });
    });
}
