function hide(link_id, target_id) {
    document.getElementById(link_id).style.backgroundImage ='url(http://resources.ableton.com/common/textbox_arrow.png)';
    document.getElementById(target_id).style.display='none';
}

function show(link_id, target_id) {
    document.getElementById(link_id).style.backgroundImage ='url(http://resources.ableton.com/common/arrow_open.png)';
    document.getElementById(target_id).style.display='block';
}

function toggle(element_name) {
    link_id = element_name + '_link';
    target_id = element_name + '_target';
    if(document.getElementById(target_id).style.display == 'block') {
        hide(link_id, target_id);
    }
    else {
        show(link_id, target_id);
    }
}

/**
 * Prevent double submission of forms in old-style pages. TG forms are handled
 * in the AbletonFormMixin.
 */
function process_form_submission(form) {
    var already_submitted = $(form).data("submitted");
    $(form).data("submitted", true);
    return !already_submitted;
}

function submit_address_form() {
    document.getElementById("address_form").submit();
}

function connect(link) {
  window.location.href = link;
  return true;
}

/**
 * Apply some CSS changes for the case
 * Javascripts is enabled
*/
$(document).ready(function() {
    $('div.toggle_headline').each(function () {$(this).addClass('toggle_headline_js')});
    $('div.toggle_content').each(function () {$(this).addClass('toggle_content_js')});
    $('div.popup a').hover(function() {
        $(this).find('.pop').each(function () {
            $(this).css('display', 'block');
            $(this).css('height', '1px');
            $(this).css('left', '-1px');
            $(this).css('position', 'absolute');
            $(this).css('top', '-1px');
            $(this).css('width', '0px');;
        });
    });
});


 // prevent soundcloud link to go to soundcloud page
 // but start playing
$(document).ready(function() {
    $('.sc-player .sc-info a').live('click', function() {
        $(this).parent().parent().siblings('ol').find('li:first').click();
        return false
    });
});


/* Enable the More button on the mobile menu to work */
$(document).ready(function() {
    $("#menu_mobile_more").toggle(function() {
    $("#menu_shop").show();
    $("#menu_education").show();
    $("#menu_company").show();
     },
    function() {
        $("#menu_shop").hide();
        $("#menu_education").hide();
        $("#menu_company").hide();
    })
});


/* Set navigation menu font for XP */
$(document).ready(function() {
                      if (/Windows NT 5\.1/.test(navigator.userAgent)) {
                          $("body").addClass("xp");
                      }
                  });

/* GA event tracking for search */
/* and menu search autocomplete */
$(document).ready(function() {
    $('div#search div#search_results a').live('click', function() {
        var lang = $('body').attr('class').slice(5,7);
        var name = "SearchPosition_" + lang;
        var search_term = $('input#search_form_query').val();
        var click_elem = $(this);
        var url = click_elem.attr('href');
        var result_elem = click_elem.parent().parent().parent();
        var pos = result_elem.prevAll().length;
        var page = $('div#search div#paginator span.pager_curpage').html() || 1;
        var page_pos = (parseInt(page) - 1) * 10 + pos + 1;
        pageTracker._trackEvent(name, search_term, url, page_pos);
        return true;
    });

    $("li#menu_search input#search_query").autocomplete({"source": "/search/term_suggest",
                                                         "minLength": 3,
                                                         "delay": 10,
                                                         "appendTo": '#menu_search',
                                                         "position": {my: "left top",
                                                                      at: "left bottom",
                                                                      collision: "none",
                                                                      offset: "0 2px;"},
                                                         "select": function(event, ui) {
                                                             $(this).val(ui.item.label);
                                                             $(this).parent('form').submit();
                                                          }
                                                         }).live('keydown', function (e) {
                                                            var keyCode = e.keyCode || e.which;
                                                            if(keyCode == 13) {
                                                                $(this).parent('form').submit();
                                                            }
                                                         });

});


