

function f_scrollTop() {
    scroll_top_now = 0
    scroll_top_now = window.pageYOffset ? window.pageYOffset : 0;
    scroll_top_now = document.documentElement ? document.documentElement.scrollTop : scroll_top_now;
    //scroll_top_now = document.body ? document.body.scrollTop : scroll_top_now;
    return scroll_top_now
}

function getQuerystring(key, default_) {
    if (default_ == null) default_ = "";
    key = key.replace(/[\[]/, "\\\[").replace(/[\]]/, "\\\]");
    var regex = new RegExp("[\\?&]" + key + "=([^&#]*)");
    var qs = regex.exec(window.location.href);
    if (qs == null)
        return default_;
    else
        return qs[1];
}

function getQuerystring2(key, default_) {
    if (default_ == null) {
        default_ = "";
    }
    var search = unescape(location.search);
    if (search == "") {
        return default_;
    }
    search = search.substr(1);
    var params = search.split("&");
    for (var i = 0; i < params.length; i++) {
        var pairs = params[i].split("=");
        if (pairs[0] == key) {
            return pairs[1];
        }
    }
    return default_;
}

function get_win_size1() {
    if (parseInt(navigator.appVersion) > 3) {
        if (navigator.appName == "Netscape") {
            winW = window.innerWidth;
            winH = window.innerHeight;
        }
        if (navigator.appName.indexOf("Microsoft") != -1) {
            winW = document.body.offsetWidth;
            winH = document.body.offsetHeight;
        }
        win_height = winH
    }
}

function get_win_size() {
    var myWidth = 0, myHeight = 0;
    if (typeof (window.innerWidth) == 'number') {
        //Non-IE
        myWidth = window.innerWidth;
        myHeight = window.innerHeight;
    } else if (document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
        //IE 6+ in 'standards compliant mode'
        myWidth = document.documentElement.clientWidth;
        myHeight = document.documentElement.clientHeight;
    } else if (document.body && (document.body.clientWidth || document.body.clientHeight)) {
        //IE 4 compatible
        myWidth = document.body.clientWidth;
        myHeight = document.body.clientHeight;
    }
    win_height = myHeight;

    //window.alert( 'Width = ' + myWidth );
    //window.alert( 'Height = ' + myHeight );
}

function check_scroll() {

//    if (MainSeqNum == 0) {
//        window.scroll(0, 0);
//        state_ctl('check_scroll')
//        return;
//    }

    //  ############## Make it bidirectional ##############
    //

    if (ListOne == true || NoList == true) {
        state_ctl('check_scroll');
        return;
    }

       
    get_win_size();  // win_height
    var scroll_top = f_scrollTop();

    //var ItemVert = (listing_item_height * (MainSeqNum + 1)) + listing_top;

    var ItemVert = (listing_item_height * (MainSeqNum)) + listing_top;
    var BoxHeight = (UseMainSeq == true) ? heightary[MainSeqNum] + 200 : sub_heightary[SubSeqNum] + 200;
    //         GalCur = (extrefary[MainSeqNum] == 'None') ? false : true;

    var screen_top = scroll_top_now;
    var screen_btm = scroll_top_now + win_height;

    // ### Scroll Down ?? ###
    // If the ItemVert is within NN of the bottom of the screen
    // if the bottom of the box can't be seen # ItemVert + BoxHeight > screen_btm

    // How high can the box go? before scroll 
    //  ### Scroll Down ###
    // If the item is < 200 from the bottom
    // If the box bottom is off the screen
    if ((screen_btm - ItemVert) < 200 || (ItemVert - scroll_top_now) + BoxHeight > screen_btm) {
        scroll_amt = Math.abs(scroll_amt);
        scroll_win_center();
    }

    // ### Scroll Up ### 
    // need top check top also scroll_amt
    else if (ItemVert < scroll_top) {
        scroll_amt = -Math.abs(scroll_amt);
        scroll_win_center();
    }
    else state_ctl('check_scroll');
}

function scroll_win_center() {
    var Mylasttop = scroll_top_now;
    window.scrollBy(0, scroll_amt); // horizontal and vertical scroll increments
    // if it hasn't changed stop
    var scroll_top = f_scrollTop();
    if (scroll_top_now == Mylasttop) {
        state_ctl('check_scroll');
        return;
    }

    var screen_btm = scroll_top + win_height;
    var ItemVert = (listing_item_height * (MainSeqNum)) + listing_top;
    var BoxHeight = (UseMainSeq == true) ? heightary[MainSeqNum] + 200 : sub_heightary[SubSeqNum] + 200;

    // scroll down scroll_amt > 0
    // center it or show bottom
    if (scroll_amt > 0) {
        if ((screen_btm - ItemVert) < 200 || (ItemVert - scroll_top_now) + BoxHeight > screen_btm) {

            //if (ItemVert > scroll_top + (win_height / 2) || ItemVert + BoxHeight + 100 > screen_btm) { 

            scrolldelay = setTimeout('scroll_win_center()', 50); // scrolls every 100 milliseconds
        }
        else state_ctl('check_scroll');
    }
    // scroll up scroll_amt < 0  ##### don't scroll up past top if image
    if (scroll_amt < 0) {
        if (ItemVert < scroll_top) {

            //if (((listing_item_height * (MainSeqNum + 1)) + listing_top) < scroll_top + (win_height / 2)) {

            scrolldelay = setTimeout('scroll_win_center()', 50); // scrolls every 100 milliseconds
        }
        else state_ctl('check_scroll');
    }    
}

function position_slidebox() {

    if (ListOne == true || NoList == true) {
        //state_ctl('check_scroll');
        return;
    }
    
    var MyBoxTop = 0;
    var scroll_top = f_scrollTop();
    get_win_size();
    get_box_size();

    MyItemOver = (listing_item_height * (MainSeqNum + 1)) + listing_top;
//    var Item_Height = next_box_height + slider_top + slider_btm;
    var Item_Top = MyItemOver - (next_box_height / 2);
    var Item_Btm = MyItemOver + (next_box_height / 2);

    // The top of the screen is scroll_top and win_height
    // is the banner visible? add that portion visible to the screen top
    var screen_top = scroll_top;
    var screen_btm = scroll_top + win_height;

// ####### 
    if (scroll_top < listing_top) {
        screen_top = listing_top;
    }

    MyBoxTop = Item_Top;
    // Against the Bottom
    if (Item_Btm > (screen_btm - slider_min_btm)) {
        // the top of the box = scroll top +  (win_height - next_box_height)
        MyBoxTop = screen_btm - (next_box_height + slider_min_btm);
    }
    // Against the Top
    if (Item_Top < screen_top) {
        MyBoxTop = screen_top;
    }

    SLBox = document.getElementById("SlideBox")
    SLBox.style.top = MyBoxTop + 'px';
}


