﻿function CheckPage() {
    //var jresults = eval(results);
    if (rets.IsAuthenticated) {
        alert('true');
        if (rets.FunctionVal == "FindaParty") {
            FindaParty(rets.FormValues);
        }
    }
}
function FindaParty(formvals) {
    alert('this works');
    $('#dvPartyAdminInfo').addFBUserTags(formvals.Parties[0].PartyAdmin.FacebookID);
}
$(document).ready(function() {
    $('input[type="submit"]').mouseover(function() {
        $(this).css("background", "#0daeda");
        $(this).css("cursor", "pointer");
    })

    $('input[type="submit"]').mouseout(function() {
        $(this).css("background", "#000000");
    })

    $('a[href^="http://"]').attr("target", "_blank");
    $('#hrAwkward').removeAttr("target");
    $('#hrAwk').removeAttr("target");
    $('#lf2').removeAttr("target");
    //$('#dvFaceBookError')[0].style.display = "none";
    $($("form")[0]).bind("keypress", function(e) {
        if (e.keyCode == 13) {

            return false;
        };

    });
    try {
        $("#twitter_statuses").tweet({
            username: "koodoparty",
            count: 3,
            loading_text: "Loading tweets..."
        });
    }
    catch (e)
        { };
    $(".koodo_modal .button_close").click(function() {
        $(".koodo_modal").fadeOut();
    })
    $('#homenote_link').click(function(e) {
        e.preventDefault;
        FB.ensureInit(function() {
            FB.Connect.requireSession();
        });
    });

    $('#homenote_link2').click(function(e) {
        e.preventDefault;
        FB.ensureInit(function() {
            FB.Connect.requireSession();
        });
    });
    $('#ct2Login').click(function(e) {

        FB.ensureInit(function() {
            FB.Connect.requireSession();
        });
    });
    $("#button_notify .button_wrapper").click(function() {
        //var maskHeight = $(document).height();
        var maskWidth = $(window).width();

        var maskHeight = $(window).height();
        $("#modal_koodocode").css('top', maskHeight / 2 - $("#modal_koodocode").height() / 2);
        $("#modal_koodocode").css('left', maskWidth / 2 - $("#modal_koodocode").width() / 2);
        $("#modal_koodocode").fadeIn();
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

            window.scrollTo(0, 0);
        };
    });
    
    
    $("#whatIsFBCLink, .homenote_whatisfblink").click(function() {
        var maskWidth = $(window).width();
        var maskHeight = $(window).height();
        var ff = "<div style=\"width:450px;\"><h2 class=\"content_header\">What is Facebook Connect?</h2>";
        ff += "<p style=\"line-height: 22px;\">Facebook Connect allows you to use BuildABash.ca as an extension of Facebook and your Facebook account. This makes it easy to invite friends and find parties you want to join. When you invite a friend to join your Build-a-Bash party, that request will appear in their notifications the same way that a Facebook event invitation would.</p>";
        ff += "<p style=\"line-height: 22px;\">Build-a-Bash is not just another Facebook application — it is a secure third-party website that utilizes the Facebook platform.</p></div>";
        $("#dvRegStuff").children().appendTo($('#dvHideContent'));
        $("#dvRegStuff").html(ff);
        $("#modal_koodocode").css('top', maskHeight / 2 - $("#modal_koodocode").height() / 2);
        $("#modal_koodocode").css('left', maskWidth / 2 - $("#modal_koodocode").width() / 2);
        $("#modal_koodocode").fadeIn();
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

            window.scrollTo(0, 0);
        };
    });

    

    $("#modal_notify .button_wrapper").click(function() {
        $("#modal_notify").fadeOut();

    });

});

function auth_fb2() {
    $.post('/Connect/Logout', "", function(result, status) {
        auth_check(result);
    }, "json");
}
function auth_fb() {
	FB.ensureInit(function() {
    var gg;
    if (FB.Facebook.apiClient.get_session() != null)
        gg = FB.Facebook.apiClient.get_session().uid;
    if (gg != null) {
        $('#hdID')[0].value = gg;
        var form = $($("form")[0]);
        var data = form.serialize();
        $.post(form.attr("action"), data, function(result, status) {
            auth_check(result);
        }, "json");
        //});
    };
});
}
var PageStuff;
function auth_check(result) {
    PageStuff = result;
    if (!result.isRegistered && result.fbID != null) {
        $("#dvRegStuff").children().appendTo($('#dvHideContent'));
        $("#dvRegStuff").html($('#dvRegisterControl'));
        $('#dvRegisterControl')[0].style.display = 'block';
        var maskWidth = $(window).width();
        var maskHeight = $(window).height();
        $("#modal_koodocode").css('top', maskHeight / 2 - $("#modal_koodocode").height() / 2);
        $("#modal_koodocode").css('left', maskWidth / 2 - $("#modal_koodocode").width() / 2);
        $('#notify_fb_button')[0].style.display = 'none';
        $('#h2RegNow')[0].style.display = 'none';
        fb_Reg(result);
    }
    else {
        if (result.RefreshPage) {
            if (result.Location == "") {
                if (result.isRegistered) {
                    FB.ensureInit(function() {
                        //var api = FB.Facebook.apiClient;
                        //api.fql_query("SELECT uid FROM user WHERE  uid IN (SELECT uid2 FROM friend WHERE uid1 = " + result.fbID + ") order by first_name", function(res4, ex) {
                            //var data = "fbUsers=" + $.toJSON(res4);
                            //$.post("/dashboard/addfriends", data, function(r2, status) {
                                window.location = window.location;
                            //});
                        //});
                    });
                }
                else {
                    window.location = window.location;
                }
            }
            else {
                FB.ensureInit(function() {
                    //var api = FB.Facebook.apiClient;
                    //api.fql_query("SELECT uid FROM user WHERE  uid IN (SELECT uid2 FROM friend WHERE uid1 = " + result.fbID + ") order by first_name", function(res4, ex) {
                       // var data = "fbUsers=" + $.toJSON(res4);
                        //$.post("/dashboard/addfriends", data, function(r2, status) {
                        window.location = PageStuff.Location;
                        //});
                    //});
                });
            }
        }
        else {
            if ($('#dvRegisterControl')[0] != null) {
                $('#dvRegisterControl')[0].style.display = 'none';
            };
            if (result.ClaimPrize) {
                $("#dvRegStuff").children().appendTo($('#dvHideContent'));
                $("#dvRegStuff").html("<p style='font-size: 12px; line-height: 18px;'>You’ve won one of 10 weekly prizes with BuildABash.ca! </p><div class=\"party_join\"><a href=\"/prizing\">Click here to claim your prize.</a></div><div class=\"clear\"/>");
                var maskWidth = $(window).width();

                var maskHeight = $(window).height();
                $("#modal_koodocode").css('top', maskHeight / 2 - $("#modal_koodocode").height() / 2);
                $("#modal_koodocode").css('left', maskWidth / 2 - $("#modal_koodocode").width() / 2);
                $("#modal_koodocode").fadeIn();
                if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

                    window.scrollTo(0, 0);
                };
            }; 

        }
    }
    if (result.isRegistered) {
        if ($('#dvNamePic')[0] != null) {
            $(document).ready(function() { $('#dvNamePic').addFBUserTags(PageStuff.fbID); });
        }
    };
}
$.fn.addFBUserTags = function(data) {
    this.empty();
    var pic = 'pic' + data;
    var name = 'n' + data;
    $(this).append('<span id="' + pic + '" uid="' + data + '" facebook-logo="true" size="square"></span>');
    $(this).append('<span id="' + name + '" uid="' + data + '" useyou="false"></span>');
    FB.ensureInit(function() {
        FB.XFBML.Host.addElement(new FB.XFBML.ProfilePic($('#' + pic)[0]));
        FB.XFBML.Host.addElement(new FB.XFBML.Name($('#' + name)[0]));
    });
}
$.fn.addItems = function(data) {
    this.empty();
    return this.each(function() {
        var list = this;
        $.each(data, function(index, itemData) {
            var option = new Option(itemData.Text, itemData.Value);
            if ($.browser.msie) {
                list.add(option);
            }
            else {
                list.add(option, null);
            }
        });
    });
};
function DisplayDashBoard(refresh) {
    $.ajax({ type: "GET", url: "/dashboard/GetPointsBank", success: function(result, status) {
        var fc = "";
        fc += "<div id=\"modal_partybank_content\" style=\"width:650px;\"><h2>Point Bank</h2>";
        fc += "<div id=\"modal_partybank_wrapper\" >";
        if (result.MyParties.Parties.length == 0 && result.BankedItems.length > 0) {
            fc += "<p>You’ve earned points but haven’t joined any parties.  Join or create a party now. </p>";
        }
        else if (result.MyParties.Parties.length > 0 && result.BankedItems.length > 0) {
            fc += "<p>You’ve earned points but haven’t assigned them to a specific party yet! It’s time to allocate!</p>";
            var ii = 1;
            jQuery.each(result.BankedItems, function(inx, Votes) {
                fc += "<div class=\"modal_partybank_item\" style=\"width:645px;float:left;height:35px\">";
                if (Votes.PointType == 1 || Votes.PointType == 3) {
                    fc += "<div class=\"modal_partybank_copy\" style=\"float:left;\">You have earned " + Votes.Value + " points for Koodo Code " + Votes.Item + " Select a party: <select id=\"party" + ii.toString() + "\" class=\"Party_Code_Select\" myID=\"" + ii.toString() + "\" style=\"width:130px;\"/></div>";
                }
                else {
                    fc += "<div class=\"modal_partybank_copy\" style=\"float:left;\">You have earned " + Votes.Value + " points for " + Votes.Item + " Select a party: <select id=\"party" + ii.toString() + "\" class=\"Party_Code_Select\" myID=\"" + ii.toString() + "\" style=\"width:130px;\"/></div>";
                }

                fc += "<div class=\"modal_partybank_Button\" id=\"button" + ii.toString() + "\" myID=\"" + ii.toString() + "\" ItemID=\"" + Votes.ItemID.toString() + "\" TypeID=\"" + Votes.PointType.toString() + "\" style=\"padding: 5px;margin:0 0 0 10px; background:#000000; color: #ffffff; font-weight: bold;width:45px; cursor: pointer;float:left;\">Submit</div></div>";
                fc += "<div class=\"clear\"/>";
                ii++;
            });
        }
        else {
            fc += "<p>You currently have no points to allocate. Want to get more party points? Use a Koodo Code or play Koodo-tainment today!</p>";
        }
        fc += "</div></div>";
        $('#modal_partybank_content').remove();
        $("#dvRegStuff").children().appendTo($('#dvHideContent'));
        $("#modal_koodocode").addClass('pointsbankmodal');
        $("#modal_koodocode .button_wrapper").unbind('click');
        $("#modal_koodocode .button_wrapper").click(function() {
            if (refresh != null) {
                if (refresh) {
                    window.location = window.location;
                }
            }
            $("#modal_koodocode").fadeOut();
            $("#modal_koodocode").removeClass('pointsbankmodal');

        });
        $("#dvRegStuff").html(fc);
        $('.Party_Code_Select').addItems(result.MyParties.Parties);

        $('.modal_partybank_Button').mouseover(function() {
            $(this).css("background", "#0daeda");
        })

        $('.modal_partybank_Button').mouseout(function() {
            $(this).css("background", "#000000");
        })
        if (refresh != null) {
            $('.modal_partybank_Button').click(function() {
                var myid = $(this).attr("myID");
                var ItemID = $(this).attr("ItemID");
                var TypeID = $(this).attr("TypeID");
                var s = "#party" + myid;
                var sel = $(s).val();
                var y = "its=" + ItemID + ";" + TypeID + ";" + sel;

                $.post("/dashboard/SavePointsBank", y, function(r2, status) {
                    DisplayDashBoard(true);
                });
            });
        }
        else {
            $('.modal_partybank_Button').click(function() {
                var myid = $(this).attr("myID");
                var ItemID = $(this).attr("ItemID");
                var TypeID = $(this).attr("TypeID");
                var s = "#party" + myid;
                var sel = $(s).val();
                var y = "its=" + ItemID + ";" + TypeID + ";" + sel;

                $.post("/dashboard/SavePointsBank", y, function(r2, status) {
                    DisplayDashBoard();
                });

            });
        }
        var maskWidth = $(window).width();
        var maskHeight = $(window).height();
        $("#modal_koodocode").css('top', maskHeight / 2 - $("#modal_koodocode").height() / 2);
        $("#modal_koodocode").css('left', maskWidth / 2 - $("#modal_koodocode").width() / 2);
        $("#modal_koodocode").fadeIn();
        if ($.browser.msie && $.browser.version.substr(0, 1) < 7) {

            window.scrollTo(0, 0);
        };
    }, dataType: "json", cache: false
    });
    
};