Type.registerNamespace('DeveloperFusion.Library.Controls');

DeveloperFusion.Library.Controls.SourceView = function(element)
{
    DeveloperFusion.Library.Controls.SourceView.initializeBase(this, [element]);
}
DeveloperFusion.Library.Controls.SourceView.prototype =
{
    initialize : function()
    {
        //this._currentItem = null;
        //this._language = 'csharp';
        this._currentPath = "";
        DeveloperFusion.Library.Controls.SourceView.callBaseMethod(this, 'initialize');
        //dp.SyntaxHighlighter.ClipboardSwf = '/assets/highlighter/clipboard.swf';
        //alert(this.get_element().id);
      //  dp.SyntaxHighlighter.HighlightAll(this.get_element().id);d
        this._createTools();
    },
    dispose : function()
    {
        $clearHandlers(this.get_element());
        // call base dispose
        DeveloperFusion.Library.Controls.SourceView.callBaseMethod(this, 'dispose');
    },
    refresh : function() {
        $(this.get_element()).setText('Loading...');
        DeveloperFusion.Web.Labs.Services.SourceViewer.GetCode(this._currentItem,this._language,Function.createDelegate( this, this._updateDisplayCallback ), Function.createDelegate( this, this._updateDisplayFailed ));
    },
    _createTools : function() {
        var div = new Element('div');
	    div.className = 'tools';
	    var viewSrc = new Element('a');
	    viewSrc.setText('view source');
	    $addHandler(viewSrc,Function.createDelegate(viewSource));
	    
	    viewSrc.injectInside(div);
//	    div.injectInside(viewSrc);
	    
//	    $addHandler(viewSrc,Function.createDelegate(viewSource));
	    
    	
	    div.injectBefore(this.get_element());
    },
    _updateDisplayCallback : function(result) {
        $(this.get_element()).innerHTML = result[2];
        $('source-breadcrumb').innerHTML = result[0];
        document.title = result[1];
        this._currentPath = result[1];
    },
    _updateDisplayFailed : function(error) {
        $(this.get_element()).setText(error.get_message());
    },
    set_language : function(value) {
        if (this._language !== value) {
            this._language = value;
            this.raisePropertyChanged('language');
        }
    },
    get_language : function() {
        return this._language;
    },
    set_currentItem : function(value) {
        if (this._currentItem !== value) {
            this._currentItem = value;
            this.raisePropertyChanged('currentItem');
        }
    },
    get_currentItem : function() {
        return this._currentItem;
    },
    
    viewSource : function() {
		var code = $(this.get_element()).getText();
		var wnd = window.open('', '_blank', 'width=750, height=400, location=0, resizable=1, menubar=0, scrollbars=0');
		wnd.document.write('<textarea style="width:99%;height:99%">' + code + '</textarea>');
		wnd.document.close();
	},
	
	// Copies the original source code in to the clipboard. Uses either IE only method or Flash object if ClipboardSwf is set
	copyToClipboard: {
		var code = $(this.get_element()).getText();
			
		if(window.clipboardData)
		{
			window.clipboardData.setData('text', code);
		}
		else
		{
			var flashcopier = this.flashCopier;
			
			if(flashcopier == null)
			{
				flashcopier = document.createElement('div');
				this.flashCopier = flashcopier;
				this.get_element().appendChild(flashcopier);
			}
			alert(code);
			code = code.replace("\n","%0A");
			
			flashcopier.innerHTML = '<embed src="/SourceViewer/assets/clipboard.swf" FlashVars="clipboard='+encodeURIComponent(code)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		}
		
		alert('The code is in your clipboard now');
	}
    
}


DeveloperFusion.Library.Controls.SourceView.registerClass(
    'DeveloperFusion.Library.Controls.SourceView', Sys.UI.Control);

if (typeof(Sys) !== 'undefined') Sys.Application.notifyScriptLoaded();