﻿(function () {
    var doc = document, win = window;



    function LoadAffiliatePage() {
        $.ajax({
            url: "GetData.aspx?affiliatedata",
            success: function (response) {
                BuildList(response.items);
            }
        });
        $('input#btnSubmit').bind('click', function () {
            var aID = $('.as-values').first().val();
            $.get('mbg.asmx?addstore', { afilliateID: aID }, function (ret) {
                //document.location = '/thank-you?c=' + cID;
            });
            return false;
        });
    }

    function LoadCausePage() {
        $.ajax({
            url: "GetData.aspx?causedata",
            success: function (response) {
                BuildList(response.items);
            }
        });
        $('input#btnSubmit').bind('click', function () {
            var cID = $('.as-values').last().val();
            $.get('mbg.asmx?addcause', { causeID: cID }, function (ret) {
                document.location = 'affiliate.aspx';
            });
            return false;
        });
    }

    function LoadHomePage() {
        $('a#sign-in').bind('click', function () {
            var $clone = $('#signup').clone();
            $clone.find('h4').text('MyBigGive Sign In').parent().find('button').text('sign in').parent().find('div#namezone').remove();
            $.fancybox({ 'content': $clone, 'autoScale': false, 'modal': false,
                onStart: function () {

                },
                onClosed: function () {
                    $('div.error').hide();
                }, onComplete: function () {
                    $('#fancybox-content button#btnSubmit').bind('click', function () {
                        var email = $('#fancybox-content #txtEmail').val();
                        if (email == '') {
                            $('div.error').text('email is required').show();
                        } else if (email != '' && !email.match('[^ @]*@[^ @]*')) {
                            $('div.error').text('email does not appear to be a valid format').show();
                        } else {
                            $('div.error').hide();
                            $.ajax({
                                url: "signin.aspx?emailsignin",
                                data: { email: email },
                                success: function (response) {
                                    document.location = 'causes.aspx';
                                }
                            });
                        }

                        return false;
                    });
                }
            });
        });

        $('a#sign-up').bind('click', function () {
            $.fancybox({ 'content': $('#signup').clone(), 'autoScale': false, 'modal': false,
                onClosed: function () {
                    $('div.error').hide();
                }, onComplete: function () {
                    $('#fancybox-content button#btnSubmit').bind('click', function () {
                        var name = $('#fancybox-content #txtName').val();
                        var email = $('#fancybox-content #txtEmail').val();
                        if (email == '') {
                            $('div.error').text('email is required').show();
                        } else if (email != '' && !email.match('[^ @]*@[^ @]*')) {
                            $('div.error').text('email does not appear to be a valid format').show();
                        } else {
                            $('div.error').hide();
                            $.post('@Url.Action("Signup", "Home")', { email: email, home: home }, function (result) {
                                $('#selectOS').append($('<div />', { html: result }));
                            });
                        }

                        return false;
                    });
                }
            });
        });
    }

    win.mbg = {
        AffiliatePage: LoadAffiliatePage,
        CausePage: LoadCausePage,
        HomePage: LoadHomePage,
        version: '3.0.0'
    };
})();


function BuildList(list) {
    $('#txtCause').autoSuggest(list, { selectedItemProp: "name", searchObjProps: "name", selectionLimit: 1, startText: "Find your cause...", retrieveLimit: 40 });
}

function BuildRecent(list) {

    $('<div />', { id: 'recent', html: '<ul class="recentList"></ul>', style: 'width:500px;' }).appendTo($('div.hiddenzone'));
    $.each(list, function (i, v) {
        $('<li />', { html: '<img src="' + v.img + '" alt="' + v.name + '" />' }).appendTo($('div#causes ul'));
    });
    $('div.hiddenzone').show();
}

var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-7821065-2']);
_gaq.push(['_trackPageview']);

(function () {
    var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
    ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
    var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
