/*
* Skript dinami4eski formiruet menu smajlikov
*
* @author Mike
*
* Created on 11.11.2005
******
* $Id: smiles.js 37612 2009-03-12 11:42:32Z lev $
* $URL: http://svn.sk/main/person/branches/0.18.x/person.com/public_static/js/smiles.js $
*/

var lang_sm_miscellaneous = "Miscellaneous";
var lang_sm_crying = "Crying";
var lang_sm_fighting = "Fighting";
var lang_sm_love = "Love";
var lang_sm_shock = "Shock";
var lang_sm_anger = "Anger";
var lang_sm_thoughts = "Thoughts";

var sm_t_close = null
var sm_old_id = null
var base_smiles;
var smiles;
var sm_dir = 'smiles/';
var smile_pattern1 = '<img src="' + _CONF.host.web.image_dir + sm_dir;
var smile_pattern2 = '.gif" />';
var sm_body
var smiledialog
var smile_menu
var main_smile_menu, smile_content, smile_menu_content;
var base_smile_params = "width=15 height=15";
var cached_smiles = Array();

function sm_Init_main_smiles()
{
	base_smiles = ['',['el','approve','biggrin','cool','lol','net','lox','rofl','rotate','bue3','smirk','tongue','winka','winkp','winky','sm12','sm13','sm14','sm15','sm16','sm0','sm1','sm4','sm6','sm7','sm2','sm10','sm11','sm8','sm9'], 10 ]
	smiles = [
				 [ 'miscellaneous/',[ 'bis','tomato','agree','alc','all_coholic','art','ass','baby','banka','beer','birthday','blbl','bow','bsod','bubble','burnout','bye2','byeforev','cold','dinner','do_not','dunce','frosty','grob','hacker','helpred','inc','lamer','ne_fart','party','patty','pomidor','shuffle','shy','sleep','sleepme','sleepy','smash','smoke','smoke_1','stid','tasty','telek','type','walkman','war','wavey','wc','xmas' ], 5, lang_sm_miscellaneous],
				 [ 'crying/',[ 'bawling','cry','crying','emb','heul','sad','traurig','weep' ], 4, lang_sm_crying],
				 [ 'fighting/',[ 'box','sword','duel','draka','gun','isabre','plusomet','swduel' ], 3, lang_sm_fighting],
				 [ 'love/',[ 'ura','deti','flower','girl','galant','fslap','girl2bant','girlbant','gkiss','rev','heartbt','inlove','horned','loveu','klips','love','mar','chmok','lovely','kiss','lu','love2','qgirl','heart','roga','two','sex','lovehb','hug','rose3','rose2','rose1' ], 4, lang_sm_love ],
				 [ 'shock/',[ 'amazed','bored','boyazn','chih','confused','crazy','eek','eekd','fie','puke','insane','looney','no','grazy','pukeface','redface','repa','shok','shudder' ], 5, lang_sm_shock ],
				 [ 'anger/',[ 'fuck','kaioken','devil','evil','beast','bur','splat','madbla','madno','redhot','roof','ssora','mad','smile_evil' ], 5, lang_sm_anger],
				 [ 'thoughts/',[ 'poet','genius','glasses','goodbad','idea','kos','smart','ponder','chainik' ], 5, lang_sm_thoughts]
			 ];

	var tmp = "";
    smiles = [].concat(smiles);
    smiles.each(function(smile, k)
    {
        tmp += '<tr><td id="' + k + '"  onmouseover="sm_menu_ShowMenu(' + k + ', false);" nowrap height="15"><img src="' + _CONF.host.web.image_dir + 'html_editor/sm_menu_arr.gif" align="right" hspace="0">' + smile[3] + '</td></tr>';
    });
	main_smile_menu = '<table id="tableSdialog" border=0 cellspacing=0 cellpadding=0 height="100%"><tr><td><table border=0 cellspacing=2 cellpadding=0 class=sm_menu width="100%">' + tmp + '</table></td></tr><tr><td><img src="' + _CONF.host.web.image_dir + 'spacer.gif" height="1"></td></tr><tr onmouseover="sm_listner_off()" onmouseout="sm_listner_on()"><td><table border=0 cellspacing=0 cellpadding=0 class=sm_smiles width="100%"><tr><td rowspan="3" style="padding: 2px" width="5"><img src="' + _CONF.host.web.image_dir + 'html_editor/input_buttons_delimiter.gif" width="5" height="49"></td>%smile_content%</tr></table></td></tr></table>';
	smile_content = '<td id="smile" name="smile" class="sm_normal_smile" align="center" valign="middle"><img src="' + _CONF.host.web.image_dir + '%dir%%src%%name%.gif" %params%></td>';
	smile_menu_content = '<table onmouseover="sm_menu_ShowMenu(\'%id%\', true);" id="tableSmenu" border=0 cellspacing=0 cellpadding=0 class=sm_smiles style="border-top-width:4px;"><tr>%smile_content%</tr></table>';

	sm_body = document.getElementsByTagName("body")[0]
	smiledialog = document.getElementById('smile_dialog')
	smile_menu = document.getElementById('smilemenu')
	var max_in_row = base_smiles[2]
	var smilesarray	= base_smiles
	var tmpl = smile_content
	var content = sm_fill(max_in_row, smilesarray, tmpl, true)
	content = main_smile_menu.replace(/%smile_content%/g, content)
	smiledialog.innerHTML = content

	tsd = document.getElementById('tableSdialog')
}

function sm_Init()
{
	var kids = document.getElementsByName('smile')

	for (var i=0; i < kids.length; i++)	{
		kids[i].onmouseover = sm_menu_smouseover
		kids[i].onmouseout = sm_menu_smouseout
		kids[i].onmousedown = sm_InsertSmile
	}
}

function sm_fill(max_in_row, smilesarray, tmpl, _base_smiles)
{
	var content = ''
	var i = 0
	var counter = 1

	while (true) {
		if (counter > max_in_row) {
			if (smilesarray[1][i] == null) {
				break
			}
			counter = 1
			content += '<tr>'
		}
		if (smilesarray[1][i] != null) {
        	content += 	smile_content
        		.replace(/%dir%/g, sm_dir)
        		.replace(/%src%/g, smilesarray[0])
        		.replace(/%name%/g, smilesarray[1][i])
        		.replace(/%params%/g, _base_smiles ? base_smile_params : "")
        }
		counter++
		i++
	}
	return content
}

function sm_menu_ShowMenu(id, showSmileMenu)
{
	sm_listner_off()

	if (sm_t_close != null) {
		clearTimeout(sm_t_close)
		sm_menu_CloseMenu()
	}

	if (sm_old_id != null) {
		document.getElementById(sm_old_id).className = "sm_normal_menu"
	}

	document.getElementById(id).className = "sm_sel_menu"

	if (!showSmileMenu) {
		sm_Init_submenu(id)
	}

	smile_menu.style.display = 'block'
	sm_old_id = id
}

function sm_menu_StartHide()
{
	sm_listner_on()
	sm_t_close = setTimeout('sm_menu_CloseMenu()', 500)
}

function sm_menu_CloseMenu()
{
	if (sm_old_id != null) {
		document.getElementById(sm_old_id).className = "sm_normal_menu"
	}
}

function sm_Init_submenu(id)
{
	if (id == 0) {
		smile_menu.style.left = parseInt(smiledialog.style.left) + parseInt(tsd.offsetWidth) - 77 + 'px'
		smile_menu.style.top = parseInt(smiledialog.style.top) + (id)*17 - 80 + 'px'
	} else {
		smile_menu.style.left = parseInt(smiledialog.style.left) + parseInt(tsd.offsetWidth) - 9 + 'px'
		smile_menu.style.top = parseInt(smiledialog.style.top) + (id)*17 + 1 + 'px'
	}

	var max_in_row = smiles[id][2]
	var smilesarray	= smiles[id]
	var tmpl = smile_content
	if(cached_smiles[id] == null){
		var content = sm_fill(max_in_row, smilesarray, tmpl)
		cached_smiles[id] = smile_menu_content.replace(/%smile_content%/g, content).replace(/%id%/g, id)
	}
	smile_menu.innerHTML = cached_smiles[id];
	sm_Init()
}

function sm_menu_smouseover(e)
{
	this.className = "sm_sel_smile"
}

function sm_menu_smouseout(e)
{
	this.className = "sm_normal_smile"
}

function sm_InsertSmile(e)
{
    if (isMozilla)
    {
        mainedit.document.execCommand('InsertImage', false, this.firstChild.src)
    }
    else if (browser_type() == 'opera')
    {
        var smile = mainedit.document.createElement('img');
        smile.src = this.firstChild.src;
        insertNodeAtSelection(smile);
    }
    else
    {
		InsertHTML("<img src=" + this.firstChild.src + ">")
	}
	sm_menu_hide()
	this.className = "sm_normal_smile"
	sm_menu_CloseMenu()
	checkInputTextLength();
	mainedit.focus()
}

function InsertHTML(newhtml){
	mainedit.focus()
	if (!isMozilla) {
		var range = mainedit.document.selection.createRange()
		range.pasteHTML(newhtml)
	}
}

function sm_menu_hide()
{
	sm_listner_off()
	smiledialog.style.display = 'none'
	smile_menu.style.display = 'none'
}

function sm_listner_on()
{
	sm_body.old_onmousedown = sm_body.onmousedown
	sm_body.old_onclick = sm_body.onclick

	if (isMozilla) {
		sm_body.addEventListener("mousedown", sm_onmousedown, true)
		sm_body.addEventListener("click", sm_onclick, true)
	} else {
		sm_body.onmousedown = sm_onmousedown
		sm_body.onclick = sm_onclick
	}
}

function sm_listner_off()
{
	if (isMozilla) { 
		try {
			sm_body.removeEventListener("mousedown", sm_onmousedown, true)
			sm_body.removeEventListener("click", sm_onclick, true)

			sm_body.addEventListener("mousedown", sm_body.old_onmousedown, true)
			sm_body.addEventListener("click", sm_body.old_onclick, true)
		} catch(e) {
		}
	} else {
		sm_body.onmousedown = sm_body.old_onmousedown
		sm_body.onclick = sm_body.old_onclick
	}
}

function sm_onmousedown()
{
	sm_menu_hide()
}

function sm_onclick()
{
}

function replace_smiles(s)
{
    if (!s) return '';

    var pos;
    var reg, pattern;
    for (var v in smiles_txt)
    {
        pos = 0;
        pattern = '((?:^|>)?(?:[^<]*?))' + unescape(v);
        reg = new RegExp(pattern, 'g');
        s = s.replace(reg, '$1' + smile_pattern1 + smiles_txt[v] + smile_pattern2);
    }
    return s;
}

var smiles_txt = {
	":o" 	   : "sm0",
	":\\(\\("  : "sm1",
	":\\(" 	   : "sm4",
	":\\-\\("  : "sm4",
	"8\\|" 	   : "sm6",
	":\\?" 	   : "sm7",
	":D" 	   : "sm2",
	":\\)\\)"  : "lol",
	":\\)" 	   : "sm2",
	":P" 	   : "sm10",
	":p" 	   : "sm10",
	";\\(" 	   : "sm11",
	";\\-\\("  : "sm11",
    ':\\|'     : 'sm11',
	"8\\)" 	   : "sm8",
	":x" 	   : "sm9",
	"=\\)"	   : "sm2",
	";\\)"	   : "winka",
	";\\-\\)"  : "winka",
    //zachet
    "%3A%u0437%u0430%u0447%u0451%u0442%3A": "smile_zachet"
}

function CSmilesPackage(){}
Object.extend(CSmilesPackage, {
    tabs_html: ['', '', '', '', '', '', '', ''],

    smiles: [
        {name: _LANG.general.extra_smiles_smiley, folder: '', codes: ['lol', 'approve', 'loly', 'cool', 'rotate', 'lox', 'biggrin', 'hardlaugh', 'el', 'finger', 'smirk', 'rofl', 'tongue', 'winka', 'winkp', 'winky']},
        {name: _LANG.general.extra_smiles_love, folder: 'love', codes: ['girl', 'girl2bant', 'klips', 'love2', 'heart', 'roga', 'lovely', 'kiss', 'two', 'inlove', 'lovehb', 'love', 'chmok', 'flower', 'galant', 'gkiss', 'fslap', 'horned', 'loveu', 'lu', 'mar', 'sex', 'ura', 'rev', 'qgirl', 'deti', 'heartbt', 'girlbant', 'hug', 'rose3', 'rose2', 'rose1']},
        {name: _LANG.general.extra_smiles_thoughts, folder: 'thoughts', codes: ['idea', 'genius', 'tomato', 'glasses', 'goodbad', 'ponder', 'poet', 'kos', 'chainik', 'bis', 'smart']},
        {name: _LANG.general.extra_smiles_shock, folder: 'shock', codes: ['repa', 'confused', 'shok', 'redface', 'eek', 'bored', 'pukeface', 'boyazn', 'fie', 'looney', 'chih', 'whoa', 'crazy', 'no', 'puke', 'shudder', 'insane', 'grazy', 'eekd', 'amazed']},
        {name: _LANG.general.extra_smiles_anger, folder: 'anger', codes: ['devil', 'ssora', 'mad', 'smile_evil', 'evil', 'splat', 'kaioken', 'madbla', 'madno', 'redhot', 'beast', 'bur', 'roof', 'fuck']},
        {name: _LANG.general.extra_smiles_fight, folder: 'fighting', codes: ['sword', 'duel', 'couto', 'box', 'hang', 'gun', 'dark2', 'stab', 'plusomet', 'ink_stab', 'isabre', 'samurai', 'itschy', 'swduel', 'draka']},
        {name: _LANG.general.extra_smiles_cry, folder: 'crying', codes: ['cry', 'heul', 'sad', 'emb', 'bawling', 'crying', 'traurig', 'weep']},
        {name: _LANG.general.extra_smiles_misc, folder: 'miscellaneous', codes: ['bye', 'bye2', 'all_coholic', 'walkman', 'party', 'type', 'shuffle', 'shy', 'sleep', 'grob', 'sleepy', 'dinner', 'smoke', 'dunce', 'patty', 'cold', 'smash', 'wavey', 'net', 'tasty', 'smoke_1', 'birthday', 'smoker', 'frosty', 'xmas', 'burnout', 'baby', 'war', 'art', 'bow', 'bubble', 'blackeye', 'beer', 'byeforev', 'bue3', 'stid', 'pomidor', 'alc', 'ne_fart', 'agree', 'do_not', 'wc', 'ass', 'inc', 'blbl', 'lamer', 'banka', 'bsod', 'hacker', 'helpred', 'sleepme', 'telek', 'sleepg']}
    ],

    textarea_id: '',

    open: function(id, link)
    {
        this.textarea_id = id;
        dhtml_popup_open('dp_extra_smiles', '', {}, link);
        if (this.tabs_html[0] == '')
        {
            this.show(0);
        }
        return false;
    },

    show: function(tab)
    {
        $('smile_extra_title').innerHTML = this.smiles[tab].name;
        if (this.tabs_html[tab] == '')
        {
            $('extra_smiles_list').innerHTML = '';
            this.smiles[tab].codes.each((function(code)
            {
                this.tabs_html[tab] += '<a href="" onclick="return CSmilesPackage.insert(\'' + code + '\')"><img src="/static_new/img/smiles/' + this.smiles[tab].folder + '/' + code + '.gif" alt=":' + code + ':" title=":' + code + ':" ></a> ';
            }).bind(this));
            }
        $('extra_smiles_list').innerHTML = this.tabs_html[tab];
    },

    insert: function(smile)
    {
        $(this.textarea_id) && ($(this.textarea_id).value += ' :' + smile + ': ');
        dhtml_popup_close('dp_extra_smiles');
        return false;
    },

    find_folder: function(code)
    {
        var folder = '';
        this.smiles.each(function(smiles)
        {
            if (smiles.codes.include(code))
            {
                folder = smiles.folder;
                return;
            }
        });
        return folder;
    },

    code2image: function(s)
    {
        var match;
        var regexp = /:([a-z][a-z0-9\-_]+):/ig;
        while (true)
        {
            match = regexp.exec(s);
            if (match == null) break;
            var folder = this.find_folder(match[1]);
            s = s.replace(match[0], smile_pattern1 + (folder ? folder + '/' : '') + match[1] + smile_pattern2);
        }

        s = replace_smiles(s);

        return s;
    }
});

if (window.register_load)
    register_load('smiles');
