
var CHPaccheight = menutop = menuheight = totalBrands = count = 0;
var Pcounter = Pmenuwidth = Pmenuleft = 0;
var curBrand = 1;
var CHPps1 = CHPps2 = "";

function createMenus() {
	var menuNum = 0;
	var DIVs = document.all ? document.all.tags("div") : document.getElementsByTagName("div");
	for (d=0; d<DIVs.length; d++) {
		if (DIVs[d].className == "CHPmenu")
		{
			menuNum++;
			var addMenu = "<h3><a id='headlink" + menuNum + "' onclick='displayMenu(" + menuNum + ");dcsMultiTrack(\"WT.ad\",\"\",\"WT.ac\",\"ProductMenu" + menuNum + "\");return false;'";
			DIVs[d].innerHTML = DIVs[d].innerHTML.replace(/\<h3\>\<a/i,addMenu);
			
        }
    }
}

function OCunderline() {
    var leftPL = document.getElementById("CHPproductlinkleft");
    var rightPL = document.getElementById("CHPproductlinkright");
    if (leftPL.href.indexOf("#") != -1) leftPL.style.display = "none";
    if (rightPL.href.indexOf("#") != -1) rightPL.style.display = "none";
    if (leftPL.href.indexOf("#") != -1 || rightPL.href.indexOf("#") != -1) document.getElementById("CHPproductdivider").style.display = "none";
	
	 var AI = document.getElementById("CHPadditionalinformation").getElementsByTagName("h2");
        if (AI.length <= 0) {
        	document.getElementById("CHPnewsinnerwrapper").style.borderTopWidth="0";
        	document.getElementById("CHPadditionalinformation").style.display="none";
        	document.getElementById("CHPnewsouterwrapper").style.backgroundImage="url(\"/3MContentRetrievalAPI/BlobServlet?univid=1180600377984&fallback=true&assetType=MMM_Image&blobAttribute=ImageFile\")";
        	document.getElementById("CHPnewsouterwrapper").style.backgroundRepeat="repeat-x";
        	document.getElementById("CHPnewsinnerwrapper").style.paddingTop="2";
        }
}

function displayMenu(x) {
    
    var expanded = false;
    if (CHPaccheight == 0) {
	    CHPaccheight = document.getElementById("CHPmenuH").offsetHeight;
	    CHPacctitleheight = 16;
	    CHPaccpagetitleheight = document.getElementById("CHPpagetitle").offsetHeight;
	    CHPaccordianheight = CHPaccheight - CHPaccpagetitleheight;
	    CHPaccmenuheight = CHPaccheight - CHPacctitleheight - CHPaccpagetitleheight;
	    document.getElementById("CHPaccordian").style.height = CHPaccordianheight + "px";
	    document.getElementById("CHPaccMenu").style.height = CHPaccmenuheight + "px";
	}
	
    if (x) {
        if (document.getElementById("CHPprod"+x).className == "CHPmenuON") {
            expanded = true;
        }

        var As = document.getElementById("CHPprod" + x).getElementsByTagName("a");
        if (As.length > 0) {
            if (As[0].href == "#" || As[0].href == window.location.href + "#") {
                As[0].className = "CHPMenuHide";
            }
        }

        var H3s = document.getElementById("CHPprod" + x).getElementsByTagName("h3");
        if (H3s.length > 0) {
            document.getElementById("CHPaccMenuTitle").innerHTML = H3s[0].innerHTML.replace("onclick","oclick");
            H3s[0].className = "CHPMenuHide";
        }

   
    }

    var DIVs = document.all ? document.all.tags("div") : document.getElementsByTagName("div");
       
	for (d=0; d<DIVs.length; d++)
		if (DIVs[d].className == "CHPmenuON")
			DIVs[d].className = "CHPmenu";

	if (x)
	{
	    if (expanded) {
	        document.getElementById("CHPaccordian").style.display = "none";
	        document.getElementById("CHPaccordianshadow").style.display = "none";
	        
	    } else {
		    document.getElementById("CHPprod"+x).className = "CHPmenuON";
		    
		    document.getElementById("CHPaccMenu").innerHTML = document.getElementById("CHPprod" + x).innerHTML.replace("displayMenu(", "h3Link(this,");
		    document.getElementById("CHPaccMenu").scrollTop=0;
		    
		    if (document.getElementById("CHPaccordian").style.display != "block")
			    setTimeout("animate(" + new Date().getTime() + "," + TimeToSlide + ")", 33);
	    }
    } else {
        document.getElementById("CHPaccordian").style.display = "none";
        document.getElementById("CHPaccordianshadow").style.display = "none";
	}
}

function h3Link(x)
{
	location = x;
}

function slideBar()
{
}


var ContentWidth = 255;
var TimeToSlide = 300;

function animate(lastTick, timeLeft)
{
	var curTick = new Date().getTime();
	var elapsedTicks = curTick - lastTick;
	var opening = document.getElementById("CHPaccordian");

	if (timeLeft <= elapsedTicks)
	{
		timeLeft = elapsedTicks = lastTick = 0;
		opening.style.width = ContentWidth + "px";
		document.getElementById("CHPaccordianshadow").style.left = ContentWidth + "px";
		return;
	}

	timeLeft -= elapsedTicks;
	var newClosedWidth = Math.round((timeLeft/TimeToSlide) * ContentWidth);

	if (opening != null)
	{
		if (opening.style.display != "block") opening.style.display = "block";
		opening.style.width = (ContentWidth - newClosedWidth) + "px";
		document.getElementById("CHPaccordianshadow").style.left = opening.style.width;
		document.getElementById("CHPaccordianshadow").style.display = "block";
}
	
	setTimeout("animate(" + curTick + "," + timeLeft + ")", 1);
}




/*function Protate(x)
{
	if (Pmenuwidth == 0)
	{
	    var Pthree = document.getElementById("CHPmultiplyP").innerHTML;
	    Pmenuwidth = document.getElementById("Pitems").offsetWidth;
		Pmenuleft = 0 - 2*Pmenuwidth;
		document.getElementById("CHPpromomove").innerHTML = "<table cellpadding='0' cellspacing='0' border='0'><tr>" + Pthree + Pthree + Pthree + "</tr></table>";
	}
	
	if (x == "lt") Pmenuleft += 342;
	else if (x == "rt") Pmenuleft -= 342;
	if (Pmenuleft < -2*Pmenuwidth) Pmenuleft += Pmenuwidth;
	if (Pmenuleft > -Pmenuwidth) Pmenuleft -= Pmenuwidth;
	document.getElementById("CHPpromomove").style.marginLeft = Pmenuleft + "px";
	if (document.getElementById("CHPpromoleft") != null) document.getElementById("CHPpromoleft").style.paddingTop = ((document.getElementById("CHPpromomove").clientHeight / 2) - 20) + "px";
	if (document.getElementById("CHPpromoright") != null) document.getElementById("CHPpromoright").style.paddingTop = ((document.getElementById("CHPpromomove").clientHeight / 2) - 20) + "px";
}
*/
var Bcounter = Bmenuwidth = Bmenuleft = 0;


function Brotate(x,y)
{
	if (Bmenuwidth == 0)
	{
	    var Bthree = document.getElementById("CHPmultiplyB").innerHTML;
		Bmenuwidth = document.getElementById("Bitems").offsetWidth;
		document.getElementById("CHPbrandsmove").innerHTML = "<table cellpadding='0' cellspacing='0' border='0'><tr>" + Bthree + Bthree + Bthree + "</tr></table>";
	}

	if (y == 0) document.getElementById("CHPbrandstage").style.display = "none";
	if (x == "lt") Bmenuleft += 170;
	else if (x == "rt") Bmenuleft -= 170;
	if (Bmenuleft < -2*Bmenuwidth) Bmenuleft += Bmenuwidth;
	if (Bmenuleft > -Bmenuwidth) Bmenuleft -= Bmenuwidth;

	document.getElementById("CHPbrandsmove").style.marginLeft = Bmenuleft + "px";
}

/*FWIP code*/
Scroller = Class.create({
    defaults: {
      size:2,
      duration:800,
      autoscroll:false
    },
    initialize: function(element, options) {
        this.element  = $(element);
        this.options  = (options != null) ? options : this.defaults;
        this.pageSize = this.options.size;
        this.duration = this.options.duration;
        this.auto     = this.options.autoscroll;
        this.scroller = this.element.down('div.items');
        this.items    = this.scroller.childElements();
        this.locked   = false;
        this.arrowL   = this.element.down('a.prev');
        this.arrowR   = this.element.down('a.next');
        this.positions= [];
        this.posIndex = 1;
        
        if (this.items.length > this.options.size) {
            this.pageWidth = this.items[0].offsetWidth;
            if (this.options.size == 2) {
              this.scroller.insert({ bottom:'<div class="'+this.items[0].className+'">'+this.items[0].innerHTML+'</div>' });
              this.scroller.insert({ top:'<div class="'+this.items[0].className+'">'+this.items[this.items.length-1].innerHTML+'</div>' });
              this.items.unshift(this.items.last());
              for (var i=0; i < this.items.length; i++) {
                var position = -this.pageWidth * i;
                this.positions.push(position);
              };
              this.items.push(this.items[1]);
            } else if (this.options.size == 4) {
              this.scroller.insert({ bottom:'<div class="'+this.items[0].className+'">'+this.items[0].innerHTML+'</div>' });
              this.scroller.insert({ bottom:'<div class="'+this.items[0].className+'">'+this.items[1].innerHTML+'</div>' });
              this.scroller.insert({ top:'<div class="'+this.items[0].className+'">'+this.items[this.items.length-1].innerHTML+'</div>' });
              this.scroller.insert({ top:'<div class="'+this.items[0].className+'">'+this.items[this.items.length-2].innerHTML+'</div>' });
              this.items.unshift(this.items[this.items.length-2], this.items[this.items.length-1]);
              for (var i=0; i < this.items.length-1; i++) {
                var position = -this.pageWidth * i;
                this.positions.push(position);
              };
              this.items.push(this.items[1]);
              this.items.push(this.items[2]);
              this.posIndex = 2;
            } else {
              if (!window.console || !console.firebug){ // CONSOLE LOGGING FOR IE
                  var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group", "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
                  window.console = {};
                  for (var i=0; i<names.length; ++i) window.console[names[i]] = function(){}
              }
              console.log("Size must be 2 or 4");
            }
            this.scroller.setStyle({ width: (this.pageWidth*this.items.length)+'px' });
            this.scroller.setStyle({ left: this.positions[this.posIndex]+'px' });
            this.arrowR.observe('click', this.next.bindAsEventListener(this));
            this.arrowL.observe('click', this.prev.bindAsEventListener(this));
        }
        else {
        	this.arrowR.innerHTML = "<img src='/wps/themes/html/3M.com/pxClr.gif' height='42' width='20' border='0' />";
	        this.arrowL.innerHTML = "<img src='/wps/themes/html/3M.com/pxClr.gif' height='42' width='20' border='0' />";
        }
        
        this.equalHeight(this.items.concat(this.element, this.scroller, this.element.down('.scrollable')));
        
        if (this.auto) {
            this.rand = Math.round(Math.random() * 10) * 1000 + (2000 * this.items.length);
            this.autoScroll = setInterval(this.next.bind(this), 2000); 
            setTimeout(function() {
                stopScroll();
            }, this.rand);
        };
        this.element.observe('mouseover', function(){
            stopScroll();
        });
        var stopScroll = function() {
            clearInterval(Scroller.features.autoScroll);
            //clearInterval(Scroller.brands.autoScroll);
        }
    },
    equalHeight: function(group) {
        tallest = 0;
        group.each(function(el) {
            thisHeight = el.getHeight();
            if(thisHeight > tallest) {
                tallest = thisHeight;
            }
        });
        group.invoke('setStyle', { height:tallest+'px' });
        this.centerVertically(tallest);
    },
    centerVertically: function(tallest) {
      var arrowheight = this.arrowL.getHeight();
      var pad = ((tallest + 15) / 2) - 21;
      this.arrowL.setStyle({ paddingTop:pad+'px' });
      this.arrowR.setStyle({ paddingTop:pad+'px' });
    },
    next: function(event) {
      if ((event) && typeof event != 'number') event.stop();
      if (this.posIndex == this.positions.length-1 && !this.locked) {
        this.posIndex = 0;
        this.updateScroller(true, 'next');
      } else if (!this.locked) {
        this.posIndex++;
        this.updateScroller(false, 'next');
      }
    },
    prev: function(e) {
      e.stop();
      if (this.posIndex == 0 && !this.locked) {
        this.posIndex = this.positions.length-1;
        this.updateScroller(true, 'prev');
      } else if (!this.locked) {
        this.posIndex--;
        this.updateScroller(false, 'prev');
      }
    },
    updateScroller: function(reset, dir) {
      if (reset == true && dir == 'next') {
        this.scroller.setStyle({ left:this.positions[this.posIndex] });
        this.posIndex++;
      } else if (reset == true && dir == 'prev') {
        this.scroller.setStyle({ left:this.positions[this.positions.length-1]+'px' });
        this.posIndex--;
      }
      if (!this.locked) {
        this.lock();
        var effect = new Fx.Style(this.scroller, 'left', {duration: this.duration, onComplete: this.unlock.bind(this)});
            effect.custom(this.scroller.offsetLeft, this.positions[this.posIndex]);
      }
    },
    lock: function() {
        this.locked = true;
    },
    unlock: function() {
        this.locked = false;
    }
});

function loadPage()
{
    if (document.getElementById('corpfooters') != null) {
        createMenus();
        /*Protate();
	    if (Pmenuwidth <= (document.getElementById("CHPpromocarousel").offsetWidth + 1) && document.getElementById("CHPpromoright")) {
	        document.getElementById("CHPpromoleft").innerHTML = "&nbsp;";
	        document.getElementById("CHPpromoright").innerHTML = "&nbsp;";
	    }*/
	    try {
	    	Brotate();
	    }
	    catch(e) {
	    	//catch and supress error
	    }
	    if (location.href.toString().indexOf("menu") != -1) {toggleMenuB();displayMenu(1);}
	    if (location.href.toString().indexOf("menu") != -1) displayMenu(1);
		try {
	    	OCunderline();
	    }
	    catch(e) {
	    	//catch and supress error
	    }
    } else {
        setTimeout("loadPage();",500);
    }
}

function SwitchBrandsAndPromos() {
    if (document.getElementById("CHPPromos") != null && document.getElementById("CHPBrands") != null) {
        var oldTop = document.getElementById("CHPPromos").innerHTML;
        document.getElementById("CHPPromos").innerHTML = document.getElementById("CHPBrands").innerHTML;
        document.getElementById("CHPBrands").innerHTML = oldTop;
    }
    
}
document.onload = setTimeout("loadPage();",1);
