﻿
var Bylectric = {
    isIE:function(){
        return '\v'=='v';
    },
    // a counter used to create unique IDs
    eventGuid:1,
    addEvent: function( element, type, handler ){        
        if(window.addEvent){            
            return addEvent(element,type,handler);
        }
        if ( !element ) element = window;	// fix: sometimes element does not seem to exist, even though one is passed
	    if ( element.addEventListener ) {
		    element.addEventListener( type, handler, false );
	    } else {
		    // assign each event handler a unique ID
		    if ( !handler.$$guid ) handler.$$guid = Bylectric.eventGuid++;
		    // create a hash table of event types for the element
		    if ( !element.events ) element.events = {};
		    // create a hash table of event handlers for each element/event pair
		    var handlers = element.events[ type ];
		    if ( !handlers ) {
			    handlers = element.events[ type ] = {};
			    // store the existing event handler (if there is one)
			    if ( element[ "on" + type ] ) {
				    handlers[ 0 ] = element[ "on" + type ];
			    }
		    }
		    // store the event handler in the hash table
		    handlers[ handler.$$guid ] = handler;
		    // assign a global event handler to do all the work
		    element[ "on" + type ] =Bylectric.handleEvent;
	    }
    },
    handleEvent: function( event ) {
	    var returnValue = true;
	    // grab the event object (IE uses a global event object)
	    event = event ||Bylectric.fixEvent( (( this.ownerDocument || this.document || this ).parentWindow || window ).event );
	    // get a reference to the hash table of event handlers
	    var handlers = this.events[ event.type ];
	    // execute each event handler
	    for ( var i in handlers ) {
		    this.$$handleEvent = handlers[ i ];
		    if ( this.$$handleEvent( event ) === false ) {
			    returnValue = false;
		    }
	    }
	    return returnValue;
    },
    fixEvent: function( event ) {
	    // add W3C standard event methods
	    event.preventDefault = Bylectric.preventDefault;
	    event.stopPropagation = Bylectric.stopPropagation;
	    return event;
    },
    preventDefault: function() {
	    this.returnValue = false;
    },
    stopPropagation: function() {
	    this.cancelBubble = true;
    },
    stopDefault: function( e ) {
	    if ( e && e.preventDefault ) e.preventDefault();	// Prevent default browser action (W3C)
	    else window.event.returnValue = false;					// IE version
	    return false;
    },
    normEvent:function( e ) {
	    e = e || window.event;
	    if ( !e.target ) e.target = e.srcElement;
	    return e;
    },
    proofReading:{
        initialize:function(){
            Bylectric.domReady(function(){
                Bylectric.addEvent(document,"keypress",function(evt){
                    if(evt.ctrlKey&&(evt.keyCode==13||evt.keyCode==10)){
                        Bylectric.proofReading.show();
                    }
                });
            });
        } ,
        callBackStore:{},
        callBackID:0,
        addCallBackFunction:function(f){
            Bylectric.proofReading.callBackStore[Bylectric.proofReading.ID++]=f;
        },
        callBack:function(){
            for(funcID in Bylectric.proofReading.callBackStore){                
                Bylectric.proofReading.callBackStore[funcID]();
                delete Bylectric.proofReading.callBackStore[funcID];
            }
        },
        show:function(){
            
            
            var dlg = document.createElement("div");
            dlg.className="bylectricDialog";
            dlg.style.marginTop="-150px";
            dlg.style.marginLeft="-300px";
            dlg.style.width="600px";
            dlg.style.height="300px";
            
            var frm=document.createElement("iframe");
            frm.className="bylectricDialog";
            frm.style.marginTop="-150px";
            frm.style.marginLeft="-300px";
            frm.style.width="600px";
            frm.style.height="300px";
            //frm.style.border="0";
            frm.frameBorder="0";
            frm.scrolling="no";
            frm.setAttribute("src","/sitecore modules/Proof Reading/dialog.aspx");            
            //dlg.appendChild(frm);
            Bylectric.dialog.show(frm);            
        },
        getSelectedText:function(){
            var selectedText=null;
            if(Bylectric.isIE()){
                selectedText=document.selection.createRange().text;
            }else if(document.getSelection){
                selectedText=document.getSelection();
            }
            return selectedText;
        }   
    },
    domReady:function(f){        
        if ( Bylectric.domReady.done ) return f();								
	    if ( Bylectric.domReady.timer ) {										
		    Bylectric.domReady.ready.push( f );								
	    } else {
		    Bylectric.addEvent( window, "load", Bylectric.isDOMReady );			
		    Bylectric.domReady.ready = [ f ];						
		    Bylectric.domReady.timer = setInterval( Bylectric.isDOMReady, 13 );
	    }
    },
    isDOMReady:function() {		
	    if ( Bylectric.domReady.done ) return false;	
	    if ( document && document.getElementsByTagName && document.getElementById && document.body ) {	
		    clearInterval( Bylectric.domReady.timer );
		    Bylectric.domReady.timer = null;		
		    for ( var i = 0; i < Bylectric.domReady.ready.length; i++ ) Bylectric.domReady.ready[ i ]();
		    Bylectric.domReady.ready = null;
		    Bylectric.domReady.done = true;
	    }
    },
    dialog:{
        initialize:function(){
            Bylectric.domReady(function(){
                var background=document.createElement("div");
                background.id="bylectricBackground";
                Bylectric.dialog.background=background;
            });
        },
        show:function(dialogWindow){
             document.body.appendChild(Bylectric.dialog.background);
             document.body.appendChild(dialogWindow);
             Bylectric.dialog.currentWindow=dialogWindow;
        },
        close:function(){
            document.body.removeChild(Bylectric.dialog.background);
            if(Bylectric.dialog.currentWindow){
                document.body.removeChild(Bylectric.dialog.currentWindow);
            }
        }
    }
};

if(window.parent.Bylectric===Bylectric){
    Bylectric.dialog.initialize();
    Bylectric.proofReading.initialize();
}
