var Dom = YAHOO.util.Dom;
var Event = YAHOO.util.Event;
var YD = YAHOO.util.Dom;
var YE = YAHOO.util.Event;
var YC = YAHOO.util.Connect;

/////////////////////////////
// GLOBAL NAMESPACE

var DISCOGS = {

    set: function (namespace, property) {
        if (!DISCOGS[namespace]) {
            DISCOGS[namespace] = property;
        } else {
            for (key in property) {
                DISCOGS[namespace][key] = property[key];
            }
        }
    },

    get: function (namespace, key) {
        if (!DISCOGS[namespace]) {
            return false;
        }
        if (!key) {
            return DISCOGS[namespace];
        }
        return DISCOGS[namespace][key];
    },

    redirect: function(url) {
        window.location.href = url;
        window.event.returnValue = false;
    }

};

/////////////////////////////

if (!String.prototype.trim)
    String.prototype.trim = function() { return this.replace(/^\s+|\s+$/g,""); }

function truef() { return true; }

function highlight(el) {
    var attrs = {backgroundColor: {from: '#ff0', to: '#fff'}};
    var anim = new YAHOO.util.ColorAnim(el, attrs);
    anim.animate();
}

function activity_on(msg) {
    var el = YD.get('act');
    YD.setStyle(el, 'display', 'block');
    el.innerHTML = msg;
    YD.setXY(el, [(YD.getViewportWidth() - el.clientWidth)-2, YD.getClientRegion()['top']+2])
}

function activity_off() {
    YD.setStyle('act', 'display', 'none');
}

function fade(off) {
    var f = YD.getElementsByClassName('fade', 'div');
    if (f.length) {
        if (off) {
            f[0].parentNode.removeChild(f[0]);
            f = YD.getElementsByClassName('fade_msg', 'div');
            if (f.length)
                f[0].parentNode.removeChild(f[0]);
        }
    }
    else {
        f = document.createElement('div');
        f.className = 'fade';
        document.body.appendChild(f);
    }
}

function fadeMsg(s) {
    fade();
    var m = document.createElement('div');
    m.className = 'fade_msg';
    m.innerHTML = s;
    document.body.appendChild(m);
}

function reloadPage() {
    fadeMsg('Reloading...');
    document.location.reload();
}

function selectMe(e) {
    if (document.selection) {
        document.selection.empty();
        var r = document.body.createTextRange();
        r.moveToElementText(e);
        r.select();
    }
    else if (window.getSelection) {
        var s = window.getSelection();
        var r = document.createRange();
        r.selectNode(e);
        s.removeAllRanges();
        s.addRange(r);
    }
}

function uaIE() {
    return Boolean(YAHOO.env.ua.ie);
}

function iecs() {
    return (YAHOO.env.ua.ie ? ('&iecs='+String(Math.random()).substr(2,8)) : '');
}

function browserSupported() {
    var ie = YAHOO.env.ua.ie;
    if (ie) {
        if (ie < 7) {
            alert('Sorry, this operation does not support Internet Explorer 6. Please consider upgrading your browser.');
            return false;
        }
    }
    return true;
}

function stripCode(s) {
    return s.split("&").join("&amp;").split("<").join("&lt;").split(">").join("&gt;");
}

function dismiss_alert(id) {
    YAHOO.util.Cookie.set('pa_dismiss_'+id, 1, {path: '/', domain: 'discogs.com', expires: new Date("December 31, 2030")});
    YD.get('page_alert').style.display = 'none';
}

/////////////////////////////
// MENUS

function moveMenu(head, menu, right, offsets) {
    var r1 = YD.getRegion(head);
    var r2 = YD.getRegion(menu);
    var x = (right ? r1.right - r2.width : r1.left) + (offsets ? offsets[0] : 0);
    // YUI 2.7.0 FF bug partial workaround
    if (window.navigator.userAgent.search('Firefox/3') != -1) {
        var z = head.getBoundingClientRect().left;
        if (Math.round(z) != Math.floor(z))
            x += 1;
    }
    var y = r1.bottom + (offsets ? offsets[1] : 0);
    YD.setXY(menu, [x, y]);
    // only set width once - check for explicit width style
    // unassigned width will be 'auto' in IE; firefox and safari provide style.cssText for the element
    var w = YD.getStyle(menu, 'width');
    if ((!w) || (w == 'auto') || (menu.style.cssText && !menu.style.cssText.match(/width/)))
        YD.setStyle(menu, 'width', Math.max(r1.width, r2.width) - 2 + 'px');
}

function showMenu(e, a) {
    var b = YD.addClass([a.head, a.menu], 'active');
    if (b[0] || b[1])
        moveMenu(a.head, a.menu, a.right, a.offsets);
}

function hideMenu(e, a) {
    YD.removeClass([a.head, a.menu], 'active');
}

function initMenuItem(head, menu, right, offsets) {
    var a = {'head':head, 'menu':menu, 'right':Boolean(right), 'offsets':offsets};
    YE.on([head, menu], 'mouseover', showMenu, a);
    YE.on([head, menu], 'mouseout', hideMenu, a);
    YE.on(menu, 'click', hideMenu, a);
}

/////////////////////////////
// MENU B

function showMenuB(e, a) {
    if (a.move_dom)
        a.head.parentNode.appendChild(a.menu);
    YD.addClass([a.head, a.menu], 'active');
    moveMenu(a.head, a.menu, 1, a.offsets);
    YE.on(document, 'mouseup', hideMenuB, a);
}

function hideMenuB(e, a) {
    YD.removeClass([a.head, a.menu], 'active');
    YE.removeListener(document, 'mouseup');
}

function initMenuB(menu, head, move_dom, offsets) {
    YE.on(head, 'click', showMenuB, {'menu':menu, 'head':head, 'move_dom':move_dom, 'offsets':offsets});
}

/////////////////////////////
// HEADER MENUS

function searchFilter(e, a) {
    YD.get('search_type').value = a.name;
    YD.get('search_filter').innerHTML = a.html;
    var x = 280-YD.getRegion('menu_head_search').width-YD.getRegion(YD.get('search_submit').parentNode).width;
    YD.setStyle('search_q', 'width', x+'px');
}

function initSearchMenuItem(name) {
    var e = YD.get('search_'+name);
    if (e)
        YE.on(e, 'click', searchFilter, {'name':name, 'html':e.innerHTML.toLowerCase()});
}

function initMenu() {
    var menus = ['my', 'mp', 'browse', 'groups'];
    for (var i in menus) {
        initMenuItem(YD.get('menu_head_'+menus[i]), YD.get('menu_'+menus[i]));
    }
    initMenuItem(YD.get('menu_head_search'), YD.get('menu_search'), 1, [0, -1]);
    var searches = ['all', 'artists', 'labels', 'releases', 'needsvote', 'catno', 'forsale'];
    for (var i in searches)
        initSearchMenuItem(searches[i]);
}

/////////////////////////////
// AUTOCOMPLETE

var acds;

acds = new YAHOO.util.XHRDataSource('http://www.discogs.com/search/ac');
acds.connXhrMode = YAHOO.util.XHRDataSource.cancelStaleRequests;
acds.responseType = YAHOO.util.XHRDataSource.TYPE_TEXT;
acds.responseSchema = { recordDelim: '\n', fieldDelim: '\t' };
acds.maxCacheEntries = 0;

function ac_gr_all(sQuery) { return '?q=' + sQuery; }
function ac_gr_artist(sQuery) { return '?q=' + sQuery + '&type=artist'; }
function ac_gr_label(sQuery) { return '?q=' + sQuery + '&type=label'; }

function initAC(tbn, fgr, bec) {
    var oAC = new YAHOO.widget.AutoComplete(tbn, 'ac_' + tbn, acds);
    oAC.prehighlightClassName = 'yui-ac-prehighlight';
    oAC.animVert = false;
    oAC.queryDelay = .5;
    oAC.minQueryLength = 3;
    oAC.autoHighlight = false;
    oAC.generateRequest = fgr;
    if (bec)
        oAC.doBeforeExpandContainer = bec;
    return oAC;
}

var mainAC = null;

function itemSelectMainAC() {
    YD.getAncestorByTagName('search_q', 'form').submit();
}

function becMainAC(elTextbox, elContainer, sQuery, aResults) {
    var pos = YD.getXY(elTextbox.parentNode.parentNode);
    pos[1] += 24;
    YD.setXY(elContainer, pos);
    elContainer.style.width = '214px';
    return true;
}

function becAdvSearch(elTextbox, elContainer, sQuery, aResults) {
    var pos = YD.getXY(elTextbox);
    pos[1] += elTextbox.offsetHeight;
    YD.setXY(elContainer, pos);
    elContainer.style.width = elTextbox.clientWidth + 3 + 'px';
    return true;
}

function initMainAC() {
    if (mainAC)
        mainAC.destroy();
    mainAC = initAC('search_q', ac_gr_all, becMainAC);
    if (mainAC)
        mainAC.itemSelectEvent.subscribe(itemSelectMainAC);
}

/////////////////////////////
// generic async handlers

function updateElement(n, html, append) {
    var el = YD.get(n);
    if (el) {
        if (append)
            el.innerHTML += html;
        else
            el.innerHTML = html;
    }
}

function updateElements(n, html, append) {
    var els = YD.getElementsByClassName(n);
    for (var i in els)
        updateElement(els[i], html, append);
}

function async_success(o) {
    activity_off();
    var r = eval('(' + o.responseText + ')');
    for (var i = 0; i < r.length; i++) {
        if (!r[i].cond || eval(r[i].cond)) {
            if (r[i].cmd == 'update') {
                el = YD.get(r[i].el);
                if (el)
                    el.innerHTML = r[i].html;
            }
            else if (r[i].cmd == 'append') {
                el = YD.get(r[i].el);
                if (el)
                    el.innerHTML += r[i].html;
            }
            else if (r[i].cmd == 'execute') {
                var data = r[i].data;
                zzz = r[i].code;
                eval(r[i].code);
            }
        }
    }
}

function async_fail(o) {
    activity_off();
}

function async_s(msg) {
    if (!DISCOGS.get('Session')['loggedin']) {
        DISCOGS.redirect('/users/login?nologin=1&return='+encodeURIComponent(window.location.pathname));
        return false;
    } else {
        if (msg != '')
            activity_on(msg ? msg : 'Updating...');
        return true;
    }
}

function async_g(url, msg) {
    if (async_s(msg))
        YC.asyncRequest('GET', url, {success:async_success, failure:async_fail});
}

function async_p(url, msg, data) {
    if (async_s(msg))
        YC.asyncRequest('POST', url, {success:async_success, failure:async_fail}, data);
}

function async_html_success(o) {
    activity_off();
    var el = YD.get(o.argument);
    if (el)
        el.innerHTML = o.responseText.trim();
}

function async_html(url, data, el, msg, get) {
    if (async_s(msg))
        YC.asyncRequest(get ? 'GET' : 'POST', url, {success:async_html_success, failure:async_fail, argument:el}, data);
}

function async_html_g(url, el) {
    YC.asyncRequest('GET', url, {success:async_html_success, failure:async_fail, argument:el});
}

function async_panel_success(o) {
    activity_off();
    var p = o.argument;
    p.setBody(o.responseText);
    p.render(document.body);
    p.show();
}

function async_panel(url, panel, msg) {
    if (async_s(msg))
        YC.asyncRequest('GET', url, {success:async_panel_success, failure:async_fail, argument:panel});
}

function async(url, message) {
    // deprecated - use async_g and async_p for GET and POST
    return async_p(url, message);
}

/////////////////////////////
// In-Line Editing

function ileEdit(ev, target) {
    if (YD.hasClass(document.body, 'ile_off')) return;
    var p  = DISCOGS.get('ile')[target.id];
    if (!p)
        return;
    YD.replaceClass(document.body, 'ile_on', 'ile_off');
    var s = null;
    var x = DISCOGS.get('ile_fields', p.opts_id);
    if (YD.hasClass(target, 'ile_list')) {
        s = '<select id="ile_select" size="' + (x.length+1) + '" onclick="ileSave(\'' + target.id + '\');">' +
            '<option> </option>';
        for (var i in x)
            s += '<option' + (x[i] == p.content ? ' selected="true"' : '') + ' value="' + x[i] + '">' + x[i] + '</option>';
        s += '</select>';
        setTimeout('YE.on(document.body,"click",ileListCancel,"'+target.id+'");', 0);
    }
    else {
        s = '<textarea id="ile_ta" rows="' + ((x-1) || 3) + '">' + p.content + '</textarea>' +
            '<a onclick="ileSave(\'' + target.id + '\');">Save</a> ' +
            '<a onclick="ileDone(\'' + target.id + '\');">Cancel</a>';
        setTimeout('YD.get("ile_ta").focus();', 0);
    }
    target.innerHTML = s;
    if (p.edit)
        p.edit(target);
}

function ileSave(target_id) {
    var p = DISCOGS.get('ile')[target_id];
    if (p && p.save) {
        var t = YD.get(target_id);
        var v = null;
        if (YD.hasClass(t, 'ile_list'))
            v = YD.get('ile_select').value;
        else
            v = YD.get('ile_ta').value;
        p.save(t, v);
    }
}

function ileSaveReturn(target_id, content, html) {
    var target = YD.get(target_id);
    var p = DISCOGS.get('ile')[target_id];
    p.content = content;
    p.html = html;
    ileDone(target_id);
}

function ileDone(target_id) {
    var target = YD.get(target_id);
    var p = DISCOGS.get('ile')[target_id];
    if (p) {
        if (p.done)
            p.done(target);
        setTimeout("YD.replaceClass(document.body, 'ile_off', 'ile_on');", 0);
        target.innerHTML = p.html;
    }
}

function ileListCancel(e, target_id) {
    if (YE.getTarget(e).id != 'ile_select') {
        ileDone(target_id);
    }
    YE.removeListener(document.body, 'click', ileListCancel);
}

function ileInit(target, content, edit, save, done, opts_id) {
    if (YE.getListeners(target, 'click')) return;
    if (!YD.hasClass(document.body, 'ile_off')) YD.addClass(document.body, 'ile_on');
    var d = {};
    d[YD.generateId(target)] = {content:content, html:target.innerHTML, edit:edit, save:save, done:done, opts_id:opts_id};
    DISCOGS.set('ile', d);
    YE.on(target, 'click', ileEdit, target);
}

function ileInitClass(classname, content, edit, save, done, opts_id) {
    var t = YD.getElementsByClassName(classname, 'div');
    for (var i in t)
        ileInit(t[i], content, edit, save, done, opts_id);
}

/////////////////////////////
// Lists

function swap_listpicker(b) {
    YD.setStyle(b ? 'listadd_new' : 'listadd_old', 'display', 'block');
    YD.setStyle(b ? 'listadd_old' : 'listadd_new', 'display', 'none');
}

function listAdd(what, obid) {
    if (!DISCOGS.list_panel) {
        YD.addClass(document.body, 'yui-skin-sam');
        DISCOGS.list_panel = new YAHOO.widget.Panel("list_add_panel",
            { width:"226px", fixedcenter:true, close:false, draggable:false,
              zindex:777, modal:true, visible:false });
        DISCOGS.list_panel.setHeader('Add to List');
    }
    async_panel('/lists/add?what='+what+'&obid='+obid, DISCOGS.list_panel);
}

function listAddSave() {
    var comment = stripCode(YD.get('list_comments').value) || ' ';
    var data = 'what=' + YD.get('list_what').value + '&obid=' + YD.get('list_obid').value +
               '&comments=' + encodeURIComponent(comment);
    if (YD.get('listpick_new').checked) {
        if (!YD.get('list_title').value) {
            alert('Please enter a title.');
            return;
        }
        var title = stripCode(YD.get('list_title').value);
        var description = stripCode(YD.get('list_description').value) || ' ';
        data += '&list=new' +
                '&title=' + encodeURIComponent(title) +
                '&description=' + encodeURIComponent(description);
    }
    else {
        if (YD.get('list_oldpick').selectedIndex == -1) {
            alert('Please select an existing list.');
            return;
        }
        data += '&list=' + YD.get('list_oldpick').value;
    }
    async_p('/lists/addsave', 'Saving...', data);
}

function listAddCancel() {
    DISCOGS.list_panel.hide();
}

function listReport(id) {
    var proceed = confirm('Report this list as inappropriate or Discogs guidelines?');
    if (proceed) {
        async_p('/lists/report', 'Saving...', 'id='+id);
    }
}

/////////////////////////////
// Sliders

function sliderPage(ev, a) {
    a.slider = YD.get(a.slider);
    var p = YD.getElementsByClassName('slider_page', 'ul', a.slider);
    var c = null;
    for (var i in p)
        if (!YD.hasClass(p[i], 'hidden')) {
            c = Number(i);
            break;
        }
    if (c == null)
        return;
    var n = ('inc' in a) ? (c + a.inc) : (('to' in a) ? a.to : null);
    if ((n != null) && (n != c) && (n in p)) {
        YD.addClass(p[c], 'hidden');
        YD.removeClass(p[n], 'hidden');
        (n ? YD.removeClass : YD.addClass)(YD.getElementsByClassName('slider_back', 'a', a.slider), 'disabled');
        (n+1 == p.length ? YD.addClass : YD.removeClass)(YD.getElementsByClassName('slider_next', 'a', a.slider), 'disabled');
        var q = YD.getElementsByClassName('pagers', 'div', a.slider.parentNode);
        if (q && q[0]) {
            YD.addClass(YD.getElementsByClassName('pager'+n, 'div', q[0]), 'active');
            YD.removeClass(YD.getElementsByClassName('pager'+c, 'div', q[0]), 'active');
        }
    }
    if (a.fn) {
        a.fn(p[n]);
    }
}

function initSlider(s, fn_click, fn_slide) {
    YE.on(YD.getElementsByClassName('slider_next', 'a', s), 'click', sliderPage, {'slider':s, 'inc':1, 'fn':fn_slide});
    YE.on(YD.getElementsByClassName('slider_back', 'a', s), 'click', sliderPage, {'slider':s, 'inc':-1, 'fn':fn_slide});
    YE.on(YD.getElementsBy(truef, 'li', s), 'click', fn_click);
}

/////////////////////////////
// Videos

function videoSlide(ev) {
    var t = YE.getTarget(ev);
    if (t.tagName != 'IMG')
        t = YD.getElementBy(truef, 'img', t);
    var id = t.id.match(/\d+$/)[0];
    YD.get('video_title').innerHTML = t.title;
    videoShow(id);
}

function videoShow(id) {
    var cd = YD.get('video_content_'+id);
    YD.removeClass(YD.getElementsByClassName('video_content', null, YD.getAncestorByClassName(cd, 'video')), 'shown');
    if (!cd.innerHTML) {
        swfobject.embedSWF('http://www.youtube.com/v/'+video_data[id]+'&border=0&enablejsapi=1&rel=0&fs=1',
                           'video_content_'+id, '280', '229', '8', null, null,
                           {'allowFullScreen': 'true', 'allowScriptAccess': 'always'},
                           {'class': 'video_content shown'});
        YD.setStyle('video_content_'+id, 'visibility', '');
    }
    else
        YD.addClass(cd, 'shown');
}

function videoPopup() {
    v = YD.getElementsByClassName('video_content shown')[0];
    v.stopVideo();
    var v_id = video_data[v.id.match(/.+_(\d+)/)[1]];
    window.open('/videos/only?video='+v_id, 'discogs_video_window', 'status=0,toolbar=0,location=0,menubar=0,width=300,height=235');
}

/////////////////////////////
// Rating Bar

function ratingSet(release_id, my, value, count) {
    updateElements('rating_count_r'+release_id, count);
    updateElements('rating_value_r'+release_id, value);
    var bars = YD.getElementsByClassName('rating_r'+release_id);
    for (var i = 1; i < 6; i++)
        YD.removeClass(bars, 'value'+i);
    if (my > 0)
        YD.addClass(bars, 'value'+my);
    for (var b in bars)
        (my ? YD.removeClass : YD.addClass)(YD.getElementsByClassName('zero', 'a', bars[b]), 'disabled');
}

function ratingClick(ev, a) {
    if (!YD.hasClass(a[0], 'disabled'))
        async_p('/release/rate', null, 'release_id='+a[2]+'&value='+a[1]);
}

function ratingHover(ev, a) {
    a[0](a[1], 'hover'+a[2]);
}

function initRating(release_id) {
    var bars = YD.getElementsByClassName('rating_r'+release_id);
    for (var i in bars) {
        var b = YD.getElementsBy(truef, 'a', bars[i]);
        for (var j in b)
            if (!YE.getListeners(b[j])) {
                var k = Number(j);
                YE.on(b[j], 'mouseover', ratingHover, [YD.addClass, bars[i], k]);
                YE.on(b[j], 'mouseout', ratingHover, [YD.removeClass, bars[i], k]);
                YE.on(b[j], 'click', ratingClick, [b[j], k, release_id]);
            }
    }
}

/////////////////////////////
// Wantlist

function wantAdd(id) {
    async_p('/list/want_add', null, 'release_id='+id);
}

function wantRemove(id) {
    if (!DISCOGS.get('lock', 'want_remove'))
        async_p('/list/want_remove', null, 'release_id='+id);
}

function wantNotesEdit(target) {
    DISCOGS.set('lock', {want_remove:true});
    target.innerHTML += '<span class="nowrap"><input id="want_notes_public" type="checkbox"' + (YD.hasClass(target, 'public') ? ' checked="true"' : '') + '" /><label for="want_notes_public">Public</label></span>';
}

function wantNotesSave(target, content) {
    var release_id = target.className.match(/wantlist_r(\d+)_notes/)[1];
    var data = 'release_id=' + release_id +
               '&notes=' + encodeURIComponent(content || ' ') +
               '&public=' + (YD.get('want_notes_public').checked ? '1' : '0');
    async_p('/list/want_update', null, data);
}

function wantNotesSaveReturn(release_id, content, html, pub) {
    var t = YD.getElementsByClassName('wantlist_r'+release_id+'_notes');
    (pub ? YD.addClass : YD.removeClass)(t, 'public');
    for (var i in t)
        ileSaveReturn(YD.generateId(t[i]), content, html);
}

function wantNotesDone(target) {
    DISCOGS.set('lock', {want_remove:false});
}

function wantNoteInit(release_id, content) {
    ileInitClass('wantlist_r'+release_id+'_notes', content, wantNotesEdit, wantNotesSave, wantNotesDone);
}

/////////////////////////////
// Collection

function collAdd(id) {
    async_p('/list/coll_add', null, 'release_id='+id);
}

function collRemove(id, inst) {
    if (!DISCOGS.get('lock', 'coll_remove'))
        async_p('/list/coll_remove', null, 'release_id='+id+'&inst='+inst);
}

function collNotesEdit(target) {
    DISCOGS.set('lock', {coll_remove:true});
}

function collNotesSave(target, content) {
    var ids = target.className.match(/collection_r(\d+)_i(\d+)_f(\d+)/);
    var data = 'release_id=' + ids[1] +
               '&inst=' + ids[2] +
               '&field_id=' + ids[3] +
               '&val=' + encodeURIComponent(content || ' ');
    async_p('/list/coll_update', null, data);
}

function collNotesSaveReturn(release_id, inst, field_id, content, html) {
    var t = YD.getElementsByClassName('collection_r'+release_id+'_i'+inst+'_f'+field_id);
    for (var i in t)
        ileSaveReturn(YD.generateId(t[i]), content, html);
}

function collNotesDone(target) {
    DISCOGS.set('lock', {coll_remove:false});
}

function collNoteInit(release_id, inst, field_id, content) {
    ileInitClass('collection_r'+release_id+'_i'+inst+'_f'+field_id, content, collNotesEdit, collNotesSave, collNotesDone, field_id);
}

/////////////////////////////
// Squish

function squish(ev, args) {
    if (YD.hasClass(args.el, 'squish')) {
        YD.removeClass(args.el, 'squish');
        YD.setStyle(args.el, 'max-height', '');
        args.a.innerHTML = '&#9650; less';
    }
    else {
        YD.addClass(args.el, 'squish');
        YD.setStyle(args.el, 'max-height', args.height+'px');
        args.a.innerHTML = '&#9660; more';
    }
}

function initSquish(e, rows) {
    var d = document.createElement('div');
    var z = new Array(rows);
    for (var i = 0; i < z; i++) z[i] = 'X';
    d.innerHTML = z.join('<br />');
    document.body.appendChild(d);
    var y = YD.getRegion(d).height;
    document.body.removeChild(d);
    e = YD.get(e);
    var r = YD.getRegion(e);
    if (r.height > y) {
        var d = document.createElement('div');
        d.className = 'squisher';
        e.parentNode.appendChild(d);
        var a = document.createElement('a');
        d.appendChild(a);
        var args = {'el': e, 'a': a, 'height': y};
        YE.on(a, 'click', squish, args);
        squish(null, args);
    }
}

/////////////////////////////

YE.onDOMReady(function() {
    initMenu();
    initMainAC();
    YD.get('search_q').autocomplete = 'on';
});
