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() {
        $('#membership a.btn').click(function(e) {
            e.preventDefault();
            // grab dialogue id (file name)
            var id = this.href.replace(/(.*\/.*\/.*\/)(.*)(\.aspx)/, "$2");
            Dialogue.launch(id);
        });
        var widget_action_buttons = $('#widgets .widget .action a');
        if (widget_action_buttons.length > 0) {
            widget_action_buttons.click(function(e) {
                e.preventDefault();
                var id = 'widget_' + this.hash.substr(1);
                Dialogue.launch(id, "id=" + this.hash.substr(1));
            });
        }

        $('#rules a').click(function(e) {
            e.preventDefault();
            // grab dialogue id (file name)
            var id = this.href.replace(/(.*\/.*\/.*\/)(.*)(\.html)/, "$2");
            Dialogue.launch(id);
        });
    },

    setupWidgetButtons: function() {
        $(".game_thumb a.copy_button").unbind('click', Dialogue.onCopyWidgetCode);
        $(".game_thumb a.copy_button").bind('click', Dialogue.onCopyWidgetCode);
    },

    onCopyWidgetCode: function(e) {
        e.preventDefault();
        if (window.clipboardData && clipboardData.setData) {
            var content = $(".game_widget 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) {
            if (Arcade.openGame !== undefined) {
                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 = '/dialogues/' + 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();

                    if (Forms[id + "Init"]) {
                        Forms[id + "Init"]();
                    }
                    if (id.indexOf("widget_") >= 0) {
                        Dialogue.setupWidgetButtons();
                    }


                    Dialogue.position(id);
                    Dialogue.setUpClose(id);
                    Dialogue.focus(id);
                }
            });
        }

        // Stop carousel if it's running
        if (typeof Carousel != "undefined" && Carousel.running) {
            Carousel.stopCycle();
        }

        // $('body').addClass('dialogue_shown');
    },
    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();
            }
        });
    }
};

function popWindow(url, args) {
    window.open(url, "", args);
}

///* top banner loader */


//flashvars = {
//    clickTag: "http://www.awesomeadventurebooks.com/incorrigible/index.html"
//};
//var params = {
//    movie: "/swf/hcc801_incorrigablechildren728x90.swf",
//    allowScriptAccess: "sameDomain",
//    wmode: "transparent",
//    allowFullScreen: "true"
//};

//swfobject.embedSWF("/swf/hcc801_incorrigablechildren728x90.swf", "banner", "728", "90", "8.0.0", "", flashvars, params);
