

		var bm = {};
		var rs;
        bm.fbappid='155815161106088';
        bm.perms='create_event,rsvp_event,read_stream,publish_stream,read_friendlists,email,user_activities,user_about_me,user_birthday,user_interests,user_likes,user_location,user_events';
        bm.polltf;
        bm.polltt;
        bm.polltc;
        bm.lock = false;
        bm.type;
        bm.setCookie = function (c_name, value, expiredays) {
            var exdate = new Date();
            exdate.setDate(exdate.getDate() + expiredays);
            document.cookie = c_name + "=" + escape(value) +
                ((expiredays == null) ? "" : ";expires=" + exdate.toGMTString());
        }
        bm.getCookie = function (c_name) {
            if (document.cookie.length > 0) {
                c_start = document.cookie.indexOf(c_name + "=");
                if (c_start != -1) {
                    c_start = c_start + c_name.length + 1;
                    c_end = document.cookie.indexOf(";", c_start);
                    if (c_end == -1) c_end = document.cookie.length;
                    return unescape(document.cookie.substring(c_start, c_end));
                }
            }
            return "";
        }
        bm.reg = function () {
            bm.lock = true;
            $(".register .submit").attr("disabled", "true");
            $.post("/?o=reg&type=" + bm.type,
                   {
                     username:$(".register .username").val(),
                     email:$(".register .email").val(),
                     newsletter:$(".register .newsletter").is(":checked"),
                     image:$(".register .image").val()
                   }
              ,function (msg) {
                    if (msg == null || msg == "") {
                        location.reload();
                    } else { $(".register .error").css("display","block").html("<p style='color:red;'>"+msg+"</p>"); }
                    bm.lock = false;
                    $(".register .submit").get(0).disabled=false;
                }
            );
        }
        bm.pollf = function () {
            $.get('/?o=flm', function (data) {
                data = eval(data);
                if (data != null) {
                    if (data.Registered) { location.reload(); }
                    else {
                        //set register fields
                        $(".register .username:input").val(data.FbScreenName);
                        $(".register .email:input").val(data.Email);
                        $(".register .image").attr("src", data.FbImage);
                        $(".ui-dialog-content").animate({ height: 360 }, "normal", function () {
                            $(".register").show();
                        });
                    }
                } else {
                    bm.polltf = setTimeout(bm.pollf, 3000);
                }
            },"json");
        };
        bm.pollt = function () {
            $.get('/?o=tlm', function (data) {
                data = eval(data);
                if (data != null) {
                    if (data.Registered) { location.reload(); }
                    else {
                        //set register fields
                        $(".register .username:input").val(data.TwScreenName);
                        $(".register .email:input").val(data.Email);
                        $(".register .image").attr("src", data.TwImage);
                        $(".ui-dialog-content").animate({ height: 360 }, "normal", function () {
                            $(".register").show();
                        });
                    }
                } else {
                    bm.polltt = setTimeout(bm.pollt, 3000);
                }
            },"json");
        };
        bm.pollc = function (typ) {
            $.get('/?o=lnk&type='+typ, function (msg) {
                if (msg == null || msg == "") {
                  location.reload();
                }
                else {
                  bm.polltc = setTimeout(function(){bm.pollc(typ)}, 3000);
                }
            });
        };
        bm.setupevents = function (login) {
        	$(document).ready(function () {
        		if (login) { bm.pollf(); }
        		if ($(".fbAttending").length > 0) {
        			FB.api('/me/events', function (response) {
        				if (response && !response.error) {
        					$(".fbAttending").each(function () {
        						var el = this;
        						var id = $(el).find('input[name="fbEventId"]').val();
        						if (id == "" || isNaN(id)) return;
        						var attending = false;
        						var attendingcount = 0;
        						//var eventMembers = FB.Data.query('select "" from event_member where eid={0} and rsvp_status="attending"', id);
        						//var query = FB.Data.query('select "" from event_member where eid={0} and rsvp_status="attending"', id);

        						var eventMembers = FB.Data.query("select pic_small, first_name from user where uid in (select uid from event_member where eid={0})", id);

        						//        						FB.Data.waitOn([query, eventMembers], function (args) {
        						//        							attendingcount = args[0].length;
        						//        							if (attendingcount > 0)
        						//        								$(el).parent().find(".attendees").show();
        						//        							if (attendingcount == 1)
        						//        								$(el).parent().find(".attendingCount").html(attendingcount + ' person attending so far');
        						//        							else
        						//        								$(el).parent().find(".attendingCount").html(attendingcount + ' people attending so far');
        						//        						});

        						eventMembers.wait(function (rows) {

									//display number of people attending
        							attendingcount = rows.length;
        							if (attendingcount > 0)
        								$(el).parent().find(".attendees").show();
        							if (attendingcount == 1)
        								$(el).parent().find(".attendingCount").html(attendingcount + ' person attending so far');
        							else
        								$(el).parent().find(".attendingCount").html(attendingcount + ' people attending so far');


									// add user photos
        							for (var i = 0; i < attendingcount; i++) {
        								$(el).parent().find(".attendees").append(
        						        	'<figure>'
        						        	+ '<img src="' + rows[i].pic_small + '" alt="' + rows[i].first_name + '" height="40" title="' + rows[i].first_name + '" />'
        						        	+ '<figcaption>' + rows[i].first_name + '</figcaption>'
        						        	+ '</figure>'
        						        );
        							}

        						});




        						//        						//        						query = FB.Data.query('select pic_small, first_name from user where uid in '
        						//        						//                                    + '(select uid from event_member where eid={0} and rsvp_status="attending" '
        						//        						//                                    + 'and uid in (select uid2 from friend where uid1={1}))', id, bm.userid);
        						//        						query = fb.data.query('select pic_small, first_name from user where uid in (select uid from event_member where eid={0} and rsvp_status="attending")', id);

        						//        						query.wait(function (rows) {
        						//        							var friendcount = rows.length;
        						//        							/*
        						//        							if (friendcount > 0) {
        						//        							$(el).find(" div.view").prepend(
        						//        							'<ul class="list-view eventfriends">&nbsp;</ul>'
        						//        							);
        						//        							$(el).find(" .attendingcount").html(attendingcount + " people going, including " + friendcount + " of your friends!");
        						//        							}
        						//        							*/
        						//        							for (var i = 0; i < friendcount && i < 3; i++) {
        						//        								//alert(id);
        						//        								$(el).parent().find(".attendees").append(
        						//                                            '<figure>'
        						//                                            + '<img src="' + rows[i].pic_small + '" alt="' + rows[i].first_name + '" height="40" title="' + rows[i].first_name + '" />'
        						//                                            + '<figcaption>' + rows[i].first_name + '</figcaption>'
        						//                                            + '</figure>'
        						//                                        );
        						//        							}
        						//        						});

//        						for (var i = 0; i < response.data.length; i++) {
//        							if (response.data[i].id == id && response.data[i].rsvp_status == 'attending')
//        								attending = true;
//        						}

        						for (var i = 0; i < response.data.length; i++) {
        							if (response.data[i].id == id) {
        								switch (response.data[i].rsvp_status) {
        									case 'attending':
        										$(el).find('button[value="attending"]').addClass('selected');
        										break;
        									case 'unsure':
        										$(el).find('button[value="maybe"]').addClass('selected');
        										break;
        								}
        								break;
        							}
        						}

        					});
        				}
        			}); //end fbapi call
        		}
        		$(".logout").click(function () {
        			FB.logout(function (response) {
        				$.get("/?o=out", function (response) {
        					location.reload();
        				});
        			});
        		});
        		$(".fbshare").click(function () {
        			var share = { method: 'stream.share', u: 'http://becausemagazine.com' };
        			FB.ui(share, function (response) { });
        			return false;
        		});

        	});

        }
        $(document).ready(function () {
        	var bg = $(".sectionBackgroundImage").val()
        	if (bg != '') {
        		$("body").css({
        			backgroundImage: "url('" + bg + "')"
              , backgroundAttachment: "fixed"
              , backgroundOrigin: "initial"
              , backgroundClip: "initial"
              , backgroundPosition: "50% 0%"
              , backgroundRepeat: "no-repeat no-repeat"
        		});
        	}
        	$(".diaryinteractivenext").click(function () {
        		$(".diarydate").hide();
        		$(".diarydate:last").show();
        		$(".diaryinteractive .events-list").hide();
        		$(".diaryinteractive .events-list:last").show();
        	});
        	$(".diaryinteractiveprevious").click(function () {
        		$(".diarydate").hide();
        		$(".diarydate:first").show();
        		$(".diaryinteractive .events-list").hide();
        		$(".diaryinteractive .events-list:first").show();
        	});
        	var callistnext = function (el) {
        		var p = el.parents(".tab-content:first");
        		p.find(".calendar-list-holder").hide();
        		p.find(".calendar-list-holder:last").show();
        	}
        	var callistprevious = function (el) {
        		var p = el.parents(".tab-content:first");
        		p.find(".calendar-list-holder").hide();
        		p.find(".calendar-list-holder:first").show();
        	}
        	var calprevious = function (el) {
        		var p = el.parents(".tab-content:first");
        		p.find(".calendar").hide();
        		p.find(".calendar:first").show();
        	}
        	var calnext = function (el) {
        		var p = el.parents(".tab-content:first");
        		p.find(".calendar").hide();
        		p.find(".calendar:last").show();
        	}
        	$(".calendarlistnext").click(function () {
        		callistnext($(this));
        	});
        	$(".calendarlistprevious").click(function () {
        		callistprevious($(this));
        	});
        	$(".calendarnext").click(function () {
        		calnext($(this));
        	});
        	$(".calendarprevious").click(function () {
        		calprevious($(this));
        	});
        	$(".buy-list:last .tooltip").each(function () {
        		var el = $(this);
        		var p = el.parent();
        		var left = p.position().left + p.outerWidth();
        		var top = p.position().top + p.outerHeight();
        		left = left - el.outerWidth();
        		el.css({ left: left + "px", top: top + "px" });
        	});
        	$("a.expand").each(function () {
        		var link = this;
        		$(this).find(" span").click(function (event) {
        			location.href = link.href;
        			$("div").stop();
        		});
        	});
        	var smapt;
        	var sof = parseInt($("a.sitemap").position().top) - 235;
        	$("div.sitemap").css("top", sof + "px");
        	$("a.sitemap").click(function () { return false; });
        	var sotops = [];
        	$("div.sitemap ul").each(function () {
        		if ($.inArray($(this).offset().top, sotops) == -1)
        			sotops[sotops.length] = $(this).offset().top;
        	});
        	for (var si = 0; si < sotops.length; si++) {
        		var uls = [];
        		$("div.sitemap ul").each(function () {
        			if ($(this).offset().top == sotops[si]) uls[uls.length] = $(this);
        		});
        		if (uls.length > 0) uls[uls.length - 1].find(" li").css({ background: "none" });
        	}
        	var sitemap = function () {
        		$("div.sitemap").css({
        			visibility: "visible"
        		});
        	};
        	$("a.sitemap,div.sitemap").hover(
                function () {
                	clearTimeout(smapt);
                	sitemap();
                },
                function () {
                	smapt = setTimeout(function () { $("div.sitemap").css("visibility", "hidden"); }, 250);
                }
            );
        	$(".connect").click(function () {
        		FB.getLoginStatus(function (response) {
        			if (response.session) {
        				// logged in and connected user, someone you know
        			} else {
        				// no user session available, someone you dont know
        				$(".login").dialog({ height: 280, width: 750, modal: true });
        				$('.connect').parent().submit(function () {
        					return false;
        				});
        			}
        		});
        	});
        	$(".connectTwitter").click(function () {
        		bm.win = window.open("/?o=twreq", "_blank", "width=770,height=362", "");
        		bm.pollc("tw");
        		setTimeout(function () { clearTimeout(bm.polltc); }, 300000);
        	});
        	$(".connectFacebook").click(function () {
        		FB.login(function (response) {
        			if (response.session) {
        				if (response.perms) {
        					bm.pollc("fb");
        					setTimeout(function () { clearTimeout(bm.polltc); }, 300000);
        				}
        			}
        		}, { perms: bm.perms });
        	});
        	$(".register .submit").click(function () {
        		bm.reg();
        	});
        	$(".twlogin").click(function () {
        		bm.win = window.open("/?o=twreq", "_blank", "width=770,height=362", "");
        		setTimeout(bm.pollt, 3000);
        		setTimeout(function () { clearTimeout(bm.polltt); }, 300000);
        		bm.type = "tw";
        	});
        	$(".fblogin").click(function () {
        		FB.login(function (response) {
        			if (response.session) {
        				if (response.perms) {
        					// user is logged in and granted some permissions.
        					// perms is a comma separated list of granted permissions
        					setTimeout(bm.pollf, 3000);
        					setTimeout(function () { clearTimeout(bm.polltf); }, 300000);
        					bm.type = "fb";
        				} else {
        					// user is logged in, but did not grant any permissions
        				}
        			} else {
        				// user is not logged in
        			}
        		}, { perms: bm.perms });
        	});
        	$(".retweet").click(function () {
        		var twid = $(this).attr("id");
        		$.get(
                    '/?o=ret&tweetid=' + twid
                    , function (data) {

                    }
                    , "json"
                );
        	});


        });











             (function () {
             	var e = document.createElement('script');
             	e.type = 'text/javascript';
             	e.src = document.location.protocol +
                '//connect.facebook.net/en_US/all.js';
             	e.async = true;
             	document.getElementById('fb-root').appendChild(e);
             } ()
        );
             window.fbAsyncInit = function () {
             	FB.init({ appId: bm.fbappid, status: true, cookie: true, xfbml: true });
             	FB.getLoginStatus(function (response) {
             		if (response.session) {
             			// logged in and connected user, someone you know
             			var login = false;
             			if ($("#isLoggedIn").val() == "false") {
             				login = true;
             			}
             			bm.userid = response.session.uid;
             			bm.setupevents(login);
             			$(".fbshare").show();
             		} else {
             			//hide fbstuff

             		}
             	});
             }








