﻿
//taken from : http://bindzus.wordpress.com/2007/12/24/adding-dynamic-contents-to-iframes/

function IFrame(parentElement) {
    // Create the iframe which will be returned
    var iframe = document.createElement("iframe");

    // If no parent element is specified then use body as the parent element
    if (parentElement == null)
        parentElement = document.body;

    // This is necessary in order to initialize the document inside the iframe
    parentElement.appendChild(iframe);

    // Initiate the iframe's document to null
    iframe.doc = null;

    // Depending on browser platform get the iframe's document, this is only
    // available if the iframe has already been appended to an element which
    // has been added to the document
    if (iframe.contentDocument)
    // Firefox, Opera
        iframe.doc = iframe.contentDocument;
    else if (iframe.contentWindow)
    // Internet Explorer
        iframe.doc = iframe.contentWindow.document;
    else if (iframe.document)
    // Others?
        iframe.doc = iframe.document;

    // If we did not succeed in finding the document then throw an exception
    if (iframe.doc == null)
        throw "Document not found, append the parent element to the DOM before creating the IFrame";

    // Create the script inside the iframe's document which will call the
    iframe.doc.open();
    iframe.doc.close();

    // Return the iframe, now with an extra property iframe.doc containing the
    // iframe's document
    return iframe;
}

function mcat_setIframe() {
    var iframe = document.getElementById("mcatIframe");
   // iframe.document.write("<div style=color:red;>test test test</div>");
    
}

function my_getSelection() {
    if (document.getSelection) {
        var str = document.getSelection();
    } else if (document.selection && document.selection.createRange) {
        var range = document.selection.createRange();
        var str = range.text;
    } else {
        var str = "Sorry, this is not possible with your browser.";
    }
    document.getElementById("outputDiv").innerHTML = str;
}
function getHTMLSelection() {
    var userSelection;
    var str;
    if (window.getSelection) {
        // W3C Ranges
        userSelection = window.getSelection();
        // Get the range:
        if (userSelection.getRangeAt)
            var range = userSelection.getRangeAt(0);
        else {
            var range = document.createRange();
            range.setStart(userSelection.anchorNode, userSelection.anchorOffset);
            range.setEnd(userSelection.focusNode, userSelection.focusOffset);
        }
        // And the HTML:
        var clonedSelection = range.cloneContents();
        range.deleteContents();
        var div = document.createElement('div');
        div.appendChild(clonedSelection);
        str =  div.innerHTML;
    } else if (document.selection) {
        // Explorer selection, return the HTML
    userSelection = document.selection.createRange();


    newRange = userSelection.duplicate();
    newRange.setEndPoint("StartToEnd", userSelection);
    newRange.pasteHTML("<span style='background-color:green;'>this is the pasted text</span>"); 
    
    
        str =  userSelection.htmlText;
    } else {
        str =  '';
    }
    document.getElementById("outputDiv").innerHTML = str;
}
function bordercontainer() {
    var node;
    if (window.getSelection) {
        // W3C Ranges
        userSelection = window.getSelection();
        node = userSelection.anchorNode
    } else {
        if (document.selection) {
            // Explorer selection, return the HTML
            node = document.selection.createRange().parentElement();
        }
    }
    if (node == null) {
        alert("node is null");
    } else {
        while (node.nodeType != 1) {
            node = node.parentNode;
        }
        node.className = "highlighted"
    }

}
/*****************************************************************************************************/
/*****************************************************************************************************/

var HIGHLIGHTED_CLASSNAME = "highlighted";

var highlightObj = (document.selection) ? new IESelectionHighlight() : new FFSelectionHighlight();
function highlight() {
    highlightObj.highlightSelection();
}
function unHighlight() {
    highlightObj.unHighlightSelection();
}

// IE implementation:
function IESelectionHighlight(){
    this.highlightSelection = function() {
        if (document.selection) {
            userSelection = document.selection.createRange();
            newRange = userSelection.duplicate();
            //newRange.setEndPoint("StartToEnd", userSelection);
            //newRange.pasteHTML("<span class='" + HILIGHTED_CLASSNAME + "'>" + document.selection.createRange().htmlText + "</span>");
            var element = document.createElement("span");
            element.innerHTML = "<span class='" + HIGHLIGHTED_CLASSNAME + "'>" + document.selection.createRange().htmlText + "</span>"
            forEachTag(element, removeStyleBG, true);
            newRange.pasteHTML(element.innerHTML);
        } else {
            throw ("document.selection not supported.(Is this IE browser?");
        }
    }
    this.unHighlightSelection = function() {
        if (document.selection) {
            userSelection = document.selection.createRange();
            newRange = userSelection.duplicate();
            var container = document.createElement("span");
            container.innerHTML = document.selection.createRange().htmlText;
            newRange.pasteHTML(removeHighlightedClass(container));
        } else {
            throw ("document.selection not supported.(Is this IE browser?");
        }
    }
}

//Mozzila implementation:
function FFSelectionHighlight(){
    this.highlightSelection = function() {
        // W3C Ranges
        userSelection = window.getSelection();
        // Get the range:
        if (userSelection.getRangeAt)
            var range = userSelection.getRangeAt(0);
        else {
            var range = document.createRange();
            range.setStart(userSelection.anchorNode, userSelection.anchorOffset);
            range.setEnd(userSelection.focusNode, userSelection.focusOffset);
        }
        // And the HTML:
        var clonedSelection = range.cloneContents();
        range.deleteContents();
        var span = document.createElement('span');
        span.setAttribute("class", HIGHLIGHTED_CLASSNAME);
        span.appendChild(clonedSelection);
        range.insertNode(span);
        forEachTag(span, removeStyleBG, true);

    }
    this.unHighlightSelection = function() {
        // W3C Ranges
        userSelection = window.getSelection();
        // Get the range:
        if (userSelection.getRangeAt)
            var range = userSelection.getRangeAt(0);
        else {
            var range = document.createRange();
            range.setStart(userSelection.anchorNode, userSelection.anchorOffset);
            range.setEnd(userSelection.focusNode, userSelection.focusOffset);
        }
        // And the HTML:
        var clonedSelection = range.cloneContents();
        range.deleteContents();
        var span = document.createElement('span');
        span.appendChild(clonedSelection);
        removeHighlightedClass(span)
        range.insertNode(span);
    }
}


function removeHighlightedClass(containerNode) {
    var spans = containerNode.getElementsByTagName("span")
    for (var i = 0; i < spans.length; i++) {
        if (spans[i].className == HIGHLIGHTED_CLASSNAME) {
            spans[i].className = "";
            continue;
        }
        spans[i].style.backgroundColor = "";
        
    }
    containerNode.style.backgroundColor = "white"; 
    return containerNode.outerHTML;
}

//Find the first highlighted element containing the given node.
function getHighlightedContainer(childNode) { 
    var tmpNode = childNode;
    while (tmpNode.tagName != "body") {
        tmpNode = tmpNode.parentNode;
        if (tmpNode.className == HIGHLIGHTED_CLASSNAME)
            return tmpNode;
    }
    return null;
}



//apply a functor for each element under the given node.
function forEachTag(parentNode,functor, bool_recurse) {
    for (var i = 0; i < parentNode.childNodes.length; i++) {
        var node = parentNode.childNodes[i];
        if (node.nodeType == 1) {
            functor(parentNode.childNodes[i]);
            if (bool_recurse)
                forEachTag(node, functor, bool_recurse);
        }
        
    }
}

/*************************** FUNCTORS **************************/
function removeStyleBG(element) {
    element.style.backgroundColor = "";
}
/********************** End of FUNCTORS ************************/



