
// Couturious A/B test container JS

// A/B test JS
// Cookie Cutter class
var universalCookieCutter = Class.create();

universalCookieCutter.prototype = {

  initialize:function(CookieVarName,CookieUsedName,CookieValueBase,ForcedModeBaseName,SegmentBreakdown,theEvar,plExcept) {

	//Keeping it neat by having two discreet variables that check for and gather cookie information
	this.EvarToUse = theEvar != "" ? theEvar : false;
	this.CookieVariableName = CookieVarName;
	this.CookieUsed = CookieUsedName;
	this.CookieValueBase = CookieValueBase;
	this.SegBreakdown = SegmentBreakdown.split("-");
	this.DoesTheCookieExist = gidLib.getCookieVar(this.CookieUsed, this.CookieVariableName);
	this.RetrievedCookieValue = gidLib.getCookieVar(this.CookieUsed, this.CookieVariableName);
	this.QuickSegment = '';
	this.piperlimeException = plExcept;
	this.userNeedsSync = false;
	this.ForcedModeBase = ForcedModeBaseName;
	this.ModeArrayIndex = this.SegBreakdown.length; //segment breakdown determines how many segments too!
	this.modeArray = new Array();
	this.modeArray[0] = "a";
	this.modeArray[1] = "b";
	this.modeArray[2] = "c";
	this.modeArray[3] = "d";
	this.modeArray[4] = "e";
	this.modeArray[5] = "f";
	this.modeArray[6] = "g";
		
	if (location.href.indexOf(this.ForcedModeBase) >= 1) {
		this.forcedMode();
	}
	else{
		this.cookieScanner();
	}
  },

  // Function to force A/B test
  forcedMode:function() {
	for (i = 0; i < this.ModeArrayIndex; i++) {
		if (location.href.indexOf(this.ForcedModeBase + "=" + this.modeArray[i]) >= 1) {
			this.UserSegment = this.CookieValueBase + this.modeArray[i].toUpperCase();
			this.QuickSegment = this.modeArray[i].toUpperCase();
		}
	}
		
	gidLib.setCookieVar(this.CookieUsed,this.CookieVariableName,this.UserSegment);
	if (this.EvarToUse) {
		this.setABTestVariable(this.UserSegment);
	}

	userNeedsSync = true;
  },

  // Checks for A/B test cookie
  cookieScanner:function() {
	if (this.DoesTheCookieExist) {
		if (this.EvarToUse) {
			this.setABTestVariable(this.RetrievedCookieValue);
		}
		for (i = 0; i < this.ModeArrayIndex; i++) {
			if (this.RetrievedCookieValue == (this.CookieValueBase + this.modeArray[i].toUpperCase())) {
				this.QuickSegment = this.modeArray[i].toUpperCase();
				break;
			}
		}
	}
	else {
		this.tagManager();
	}
  },

  tagManager:function() {
	this.Segmentation = Math.floor((Math.random()*100)+1); // chooses 1 to 100
	this.FloorThreshold = 0;
	this.TopThreshold = 0;
	
	for (i = 0; i < this.ModeArrayIndex; i++){
		this.TopThreshold = this.FloorThreshold + parseInt(this.SegBreakdown[i]);
		if ((this.Segmentation > this.FloorThreshold) && (this.Segmentation <= this.TopThreshold)){
			this.UserSegment = this.CookieValueBase + this.modeArray[i].toUpperCase();
			this.QuickSegment = this.modeArray[i].toUpperCase();
			break;
		} else {
			this.FloorThreshold = this.TopThreshold;
		}
	}
	gidLib.setCookieVar(this.CookieUsed,this.CookieVariableName,this.UserSegment);

	if (this.EvarToUse) {
		this.setABTestVariable(this.UserSegment);
	}
  },
	
  setABTestVariable: function(setValue) {
	thisEvar = 'eVar' + this.EvarToUse;
	ABTestVariables[thisEvar] = setValue;
  }

};

var alphaUniversalCOBRA4 = Class.create();

alphaUniversalCOBRA4.prototype = {
	initialize:function(QuickVar) {
		this.displayManager();
	},
	
	displayManager:function() {
		switch (ABTestVariables.eVar42) {
			case '0809_PL_COUTUREPRODUCT_SEGA':
				couturiousStep1.couturiousCheck();
				break;
				
			default:
				break;
		}
	}	
};

var newCookieCutter = new universalCookieCutter("0809_PL_COUTUREPRODUCT","mktUniversalPersist","0809_PL_COUTUREPRODUCT_SEG","coutureForce","50-50","42");

// Couturious check/iso layer
var CouturiousStep1 = Class.create();

CouturiousStep1.prototype = {

  initialize: function() { },
	
  couturiousCheck: function() {
   // First check that we are on a product detail page
   var page = document.location.toString();
   if( page.indexOf('browse/product.do') >= 0 ) {
    var validSCIDs = '794588002,794601002,794602002,796646002,796646012,796659002,796667002,807363002,807366002,807366012,809910002,812366002,812366012,821675002,704964002,797822002,797822012,694294002,786678002,786678012,797177002,797177012,805821002,805821012,807246012,681791022,681791032,796362002,803066002,803941002,811623002,811623012,813913002,814354002,814354012,816901002,790200002,789634012,789634002,799106002,796335002,796335012,796334002,771850022,771850032,703889012,802382002,687417002,738930072,738930042,621413002,621413122,794276002,794276012,796741002,796745002,796870002,797788002,807219002,807219012,807520002,807520012,809856002,809856012,809935002,812630002,702390002,702390042,803075002,783527002,810975002,810974002,803050002,803041002,803046002,804944002,796909002,796909012,809229002,809464002,812658002,812653002,812655002,811813002,809231002,809234002,808300002,808317002,808323002,796894002,796895002,804332002,804332012,804340002,796893002,803622002,796907002,803617012,803617002,803617022,803634002,803630002,803628002,765439012,803632002,803632012,803625002,803086002,811962002,811951002,811961002,811959002,811963002,811963012,811965002,802655002,802655002,803310002,803306002,803314002,807142002,782821002,807147002,807155002,807153002,807145002,807150002,807146002,796945002,803871002,803869002,812629002,812629012,812635002,802614002,802678002,796877002,802685002,802680002,802688002,809868012,796967002,806960002,809867002,806955002,809863002,796963002,796981002,809866002,809876002,809861002,806958002,809860002,804117002,804135002,804135012,807452002,764979002,807453002,807453012,807439012,768869002,768869012,768869022,796994002,807456002,796993002,796993012,807446002,807451002,802904002,802899002,790303002,790303012,802901002,802900002,802898002,796996002,813610002,796930002,807262002,807265002,803668002,803669002,803659002,803667002,803671002,803670002,803661002,802987002,803204002,803178002,803181002,803021002,804210002,804215002,803292002,803295002,803293002,803368002,803100002,803327002,803331002,796925002,808988002,809078002,808995002,804148002,809064012,796920002,808981002,803944002,803940002,803940012,809947002,809963002,809953002,809965002,809961002,811590002,808248002,808241002,808241012,810849002,810857002,810857012,811573002,811573012,810842002,810842012,812545002,812541002,812541012,809265002,809349002,796921002,802535002,802534002,803145002,803143002,803138002,803141002,812530002,812532002,807837002,807852002,811568012,811556002,811571002,811558002,811566002,811557002,811557002,811574002,709310052,812212002,812211002,812213002,802927002,802954002,802938002,802943002,802962002,812226002,803932002,803933002,803935002,803361002,808365002,808359002,808354002,808351002,808864002,808876002,808870002,808870012,796991002,796991012,808874002,808869002,808873002,812218002,812216002,812219002,812224002,809340022,809333002,809339012,809339022,809372002,812240002,811955002,811941002,811937002,803096012,796900002,805918012,805921002,805924002,805816002,796904002,739686002,805814002,802641002,805917002,802642002,811549002,811550002,811555002,813314002,813292002,812369002,812346002,796988002,812368002,812368012,812367012,811932012,803926002,812651002,812668002,806957002,812311002,812335002,806138002,811778002,811780002,809255002,809254002,809250002,809257002,796884002,796882002,809252002,809132002,809132012,809132022,809156002,809151002,809150002,809149002,809149012,803339002,803338002,803344002,803336002,803355002,803553002,803552002,803342002,803337002,803340002,803353002,814320002,811603002,810933002,810930002,810931002,802889002,802894002,802892002,796947012,796947002,800354002,792033002,800055002,800452022,800056002,792963002,603070112,794748002,804295002,804201002,804306002,805123002,804293002,804307002,810887002,810882002,810886002,804310002,804324002';
	
    var currentSCID = getQuerystringParam( 'scid' );
    //var currentPID = parseInt( getQuerystringParam( 'pid' ) );
    var currentPID = getQuerystringParam( 'pid' );
	
	if( currentPID.length == 9 ) {
		currentSCID = currentPID;
	}
	
	if( currentPID.length == 13 ) {
		currentSCID = currentPID.substr(0,9);
	}
	
	if( currentSCID == "" || currentSCID.length != 9 ) {
		//currentSCID = "794601002";
		var gidProds = objGIDPageViewAdapter.objGIDProducts.arrayProducts;
		var defaultVariantId = gidProds[currentPID].strDefaultVariantId;
		currentSCID = gidProds[currentPID].arrayVariantStyles[defaultVariantId].arrayVariantStyleColors[0].strColorCodeId;
	}

    if( validSCIDs.indexOf( currentSCID ) >= 0 ) {
	  // Data check to determine whether Couturious prod has any outfits
	  gidLib.loadScript({ callerObject: this, src:'http://www.couturious.com/feed/pl/look.js?sku=' + currentSCID, callback:{ handler:this.displayWidget, args:{ "currentSCID": currentSCID }, timeDelay:0.5 } });
    }
   }
  },

  displayWidget: function( args, periodicalExecuterRef ) {
	//window.loadScriptTimedOut = true;
	periodicalExecuterRef.stop();
	var currentSCID = args.currentSCID;
	if( coutureJSONObject && coutureJSONObject.outfitObjects != null && coutureJSONObject.mainProduct != null && coutureJSONObject.outfitObjects[0].productsInOutfit != null ) {
	  var iframeURL = '/browse/info.do?cid=57443&coutureProd=' + currentSCID;
	  var iframeString = '<iframe src="' + iframeURL + '" class="content" style="width:251px; height:154px;" frameborder="0" scrolling="no" width="251" height="154"></iframe>';
      var coutIframeElt = new Element('div',{ id: 'coutureIfrHolder' }).update( iframeString );
	  coutIframeElt.setStyle( { width:'251px', height:'154px', marginTop:'5px' } );
	  
	  // Check for product thumbnail images and, if present, place unit below
	  if( $('imageThumbs') ) {
        $('imageThumbs').insert({ after: coutIframeElt });
	  }
	  else if( $('imageTools') ) {
        $('imageTools').insert({ after: coutIframeElt });
	  }
	}
  },

  /** Launch iso layer function **/
  launchCoutureIsoLayer: function( coutureProd, outfitID ) {
	
	var isoURL = '/browse/info.do?cid=57444&coutureProd=' + coutureProd + '&outfitId=' + outfitID;
	//var isoURL = '/browse/info.do?cid=58615&coutureProd=' + coutureProd + '&outfitId=' + outfitID;

	//var currentSCID = getQuerystringParam( 'coutureProd' );
	if( $('crossLinkModule') ) { $('crossLinkModule').setStyle({ 'z-index':97 }); }
	var coutureIso = new LayeredSpawnerExpanded( isoURL, 'coutureLayer', 713, 554, true, { color:'#fff', opacity:'0.85', buzz:false }, { show:{ method:Effect.Appear, args:{ duration:0.7 } }, hide:{ method:Effect.Fade, args:{ duration:0.7 } } } );
	//var coutureIso = new LayeredSpawnerExpanded( '/browse/info.do?cid=57444&coutureProd=' + coutureProd + '&outfitId=' + outfitID, 'coutureLayer', 713, 554, true, { color:'#fff', opacity:'0.85', buzz:false }, { show:{ method:Effect.Appear, args:{ duration:0.7 } }, hide:{ method:Effect.Fade, args:{ duration:0.7 } } } );
	coutureIso.spawn();
	document.body.id = 'coutureProduct';
	$('popupContent').addClassName('couturePopup');
	//$('popupContent').select('div.topCloseButton a').href = 'javascript:removeCoutureStyle()';
	return false;
  },
  
  removeCoutureStyle: function() {
	if( popup.hasClassName('couturePopup') ) {
	  popup.removeClassName('couturePopup');
	}
	$('quickLookLauncher').style.display = "block";
	return false;
  },
  launchQLFromChild: function( scid ) {
	var scidToQL = scid.toString();
	var prodToQL = scidToQL.substr(0,6);

    var browserName=navigator.appName; 
	if (browserName=="Netscape") { 
	 var targetFrame = $('coutureLayerPopupContent').contentDocument;
	}
	else { 
	 if (browserName=="Microsoft Internet Explorer") {
	  var targetFrame = window.frames['coutureLayerPopupContent'].document;
	 }
	 else {
	  var targetFrame = $('coutureLayerPopupContent').contentDocument;
	 }
	}

	
	//var targetFrame = window.frames['coutureLayerPopupContent'].document;
	//var targetFrame = $('coutureLayerPopupContent').contentDocument;
	
	var targetImg = targetFrame.getElementById('coutureOutfit'+scidToQL).id;
	quickLook.openQuickLookLauncher( prodToQL,scidToQL,'59556',-1,targetImg,false,'4',0,true);
	$('quickLookLauncher').style.display = "none";
	quickLook.launchQuickLook( scidToQL );

	//var checkTop = $('quickLookWindow').getStyle( 'top' );
	//checkTop = checkTop.parseInt( checkTop.replace('px','') );
	//if( checkTop < 0 ) { $('quickLookWindow').setStyle({ 'top':'20px' });
	
	quickLook.parseInlineBagAjaxResponse = quickLook.parseInlineBagAjaxResponse.wrap( function( originalMethod,transport ) { 
      // CLOSE ISO LAYER
	  coutureLib.layeredPopup.closeLayeredPopup();
      originalMethod( transport );
    } );
  }
}

var couturiousStep1 = new CouturiousStep1();

// LayeredSpawnerExpanded - utilizes more of the native gidLib LayeredPopup functionality
var LayeredSpawnerExpanded = Class.create();

LayeredSpawnerExpanded.prototype = {
  initialize:function(TargetURL,WindowName,contentWidth,contentHeight,isDraggable,interstitial,effects) {
	this.URL = TargetURL;
	this.WindowTag = WindowName;
	this.originWidth = contentWidth;
	this.originHeight = contentHeight;
	this.windowWidth = contentWidth+2;
	this.windowHeight = contentHeight+16;
	this.isDraggable = isDraggable;
	this.interstitial = interstitial;
	this.effects = effects;
  },
	
  spawn:function() {
	return coutureLib.layeredPopup.openLayeredPopup({str:this.URL,id:this.WindowTag,width:this.windowWidth,height:this.windowHeight,isDraggable:this.isDraggable,interstitial:this.interstitial,effects:this.effects});
  },
	
  nonLayeredSpawn:function() {
	window.open(this.URL,this.WindowTag,'status=yes, toolbar=no, location=no, menubar=no, resizable=yes, scrollbars=yes, width='+this.originWidth+',height='+this.originHeight);
  }
};

