﻿//Define Kpw object only if it's not already defined
if ("undefined" == typeof Kpw) {Kpw = {};}

//Initialize ImageClick
$().ready(function(){
    Kpw.FeaturedWedding.init();
});

if ("undefined" == typeof Kpw.FeaturedWedding) {
    Kpw.FeaturedWedding = {
        init: function() {
            Kpw.FeaturedWedding.EnterEditMode(); //testing
            Kpw.Filter.init();

            var contentUrl = $('.contentUrl').val();
            var contentTitle = $('.contentTitle').val();
            $(".contentBookmarks").bookmark({
                url: contentUrl,
                title: contentTitle,
                sites: ['twitter']
            });

        },
        EnterEditMode: function() {
            Kpw.FeaturedWedding.Images.initDragDrop();
        },
        Delete: function() {
            if (confirm("Are you sure you want to delete this wedding?")) {
                __doPostBack('ctl00$phContent$lbDelete', '');
            }
        },
        Dialogs: {
            LoadVendorSelector: function() {
                //init dialog
                $("div.vendorSelector").dialog({
                    title: 'Wedding Vendors',
                    autoOpen: false,
                    resizable: false,
                    height: 600,
                    width: 700,
                    modal: true,
                    overlay: {
                        backgroundColor: '#000',
                        opacity: 0.5
                    },
                    open: function() {
                        $(this).parent().appendTo($("form:first"));
                    },
                    closeOnEscape: true
                });

                $("div.vendorSelector").prev().addClass("vendorSelectorHeader");
                $("div.vendorSelector").addClass("vendorSelectorFooter");
                $("div.vendorSelector").parent().addClass("vendorSelectorWrapper");
                $("div.vendorSelectorWrapper span.ui-dialog-title").after('<span class="caption">Select Vendors</span>');
                
                $('div.vendorSelector input.vendor').autocomplete({
                    source: function(request, response) {
                        $.getJSON("handlers/GetVendors.ashx", {
                            term: extractLast(request.term)
                        }, response);
                    },
                    focus: function() {
                        // prevent value inserted on focus
                        return false;
                    },
                    select: function(event, ui) {
                        var terms = split(this.value);
                        // remove the current input
                        terms.pop();
                        // add the selected item
                        terms.push(ui.item.value);
                        // add placeholder to get the comma-and-space at the end
                        terms.push("");
                        this.value = terms.join(", ");
                        return false;
                    }
                });

                $("div.vendorSelector").dialog('open');
            },
            LoadContentEditor: function() {

                //init dialog
                $("div.weddingContentEditor").dialog({
                    title: 'Wedding Content',
                    autoOpen: false,
                    resizable: false,
                    height: 600,
                    width: 700,
                    modal: true,
                    overlay: {
                        backgroundColor: '#000',
                        opacity: 0.5
                    },
                    open: function() {
                        $(this).parent().appendTo($("form:first"));
                    },
                    closeOnEscape: true
                });

                $("div.weddingContentEditor").prev().addClass("weddingContentEditorHeader");
                $("div.weddingContentEditor").next().addClass("weddingContentEditorFooter");
                $("div.weddingContentEditor").parent().addClass("weddingContentEditorWrapper");
                $("div.weddingContentEditorWrapper span.ui-dialog-title").after('<span class="caption">Add Wedding Text</span>');


                Kpw.FeaturedWedding.TinyMce.init();
                $("div.weddingContentEditor .PaletteSelect").multiSelect({
                    selectAll: false,
                    noneSelected: '',
                    oneOrMoreSelected: '*'
                });

                $('div.multiSelectOptions label input').each(function() {
                    var color = $(this).val();
                    $(this).parent().addClass(color);
                });


                $(".weddingDate").datepicker();

                $("div.weddingContentEditor").dialog('open');
            }
        },
        Images: {
            initDragDrop: function() {
                // let the trash be droppable, accepting the gallery items
                $("div.featuredWedding div.featuredWeddingBody li.imageDrop,div.featuredWedding div.featuredWeddingBody div.imageDrop").droppable({
                    activeClass: 'ui-state-highlight',
                    hoverClass: 'ui-drophover',
                    drop: function(ev, ui) {
                        $(this).html("");

                        var removeImage = '<a href="#Remove" class="removePhoto" title="remove" onclick="Kpw.Vendor.Photos.remove(this);return false;"></a>';
                        $(this).prepend(removeImage);
                        $("a.removePhoto").fadeIn("fast");

                        var image = $(ui.draggable).clone();
                        $(this).find("a.removePhoto").after(image.html());
                        image.remove();

                        var fullsizePath = $(this).find("span input.fullsizePath").attr("value");

                        if ($(this).hasClass("largeImage")) {
                            fullsizePath = fullsizePath.replace("245.232", "495.469");
                        }

                        if ($(this).hasClass("tallImage")) {
                            fullsizePath = fullsizePath.replace("245.232", "245.315");
                        }

                        $(this).find("span img")
					            .attr("src", fullsizePath)

                        Kpw.FeaturedWedding.Images.updateImageData();
                    }
                });

            },
            updateImageData: function() {
                //re-initialize data array
                Kpw.FeaturedWedding.Images.data = new Array();

                var photo;

                //populate secondary photo data
                $("div.featuredWedding div.featuredWeddingBody li.imageDrop, div.featuredWedding div.featuredWeddingBody div.imageDrop").each(function(i) {
                    photo = {
                        Position: i + 1,
                        Id: $(this).find("input.photoId").attr("value"),
                        Alt: $(this).attr("alt"),
                        Title: $(this).attr("title"),
                        ThumbnailPath: $(this).find("input.thumbnailPath").attr("value"),
                        FullsizePath: $(this).find("input.fullsizePath").attr("value")
                    }
                    Kpw.FeaturedWedding.Images.data.push(photo);
                });

                $("div.featuredWedding div.dialogs input.data").attr("value", JSON.stringify(Kpw.FeaturedWedding.Images.data));
            }
        },
        TinyMce: {
            init: function() {

                var editor = $(".txtWeddingContent").attr('id');

                tinyMCE.init({
                    // General options
                    mode: "exact",
                    elements: editor,
                    theme: "advanced",
                    plugins: "safari,pagebreak,style,layer,save,advimage,advlink,preview,contextmenu,paste,spellchecker",

                    // Theme options
                    theme_advanced_buttons1: "newdocument,|,bold,italic,underline,|,justifyleft,justifycenter,justifyright,justifyfull,|,bullist,numlist,|,blockquote,|,image,|,spellchecker,code",
                    theme_advanced_buttons2: "",
                    theme_advanced_buttons3: "",
                    theme_advanced_disable: "hr,sub, sup, charmap, visualaid",
                    theme_advanced_toolbar_location: "top",
                    theme_advanced_toolbar_align: "left",
                    theme_advanced_statusbar_location: "bottom",
                    theme_advanced_resizing: true,

                    //Spellchecker
                    spellchecker_languages: "English=en",
                    spellchecker_rpc_url: "TinyMCE.ashx?module=SpellChecker",

                    // Example content CSS (should be your site CSS)
                    content_css: "../css/kpw.css,../Editor/tinymce/examples/css/word.css"
                });
            }
        }
    }

}
