
/***************************************************
  SURVEY OBJECT CODE START
***************************************************/
function PopDispatcher(surveyTypes, surveyDist) {
	this.types = surveyTypes;
	this.dist = surveyDist;
	this.currentBrand = '';
	this.currentProbs = 0;
	this.currentDist = 0;
    this.width = 0;
    this.height = 0;
}

PopDispatcher.prototype.getRandType = function(page, Dist) {
    var total = 0;

    for(var i=0; i < Dist.length; i++ )
        total += Dist[i];

    Probs = [];
    var j, k, l = Dist.length;
    var Q = 1;

    for (j = Dist.length-1 ; j >= 0 ; j-- ) {
        Probs[j] = Q ;
        Q -= Dist[j]/total;
    }

    k = Math.random()%1;
    for (j=0 ; j < Probs.length ; j++)
        if (Probs[j] > k) break;

    return {'rand':k, 'index': j};
}

PopDispatcher.prototype.dispatch = function(page) {
    var Dist = eval( 'this.dist.' + page );
    var objSurvey = Survey.getSurveyFromCookie(window.location.href);

    if( objSurvey == null ) {
        var selected = this.getRandType(page, Dist);
        popBrand = ( this.types[selected.index] == '' ) ? 'None' : this.types[selected.index];
        this.currentBrand = popBrand;
        this.currentIndex = selected.index;
        this.currentDist = Dist[selected.index];
        this.currentThreshHold = selected.rand;

        if( popBrand == 'None' ) return null;
        else return eval(popBrand);
     } else return objSurvey;
}

var popDispatcher = new PopDispatcher(brandConst.SURVEY_BRAND, brandConst.SURVEY_POPUP_DISTRIBUTION);


function Survey(name) {
    this.url = '';
    this.name = name;
    this.params = "toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=no,resizable=no,width=330,height=280,top=200,left=200";
    this.width = 330;
    this.height = 280;
    this.cookieExpirationTime = new Date((new Date()).getTime() + brandConst.BIZRATE_COOKIE_EXPIRATION_TIME);
}

Survey.prototype.getSurveyURL = function(page) { return this.url; };
Survey.prototype.getURLParams = function(page) {
    var sessionID = getCookie("JSESSIONID");
    var customerId = getCookie("customerId") || '';
    var unknownShopperId = getCookie("unknownShopperId");
    var shopperID = (customerId != "" ? (customerId.indexOf("null") == -1 ? customerId : unknownShopperId).replace(/\|\|\|/,"") : "");
    var referringPage = escape(location.pathname+location.search);
    var referringPageType = eval('brandConst.PAGE_TYPES.' + page ) ;
    return "?id=757&Q104534=" + sessionID + "&Q104535=" + shopperID + "&Q104536=" + referringPage + "&Q104533=" + referringPageType;

};

Survey.prototype.byPassPopBlocker = function(mypop, url, page) {
     try { mypop.focus(); }
        catch (e) {
         
                var popdiv = document.createElement('div');
                popdiv.id = 'surveyDiv';

                popdiv.style.width = this.width + 'px';
                popdiv.style.height = (this.height + 10) + 'px';
                gidLib.setObjCenter(popdiv, this.width, this.height + 10);

                popdiv.innerHTML = '<div style="padding-right: 10px; width: ' + this.width + 'px; text-align: right;" align="right">' +
                        '<a href="#" onclick="$(\'surveyDiv\').style.display = \'none\';">close [x]</a></div>' +
                        '<iframe id="surveyFrame" width="' + this.width + '" height="' + this.height + '" frameborder="0" scrolling="no" src="' + url + '&divPop=true' +
                                   '" />'

				var mainContent = $('mainContent');
				var mainContentContainer = $('mainContentContainer');
				if (mainContentContainer) {
					mainContentContainer.appendChild(popdiv);
				} else if (mainContent) {
					mainContent.appendChild(popdiv);
				}

         
        }
}

Survey.getSurveyFromCookie = function(strPageURL) {
    var objSurvey = null;
    var dateSixMonths = this.cookieExpirationTime;

    var surveyTestForceSurveyType = strPageURL.indexOf("surveyType") > 1 ? getQuerystringParam('surveyType') : '';
    if( surveyTestForceSurveyType == '' ) surveyTestForceSurveyType = getCookieVar("SurveyPersist","surveyType");

    if( surveyTestForceSurveyType != null && surveyTestForceSurveyType != '' ) {
        setCookieVar("SurveyPersist","surveyType",surveyTestForceSurveyType,dateSixMonths);
        if( surveyTestForceSurveyType == 'bizrate' ) objSurvey = BizrateSurvey;
        if( surveyTestForceSurveyType == 'rockbridge' ) objSurvey = RockbridgeSurvey;
    }

    return objSurvey;
}

Survey.popSurvey = function (page, survey) {
    var strPageURL = window.location.href;
    var isSurveyTestOn = strPageURL.indexOf("surveytest") != -1 || getCookieVar("SurveyPersist","surveyTest") == "1";

    if( isSurveyTestOn ) { setCookieVar("SurveyPersist","surveyTest",isSurveyTestOn ? '1' : '0',dateSixMonths); }

    if (getCookieVar("SurveyPersist","served") == "" || isSurveyTestOn || page == 'confirm' ) {

        /***************************  QA DEBUG STUFF ***************************************************************/
        if( survey != null )
            objSurvey = survey;
        else {
            objSurvey = this.getSurveyFromCookie(strPageURL);

            if( objSurvey == null )
                objSurvey = popDispatcher.dispatch(page);

        }

        if (isSurveyTestOn)
        	alert(
        		"TEST MODE:" +
                "\n\r\t1)Page type: " + page +
                ".\n\r\t2)Pop type: " + popDispatcher.currentBrand +
        		".\n\r\t3)Pop type distribution: " + popDispatcher.currentDist +
        		".\n\r\t4)Pop type (index, threshold): " + popDispatcher.currentIndex + ", " + popDispatcher.currentThreshHold +
        		".\n\r\t5)Should Pop: " + (( objSurvey == null || objSurvey == '' ) ? 'No' : 'Yes')
            );
         /***************************** DEBUG CODE ENDS *************************************************************/
        if( objSurvey == null || objSurvey == '' ) return;

        var url = objSurvey.getSurveyURL(page);
        if( url == '' ) return;

        url += objSurvey.getURLParams(page);
        var mypop = window.open(url, objSurvey.name, objSurvey.getPopParams());
        objSurvey.byPassPopBlocker(mypop, url, page);

        var dateSixMonths = this.cookieExpirationTime;
        if (strPageURL.indexOf("surveytest") != -1) alert("Survey cookie set to expire in 6 months (" + dateSixMonths + ").");
        setCookieVar("SurveyPersist","served","1",dateSixMonths);
	}
};


Survey.prototype.setPopParams = function(params) { this.params = params; };
Survey.prototype.getPopParams = function() { return this.params; }


var BizrateSurvey = new Survey('BizrateSurvey');
BizrateSurvey.url = 'http://eval.bizrate.com/vispop_popup.pl';
BizrateSurvey.getSurveyURL = function(page)  {
    if( page == 'confirm' )
        BizrateSurvey.customPop();
    else return this.url;

    return '';
}
BizrateSurvey.customPop = function() {
	document.write('<script type="text/javascript" src="https://eval.bizrate.com/js/pos_757.js"></script>');
}
BizrateSurvey.extraSurveyParams = { bfmid: '10386709', cid: '1501049', type: '307496', passin1: 'Q104534', passin2: 'Q108636' };


var RockbridgeSurvey = new Survey('RockbridgeSurvey');
RockbridgeSurvey.url = 'http://survey.confirmit.com/wix/p411305524.aspx';


RockbridgeSurvey.getURLParams = function(page) {
    var sessionID = getCookie("JSESSIONID");
    var customerId = getCookie("customerId") || '';
    var unknownShopperId = getCookie("unknownShopperId");
    var shopperID = (customerId != "" ? (customerId.indexOf("null") == -1 ? customerId : unknownShopperId).replace(/\|\|\|/,"") : "");
    var referringPageType = eval('brandConst.PAGE_TYPES.' + page ) ;
    var referringPage = escape(location.pathname+location.search);

    var paramString = "?Q104534=" + sessionID + "&Q104535=" + shopperID + "&Q104533=" + referringPageType + "&Q104536=" + referringPage;

    paramString += (RockbridgeSurvey.orderID ? "&OrderID=" + RockbridgeSurvey.orderID : '') +
                (RockbridgeSurvey.brandsVisited ? "&brandsVisited=" + RockbridgeSurvey.brandsVisited : '') +
                (RockbridgeSurvey.brandsPurchased ? "&brandsPurchased=" + RockbridgeSurvey.brandsPurchased : '');
    
    return paramString;

};

/***************************************************
  SURVEY OBJECT CODE END
***************************************************/

function setSideNavHeight() {
	var objSideNavRefinements = $('sideNavRefinements');
	var objSideNav = $('sideNav');
	var objContent = $('mainContent');


	
	if (objSideNavRefinements && objSideNav && objContent) {
		if (objContent.offsetHeight > objSideNav.offsetHeight + 60) {
			var strHeight = objSideNav.offsetHeight + (objContent.offsetHeight-objSideNav.offsetHeight) + 'px';
			objSideNav.style.height = strHeight;
		} else if (objContent.offsetHeight > objSideNav.offsetHeight) {
			var strHeight = objSideNav.offsetHeight + 60 + 'px';
			objSideNav.style.height = strHeight;
			if (clientBrowser.isIE6) {
				objContent.style.height = strHeight;
			} else {
				objContent.style.minHeight = strHeight;
			}		} else if (objSideNav.offsetHeight > objContent.offsetHeight) {
			var strHeight = objContent.offsetHeight + (objSideNav.offsetHeight-objContent.offsetHeight) + 60 + 'px';
			objSideNav.style.height = strHeight;
			if (clientBrowser.isIE6) {
				objContent.style.height = strHeight;
			} else {
				objContent.style.minHeight = strHeight;
			}
		}
	} else if (objSideNav && objContent) {
		if (objContent.offsetHeight > objSideNav.offsetHeight) {
			var strHeight = objSideNav.offsetHeight + (objContent.offsetHeight-objSideNav.offsetHeight) + 'px';
			objSideNav.style.height = strHeight;
		} else if (objSideNav.offsetHeight > objContent.offsetHeight) {
			var strHeight = objContent.offsetHeight + (objSideNav.offsetHeight-objContent.offsetHeight) + 'px';
			if (clientBrowser.isIE6) {
				objContent.style.height = strHeight;
			} else {
				objContent.style.minHeight = strHeight;
			}
		}
	}
}



/*
 * Extend siteNavigation.topNav with brand specific topNav functions
 */
Object.extend(siteNavigation.topNav,
	{

		divisionOver:function(division,isSelected) {
			if (this.lastSubDivisionOut == division) {
				clearTimeout(this.divisionTimer);
				clearTimeout(this.subDivisionTimer);
			}
			if (this.activeDivision == this.activeSubDivisions) {
				if (this.activeDivision != "") gidLib.hideSubDivisions(this.activeDivision);
		
				this.activeDivision = division;
			}
		
			var objDivision = $(division);
			var objDivisionPosition = Position.cumulativeOffset(division);
			var objSubDivisions = $(division+"Children");
			if (objSubDivisions) {
				objSubDivisions.style.display = "block";
		
				
				
				var subDivZIndex = objSubDivisions.style.zIndex;
				if (subDivZIndex <= 99 ) {
					objSubDivisions.style.zIndex = parseInt(subDivZIndex)+20;
				}
			}
		
		    var isDefaultSelected = ($(division).tagName != 'IMG' ? $(division).getElementsByTag('img')[0].hasClassName('topnavSprite_' + division + '_default')  : $(division).hasClassName('topnavSprite_' + division + '_default'));
		    if(isDefaultSelected) {
		        $(division).className = "topnavSprite_" + division + "_default";
		    } else if (isSelected) {
		    	
		    } else {
		        $(division).className = "topnavSprite_" + division + "_over";
		    } 
		},
		
		divisionOut:function(division,isSelected) {
			var objSubDivisions = $(division+"Children");
			if (objSubDivisions) {
				this.lastSubDivisionOut = division;
				this.divisionTimer = setTimeout("siteNavigation.topNav.hideSubDivisions('"+division+"',"+isSelected+")",200);
			}
		
		    var isDefaultSelected = ($(division).tagName != 'IMG' ? $(division).getElementsByTag('img')[0].hasClassName('topnavSprite_' + division + '_default')  : $(division).hasClassName('topnavSprite_' + division + '_default'));
		    var isSubDivisionSelected = false;
		
		    if( objSubDivisions ) {
		        var subdivisions = objSubDivisions.getElementsByTagName('img');
		        for(var i = 0; i < subdivisions.length; i++ ) {
		            var subdivision = subdivisions[i];
		            if( subdivision.hasClassName('topnavSprite_' + division + "_selected")) {
		                isSubDivisionSelected = true;
		                break;
		            }
		        }
		    }
		
		    if(isDefaultSelected) {
		        $(division).className = "topnavSprite_" + division + "_default";
		    } else if(isSubDivisionSelected) {
		        $(division).className = "topnavSprite_" + division + "_on";
		    } else {
		        $(division).className = "topnavSprite_" + division + "_off";
		    }
		},
		
		hideSubDivisions:function(division,isSelected) {
			if (this.activeSubDivisions != division) {
				if (!isSelected) {
		        $(division).className = "topnavSprite_" + division + "_off";
				}
				var objSubDivisions = $(division+"Children");
				if (objSubDivisions) {
					objSubDivisions.style.display = "none";
				}
				this.activeDivision = "";
			}
		},
		
		
		subDivisionOver:function(division) {
		        $(division).className = "topnavSprite_" + division + "_over";
		},
		
		subDivisionOut:function(division) {
		        $(division).className = "topnavSprite_" + division + "_off";
			
		},
		

		subDivisionsOver:function(divisionsId,isSelected) {
			this.activeSubDivisions = divisionsId;
		
		    var isDefaultSelected = $(divisionsId).tagName != 'IMG' ? $(divisionsId).getElementsByTag('img')[0].hasClassName('topnavSprite_' + divisionsId + '_default') : $(divisionsId).hasClassName('topnavSprite_' + divisionsId + '_default');

		    if(isDefaultSelected) {
		        $(divisionsId).className = "topnavSprite_" + divisionsId + "_default";
		    } else if (isSelected) {
		    	
		    } else {
		        $(divisionsId).className = "topnavSprite_" + divisionsId + "_over";
		    }
		
		},
		
		subDivisionsOut:function(division,isSelected) {
			var objSubDivisions = $(division+"Children");
			if (objSubDivisions) {
				this.subDivisionTimer = setTimeout("siteNavigation.topNav.hideSubDivisions('"+division+"',"+isSelected+")",200);
			}
			this.activeSubDivisions = "";
		}
	}
);
