﻿
//Intialiaze Kpw
$(document).ready(function() {
    Kpw.init();
    if (typeof Sys != "undefined" && Sys.WebForms && Sys.WebForms.PageRequestManager && Sys.WebForms.PageRequestManager.getInstance() != null) {
        Sys.WebForms.PageRequestManager.getInstance().add_endRequest(Kpw.PhotoInfo.init);
    }
});


if ("undefined" == typeof Kpw) {
    Kpw = {
        UserId: "",
        bindEvents: function() {
            //toggle login field default text
            $("input.username").focus(function() {
                if ($(this).attr("value").toLowerCase() == "email") {
                    $(this).attr("value", "");
                }
            });
            $("input.username").blur(function() {
                if ($(this).attr("value").length == 0) {
                    $(this).attr("value", "email");
                }
            });
            $("input.passwordFaux").focus(function() {
                $(this).hide();
                $("input.password").css("display", "inline").focus();
            });
            $("input.password").blur(function() {
                if ($(this).attr("value").length == 0) {
                    $(this).hide();
                    $("input.passwordFaux").css("display", "inline");
                }
            });
            //toggle search field default text
            $("input.search").focus(function() {
                if ($(this).attr("value").toLowerCase() == "search") {
                    $(this).attr("value", "");
                }
            });
            $("input.search").blur(function() {
                if ($(this).attr("value").length == 0) {
                    $(this).attr("value", "Search");
                }
            });
        },
        init: function() {
            //init bind events
            Kpw.bindEvents();
            Kpw.Login.init();
            Kpw.PhotoInfo.init();
            Kpw.VendorDialogs.init();
            Kpw.Dialogs.init();


            //COMING SOON
            if ($("input.showComingSoon").length > 0) {
                //alert("Coming Soon");
                Kpw.Dialogs.showComingSoon();
            }

        },
        Login: {
            init: function() {

                // Firefox does not support the ASP.NET Panel's DefaultButton
                // functionality because it does not apply a click event
                // to anchors by default. The code below prepares the login
                // link (at the top of the master page) for this.

                var anchor = $('a.login');
                if (anchor != null && anchor.length > 0) {
                    if (typeof (anchor[0].click) == 'undefined') {
                        anchor[0].click = function() {
                            var result = true;
                            if (this.onclick) result = this.onclick();
                            if (typeof (result) == 'undefined' || result) {
                                eval(this.href);
                            }
                        };
                    }
                }
            }
        },
        PhotoInfo: {
            init: function() {

                //Need a variabel telling us what page to call to get image info block.
                var imageInfoUrl = $('#imageInfoUrl').val();

                if (imageInfoUrl !== null) {
                    $("div.imagePlusTag div.hook").bind("click", function(el) {
                        var contentId = $(this).parent().parent().children(".photoWrapper").children(".photoId").val()
                        $(this).load(imageInfoUrl, contentId, Kpw.PhotoInfo.onImageInfoLoaded);
                    });
                }

            },
            onImageInfoLoaded: function() {

                $(this).css({
                    "height": "125px",
                    "width": "325px",
                    "background-color": "#F2F2F2",
                    "z-index": "1000"
                });

                $(this).hover(
                    function() {
                    },
                    function() {
                        $("this").hide("blind", 200);
                        $("div.imageInfo").remove();
                        $("div.hook").css({
                            "height": "24px",
                            "width": "24px",
                            "background-color": "transparent"
                        });
                        $(this).show();
                    }
                );
            },

            onSaveToBrideGallery: function(url) {

                $.ajax({
                    type: "POST",
                    contentType: "application/json; charset=utf-8",
                    dataType: "json",
                    url: url,
                    data: "{}",
                    // Error!
                    error: function(xhr, status, error) {
                        // Display a generic error for now.
                        //alert("Error saving content! " + error.toString());
                    },
                    success: function(data, textStatus, XMLHttpRequest) {
                        //alert("Success saving content!");
                        //alert($('div.imageInfo ul.links li span.saveFavorite').html());
                        $('div.imageInfo ul.links li span.saveFavorite').html(data.message);
                    }
                });
                //alert("Added to Favorites!");
            }
        }, //end Kpw.PhotoInfo

        VendorDialogs: {
            init: function() {
                Kpw.VendorDialogs.MyStatus.init();
            },
            MyStatus: {
                click: function() {
                    $("div.postStatus").dialog('open');
                },
                init: function() {

                    //init dialog
                    $("div.postStatus").dialog({
                        title: 'Kpw Status',
                        autoOpen: false,
                        resizable: false,
                        height: 250,
                        width: 600,
                        modal: true,
                        overlay: {
                            backgroundColor: '#000',
                            opacity: 0.5
                        },
                        open: function() {
                            $(this).parent().appendTo($("form:first"));
                            //$("textarea.mainDescriptionText").focus();
                        },
                        closeOnEscape: true
                    });

                    $("div.postStatus").prev().addClass("postStatusHeader");
                    $("div.postStatus").next().addClass("postStatusFooter");
                    $("div.postStatus").parent().addClass("postStatusWrapper");
                    $("div.postStatusHeader span.ui-dialog-title").after('<span class="caption">Update Your My Kpw Status</span>');
                }

}//End My Status
            }, //End Vendor Dialogs
            Dialogs: {
                init: function() {
                
                    $("div.showVideo").dialog({
                        dialogClass: 'showVideo',
                        autoOpen: false,
                        resizable: false,
                        height: 375,
                        width: 475,
                        modal: true,
                        overlay: {
                            backgroundColor: '#000000',
                            opacity: .9
                        },
                        open: function() {
                            $(this).parent().appendTo($("form:first"));
                            //$("textarea.mainDescriptionText").focus();
                        },
                        closeOnEscape: false
                    });
                },
                showVideo: function() {
                    $("div.showVideo").dialog('open');
                    $(".ui-widget-overlay").css("opacity", ".9");
                }
            },
            Filter: {
                init: function() {
                    $("div.filter div.colorCategoryWrapper .ddlSelectColor").multiSelect({
                        selectAll: false,
                        noneSelected: '',
                        oneOrMoreSelected: '*'
                    });

                    $("div.filter div.colorCategoryWrapper .ddlSelectCategory").multiSelect({
                        selectAll: false,
                        noneSelected: '',
                        oneOrMoreSelected: '*'
                    });

                    $('div.filter div.colorCategoryWrapper div.multiSelectOptions label input').each(function() {
                        var color = $(this).val();
                        $(this).parent().addClass(color);
                    });
                }
}//end filter
}//End KPW
}

function split(val) {
    return val.split(/,\s*/);
}
function extractLast(term) {
    return split(term).pop();
}






