﻿var Dialogue = {
    box: null,
    box_id: '',
    init: function() {
        this.setUpLinks();
        // create dialogue container
        Dialogue.box = $('<div id="dialogueContainer">')
                        .insertAfter($('#page'))
                        .css('height', $(document).height());
        if ($.browser.msie) {
            Dialogue.box.append('<div class="shade">');
        }
    },
    setUpLinks: function() {
        $('#share a.btn').click(function(e) {
            e.preventDefault();
            // grab dialogue id (file name)
            var id = this.href.replace(/(.*\/.*\/.*\/)(.*)(\.aspx)/, "$2");
            Dialogue.launch(id);
        });

    },


    onCopyWidgetCode: function(e) {
        e.preventDefault();
        if (window.clipboardData && clipboardData.setData) {
            var content = $(".widget_code .content textarea").val();
            clipboardData.setData("text", content);
        }
        else {
            alert("Please select the text and right click to copy");
        }
    },

    launch: function(id, query) {
        //url: './dialogues/' + id + '.html',
        // if the same dialogue exists show and position

        if (typeof Arcade !== 'undefined' && $.browser.msie === true) {
            Arcade.show(Arcade.openGame, true, false);
        }

        if ($('#' + id + 'Dialogue').length > 0 && (query == null || query == undefined)) {
            // Dialogue.box.show();
            Dialogue.position(id);
            Dialogue.focus(id);
            // otherwise create the dialogue and request it's content
        } else {
            var d = 'rand=' + Math.random() * 1000000000000 + ((query != null && query != undefined) ? "&" + query : "");
            var url = '/forms/' + id + '.aspx'; //?' + data
            //alert(url);
            $.ajax({
                type: 'POST',
                url: url,
                data: d,
                dataType: 'html',
                success: function(html) {
                    Dialogue.box_id = id;
                    Dialogue.box.html(""); //remove content
                    Dialogue.box.html(html);
                    // .show();


                    Dialogue.position(id);
                    Dialogue.setUpClose(id);
                    Dialogue.focus(id);
                }
            });
        }


    },
    focus: function(id) {
        var el = Dialogue.box.find('#' + id + 'Dialogue input:text');
        if (el.length != 0) {
            el.get(0).focus();
        }
    },
    position: function(id) {
        // offset top with current scroll position
        // Dialogue.box.css('top', $(window).scrollTop())

        var current_dialogue_id = '#' + id + 'Dialogue';

        // get dialogue box dimensions
        var dialogue = Dialogue.box.find(current_dialogue_id);
        var dialogue_content = dialogue.find('.content');

        dialogue.height(dialogue_content.height() + 35);
        dialogue.find('.contour').height(dialogue_content.height() + 25);
        dialogue.find('.contour .middle').height(dialogue_content.height() + 9);

        var viewport_height = $(window).height();
        var dialogue_height = dialogue.height();

        // position dialogue box in relation to its container
        dialogue.css('top', ($(window).scrollTop()) + ($(window).height() / 2) - (dialogue_height / 1.8));

        Dialogue.box.find('.dialogue:not(' + current_dialogue_id + ')').css('left', -10000);


        Dialogue.box.css('left', 0)
            .find(current_dialogue_id)
                .css('left', '');
    },
    close: function() {
        // Dialogue.box.hide();
        Dialogue.box.css('left', '');
        $('body').removeClass('dialogue_shown');
        // restart carousel
        if (typeof Carousel != "undefined" && !Carousel.running) {
            Carousel.startCycle();
        }
    },
    setUpClose: function(id) {
        // attach the close button to the dialogue if it doesn't already exist
        if ($('#' + id + 'Dialogue').find('.close').length < 1) {
            $('<a href="#" class="close">close</a>')
                .prependTo(Dialogue.box.find('#' + id + 'Dialogue'))
                .click(function(e) {
                    e.preventDefault();
                    Dialogue.close();
                });
        }

        // add click events to close dialogue
        $('#dialogueContainer').click(function(e) {
            // if clicked inside the dialogue
            if (e.target.className != 'close' && $(e.target).parents('.dialogue').length > 0 || e.target.className == 'dialogue') {
                // do nothing
                // if clicked outside of dialogue or on the close button
            } else {
                e.preventDefault();
                Dialogue.close();
            }
        });
    }
};
