/************************Mootools turned into notification bar********************/
var autoClose;
var delayMe;
var noticeBar;
var frameFx;
var currentAlert = "optin";

function closeBar(){
	if(currentAlert == "optin"){
		var myCookie = Cookie.read('JS_C');
		var cookieVals = myCookie.split(",");
		var newCookieVal = cookieVals[0] + "," + 1;
		for(i=2; i<cookieVals.length; i++){
		newCookieVal = newCookieVal + "," + cookieVals[i];
		}
		myCookie = Cookie.write("JS_C", newCookieVal, {domain: '.step2.com', path: '/', duration: 14});
	}
	noticeBar.start({'height':0});
	frameFx.start({'margin-top':10});
	}

function popNotice(myText,typeText,autoExpire,alertName){
	if(typeof alertName != 'undefined') currentAlert = alertName;
	clearTimeout(autoClose);
	// if it's open, we're gonna close it before changing
	if(parseInt($('notificationBar').style.height) > 0){
		closeBar();
		delayMe = setTimeout(function(){popNotice(myText,typeText,autoExpire)},250);
		}

	// script for now allows for types of 'question','alert', and 'savings'
	else{
		clearTimeout(delayMe);
		$('nBarText').innerHTML = myText;
		if (typeText == "question") $('iconID').src = "/images/graphics/icons/questionMark.gif";
		else if (typeText == "alert") $('iconID').src = "/images/graphics/icons/alertMark.gif";
		else if (typeText == "savings") $('iconID').src = "/images/graphics/icons/dollarMark.gif";
		noticeBar.start({'height':30});
		frameFx.start({'margin-top':40});
		if(autoExpire != 0) autoClose = setTimeout("closeBar()",autoExpire);
		}
	}

/*************** load the login box later, it's less important *************/
function loadLogin(){
	var loginIframe = new Element('iframe', {
	    'src': 'https://secure.step2.com/dhtml_login.cfm',
	    'id': 'dhtml_login',
	    'scrolling': 'no',
		'frameborder' : 0,
	    'styles': {
			'float' : 'right',
			'width' : '255px',
			'height' : '19px',
			'padding' : 0,
			'margin' : 0,
			'border' : 0
			},
		'events' : {'load' : function(){
								$('loginBox').grab($('dhtml_login'),'top');
								if($chk($('loginPlaceHolder'))) $('loginPlaceHolder').destroy();
								}
					}
		});
	$('loginPreloader').grab(loginIframe);
	}

function initForm(){
	pfield = document.getElementById('password');
	if($chk($('pfield')) && pfield.value == "") pfield.style.backgroundPosition = "2px 2px";
	ufield = document.getElementById('userName');
	if($chk($('ufield')) && ufield.value =="") ufield.style.backgroundPosition = "2px -16px";
	}
/************************ Std window popups ********************/
function createImageDetailWindow(productid,whichthumb){ var xWin = window.open('/popinfo/imgdetails.cfm?product_id='+productid+'&amp;whichthumb='+whichthumb,'window2','height=450,width=450,scrollbars,resizable');};
function createPrivacyWindow(){var xWin = window.open('/privacy/index.cfm','window2','height=450,width=700,scrollbars,resizable');};
function createWhySignupWindow(){var xWin = window.open('/whysignup.cfm','window2','height=450,width=700,scrollbars,resizable');};
function productDemo(partnumber){var xWin = window.open('/3d_demos/index.cfm?partnumber='+partnumber,'window2','height=350,width=570,scrollbars,resizable');};
function sourceCodeInfo(msgtype){var xWin = window.open('/direct/sourceinfo.cfm?msgtype='+msgtype,'window2','height=450,width=450,scrollbars,resizable');};
function shipInfo(msgtype,shiptype){var xWin = window.open('/popinfo/shipinfo.cfm?msgtype='+msgtype+'&shiptype='+shiptype,'window2','height=450,width=700,scrollbars,resizable');};
function whatsThis(whatis,state){var xWin = window.open('/popinfo/whatsthis.cfm?whatis='+whatis+'&state='+state,'window2','width=450,height=450,scrollbars,resizable');};
function createEmailAccountInfoWindow(){var xWin = window.open('/direct/sendaccountinfo.cfm','window2','width=450,height=250,scrollbars,resizable');};
function createAboutPriceWindow(){var xWin = window.open('/popinfo/aboutthisprice.cfm','window2','width=450,height=250,scrollbars,resizable');};
function createFlashCatalogWindow(){window.name = "catalogparent";	var xWin = window.open("/flashcatalog/index.cfm","FlashCatalog","height=610,width=1050,scrollbars,resizable");};
function createBMLWindow(){var xWin = window.open('https://www.securecheckout.billmelater.com/paycapture-content/fetch?hash=RT225W2P&content=/bmlweb/coreiw.html','window2','width=515,height=600,scrollbars,resizable');};

/******************************** generic vertical load defaults and collapse *************************/
var myMenuHeights = new Array();
function collapseMenus(){
	myMenusAre = $$('.collapsible');
	for(i = 0; i < myMenusAre.length; i++){
		myMenuHeights[myMenusAre[i].id] = new Array();
		arrayElement = myMenuHeights[myMenusAre[i].id];
		arrayElement['height'] = myMenusAre[i].getScrollSize().y;
		arrayElement['padding'] = myMenusAre[i].getStyle('padding');
		arrayElement['margin'] = myMenusAre[i].getStyle('margin');
		myMenusAre[i].set('styles', {'height': 0, 'overflow': 'hidden', 'padding-top':0, 'margin-top':0, 'padding-bottom':0, 'margin-bottom':0});
		}
	}

/********************************** generic vertical collapse and restor *************************/
function expandMenu(element,needsToOpen){
	if(needsToOpen == 0){
		element.morph({height:0, 'padding-top':0, 'margin-top':0, 'padding-bottom':0, 'margin-bottom':0});
		}
	else {
		setTo=myMenuHeights[element.id];
		element.morph({height:setTo['height'],padding:setTo['padding'],margin:setTo['margin']});
		}
	};

		
// left menu implementation
function toggleMenu(idName){
	toggleThis = $(idName);
	toggleHead = $(idName + "Header");
	if(parseInt(toggleThis.style.height) > 0){
		toggleHead.style.backgroundImage = "url(/images/graphics/fancyBG2.gif)";
		expandMenu(toggleThis,0);
		}
	else{
		toggleHead.style.backgroundImage = "url(/images/graphics/fancyBGexpanded.gif)";
		expandMenu(toggleThis,1);		
		}
	}

// right boxes implementation
var opennav = "optinNav";
function openthis(idName){
	if(idName != opennav){
		toOpen = $(idName);
		toClose = $(opennav);
		opennav = idName;
		expandMenu(toOpen,1);
		expandMenu(toClose,0);
		}
	}

function optinNow(key,source){
	var myRequest = new Request({method: 'post', url: '/HTTP_Utilities/optinHandler.cfm'});
	myRequest.send('key=' + key + '&source=' + source);
	popNotice('You have been added to our email newsletter!','alert',1500,'successMsg');
}

/****************** TOP banner functions ********************/
function startLoadingBanner(){
	pic1 = new Element('img', {
	id : 'bannerImg',
	style	: {
				width : 798,
				height : 113
				}
	});
	$('imageLoader').grab(pic1);
	pic1.src = bannerBackgroundURL;
	pic1.addEvent('load', function(){replaceBanner();});
	}

function replaceBanner(){
	bannerDiv = document.getElementById('banner').style;
	bannerDiv.backgroundImage = "url('" + bannerBackgroundURL + "')";
	bannerDiv.backgroundRepeat = "no-repeat";
	bannerDiv.backgroundPosition = "0px 0px";
	}

	
/*********************** still use the old method for swap on homepage.  could be reworked with mootools ************/

var swapdirection=1;
var defaultrotationspot = 1;
var protectauto = 0;
	function intRot()
		{
		olddiv = eval('product'+defaultrotationspot);
		newrotspot = defaultrotationspot+1;
		newdiv = eval('product'+newrotspot);
		protectauto = 1;
		defaultrotationspot=newrotspot;
		if(defaultrotationspot==5){
			defaultrotationspot=1;
			}
		swapmulti(olddiv,newdiv);
		};

//quit calling the dom so much!
var product0;
var product1;
var product2;
var product3;
var product4;
var product5;
	function startRotation(){
		product0=document.getElementById('product4');
		product1=document.getElementById('product1');
		product2=document.getElementById('product2');
		product3=document.getElementById('product3');
		product4=product0;
		product5=product1;
		autobot = setInterval("intRot()",4000);
		};
	

	function swapmulti(originaldiv, newdiv)
			{	
				if(newdiv==product0){newdiv=product4;}
				else if(newdiv==product5){newdiv=product1;}
				//top div is the old div, which will fade out
				//bottom div is the new div, which is merely covered up

				if(protectauto!=1){
				clearInterval(autobot);
				}
				protectauto=0;
				
				// move the old div in front and show the new div behind it
				$(originaldiv).setStyle('zIndex', 3);
				$(newdiv).setStyles({zIndex: 1, opacity: 1, display: 'block'});
				
				//now, fade the old div away.  display: none; when it's done.
				var fadeThisDiv = new Fx.Morph(originaldiv, {duration: 500, transition: 'linear'});
				fadeThisDiv.start({'opacity': 0});
				t = setTimeout(function(){originaldiv.style.display = 'none';},500);
			};


	function fademulti(changingDiv,staticDiv)
			{
			};

//Determines the proper ajax method			
var request = false;
   try {
     request = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request = false;
       }  
     }
   }

   if (!request)
     alert("Error initializing XMLHttpRequest!");

//Why not just request2 = request?  because safari sucks.
var request2 = false;
   try {
     request2 = new XMLHttpRequest();
   } catch (trymicrosoft) {
     try {
       request2 = new ActiveXObject("Msxml2.XMLHTTP");
     } catch (othermicrosoft) {
       try {
         request2 = new ActiveXObject("Microsoft.XMLHTTP");
       } catch (failed) {
         request2 = false;
       }  
     }
   }

   if (!request)
     alert("Error initializing XMLHttpRequest!");
	 
var getmainURL = "";
var thrutop = 0;

//Scrolls through reviews.  Updates reviews via Ajax and instantly updates x-x of x header/footer
function ajaxNextPage(prodID,sortBy,sortOrder,startNum,maxRev){
	// paste in animated throbber
	document.getElementById('ajaxReviewSpace').innerHTML = "<div style=\"text-align:center;border-bottom:1px solid #cccccc;\"><img src=\"/images/graphics/rotatingthingyOriginal.gif\" alt=\"Reviews are loading\" border=0 width=300 height=70 align=\"center\" style=\"padding:15px;\" /></div>";

	// set up ajax request
	getmainURL = 'http://www.step2.com/ajaxGrabReviews.cfm?id=' + prodID + '&sortby=' + sortBy + '&direction=' + sortOrder + '&startpoint=' + startNum + '&totalreviewsnum=' + maxRev;
	request.open("GET", getmainURL, true);
	request.onreadystatechange = updateRevs;
	request.send(null);

	if((startNum + 9) > maxRev){thrutop = maxRev;}
	else {thrutop = startNum + 9;}
	replaceTopNav = "<span class=\"prPageCount\">Displaying Reviews <strong>" + startNum + "-" + thrutop + "</strong> of " + maxRev + "</span><span class=\"prPageNav\">";
		 if(startNum > 10){
		 	replaceTopNav = replaceTopNav + "<a href=\"#ReviewHeader\" onclick=\"ajaxNextPage(" + prodID + ",'" + sortBy + "','" + sortOrder + "'," + (startNum - 10) + "," + maxRev + "); return false;\">&laquo; Previous</a>&nbsp;|&nbsp;";
			}
 		else {
			replaceTopNav = replaceTopNav + "Previous&nbsp;|&nbsp;";
			} 
		if((startNum + 10) < maxRev){
			replaceTopNav = replaceTopNav + "<a href=\"#ReviewHeader\" onclick=\"ajaxNextPage(" + prodID + ",'" + sortBy + "','" + sortOrder + "'," + (startNum + 10) + "," + maxRev + "); return false;\">Next &raquo;</a></span>";
			} 
		else {
			replaceTopNav = replaceTopNav + "Next</span>";
			}
	document.getElementById('ajaxTopNav').innerHTML = replaceTopNav;
	document.getElementById('ajaxBottomNav').innerHTML = replaceTopNav;	
	};

// When ajax data comes back, paste it into the website.
function updateRevs(){
		if (request.readyState == 4){
       if (request.status == 200){
			var response = request.responseText;
			document.getElementById('ajaxReviewSpace').innerHTML = response;
			}
		}

	};
	
// Handles the select box to sort reviews
function ajaxReviewSort(sortKey,prodID,maxRev){
	var midKey = "";
	if(sortKey=="date_new"){
		sB = "createddate";
		sO = "DESC";
		}
	else if (sortKey=="date_old"){
		sB = "createddate";
		sO = "ASC";
		}
	else if (sortKey=="rating_high"){
		sB = "rating";
		sO = "DESC";
		}
	else if (sortKey=="rating_low"){
		sB = "rating";
		sO = "ASC";
		}
	else if (sortKey=="helpful_high"){
		sB = "helpful_votes";
		sO = "DESC";
		}
	else if (sortKey=="helpful_low"){
		sB = "not_helpful_votes";
		sO = "DESC";
		}
	a=ajaxNextPage(prodID,sB,sO,1,maxRev);
	};

// Simultaneously occurs with PowerReviews.com vote to update our Database with a new Helpful/Not Helpful vote -- not anymore, since PR changes their .js and it's not worth trying to keep up.
// Also blurs our rating options.

function dbUpdate (reviewId,vote){
	voteURL = "http://www.step2.com/ajaxVote.cfm?globalID=" + reviewId + "&vote=" + vote;
		request2.open("GET", voteURL, true);
		request2.send(null);
	divtoClear = 'picRate' + reviewId;
	divtoClear2 = 'textRate' + reviewId;
	document.getElementById(divtoClear).innerHTML = "<span style='color:#cccccc;'>Thanks!</span>";
	document.getElementById(divtoClear2).style.display = "none";

	};

// logs sli search values on page unload
function log_sli(){
	if($('sli_search_1').value != ""){
		postVal = 'kw=' + $('sli_search_1').value;
		request.open("POST", '/HTTP_Utilities/searchLogger.cfm', true);
		request.send(postVal);
		}
	}