CD.Converter = new Class({
    initialize: function() {
        fields = new Array()
		fields.syllabic = ['CreeText'];
		fields.roman = ['RomanText'];
        CD.VKS.handleFields(fields);
		if ($defined($('CreeText'))) {
            this.cree = $('CreeText');
        }
        if ($defined($('RomanText'))) {
            this.roman = $('RomanText');
        }
	this.makeClipboard();
        this.activatePage();
    },

    activatePage: function() {
        CD.VKS.getLanguage = this.converterGetLanguage.bind(this);
        
        this.cree.addEvent('focus', this.onFieldFocus.bind(this));
        this.roman.addEvent('focus', this.onFieldFocus.bind(this));
		
		this.cree.addEvent('change', this.onClipboardCopy.bind(this));
		this.roman.addEvent('change', this.onClipboardCopy.bind(this));
        
        var creeSmbtButton = $('CreeSubmit');
        if ($defined(creeSmbtButton)) {
            creeSmbtButton.addEvent('click', this.onSubmit.bind(this));
        }
        
        var romanSmbtButton = $('RomanSubmit');
        if ($defined(romanSmbtButton)) {
            romanSmbtButton.addEvent('click', this.onSubmit.bind(this));
        }
        
        var resetButton = $('Reset');
        if ($defined(resetButton)) {
            resetButton.addEvent('click', this.onReset.bind(this));
        }
	
	//activate non-Unicode mode
	this.activateNonUnicodeMode(); 
        
    },
/* void activateNonUnicodeMode()
 * is Called at the end of activatePage()
 * The function checks if converter is running in non-Unicode mode and changes
 * converter's appearance if so.
 */
    
    activateNonUnicodeMode: function() {
	    if($('nonUnicode')) {
		CD.Conf.nonUnicode = true;
		$('RomanSubmit').setStyle('display','none');//hide submit button
		$('CreeText').setStyle('display','none');//hide input field
		$('CreeTextDiv').setStyle('display','block');//hide input field
		//hide copy buttons
		$('copyCree').getParent().setStyle('display','none');
		//show worning message
		$('copyToWordNote').setStyle('display','inline');
		$('copyToWordNote').innerHTML = 'To copy to clipboard highlight the text below and press ' + (CD.Conf.isMac ? 'Cmd+c' : 'Ctrl+c');
	    } else {
		    CD.Conf.nonUnicode = false;
	    }
    },
    
	makeClipboard: function() {
		ZeroClipboard.setMoviePath(CD.Conf.path + 'js/zero/ZeroClipboard.swf' );
		this.creeClip = new ZeroClipboard.Client();
		this.creeClip.setHandCursor( true );
        this.creeClip.addEventListener('mouseOver', function(client) {
			if ($defined(this.cree)) {
				this.creeClip.setText(CD.Conf.isIE8 ? this.cree.innerHTML : this.cree.value);
			}
         }.bind(this));  
		this.creeClip.glue('copyCree');
		
		this.romanClip = new ZeroClipboard.Client();
		this.romanClip.setHandCursor( true );
		this.romanClip.addEventListener('mouseOver', function(client) {
			if ($defined(this.roman)) {
				this.romanClip.setText(CD.Conf.isIE8 ? this.roman.innerHTML : this.roman.value);
			}
		}.bind(this));  
		this.romanClip.glue('copyRoman');
	},
    converterGetLanguage: function(language) {
        if (CD.Conf.currentLayout != language) {
            CD.Conf.currentLayout = language;
        }
        if ('latin' == language) {
            this.roman.focus();
        } else {
            this.cree.focus();
        }
    },

    onClipboardCopy: function(event){
		var e = new Event(event);
		var type = e.target.getProperty('id');
		if (type == 'CreeText') {
			this.creeClip.setText(CD.Conf.isIE8 ? $(type).innerHTML : $(type).value)
		} else {
			this.romanClip.setText(CD.Conf.isIE8 ? $(type).innerHTML : $(type).value)
		}
    },

    onSubmit: function(event) {
        var e = new Event(event);
        var type = e.target.getProperty('id');
		var textId = type == 'RomanSubmit' ? 'CreeText' : 'RomanText';
		var loadId = type == 'RomanSubmit' ? 'loadRoman' : 'loadCree';
		var values = {'type': type, 'hash': CD.Conf.langLayout, 'path': CD.Conf.path}
        values[textId] = CD.Conf.isIE8 ? $(textId).innerHTML : $(textId).value;
	values[textId] = encodeURIComponent(values[textId]);
        this.loading('add', loadId);   
	var url = CD.Conf.path + 'converter/convertajax.php' + (CD.Conf.nonUnicode ? '?mode=nonUnicode' : '');        
        var ajaxObj = new Ajax(url, {
            method: 'post',
            data: Object.toQueryString(values)
        }).request();
        ajaxObj.addEvent('onComplete', function(response){
            if (response) {
                var values = response.split('=', 2);
                if (values[0] == 'CreeText' || values[0] == 'RomanText') {
		    values[1] = decodeURIComponent(values[1]);
		    if(CD.Conf.nonUnicode) {
			$('CreeTextDiv').innerHTML = values[1];
		    } else {
			if (CD.Conf.isIE8) {
				$(values[0]).innerHTML = values[1];
				$(values[0]).getNext().value = values[1];
			} else {
				$(values[0]).value = values[1];
			}
		    }
                }
            }
            this.loading('remove', loadId);
        }.bind(this));
    },
    onReset: function(event){
		if (CD.Conf.isIE8) {
			this.cree.innerHTML = '';
			this.cree.getNext().value = '';
			this.roman.innerHTML = '';
			this.roman.getNext().value = '';
		} else {
			this.cree.value = '';
			this.roman.value = '';
		}
    },
    
    onFieldFocus: function(event) {
        var e = new Event(event).stop();
        var el = e.target;
        if ($defined(el)) {
            var fieldId = e.target.getProperty('id');
            if ('RomanText' == fieldId) {
                if ('latin' != CD.Conf.currentLayout) {
                    CD.VKS.switchLanguage();
                } 
            } else {
                if ('latin' == CD.Conf.currentLayout) {
                    CD.VKS.switchLanguage();
                }
            }
            CD.VKS.focusedEl = el;
            CD.VKS.focused = true;
            if (CD.Conf.isIE) {
                CD.VKS.setCursorPosition(el, CD.Conf.isIE8 ? el.innerHTML.length : el.value.length, CD.Conf.isIE8 ? el.innerHTML.length : el.value.length);
            }
        }
    },
    loading: function(type, loadId) {
        var buttonIds = Array('CreeSubmit', 'RomanSubmit', 'Reset');
        if ('add' == type) {
            $each(buttonIds, function(id){
                var btnEl = $(id) ;
                if ($defined(btnEl)) {
                    btnEl.disabled = true;
                    btnEl.setProperty('src', btnEl.getProperty('src').replace('_o.', '_a.'));
                    var src = btnEl.getProperty('src');
                    if (-1 == src.indexOf('_a.')) {
                        btnEl.setProperty('src', src.substr(0, src.lastIndexOf('.')) + '_a' + src.substr(src.lastIndexOf('.')));
                        
                    }
                }
            });
            $(loadId).setStyle('display', 'inline');
        } else {
            $each(buttonIds, function(id){
                var btnEl = $(id) ;
                if ($defined(btnEl)) {
                    btnEl.disabled = false;
                    var src = btnEl.getProperty('src');
                    btnEl.setProperty('src', src.replace('_a.', '.'));
                }
            });
            $(loadId).setStyle('display', 'none');
        }
    }
})

window.addEvent('onAppReady', function(){
    new CD.Converter();
})

