function commandFunc(e) {
    if(e.key == 'enter') 
    	this.tokens.each(function(token){
    		if (token[0] == this.opted) location = token[1];
    	}.bind(this));

    if(e && e.event && e.event.type == 'click')
        location = e.target.getElement('div').get('html');
}

// Merchant name Code

window.addEvent('domready', function () {
	$$('input[name=inpMerchantList]').each(function(inputbox){
    new Autocompleter.Local(inputbox, merchants, {
        'delay': 100,
        'filterSubset': true,
        'filter': function(){
    		var lang = this.element.lang;
            var regex = new RegExp('.*' + this.queryValue.escapeRegExp() + '.*', 'i');
            return this.tokens.filter(function (token) {
            	if(lang != '' && lang != undefined && lang != token[2])
            		return false;
            	
                return (regex.test(token[0]) || regex.test(token[1]));
            });
        },
        'onCommand': commandFunc,
        'injectChoice': function(choice){
            var el = new Element('li').set('html', this.markQueryValue(choice[0])).grab(new Element('div', {
                'html': choice[1],
                'styles': {
                    'display': 'none'
                }
            })).addEvent('click', commandFunc);
            el.inputValue = choice[0];
            this.addChoiceEvents(el).injectInside(this.choices);
        }
    });
	});
    
    new Element('div', {
        'id': 'backFade'
    })
    .setStyle('opacity', 0)
	.inject(document.body);
	
    $$('.infoDiv').setStyles({
        'display': 'none',
        'opacity': 0
    })
	.inject(document.body)
	.each(function (div) {
        div.grab(new Element('img', {
            'src': '/images/site/close-32.png',
            'alt': 'Close',
            'title': 'Close this pop-up',
            'class': 'close',
            'events': {
                'click': hideImageInfo
            }
        }))
    });
		
	$$('#smoothflow a img').each( function(el){

		el.addEvent('mouseenter', function(){ 
			this.set('morph', { duration: 500, transition: 'quad:out' }).morph({
				'width': '200px',
				'padding-top': 0,
				'padding-bottom': 0
			});			
		});
		
		el.addEvent('mouseleave', function(){ 
			this.set('morph', { duration: 250, transition: 'quad:in' }).morph({
				'width': '100px',
				'padding-top': 50,
				'padding-bottom': 50
			});			
		});
		
		el.addEvent('click', showImageInfo);
	
	});
		
});


function showImageInfo(e) {
    e.preventDefault();

    var target = $(e.target);
    if(target.get('tag') == 'img') 
    	target = target.getParent();

    var infoDiv = $('infoDiv-' + target.get('rel'));

    if(!infoDiv)
    	return false;
	
    infoDiv.set('morph', { duration:250 }).morph({
		'display': 'block',
        'opacity': 1
    });
    $('backFade').set('morph', { duration:250 }).morph({
        'opacity': 0.5
    });

    return false;
}


function hideImageInfo(e) {
    var infoDiv = $(e.target);

    if(!infoDiv.hasClass('infoDiv'))
    	infoDiv = infoDiv.getParent('.infoDiv');

    infoDiv.set('morph', { duration:250 }).morph({
		'display': 'none',
        'opacity': 0
    });
    $('backFade').set('morph', { duration:250 }).morph({
        'opacity': 0
    });
}