var IsNetscape = (navigator.appName.indexOf("Netscape") != -1);
var px = document.layers ? "" : "px";
var PccLocation = 0;

var position_X = 195;
var UserPCC_Stop = 0;
var max_height = 0;



function getAnElement(id)
{
    return document.getElementById?document.getElementById(id):document.all?document.all[id]:document.layers[id];
}



function setPCCStart(startY)
{
    thePCC=getAnElement("divPCC");
    PCCtop=getAnElement("topBar");
    RMessages = getAnElement("rec-window");
    VMessages = getAnElement("val-error-window");
    Vclosebox = getAnElement('val-closebox');
    Vtextbox = getAnElement('val-text');
    if (VMessages)
        InitializeVMessageHeight();
    if (RMessages)
        InitializeRMessageHeight();

    if (thePCC)
    {
        PCCstop = getAnElement("bottomBar").offsetTop - thePCC.offsetHeight - 15;

        max_height = ((window.innerHeight==undefined || window.innerHeight < 1000) ? document.body.clientHeight :window.innerHeight) - 700;
        if (max_height==undefined || max_height < 0) max_height = 200;

        if (startY)
            thePCC.y = startY;
        else
            thePCC.y = 0;

        xPositionDivPcc(thePCC);
        setPCC();
    }
}

function xPositionDivPcc(pccDiv)
{

    //var startX = 830;
    window_width = (window.innerWidth==undefined ? document.body.clientWidth :window.innerWidth);

    if (window_width < 800) window_width = 800;
    var startX = window_width/2 + position_X;

    if ( ! (pccDiv) )
        pccDiv = getAnElement("divPCC");
    if (pccDiv)
    {
        var mOffset = getMastheadOffset();
        if ( mOffset > 0 )
        {
            startX = startX -11 + mOffset;
        }
        pccDiv.style.left = startX + 'px';
        pccDiv.x = startX;
    }
}

function getMastheadOffset ()
{
    var table = document.getElementById("masthead");
    if ( table == null )
    {
        return 0;
    }
    return getLeftOffset ( table );
}


function setPCC()
{
    var pY = (IsNetscape ? window.pageYOffset : (document.documentElement && document.documentElement.scrollTop) ? document.documentElement.scrollTop : document.body.scrollTop) + 15;

    UserPCC_Stop = CookieRead('cart_scroll');

    if (PccLocation == pY &&  UserPCC_Stop == 0)
    {
        if(thePCC.y > PCCstop)thePCC.y = PCCstop;
        else
        {
            var dY = (pY > PCCtop.offsetTop) ? pY : PCCtop.offsetTop;

            //alert(thePCC.style.top + ' : ' + initial_height);
            txtCp=getAnElement("txt-cp");
            if (txtCp) {
                max_height = getElementPosition('txt-cp')-500;
            }

            if ((dY - thePCC.y) < max_height) {
                thePCC.y += (dY - thePCC.y);
                thePCC.style.top=thePCC.y + px;

            }

            xPositionDivPcc(thePCC);

            if (VMessages)
            {
                if (VMessages.y)
                    VMessages.style.top = thePCC.y + VMessages.y +  px;
                else
                    VMessages.style.top = thePCC.y + px;
            }
            if (RMessages)
            {
                if (RMessages.y)
                    RMessages.style.top = thePCC.y + RMessages.y +  px;
                else
                    RMessages.style.top = thePCC.y + px;
            }
        }
    }else{
        xPositionDivPcc(thePCC);
    }

    PccLocation = pY;

    setTimeout("setPCC()", 250);
}


function getElementPosition(element) {
    var offsetTrail = (typeof element == 'string') ? document.getElementById(element) : element;
    var offsetLeft = 0;
    var offsetTop = 0;

    while (offsetTrail) {
        offsetLeft += offsetTrail.offsetLeft;
        offsetTop += offsetTrail.offsetTop;
        offsetTrail = offsetTrail.offsetParent;
    }

    if (navigator.userAgent.indexOf('Mac') != -1 && typeof document.body.leftMargin != "undefined") {
        offsetLeft += document.body.leftMargin;
        offsetTop += document.body.topMargin;
    }

    return (offsetTop);
}



function Position_stop(){
    UserPCC_Stop = CookieRead('cart_scroll');
    if(UserPCC_Stop == 0 ){
        UserPCC_Stop = 1;
    }else{
        UserPCC_Stop = 0;
    }

    CookieWrite("cart_scroll",UserPCC_Stop,30,"/");
}

function change_icon(flg){
    var i = document.getElementById('Icon');
    var b = document.getElementById('Open');

    var scroll_on_txt = 'スクロールオフ';
    var scroll_off_txt = 'スクロールオン';

    //var pb = document.getElementById('mylistBottom');
    var gif = flg ? '.gif' : '_on.gif';

    if(!i){
        return;
    }else if(flg || i.alt == scroll_on_txt){
        i.src = '/images/img_cart/scroll_on.gif';
        i.alt = scroll_off_txt;
        i.title = scroll_off_txt;
    }else if(i.alt == scroll_off_txt){
        i.src = '/images/img_cart/scroll_off.gif';
        i.alt = scroll_on_txt;
        i.title = scroll_on_txt;
    }
}

function init_icon(flg){
    var i = document.getElementById('Icon');
    var b = document.getElementById('Open');

    var scroll_on_txt = 'スクロールオフ';
    var scroll_off_txt = 'スクロールオン';

    //var pb = document.getElementById('mylistBottom');
    var gif = flg ? '.gif' : '_on.gif';

    if(!i){
        return;
    }else if(flg==1){
        i.src = '/images/img_cart/scroll_on.gif';
        i.alt = scroll_off_txt;
        i.title = scroll_off_txt;
    }else{
        i.src = '/images/img_cart/scroll_off.gif';
        i.alt = scroll_on_txt;
        i.title = scroll_on_txt;
    }
}