var block = false;

var moved_field = '';
var IE = document.all?true:false
// If NS -- that is, !IE -- then set up for mouse capture
if (!IE) document.captureEvents(Event.MOUSEMOVE)

jQuery(document).ready(function(){
    jQuery('.overlib').bubble();
    jQuery('#input-search').autocomplete("/reifen_autocomplete.php", {
    delay:500,
    minChars:3,
    scroll:false,
    formatItem:function(data, term) {
        return data[1];
    },
    formatMatch:function(data) {
        return data[0];
    },
    highlight:function(value, term) {
        return value;
    }
    });
});

function change(toWhich) {
    if (toWhich == 'new') {
        var show = document.getElementById('search_new');
        show.style.display = 'block';
        var hide = document.getElementById('search_old');
        hide.style.display = 'none';

        var show = document.getElementById('help_new');
        if (show)
            show.style.display = 'block';
        var hide = document.getElementById('help_old');
        if (hide)
            hide.style.display = 'none';

        g = toWhich;
    } else if (toWhich == 'old') {
        var show = document.getElementById('search_old');
        show.style.display = 'block';
        var hide = document.getElementById('search_new');
        hide.style.display = 'none';

        var show = document.getElementById('help_old');
        if (show)
            show.style.display = 'block';
        var hide = document.getElementById('help_new');
        if (hide)
            hide.style.display = 'none';

        g = toWhich;
    }
}
function page(p) {
    var action = '';
    if (g=='old') action='s'; else action='s_new';
    var page = '/reifen_suchen'+urlParams+'-action-'+action+'-search-'+searchs+'-o-'+o+'-p-'+p+'-g-'+g+'-t-'+t+'-w-'+w+'-h-'+tyreH+'-inch-'+inch+'-s-'+tyreS+'-m-'+m+'-minimum_count-'+minimum+'.html';
    window.location.href = page;
}
function order(o) {
    var action = '';
    if (g=='old') action='s'; else action='s_new';
    var page = '/reifen_suchen'+urlParams+'-action-'+action+'-search-'+searchs+'-o-'+o+'-g-'+g+'-t-'+t+'-w-'+w+'-h-'+tyreH+'-inch-'+inch+'-s-'+tyreS+'-m-'+m+'-minimum_count-'+minimum+'.html';
    window.location.href = page;
}

function on_load() {
    if (g=='old') {
        document.old_search.search.focus();
    } else if (g=='new' || g=='') {
        document.search.w.focus();
        document.search.w.select();
    }
    if (getDataPopup) {
        showUserQuestion();
    }
}

function alt_search(alt_str) {
    if (g=='old') {
        document.old_search.search.value = alt_str;
        document.old_search.submit();
    } else if (g=='new') {
        var page = '/reifen_suchen'+urlParams+'-action-s_new-search-'+alt_str+'-o-'+o+'-g-'+g+'-t-g-w-'+w+'-h-'+tyreH+'-inch-'+inch+'-s-'+tyreS+'-m-'+m+'-minimum_count-'+minimum+'.html';
        window.location.href = page;
    }
}
function details(id, minimum_count,art_txt) {
    if (!block) {
        var w = 600;
        var h = 700;
        var winl = (screen.width-w)/2;
        var wint = (screen.height-h)/2;
        if (winl < 0) winl = 0;
        if (wint < 0) wint = 0;
        var page = '/reifen_details-n-'+art_txt+urlParams+'-inside_page-1-id-'+id+'-mc-'+minimum_count+'.html';
        document.location.href = page;
    }
    block = false;
}

function mOver(i, cName,img_field) {
    var tr = getObj(i);
    if (tr) {
        // set background color
        var count = tr.cells.length;
        for (j=0; j<count; j++) {
            tr.cells[j].className = cName;
        }
    }
    //showIMG(img_field,1);
}

function mOut(i, color,img_field) {
    var tr = getObj(i);
    if (tr) {
        // set background color
        var count = tr.cells.length;
        for (i=0; i<count; i++) {
            tr.cells[i].className = color;
        }
    }
    //hideIMG();
}
// Temporary variables to hold mouse x-y pos.s
var mouseX = 0
var mouseY = 0

// Main function to retrieve mouse x-y pos.s
function getMouseXY(e) {
    var scrollPos;
    if (typeof window.pageYOffset != 'undefined') {
       scrollPos = window.pageYOffset;
    }
    else if (typeof document.compatMode != 'undefined' &&
         document.compatMode != 'BackCompat') {
       scrollPos = document.documentElement.scrollTop;
    }
    else if (typeof document.body != 'undefined') {
       scrollPos = document.body.scrollTop;
    }
    mouseX = (IE) ? window.event.clientX + document.body.scrollLeft : e.pageX;
    mouseY = (IE) ? window.event.clientY + scrollPos : e.pageY;
    // catch possible negative values in NS4
    if (mouseX < 0){mouseX = 0}
    if (mouseY < 0){mouseY = 0}
    // show the position values in the form named Show
    // in the text fields named MouseX and MouseY
    if(moved_field){
        // tooltip width and height
        var tpWd = (IE)? moved_field.clientWidth: moved_field.offsetWidth;
        var tpHt = (IE)? moved_field.clientHeight: moved_field.offsetHeight;
        // document area in view (subtract scrollbar width for ns)
        var winWd = (IE)? document.body.clientWidth+document.body.scrollLeft : window.innerWidth-20+window.pageXOffset ;
        var winHt = (IE)? (screen.availHeight-tpHt)+scrollPos : window.innerHeight-20+window.pageYOffset;
        //abstand von der Zeiger
        if ((mouseY+tpHt)>winHt)
            mouseY = winHt-(tpHt);

        moved_field.style.left=(mouseX+10)+'px';
        moved_field.style.top=mouseY+'px';
    }
    return true
}

function advanced(element, type) {
    block = true;
    var divDetails = jQuery(element).nextAll('div').eq(0);
    var divTyre = jQuery(element).nextAll('div').eq(1);
    if (type == 'tyre') {
        divDetails.hide();
        if (divTyre.is(":hidden")) {
            divTyre.fadeIn('fast');
        } else {
            divTyre.fadeOut('fast');
        }
    } else {
        divTyre.hide();
        if (divDetails.is(":hidden")) {
            divDetails.fadeIn('fast');
        } else {
            divDetails.fadeOut('fast');
        }
    }
    return false;
}
