﻿  jQuery.cookie = function (name, value, options) {

                if (typeof value != 'undefined') {
                    options = options || {};
                    if (value === null) {
                        value = '';
                        options.expires = -1;
                    }
                    var expires = '';
                    if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                        var date;
                        if (typeof options.expires == 'number') {
                            date = new Date();
                            date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                        } else {
                            date = options.expires;
                        }
                        expires = '; expires=' + date.toUTCString();
                    }

                    var path = options.path ? '; path=' + (options.path) : '';
                    var domain = options.domain ? '; domain=' + (options.domain) : '';
                    var secure = options.secure ? '; secure' : '';
                    document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');

                } else {
                    var cookieValue = null;
                    if (document.cookie && document.cookie != '') {
                        var cookies = document.cookie.split(';');
                        for (var i = 0; i < cookies.length; i++) {
                            var cookie = jQuery.trim(cookies[i]);

                            if (cookie.substring(0, name.length + 1) == (name + '=')) {
                                cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                                break;
                            }
                        }
                    }

                    return cookieValue;
                }


            };

            function personalizeBar(memberInfo) {
                var $avaatarSrc = document.createElement("img");
                $avaatarSrc.setAttribute('src', 'http://wwwmedia.theknot.com/profiles/avatar.aspx?username=' + memberInfo.memberID);
                jQuery('#barUserAvatar').append($avaatarSrc);
                jQuery('#userName').text(memberInfo.memberName);
                jQuery('#bottomBarUser p').text(memberInfo.getCopy());


                /* $('#BottomBar').delegator("click", {
                    '#barMyKnot h2': function () { window.location = "http://www.theknot.com/pl_main.html"; },
                    '#barMyList h2': function () { window.location = "http://planning.theknot.com/wedding-checklist"; },
                    '#barMyFav h2': function () { window.location = "http://www.theknot.com/mynotebook.htm"; },
                    '#barMyVen h2': function () { window.location = "http://wedding.theknot.com/local-wedding-vendors/" + memberInfo.localVendor + ".aspx"; },
                    '#barMyBoards h2': function () { window.location = "http://community.theknot.com/"; },
                    '#barMyShortCut': function () { window.location = "http://global.theknot.com/join/memberprofile.aspx"; },
                    '#barMyShortCut h3': function () { window.location = "http://global.theknot.com/join/memberprofile.aspx"; },
                    '#barMyShortCut div': function () { window.location = "http://global.theknot.com/join/memberprofile.aspx"; }
                }); */
            }

            function buildBar() {
                jQuery("#BottomBar").show();
                jQuery(".userCallout dt").click(function () { $(this).find('ul').show(); });

                jQuery('.myBarPullup li').click(
                    function (e) {
                        jQuery(".userCallout dt").unbind("click");
                        jQuery(this).parent().siblings().text($(this).text());
                       jQuery(this).parent().hide();
                        jQuery(".userCallout dt").bind("click", function () { $(this).find('ul').show(); });
                    }
                );

               /*  $('#BottomBar').delegator("click", {
                    '#btnSubmitMyDate': function () { window.location = "http://global.theknot.com/join/me_loginmain.aspx?hp=logon&target=http://weddings.theknot.com/checklist/ViewByThisMonth.aspx"; }


                }); */

                jQuery('#barToggler').click(
                    function (event) {
                        if (jQuery('#BottomBar').css('bottom') == "0px") {


                            jQuery('#BottomBar').animate({ bottom: "-48px" }, "slow", function () {

                                jQuery('body').css('margin-bottom', "2px");
                                jQuery('#barToggler').find('div').removeClass('arrowUp').addClass('arrowDwn');
                                jQuery('#barDisplayState').text('open');
                                jQuery.cookie('bottomBarState', '0', { expires: 1, path: '/', domain: '.sampatti.com' });
                            });

                        }
                        else if (jQuery('#BottomBar').css('bottom') == "-48px") {

                            jQuery('#BottomBar').animate({ bottom: "0px" }, "slow", function () {
                                jQuery('#barDisplayState').text('close');
                                jQuery('#barToggler').find('div').removeClass('arrowDwn').addClass('arrowUp');
                                jQuery('body').css('margin-bottom', "50px");
                                jQuery.cookie('bottomBarState', null);
                                jQuery.cookie('bottomBarState', '1', { expires: 1, path: '/', domain: '.sampatti.com' });
                            });
                        }
                    }
                );

                jQuery("body").click(function (event) {
                    if (!jQuery(event.target).is('div.dateLabel')) {
                        jQuery('.myBarPullup').hide();
                    }
                });
            }

            function member(cookieName, cookieMemberID, val1, val2) {
                this.cookieName = cookieName;
                this.cookieMemberID = cookieMemberID;
                this.loggedIn = false;
                this.localVendor = "";
                this.memberCookie = "";
                this.memberID = 0;
                this.memberName = "";
                this.memberWeddingDate = "";
                this.memberCopy = "";
                this.localkeyword = "localkeyword";

                this.getCopy = function () {
                    var now = new Date();
                    var date = new Date();
                    var dDay = new Date(this.memberWeddingDate.substring(0, 4), this.memberWeddingDate.substring(4, 6) - 1, this.memberWeddingDate.substring(6, 8));

                    var one_day = 1000 * 60 * 60 * 24;

                    if (this.memberWeddingDate != "") {
                        this.memberWeddingDate = Math.ceil((dDay.getTime() - date.getTime()) / (one_day));

                        if (this.memberWeddingDate == 0)
                            return this.memberCopy = "It's your wedding day! Congrats and good luck!";
                        else if (this.memberWeddingDate > 0)
                            return this.memberCopy = this.memberWeddingDate + ' days until your wedding!';
                        else if (this.memberWeddingDate < 0)
                            return this.memberCopy = (this.memberWeddingDate * -1) + ' days since your wedding!';
                        else
                            return this.memberCopy = "It's your wedding day! Congrats and good luck!";

                    }
                    else {
                        return this.memberCopy = " ";
                    }

                }

                this.getCookie = function (cn) {
                    var cookies = document.cookie;

                    var member = cookies.split(";");
                    for (var i = 0; i < member.length; i++) {
                        if (member[i].substring(0, cn.length + 1) == (this.cookieName + "=")) {
                            this.loggedIn = true;
                            this.memberCookie = member[i];
                            return this.memberCookie;
                        }
                        if (member[i].substring(0, cn.length + 1) == (this.cookieMemberID + "=")) {
                            var $is = unescape(member[i]);
                            var $item = $is.substring(4, $is.length);
                            var $id = $item.split('&');
                            for (var $x = 0; $x < $id.length; $x++) {
                                var $y = $id[$x].split('=');

                                if ($y[0] == "a") {
                                    return $y[1];
                                }
                            }
                        }
                    }
                    return member = "";
                }

                this.getMember = function () {
                    if (this.getCookie(this.cookieName).length > 0) {
                        var memberInfo = this.getCookie(this.cookieName).split('|');
                        for (var i = 0; i < memberInfo.length; i++) {
                            var $val = unescape(memberInfo[i]);
                            if ($val.indexOf('^') != -1) {
                                var memberVal = $val.split('^');
                                if (memberVal[0] == val1)
                                    this.memberName = memberVal[1];
                                if (memberVal[0] == val2)
                                    this.memberWeddingDate = memberVal[1];
                                if (memberVal[0] == this.localkeyword)
                                    this.localVendor = unescape(memberVal[1].replace(/ /g, "-"));

                            }
                        }

                        this.memberID = this.getCookie(this.cookieMemberID);
                    }


                }
            }

           /*  $.fn.delegator = function (type, x) {
                return this.bind(type, function (e) {
                    for (var selector in x) {
                        var $t = $(e.target);
                        if ($t.is(selector)) return x[selector].apply(this, $.makeArray(arguments));
                    }
                })
            } */
      

