﻿function yuiYouTubeloader(myEditor) {

    myEditor.on('toolbarLoaded', function() {
        var youTubeMovie = {
            showDlg: false,
            dlg: null,
            dialog_config: { width: '300px', height: 'auto', fixedcenter: true, constraintoviewport: true, visible: false, draggable: false, modal: true },
            handleCancel: function() {
                this.cancel();
                youTubeMovie.showDlg = false;
            },
            handleSubmit: function() {
                if (!this.getData().ymovie) return false;
                youTubeMovie.showDlg = false;
                var fl_url = this.getData().ymovie.match(/(youtube).*(v=)([^&]*)/);
                fl_url = fl_url[3];
                fl_url = "http://www.youtube.com/v/" + fl_url;
                var html = '<br><br><div style="margin: auto; text-align: center; background: #eee; width: 425px; height: 350px;"><object width="425" height="350"><param name="movie" value="' + fl_url + '"></param><param name="wmode" value="transparent"></param><embed src="' + fl_url + '" type="application/x-shockwave-flash" wmode="transparent" width="425" height="350"></embed></object></div><br><br>';
                myEditor.execCommand('inserthtml', html);
                this.cancel();
            },
            createDialog: function() {
                var youtubeEle = document.createElement('div');
                youtubeEle.setAttribute('id', 'youTubeDlg');
                youtubeEle.innerHTML = '<div class="hd">Insert YouTube Movie</div><divclass="bd"><form name="youTubeForm" class="text"><br/>YouTube Movie URL: <input type="text" name="ymovie" size="30"/><br /></form></div>';
                YAHOO.util.Dom.setStyle(youtubeEle, 'visibility', 'hidden');
                document.body.appendChild(youtubeEle);
                youTubeMovie.dlg = new YAHOO.widget.Dialog(youtubeEle, youTubeMovie.dialog_config);
                youTubeMovie.dlg.cfg.queueProperty("buttons", [{ text: "Submit", handler: youTubeMovie.handleSubmit, isDefault: true }, { text: "Cancel", handler: youTubeMovie.handleCancel}]);
                youTubeMovie.dlg.render(document.body);
            },
            toggle: function() {
                youTubeMovie.showDlg = !youTubeMovie.showDlg;
                if (youTubeMovie.showDlg) {
                    youTubeMovie.dlg.show();
                } else {
                    youTubeMovie.dlg.hide();
                }
            }
        };

        myEditor.toolbar.addButtonToGroup({ type: 'push', label: 'Insert YouTube movie', value: 'youTube' }, 'insertitem');

        myEditor.toolbar.on('youTubeClick', function() {
            youTubeMovie.toggle();
        });

        youTubeMovie.createDialog();

    });
}


/************Image LIbrary Implementation*************/

function yuiImageLibrary(myEditor) {

    var Dom = YAHOO.util.Dom,
            Event = YAHOO.util.Event,
            win = null;

    myEditor.on('toolbarLoaded', function() {
        //When the toolbar is loaded, add a listener to the insertimage button
        this.toolbar.on('insertimageClick', function() {
            //Get the selected element
            var _sel = this._getSelectedElement();
            //If the selected element is an image, do the normal thing so they can manipulate the image
            if (_sel && _sel.tagName && (_sel.tagName.toLowerCase() == 'img')) {
                //Do the normal thing here..
            } else {
                //They don't have a selected image, open the image browser window
                win = window.open('/Blog/ImageBrowser', 'IMAGE_BROWSER', 'left=20,top=20,width=650,height=600,toolbar=0,resizable=0,status=0');
                if (!win) {
                    //Catch the popup blocker
                    alert('Please disable your popup blocker!!');
                }
                //This is important.. Return false here to not fire the rest of the listeners
                return false;
            }
        }, this, true);
    }, myEditor, true);
    myEditor.on('afterOpenWindow', function() {
        //When the window opens, disable the url of the image so they can't change it
        var url = Dom.get(myEditor.get('id') + '_insertimage_url');
        if (url) {
            url.disabled = true;
        }
    }, myEditor, true);
}



//===========================
