(function()
{
    var UA = navigator.userAgent;
    is_gecko = /gecko/i.test(UA);
    is_opera = /opera/i.test(UA);
    is_mac = /mac_powerpc/i.test(UA);
    is_ie = /msie/i.test(UA) && !is_opera && !is_gecko && !is_mac;
    is_ie5 = is_ie && /msie 5\.[^5]/i.test(UA);
    is_nn4 = document.layers ? true : false;
})();

function Community()
{
}

Community.i18n = function(text, arg1, arg2)
{
    if (typeof Community.I18N != "undefined" && typeof Community.I18N[text] != "undefined")
        text = Community.I18N[text];

    if (arg1)
    {
        text = text.replace(/(\(1\))/g, arg1);

        if (arg2)
            text = text.replace(/(\(2\))/g, arg2);
    }

    return text;
};


Community.CE = function(type, parent, props, style)
{
    var el = null,i;

    if (document.createElementNS)
        el = document.createElementNS("http://www.w3.org/1999/xhtml", type);
    else
        el = document.createElement(type);

    if (parent)
        parent.appendChild(el);

    if (props)
        for (i in props)
            el[i] = props[i];

    if (style)
        for (i in style)
            el.style[i] = style[i];

    return el;
};
function Messages()
{
}

var editor = null;

Messages.initEditor=function(textAreaId, lengthCounterId, maxLength, lang, formName)
{
    if (!textAreaId)
        return;
    
    if (typeof(tinymce)=='undefined')
    {
        setTimeout("Messages.initEditor('"+textAreaId+"','"+lengthCounterId+"','"+maxLength+"','"+lang+"','"+formName+"')", 200);
        return false;
    }
    
    try {
		tinyMCE.init({
		    theme : "advanced",
		    mode : "exact",
		    elements: textAreaId,
		    language : lang,
		    plugins : "safari,lengthcounter",
		    lengthCounterElementId : lengthCounterId,
		    lengthCounterMax : maxLength,
		    submitFormName : formName,
			object_resizing : false,
			force_p_newlines : true,
	        convert_fonts_to_spans : false,
	        entities : '160,nbsp',
	        verify_html : true,
            valid_elements : 'strong/b[class|style],-em/-i[class|style],-strike[class|style],-u[class|style],-p[id|style|class|align],br,img[src|border|alt=|title|hspace|vspace|width|height|align],-div[id|class|align|style],-span[style|class|align|color],-pre,-font[face|size|style|id|class|color],-li[class|style],-ol[class|style],-ul[class|style]',
	        invalid_elements : "a",
	        icons : "icons-x",
	        		
			// Theme options
			theme_advanced_buttons1 : "fontselect,fontsizeselect,cut,copy,paste",
			theme_advanced_buttons2 : "bold,italic,underline,strikethrough,|,forecolor,backcolor,|,justifyleft,justifycenter,justifyright,|,undo,redo" ,
			theme_advanced_buttons3 : "",
			theme_advanced_buttons4 : "",
			theme_advanced_toolbar_location : "top",
			theme_advanced_toolbar_align : "left",
			theme_advanced_statusbar_location : "none",
			theme_advanced_resizing : false,
			theme_advanced_fonts : "Arial=arial,helvetica,sans-serif;Courier New=courier new,courier,monospace;Georgia=georgia,times new roman,times,serif;Tahoma=tahoma,arial,helvetica,sans-serif;Times New Roman=times new roman,times,serif;Verdana=verdana,arial,helvetica,sans-serif;Impact=impact,chicago;Wingdings=wingdings,zapf dingbats",
			theme_advanced_font_sizes : "1,2,3,4"
		});
	} catch (e)	{}

	return false;
}


Messages.changeTextArea=function(form,ev,limit)
{
    ev||(ev=window.event);
    if(ev && ev.keyCode == 13 && (ev.altKey || ev.ctrlKey))
    {
      form.submit();
      return false;
    }

    var msg = form.msg;
    if (msg.value.length > limit)
      msg.value = msg.value.substring(0,limit);

    document.getElementById("fm_len").innerHTML = limit - msg.value.length;
    return false;
}

// ----------------------
// SMILES FUNCTIONALITY

Messages.changeSmiles=function(tagName)
{
    var i,t,tags=document.getElementsByTagName(tagName);
    for(i=0;i<tags.length;i++)
    {
        t=tags[i];
        if(t.className=='msg-area')
        {
            t.innerHTML=OneSmile.smilize(t.innerHTML, smilesPath);
        }
    }
}

Messages.insertSmilesToInput=function(input, abreviation, src)
{
    if(tinyMCE.activeEditor)
    {
        tinyMCE.activeEditor.contentWindow.focus();
        var sel = tinyMCE.activeEditor.selection.getSel();
        var range = tinymce._createRange(sel);

        image = tinymce.getParentElement();
        if (!image || !/^img$/i.test(image.tagName))
        {
            tinyMCE.activeEditor.execCommand("mceInsertContent",true,'&nbsp;<img src="' + src + '" alt="' + abreviation + '" onresizestart="return false;" onresizeend="return false;" />');
        }
        else
        {
            image.src = src;
            image.alt = abreviation;
        }

        if (is_ie)
        {
            range.collapse(false);
            range.select();
        }
        else
        {
	        try {
	            var n = sel.focusNode.nextSibling;
	            if (n && n.nodeType != 1 && n.nextSibling)
	                n = n.nextSibling;
	
	            if (n)
	            {
	                sel.removeAllRanges();
	                range = document.createRange();
	                range.selectNode(n.nextSibling);
	                range.collapse(true);
	                sel.addRange(range);
	            }
	        } catch (e) {}
        }

        tinyMCE.execCommand("mceCountMessageLength");
    }
    else
    {
        if(is_ie)
        {
            if(input.isTextEdit)
            {
                input.focus();
                var sel=document.selection;
                var rng=sel.createRange();
                rng.colapse;
                if((sel.type == "Text" || sel.type == "None") && rng != null)
                {
                    rng.text = abreviation;
                }
            }
            else{
                    input.value += abreviation;
            }
        }
        else
        {
            input.value+=abreviation;
        }

        input.focus();
    }
}

Messages.createSmilesArea=function(parent, input)
{
    if (is_mac)
        return;

	var C=Community,tbody,row,i,str,img,smile,src,firstDiv;
	firstDiv=C.CE("div",parent,{},{position:"relative"});
    tbody=C.CE("tbody",C.CE("table",firstDiv));

	for(i=0;i<52;++i)
	{
        if (i==47)
            continue;

        if(i%17==0 && i<51)
            row=C.CE("tr",tbody);

        smile=OneSmile.smiles[i];
		img=C.CE("img",C.CE("td",row),{src:smilesPath+smile[0],title:smile[1]},{cursor:"pointer"});

        img.onclick=function()
        {
            Messages.insertSmilesToInput(input,this.title,this.src);
        };

		img.onmousedown=function()
		{
			return false;
		};
	}
}

function OneSmile()
{
}

OneSmile.smiles=[
    ['04a.gif',':-)',[/(:|=)[-]?\)/gi]],
    ['06a.gif',':-(',[/(:|=)[-]?\(/gi]],
    ['09a.gif',';-)',[/;[-]?\)/gi]],
    ['11a.gif',':-D',[/(:|=)[-]?(D|d)/gi]],
    ['02.gif',':-@',[/(:|=)[-]?@/gi]],
    ['01.gif','^o)',[/\^o\)/gi]],
    ['03a.gif',':-S',[/(:|=)[-]?(S|s)/gi]],
    ['07a.gif','*-)',[/\*-\)/gi]],
    ['08a.gif',':-|',[/[:=][-]?[|I]/gi]],
    ['10a.gif','8oI',[/8o(I|\|)/gi]],
    ['13a.gif',":'(",[/(:|=)(\'|&#39;)\(/gi]],
    ['14a.gif',':-P',[/(:|=)[-]?(P|p)/gi]],
    ['26a.gif',':-$',[/(:|=)[-]?\$/gi]],
    ['27a.gif',':-O',[/(:|=)[-](O|o|0)/gi]],
    ['28a.gif','|-)',[/\|-\)/gi]],
    ['41.gif','(ch)',[/\((C|c)(H|h)\)/gi]],
    ['44a.gif','(lo)',[/\((L|l)(O|o)\)/gi]],
    ['45a.gif','(sr)',[/\((S|s)(R|r)\)/gi]],
    ['46a.gif','|-(',[/\|-\(/gi]],
    ['47a.gif','(H)',[/\((H|h)\)/gi]],
    ['48a.gif','(hu)',[/\((H|h)(U|u)\)/gi]],
    ['49a.gif','(tr)',[/\((T|t)(R|r)\)/gi]],
    ['50a.gif','(md)',[/\((M|m)(D|d)\)/gi]],
    ['51a.gif','(fr)',[/\((F|f)(R|r)\)/gi]],
    ['52a.gif','(dt)',[/\((D|d)(T|t)\)/gi]],
    ['53a.gif','(sc)',[/\((S|s)(C|c)\)/gi]],
    ['12.gif','(Y)',[/\((Y|y)\)/gi]],
    ['15.gif','(N)',[/\((N|n)\)/gi]],
    ['34.gif','(v)',[/\((V|v)\)/gi]],
    ['16.gif','(L)',[/\((L|l)\)/gi]],
    ['17.gif','(U)',[/\((U|u)\)/gi]],
    ['24.gif','(K)',[/\((K|k)\)/gi]],
    ['23.gif','(F)',[/\((F|f)\)/gi]],
    ['32.gif','(*)',[/\(\*\)/gi]],
    ['19.gif','(^)',[/\((\^)\)/gi]],
    ['21.gif','(G)',[/\((G|g)\)/gi]],
    ['25.gif','(B)',[/\((B|b)\)/gi]],
    ['29.gif','(D)',[/\((D|d)\)/gi]],
    ['22.gif','(C)',[/\((C|c)\)/gi]],
    ['30.gif','(pi)',[/\(pi\)/gi]],
    ['33.gif','(pl)',[/\(pl\)/gi]],
    ['36.gif','(ic)',[/\((I|i)(C|c)\)/gi]],
//    ['37.gif','(dc)',[/\((D|d)(C|c)\)/gi]],
    ['baks.gif','($)',[/\(\$\)/gi]],
    ['35.gif','(co)',[/\((C|c)(O|o)\)/gi]],
    ['38.gif','(so)',[/\((S|s)(O|o)\)/gi]],
    ['40.gif','(te)',[/\((T|t)(E|e)\)/gi]],
    ['39.gif','(nt)',[/\((N|n)(T|t)\)/gi]],
    ['18.gif','(I)',[/\((I|i)\)/gi]],
    ['20.gif','(E)',[/\((E|e)\)/gi]],
    ['42.gif','(Z)',[/\((Z|z)\)/gi]],
    ['43.gif','(X)',[/\((X|x)\)/gi]],
    ['31.gif','(S)',[/\((S|s)\)/gi]]
];

OneSmile.smilize=function(str,path)
{
	var i,j,v,regs,tag;

	for(i=this.smiles.length-1;i>=0;--i)
	{
		v=this.smiles[i];
		tag='<img align="middle" src="'+path+v[0]+'" />';

        regs=v[2];
		for(j=regs.length;--j>=0;)
			str=str.replace(regs[j],tag);
	}

    str=str.replace(/&nbsp;<img/gi,'\n<img');

	return str;
};
