
UploadPhotoIndex = 0;
function CreateUploadPhoto( parent, dest )
{
       UploadPhotoIndex++;
        d = document.createElement( "div" );
        d.id = 'UploadPhoto'+UploadPhotoIndex;
        parent.appendChild( d );
        d.innerHTML =
          '<form action="/uploadPhoto/" style="margin:0px;padding:0px;" enctype="multipart/form-data" method="post" target="updest'+UploadPhotoIndex+'">'+
          '<div style="position:relative;">'+
          '<input onchange="if( this.value != \'\' )form.parentNode.start();" name="pic" type="file" style="-moz-opacity:0;filter:alpha(opacity: 0);opacity: 0;z-index: 2;position:relative;margin-left:-150px;"/>'+
          '<div style="position: absolute;top: 0px;left: 0px;cursor:hand;"><a href="#" style="z-index:1;"><img src="/img/smgo.gif" hspace="3" border="0"/>add photo</a></div>'+
          '<input type="hidden" value="'+UploadPhotoIndex+'" name="index"/>'+
          '</form>'+
          '<iframe id="updest'+UploadPhotoIndex+'" name="updest'+UploadPhotoIndex+'" style="display:none;"></iframe>';
        d.start = UploadPhoto_start;
        d.finish = UploadPhoto_finish;
        d.dest = dest;
}

function UploadPhoto_start()
{
		var browser = new BrowserDetectLite();
        this.childNodes[0].submit();
        this.style.display="none";
        this.image = this.dest.createElement("img");
        this.image.setAttribute("src","/img/loading.gif");
        if ((browser.isIE55 || browser.isIE6up) && browser.isWin32)
        {
			this.dest.body.appendChild( this.image );
		}
		else
		{
			insertNodeAtSelection( document.getElementById('edit').contentWindow, this.image );
		}
        CreateUploadPhoto(this.parentNode,this.dest);
}
function UploadPhoto_finish( img )
{
    i = this.dest.createElement( "div" );
    i.innerHTML = img;
    this.image.parentNode.replaceChild( i, this.image );

}
function newImage( index, img )
{
        document.getElementById( 'UploadPhoto'+index ).finish(img);
}

function insertNodeAtSelection(win, insertNode)
{
    // get current selection
    var sel = win.getSelection();

    // get the first range of the selection
    // (there's almost always only one range)
    var range = sel.getRangeAt(0);

    // deselect everything
    sel.removeAllRanges();

    // remove content of current selection from document
    range.deleteContents();

    // get location of current selection
    var container = range.startContainer;
    var pos = range.startOffset;

    // make a new range for the new selection
    range=document.createRange();

    if (container.nodeType==3 && insertNode.nodeType==3) {

    // if we insert text in a textnode, do optimized insertion
    container.insertData(pos, insertNode.nodeValue);

    // put cursor after inserted text
    range.setEnd(container, pos+insertNode.length);
    range.setStart(container, pos+insertNode.length);

    } else {


    var afterNode;
    if (container.nodeType==3) {

        // when inserting into a textnode
        // we create 2 new textnodes
        // and put the insertNode in between

        var textNode = container;
        container = textNode.parentNode;
        var text = textNode.nodeValue;

        // text before the split
        var textBefore = text.substr(0,pos);
        // text after the split
        var textAfter = text.substr(pos);

        var beforeNode = document.createTextNode(textBefore);
        afterNode = document.createTextNode(textAfter);

        // insert the 3 new nodes before the old one
        container.insertBefore(afterNode, textNode);
        container.insertBefore(insertNode, afterNode);
        container.insertBefore(beforeNode, insertNode);

        // remove the old node
        container.removeChild(textNode);

    } else {

        // else simply insert the node
        afterNode = container.childNodes[pos];
        container.insertBefore(insertNode, afterNode);
    }

    range.setEnd(afterNode, 0);
    range.setStart(afterNode, 0);
    }

    sel.addRange(range);
};
