bbSwatchNavigation.Items = new Array();
bbSwatchNavigation.path = "images/product_images/swatches/";
bbSwatchNavigation.minY = 136;
var swYCMSOffset = 0;

function bbSwatchNavigation(name){
	this.name = name;
	this.Items = new Object;
	bbSwatchNavigation.Items[bbSwatchNavigation.Items.length] = this;
	this.PNGItems = new Object;
	this.largeSwatchItems = new Object;
}

bbSwatchNavigation.prototype.getItem = function(key){
	return this.Items[key];
}

bbSwatchNavigation.prototype.getPNGItem = function(key){
	return this.PNGItems[key];
}

bbSwatchNavigation.prototype.getLargeSwatchItem = function(key){
	return this.largeSwatchItems[key];
}

bbSwatchNavigation.prototype.addItem = function(oImage,sLayerName,isDefault){
	oImage.onload = '';
	oItem = this.Items[oImage.name] = new String(oImage.name);
	oItem.obj = oImage;
	oItem.name = oImage.name;
	oItem.defaultSrc = wsmlMakeResourceUrl(oImage.src);

	// large swatch is 160px by 160px
	// is this needed here?
	if ((oImage.src.lastIndexOf(".png") > -1) && (bw.win && bw.ie)) {
		oItem.obj.runtimeStyle.width="160px";
		oItem.obj.runtimeStyle.height="160px";
		oItem.obj.runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oItem.defaultSrc + "',sizingMethod='scale')";
		oItem.obj.src=wsmlMakeResourceUrl('/images/1x1.gif');
	}

	// another attempt at ie.compatibity: store dedicated image objects for later use
	if ((oImage.src.lastIndexOf(".png") > -1) && (bw.win && bw.ie)) {
		pngItem = this.PNGItems[oItem.defaultSrc] = new String(oItem.defaultSrc);
		pngItem.image = new Image();
		pngItem.image.src = oItem.defaultSrc;
	}

	oItem.isDefault = (isDefault)?1:0;
}

bbSwatchNavigation.prototype.addSwatchInfo=function(imgObj, sSwatchCode, sSwatchName, sSwatchDescription, sAnchorKey, sProductId, sHexValue, sSmooshPath){
	// for pages with more than one large swatch div, the product id becomes an infix in identifiers
	var swatchDivImgName = 'swatchlgpng';
	swatchDivImgName += (sProductId == null) ? '' : sProductId;
	var swatchDivId = 'swatchDiv';
	swatchDivId += (sProductId == null) ? '' : sProductId;
	var swatchDivImage = 'swatchDiv';
	swatchDivImage += (sProductId == null) ? '' : sProductId;
	swatchDivImage += 'Image';
	var swatchDivName = 'swatchDiv';
	swatchDivName += (sProductId == null) ? '' : sProductId;
	swatchDivName += 'Name';
	var swatchDivDesc = 'swatchDiv';
	swatchDivDesc += (sProductId == null) ? '' : sProductId;
	swatchDivDesc += 'Desc';

	// get the (generic) nav element associated with this particular small swatch gif from the matrix
	var oItemObj = this.getItem(imgObj.name);
	oItemObj.swatchDivImgName = swatchDivImgName;
	oItemObj.swatchDivId = swatchDivId;
	oItemObj.swatchName = sSwatchName;
	oItemObj.swatchDesc = sSwatchDescription;
	oItemObj.swatchCode = sSwatchCode;
	oItemObj.productId = sProductId;
	oItemObj.hexValue = sHexValue;
	oItemObj.smooshPath = sSmooshPath;

	// may wish to expand to include all win ie5.0 as 'gif' type
	if (sSmooshPath.lastIndexOf(".gif") > -1) {
		oItemObj.smooshType = 'gif';
	} else {
		oItemObj.smooshType = 'png';
	}

	oItemObj.swatchImageSubLayer = new elcLayer( swatchDivImage );
	oItemObj.swatchNameSubLayer = new elcLayer( swatchDivName );
	oItemObj.swatchDescSubLayer = new elcLayer( swatchDivDesc );

	oItemObj.anchorKey = sAnchorKey;
	oItemObj.swatchContainer = new elcLayer( swatchDivId );
	//var stats = 'Layer stats\n' + 'x : ' + oItemObj.swatchContainer.x + '\n' + 'y : ' + oItemObj.swatchContainer.y + '\n' + 'w : ' + oItemObj.swatchContainer.w + '\n' + 'h : ' + oItemObj.swatchContainer.h + '\n' + 'z : ' + oItemObj.swatchContainer.z + '\n';
        //alert( swatchDivId + ' ' + stats );


	var myLargeSwatchItem = this.getLargeSwatchItem( swatchDivId );
	if (myLargeSwatchItem == null) {
		myLargeSwatchItem = this.largeSwatchItems[swatchDivId] = new String(swatchDivId);
		myLargeSwatchItem.obj = oItemObj.swatchContainer;
	}
	oItemObj.swatchContainerName = swatchDivId;

	// the 'swatchBorder' is the border around the small swatch gif
	var swatchBorderName = 'swatchBorder' + oItemObj.swatchCode;
	oItemObj.parentDiv = new elcLayer( swatchBorderName );

	this.lastParentDiv = null;

	if ((sSmooshPath.lastIndexOf(".png") > -1) && (bw.win && bw.ie)) {
		pngItem = this.PNGItems[sSmooshPath] = new String(sSmooshPath);
		pngItem.image = new Image();
		pngItem.image.src = wsmlMakeResourceUrl(sSmooshPath);
	}
}

bbSwatchNavigation.prototype.setOn = function(key,obj,forceAltKey) {
	var oItem = this.getItem(key);
	if (typeof this.hSetOn == 'function') this.hSetOn(oItem,forceAltKey);

	if(oItem.layer){
		this._processSubLayerSetOn(oItem);
	} else if(this.defaultItemKey) {
		this.setDefaultItemOn();
	}
}

bbSwatchNavigation.prototype.setOff = function(key){
	oItem = this.getItem(key);
	if (!oItem.isDefault){
		if (typeof this.hSetOff == 'function') {
			this.hSetOff(oItem);
		}
	}
	if (oItem.layer) this._processSubLayerSetOff(oItem);
}

bbSwatchNavigation.prototype._processSubLayerSetOn = function(key){
	var obj = this.getItem(key);
	obj.layer.show();	
}

bbSwatchNavigation.prototype._processSubLayerSetOff = function(key){
	var obj = this.getItem(key);
	obj.layer.hide();
}

bbSwatchNavigation.prototype.hSetOn=function(key,forceAltKey){
	var obj = this.getItem( key );
	if (!(obj && obj.swatchDivImgName)) return false;

	var swImgObj;
	var swatchPath;

	var oldParent = this.lastParentDiv;
	if (oldParent != null) {
		this.setLastParentOff( oldParent );
	}
	var swatchBorderName = 'swatchBorder' + obj.swatchCode;
	var newParent = new elcLayer( swatchBorderName );
	if (newParent != null) {
		this.setLastParentOn( newParent );
	}
	this.lastParentDiv = newParent;

	var alternateAnchorKey = (obj.productId) ? obj.productId + '_sa_alt' : '_sa_alt'; // code_sa + '_alt'
        //alert( 'alternate anchor key: ' + alternateAnchorKey );
	var hasAlternateAnchor = hasAnchor( alternateAnchorKey );
	var altAnchorCoord;
	if (hasAlternateAnchor) {
		altAnchorCoord = getAnchorPosition( alternateAnchorKey );
		//alert( 'force? ' + forceAltKey + ' ' + altAnchorCoord.x + ' ' + altAnchorCoord.y );
	}

	// move based on anchor coordinates
	var anchorCoord = getAnchorPosition( obj.anchorKey );
	//alert( 'anchorCoord: ' + obj.anchorKey + ' y: ' + anchorCoord.y );

	if (hasAlternateAnchor && (anchorCoord.y < altAnchorCoord.y)) {
		anchorCoord = altAnchorCoord;
	}
	var swY = (anchorCoord.y - 0 < bbSwatchNavigation.minY) ? bbSwatchNavigation.minY : anchorCoord.y - 0;
	//alert (alternateAnchorKey + "\n" + 'anchorCoord.y: ' + anchorCoord.y + "\n" + 'bbSwatchNavigation.minY: ' + bbSwatchNavigation.minY );

	if (forceAltKey && hasAlternateAnchor) {
		//alert( 'forceAltKey && hasAlternateAnchor: y' + altAnchorCoord.y )
		swY = altAnchorCoord.y;
	}

	// cannot hide as this blows up PNG changes in IE
	// obj.swatchContainer.hide();

	var myLargeSwatchItem = this.getLargeSwatchItem(obj.swatchContainerName);

/*
	var isInsideContentDiv = this.isDivInsideContentDiv( obj.swatchContainer.obj );
	if (isInsideContentDiv) {
		alert( 'contentDiv offset: ' + isInsideContentDiv );
		swY -= isInsideContentDiv;
                swY -= 200;
        }
*/
	if (swYCMSOffset) {
		swY -= swYCMSOffset;
	}
         
//alert( swY );
	if (myLargeSwatchItem != null) {
//alert( 'moving lsitem ' + obj.swatchContainerName );
		obj.swatchContainer.moveTo( myLargeSwatchItem.obj.x, swY );
	} else {
//alert( 'moving ' + obj.swatchContainerName );
		obj.swatchContainer.moveTo( obj.swatchContainer.x, swY );
	}
	
	// in syndication and in IE, we have a strange positioning problem
	// this is a brute-force workaround
	// worse: the anchor positions appear to be correct in some browsers, but wrong in others
	if ((bw.win && bw.ie)) {
		var swX;
		if (myLargeSwatchItem != null) {
			swX = (myLargeSwatchItem.obj.x - 0 < 50) ? 50 : myLargeSwatchItem.obj.x - 0;
		} else {
			swX = (obj.swatchContainer.x - 0 < 50) ? 50 : obj.swatchContainer.x - 0;
		}
		obj.swatchContainer.moveTo( swX, swY );
	}
	// change it
	obj.swatchNameSubLayer.writeThis('<b>' + obj.swatchName + '</b>');
	obj.swatchDescSubLayer.writeThis( obj.swatchDesc );

	// change image: gif vs jpg
	if (obj.smooshType == 'gif') {
		var myLgImg = this.getItem( obj.swatchDivImgName );
                if (myLgImg != null) {
      	            myLgImg.obj.src = obj.smooshPath;
		    obj.swatchImageSubLayer.css.backgroundColor = '#FFFFFF';
                }
	} else {
		obj.swatchImageSubLayer.css.backgroundColor = obj.hexValue;
		var myLgImg = this.getItem( obj.swatchDivImgName );
		if (myLgImg.obj.src != obj.smooshPath) {
			myLgImg.obj.src = wsmlMakeResourceUrl(obj.smooshPath);
		}
	
		// not sure why this conditional was change to always be 'true'
		// set to false 7/13/05
		if ((false || (myLgImg.defaultSrc.lastIndexOf(".png") > -1)) && (bw.win && bw.ie)) {
			var smooshPath = myLgImg.defaultSrc;
			// unfortunately changing the src attribute leads to the bg color of the page turning black
			// when the div is hidden, otherwise seems to be OK
			if ((obj.smooshPath.lastIndexOf(".png") > -1)) {
				pngObject = this.getPNGItem( obj.smooshPath );
				smooshPath = wsmlMakeResourceUrl( pngObject.image.src );
			}
			// 'canonical' technique to overcome transparency limitations in win IE
			myLgImg.obj.runtimeStyle.width="160px";
			myLgImg.obj.runtimeStyle.height="160px";
			myLgImg.obj.runtimeStyle.filter="progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + wsmlMakeResourceUrl(smooshPath) + "',sizingMethod='scale')";
			myLgImg.obj.src=wsmlMakeResourceUrl('/images/1x1.gif');
		}
	}

	this.last = obj.swatchCode;

	// show
	obj.swatchContainer.show();
}

bbSwatchNavigation.prototype.hSetOff=function(key){ return true; }

bbSwatchNavigation.prototype.setLastParentOn=function(layerObj){
	if (layerObj.css) {
		layerObj.css.borderColor='#B6B7B5';
	}
}

bbSwatchNavigation.prototype.setLastParentOff=function(layerObj){
	if (layerObj.css) {
		layerObj.css.borderColor='#FFFFFF';
	}
}

bbSwatchNavigation.prototype.isDivInsideContentDiv=function( node ){
    var element = node;
    while ((element = element.offsetParent) != null) {
        if ((element != null) && element.id == 'BBContent') {
            return element.offsetTop;
        }
    }
    return false;
}
