PollEmbedded = newClass(null, {
    /**
     * shows and hides embedded dialog,
     * also fill necessary embedded dialog fields with data.
     */
    constructor: function(options) {
        this.defaultStyleId = 3;
        this.widthMap = new Array('0', '200', '250', '310', '480');
        this.link = options.link;
        $('#poll-html-embedded-link').bind('click', {env: this, styleId: this.defaultStyleId}, this.openEmbeddedDialog);
        $('#poll-embedded-dialog-Xclose-link').bind('click', {env: this}, this.closeEmbeddedDialog);

        for (var i = 1; i < 5; i++) {
            $('#poll-select-theme' + i + '-radio').bind('click', {env: this, styleId: i}, this.themeSelected);
        }
        var head = $('head');
        for (i = 1; i < 5; i++) {
            head.append('<link type="text/css" href="/style/poll-embed/poll-embed' + i + '.css" rel="stylesheet" title="embed' + i + '"/>');
        }

        $("link[rel*=style][title^='embed']").each(function() {
            this.disabled = true;
        });

        $('#poll-select-theme' + 2 + '-radio').click();
        $('#poll-select-theme' + this.defaultStyleId + '-radio').click();

    },

    closeEmbeddedDialog: function() {
        var dialog = $("#poll-embedded-dialog");
        dialog.hide();
        $('#poll-embedded-dialog-draggable').css({left:'',top:''});
    },

    openEmbeddedDialog: function(e) {
        var env = e.data.env;
        $('#poll-select-theme' + env.defaultStyleId + '-radio').click();
        var dialog = $("#poll-embedded-dialog");

        //clean up selection
        $('#poll-select-theme' + e.data.styleId + '-radio').attr("checked", "checked");
        showDialog(dialog, true);
        if (env.minDialogWidth == undefined) {
            env.minDialogWidth = $("#poll-embedded-dialog-frame").width();
        }
    },
    themeSelected: function(e) {    	
        var styleId = e.data.styleId;
        var env = e.data.env;
        env.link = env.link.replace(/\/\d\//, "/" + styleId + "/");
        env.reloadPreview(e);
    },

    updateCss:function(styleId) {
        $("link[rel*=style][title^='embed']").each(function() {
            this.disabled = this.getAttribute('title').indexOf(styleId) < 0;
        });
    },


    update:function(e, data) {
        var env = e.data.env;
        var styleId = e.data.styleId;
        var wrapper = $("#embedded-poll-preview-content");

        env.updateCss(styleId);
        var re = new RegExp(/<link.*>/);
        data = data.replace(re, '');
        if (styleId >= 4) {
            env.updateDialogWidth( styleId);
        }
        wrapper.html(data);
        wrapper.ready(function() {
            var link = $('#poll-embedded-text').val();
            var height = $("#embedded-poll-preview-content .poll-embed-styles").outerHeight(true);
            var width = env.widthMap[parseInt(styleId)];
            link = link.replace(/\/\d\//, "/" + styleId + "/");
            link = link.replace(/width=\"\d{3}px/, 'width="' + width + 'px');
            link = link.replace(/height=\"\d.*px/, 'height="' + height + 'px');
            if (styleId < 4) {
                env.updateDialogWidth(styleId);
            }
            $('#poll-embedded-text').val(link);
            $('#poll-embeded-wrap').attr("class", "embed" + styleId);
        });
    },

    updateDialogWidth:function(styleId){
        var newDialogWidth = parseInt(this.widthMap[parseInt(styleId)]) + 17;
        if (this.minDialogWidth != undefined) {
            if (newDialogWidth > this.minDialogWidth) {
                $("#poll-embedded-dialog-frame").width(newDialogWidth);
            } else {
                $("#poll-embedded-dialog-frame").width(this.minDialogWidth);
            }
        }
        var dialog = $("#poll-embedded-dialog");        
        updateFrameSize(dialog.find(".dialog"), dialog.find(".banner-fix:first"));
    },

    reloadPreview:function(e) {
        var env = e.data.env;  
        var styleId = e.data.styleId;
        $.ajax({
            type: "GET",
            url: env.link,
            success: function(data) {
        		e.data = new Object();
        		e.data.env = env;
        		e.data.styleId = styleId;        	
                env.update(e, data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                var dialog = $("#poll-embedded-dialog");
                if (dialog.is(':visible') == true) {
                    var response = typeof XMLHttpRequest.responseText == 'string' ? XMLHttpRequest.responseText.substring(0, 150) : "";
                    showMessageParam(null, textStatus + "<br/>" + response, true);
                }
            }
        });
    }
});

