$(function() {
    $('table.rte iframe').each(function() {
        var i = this;
        i.modified = 0;
		i.limg = '';
        i.clean = function(){
            var s = d.body.innerHTML.replace( new RegExp( '<p \\/>', 'gi' ), '<p>&nbsp;</p>' );
            s = s.replace( new RegExp( '<p>\\s*<\\/p>', 'gi' ), '<p>&nbsp;</p>' );
            s = s.replace( new RegExp( '<br>\\s*<\\/br>', 'gi' ), '<br />' );
            s = s.replace( new RegExp( '<(h[1-6]|p|div|address|pre|form|table|li|ol|ul|td|b|font|em|strong|i|strike|u|span|a|ul|ol|li|blockquote)([a-z]*)([^\\\\|>]*)\\/>', 'gi' ), '<$1$2$3></$1$2>' );
            s = s.replace( new RegExp( '\\s+></','gi' ), '></' );
            s = s.replace( new RegExp( '<(img|br|hr)([^>]*)><\\/(img|br|hr)>', 'gi' ), '<$1$2 />' );
            if ( $.browser.msie ) {
                s = s.replace( new RegExp( '<p><hr \\/><\\/p>', 'gi' ), "<hr>" );
                s = s.replace( /<!(\s*)\/>/g, '');
            };
            d.body.innerHTML = s;
        };
	 		i.getDoc = function(){
            return d.body.innerHTML;
        };
        i.setDoc = function( x ) {
            d.body.innerHTML=x;
        };
        i.kb = function( e ){
            if ( e.ctrlKey ){
                var k = String.fromCharCode( e.charCode ).toLowerCase();
                var c = '';
                if ( k==ctrlb )
                    c = 'bold';
                else if ( k == ctrli )
                    c = 'italic';
                else if ( k == ctrlu )
                    c = 'underline';
                if ( c ) {
                    f.focus();
                    d.execCommand( c, false, null );
                    f.focus();
                    e.preventDefault();
                    e.stopPropagation();
                }
            }
        };
        i.hidepanels = function() {
            $($(i).parents('table')[0]).find( '.panel' ).hide();
        };

        var f = $.browser.msie ? frames[i.id] : i.contentWindow;
        var d = $.browser.msie ? f.document : i.contentDocument;
        
		d.designMode = 'on';
        try {
            d.execCommand( 'useCSS', false, true );
        } catch ( e ) {};
        d.open();
        d.write( $('#'+i.id+'-ta').text() );
        d.close();
        if ( !$.browser.msie )
            d.addEventListener( 'keypress', i.kb, true );
        else
            d.onmouseup = function(){
                if ( d.selection != null )
                    i.rng = d.selection.createRange();
            };
        $(d).keypress(function() {
            i.modified = 1;
        });
		
		$('head',d).append('<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><link rel="stylesheet" href="http://goodsmir.com.ua/templates/style1.css" style="text/css" />');
		
		$("#"+i.id+"-cmd .cmd").each(function(){
            var t = $(this);
            var a = t.attr( 'args' );
            var s = $('#'+i.id+'-'+a);
            if ( $.browser.msie )
                $('img',t).mouseover(function() {
                    this.className = 'hvr';
                }).mouseout(function() {
                    this.className='';
                });
            
			if ( typeof( a ) == 'string' ) {
                if ( a == 'emoticons' )
                    $(i).parents('table').find('.emot').each(function() {
                        
						$(this).click(function(){
                            f.focus();
							if (i.limg != $(this).children()[0].src)
							d.execCommand( 'insertimage', false, $(this).children()[0].src );
                            i.limg = $(this).children()[0].src;
							f.focus();
                            s.hide();
							$(".ediv").attr("display","none");
                            return false
                        });
                        if ( $.browser.msie )
                            $('img', $(this)).mouseover(function() {
                                this.className='hvr';
                            }).mouseout(function(){
                                this.className='';
                            });
                    });
                    $('#'+a+'-ok').click(function() {
                        f.focus();
						if ( $('#'+a).val() == '' || !d.queryCommandEnabled( t.attr('cmd') ) ) {
                            $('#'+a).val('');
                            s.hide();
							$(".ediv").attr("display","none");
                            return;
                        };
                        if ( t.attr('cmd') == 'insertimage' ) {
                            $.ajaxFileUpload({
                                url: t.attr('path'),
                                secureuri: false,
                                fileElementId: a,
                                dataType: 'json',
                                success: function(data,s) {
                                    if ( data.error )
                                        alert( data.error );
                                    else
                                        d.execCommand( t.attr('cmd'), false, t.attr('filespath')+'/'+encodeURIComponent(data.file));
                                },
                                error: function(data,s,e) {
                                    alert( e + ': ' + data.responseText );
                                }
                            });
                        }
                        else {
                            if ( $.browser.msie )
                                i.rng.select();
                            if ( a == 'html' )
                                i.rng.pasteHTML( $('#'+a).val() );
                            else
                                d.execCommand( t.attr('cmd'), false, $('#'+a).val() );
                        };
                        f.focus();
                        $('#'+a).val('');
                        s.hide();;
						$(".ediv").attr("display","none");
                    });
            };
            t.click(function() {
                i.hidepanels();
				if ( typeof(a) == 'string' ) {
                    s.show();
					if ( a == 'color' )
                        $(i).attr( 'colorcmd', t.attr('cmd') );
                    else if ( a == 'clean' )
                        i.clean();
                    else if ( t.attr( 'args') == 'text' ) {
                        t.attr( 'args', 'wysiwyg' );
                        if ( $.browser.msie ) {
                            d.body.innerText = d.body.innerHTML;
                        } else {
                            var src = d.createTextNode( d.body.innerHTML );
                            d.body.innerHTML = "";
                            d.body.appendChild(src);
                        }
                    } else if ( t.attr( 'args') == 'wysiwyg' ) {
                        t.attr( 'args', 'text' );
                        if ( $.browser.msie ) {
                            var o = escape( d.body.innerText );
                            o = o.replace( "%3CP%3E%0D%0A%3CHR%3E", "%3CHR%3E" );
                            o = o.replace( "%3CHR%3E%0D%0A%3C/P%3E", "%3CHR%3E" );
                            d.body.innerHTML = unescape( o );
                        } else {
                            var src = d.body.ownerDocument.createRange();
                            src.selectNodeContents( d.body );
                            d.body.innerHTML = src.toString();
                        }
                    }
                }
                else {
                    d.execCommand( t.attr('cmd'), false, '');
                    f.focus();
                }
                i.modified = 1;
                return false;
            })
        });
        $("#"+i.id+"-cmd select").each(function() {
            var s = $(this);
            s.change(function() {
                if ( this.selectedIndex > 0 ) {
                    d.execCommand( s.attr('cmd'), false, this.value );
                    i.modified = 1;
                }
                this.selectedIndex = 0;
                f.focus();
            });
        });
        $(i).parents('form:first').submit(function() {
            $('#'+i.id+'-ta').text( d.body.innerHTML );
        });
    });
});