// ----- BROWSER DETECTION ----- //

function UA() {
        var agt = navigator.userAgent.toLowerCase();
        var version = parseInt(navigator.appVersion);
        this.isIE=(agt.indexOf('msie') != -1) ? true : false;
        if( this.isIE ) {
                this.isIE4 = ((version==4) && (agt.indexOf("msie 4.") != -1));
                this.isIE4up = (version >= 4 );
                this.isIE5 = ((version==4) && (agt.indexOf("msie 5.") != -1));
                this.isIE5_0 = ((version==4) && (agt.indexOf("msie 5.0") != -1));
                this.isIE5_5 = ((version==4) && (agt.indexOf("msie 5.5") != -1));
                this.isIE5up = !this.isIE4;
                this.isIE6 = ((version==4) && (agt.indexOf("msie 6.") != -1));
                this.isIE6up = ( !this.isIE4 && !this.isIE5 );
        } else {
                this.isIE4 = false;
                this.isIE5 = false;
                this.isIE6 = false;
                this.isIE4up = false;
                this.isIE5up = false;
                this.isIE6up = false;
        }
        this.isNS = (
                        (agt.indexOf("mozilla") !=-1) &&
                        (agt.indexOf("compatible") == -1) );
        if( this.isNS ) {
                this.isNS3 = (version == 3);
                this.isNS4 = (version == 4);
                this.isNS4up = (version >= 4);
                this.isNS6 = (version == 5);
                this.isNS6up = (version >= 5);
        } else {
                this.isNS4 = false;
                this.isNS6 = false;
                this.isNS4up = false;
                this.isNS6up = false;
        }

        this.isMac = (agt.indexOf("mac") != -1);
}

var ua = new UA();

// ----- CURSOR ----- //

var cursorPageX, cursorPageY;
var cursorWinX, cursorWinY;

function captureCursor(watchFunc) {
        if(cursorCaptured_ == 0) {
                if(ua.isNS) document.captureEvents(Event.MOUSEMOVE);
                document.onmousemove = updateCursorPos_;
        }
        if(! isBlank(watchFunc) ) {
                var i=0;
                while( i<cursorWatchers_.length ) {
                        if( cursorWatchers_[i]=='' ) { break; }
                        i++;
                }
                cursorWatchers_[i] = watchFunc;
        }
        cursorCaptured_++;
}

function releaseCursor(watchFunc) {
        cursorCaptured_--;
        if(cursorCaptured_==0) {
                if(ua.isNS) document.releaseEvents(Event.MOUSEMOVE);
                document.onmousemove = '';
        }
        if(! isBlank(watchFunc) ) {
                for(var i=0; i<cursorWatchers_.length; i++) {
                        if( cursorWatchers_[i] == watchFunc ) {
                                cursorWatchers_[i] = '';
                                break;
                        }
                }
        }
}
   
var cursorCaptured_ = 0;
var cursorWatchers_ = new Array();

function updateCursorPos_(evt) {
        if (ua.isIE) {
                cursorWinX = event.clientX - 2;
                cursorWinY = event.clientY - 2;
                cursorPageX = cursorWinX + document.body.scrollLeft;
                cursorPageY = cursorWinY + document.body.scrollTop;
        } else {
                cursorPageX = evt.pageX;
                cursorPageY = evt.pageY;
                cursorWinX = cursorPageX - window.pageXOffset;
                cursorWinY = cursorPageY - window.pageYOffset;
        }
        for(var i=0; i<cursorWatchers_.length;i++) {
                cursorWatchers_[i](cursorPageX,cursorPageY,cursorWinX,cursorWinY);
        }
}

// ---- TIPS ---- //

var tipLayer_ = '';
var tipActive_ = false;

function startTip(tipObj) {
        if( isBlank(tipObj) ) { return; }
        if( typeof(tipObj) == 'string' ) {
                tipObj = getLayerObj(tipObj);
                if( isBlank(tipObj) ) { return; }
        }
        endTip();
        tipLayer_ = tipObj;
        captureCursor(updateTip_);
}

function endTip() {
        if(! isBlank(tipLayer_) ) {
                releaseCursor(updateTip_);
                hideLayer(tipLayer_);
                tipLayer_ = '';
        }
        tipActive_ = false;
}

function updateTip_(cX,cY,wX,wY) {
        var lw = getLayerWidth(tipLayer_);
        var lh = getLayerHeight(tipLayer_);
        var ww = getWinWidth();
        var wh = getWinHeight();
        var x = cX + 15;
        if( (wX+5+lw) > ww ) { x = cX - 5 - lw; }
        var y = cY + 15;
        if( (wY+5+lh) > wh ) { y = cY - 5 - lh; }
        moveLayerTo(tipLayer_,x,y);
        if(! tipActive_ ) {
                tipActive_ = true;
                showLayer(tipLayer_);
        }
}


// ----- LAYER OBJECT ----- //

function getLayerObj(layerID_) {
        if (ua.isIE4) {
                return document.all[layerID_];
        }
        if (ua.isNS4) {
                var tempLayerObj = null;
                var tempParentObj = (arguments.length == 1) ? document : arguments[1];
                for (var tempLayerLoop in tempParentObj.layers) {
                        var tempObj = tempParentObj.layers[tempLayerLoop];
                        var tempConstructor = tempObj.constructor + '';
                        if (tempConstructor.indexOf('function Layer()') != -1) {
                                if (tempLayerLoop == layerID_) {
                                        return tempObj;
                                }
                                if (tempObj.document.layers.length > 0) {
                                        tempLayerObj = getLayerObj(layerID_,tempObj);
                                }
                        }
                }
                return tempLayerObj;
        }
        return document.getElementById(layerID_);
}

// ----- LAYER DIMENSIONS ----- //

function getLayerHeight(layerObj) {
        if(ua.isIE4) return layerObj.clientHeight;
        if(ua.isNS4) return layerObj.clip.height;
        var val = parseInt(layerObj.style.height);
        if( isNaN(val) ) {
                return layerObj.offsetHeight;
        }
        return val;
}

function getLayerWidth(layerObj) {
        if(ua.isIE4) return layerObj.clientWidth;
        if(ua.isNS4) return layerObj.clip.width;
        var val = parseInt(layerObj.style.width);
        if( isNaN(val) ) {
                return layerObj.offsetWidth;
        }
        return val;
}

function setLayerSize(layerObj,width,height) {
        if (ua.isIE4) {
                if (!isBlank(width)) layerObj.style.pixelWidth = width;
                if (!isBlank(height)) layerObj.style.pixelHeight = height;
        } else if (ua.isNS4) {
                if (!isBlank(width)) layerObj.clip.right = width;
                if (!isBlank(height)) layerObj.clip.bottom = height;
        } else {
                if (!isBlank(width)) layerObj.style.width = width + 'px';
                if (!isBlank(height)) layerObj.style.height = height + 'px';
        }
}

// ----- LAYER POSITIONING ----- //

function getLayerLeft(layerObj) {
        if (ua.isNS4) return layerObj.left;
        return parseInt(layerObj.style.left);
}

function getLayerTop(layerObj) {
        if (ua.isNS4) return layerObj.top;
        return parseInt(layerObj.style.top);
}

function moveLayerBy(layerObj,distX,dist) {
        if (ua.isNS4) {
                if (isBlank(distX)) distX = 0;
                if (isBlank(distY)) distY = 0;
                layerObj.moveBy(distX,distY);
        } else {
                if (!isBlank(distX)) layerObj.style.left = (parseInt(layerObj.style.left) + distX) + 'px';
                if (!isBlank(distY)) layerObj.style.top = (parseInt(layerObj.style.top) + distY) + 'px';
        }
}

function moveLayerTo(layerObj,posX,posY) {
        if (ua.isNS4) {
                if (isBlank(posX)) posX = getLayerLeft(layerObj);
                if (isBlank(posY)) posY = getLayerTop(layerObj);
                layerObj.moveTo(posX,posY);
        } else {
                if (!isBlank(posX)) layerObj.style.left = posX + 'px';
                if (!isBlank(posY)) layerObj.style.top = posY + 'px';
        }
}

// ----- LAYER VISIBILITY ----- //

function hideLayer(layerObj) {
        if (ua.isNS4) {
                layerObj.visibility = 'hide';
        } else {
                layerObj.style.visibility = 'hidden';
        }
}

function showLayer(layerObj) {
        if (ua.isNS4) {
                layerObj.visibility = 'show';
        } else {
                layerObj.style.visibility = 'visible';
        }
}

// ----- WINDOW ----- //

function getWinHeight() {
        if(ua.isIE4up) return document.body.clientHeight;
        return window.innerHeight;
}

function getWinWidth() {
        if(ua.isIE4up) return document.body.clientWidth;
        return window.innerWidth;
}

// ----- UTILS ----- //

function isBlank(val) {
        if (val == null) val = '';
        val += '';
        val = val.replace(/^\s+$/g,'');
        return (val == '') ? true : false;
}

// ---- COOKIES ---- //
function setCookieValue(name,value) {
        document.cookie=name+"="+value+";"
}

function getCookieValue(name) {
         var temp=document.cookie+";";
         var pos=temp.indexOf(name+"=");
         if (pos==-1) { return ""; }
         pos=temp.indexOf("=",pos);
         var val = temp.substring(pos+1,temp.indexOf(";",pos));
         return val;
}

// ---- SKU WINDOW ---- //

var popUpWindow = "";

function unloadHandler() {
        if( popUpWindow != "" ) {
                popUpWindow.close();
        }
        return true;
}

// ---- BACK/FORWARD BROWSING ---- //

function redirectIfModified() {
        var mods = getCookieValue("cartMod");

        // if no cookies we assume we are ok
        if( mods=="" ) { return; }
        if( mods != myCartModCount ) {

    // turn off exit popup for redirect
    bLoadExitPopup=false;

                var url = myReloadURL;
                document.location.replace(url);
        }
}

function loadHandler() {
        // redirect if we need to
        redirectIfModified();

        return true;
}

function maybeLoadExitPopup(url,name,options){
    if (bLoadExitPopup){ // && !bReloadEvent
      popunder = window.open(url,name,options);
      popunder.focus();
   }
}



