/*
ADOBE CONFIDENTIAL
Copyright 2005 Adobe Systems Incorporated
All Rights Reserved.

NOTICE:  All information contained herein is, and remains the property of Adobe Systems Incorporated and its suppliers,
if any.  The intellectual and technical concepts contained herein are proprietary to Adobe Systems Incorporated and its
suppliers and may be covered by U.S. and Foreign Patents, patents in process, and are protected by trade secret or 
copyright law. Dissemination of this information or reproduction of this material is strictly forbidden unless prior 
written permission is obtained from Adobe Systems Incorporated.
*/

function SJSwatchManager(inViewer,inParent,inSlave,inMainImageList,inSwatchParams,inSuffix){
	this.zviewer = inViewer;
	this._parent = this.master = inParent;
	this.slave = inSlave;
	this.targetManager = null;
	this.mainImageList = inMainImageList;
	this.swLayout = inSwatchParams.swLayout;
	this.swCellSpacing = inSwatchParams.swCellSpacing;
	this.swHighlightThickness = inSwatchParams.swHighlightThickness;
	this.swHighlightColor = inSwatchParams.swHighlightColor;
	this.swBorderThickness = inSwatchParams.swBorderThickness;
	this.swBorderColor = inSwatchParams.swBorderColor;
	this.swBorderThickness = inSwatchParams.swBorderThickness;
	if (inSwatchParams.swRetainSelection =="false"){
		this.swRetainSelection = false;
	}else{
		this.swRetainSelection = true;
	}
	if (inSwatchParams.swDoReset == "false"){
		this.swDoReset = false;
	}else{
		this.swDoReset = true;
	}
	this.swTextPosition = inSwatchParams.swTextPosition;
	this.swTextFontName = inSwatchParams.swTextFontName;
	this.swTextSize = inSwatchParams.swTextSize;
	this.swTextBold = inSwatchParams.swTextBold;
	this.swTextColor = inSwatchParams.swTextColor;
	this.wp = (parseInt(sjGetWidth(this.master)) || 75);
	this.hp = (parseInt(sjGetHeight(this.master)) || 75);
	this.ws = (parseInt(sjGetWidth(this.slave)) || 75);
	this.hs = (parseInt(sjGetHeight(this.slave)) || 75);
	this.suffix = inSuffix || '';
}

//functions
SJSwatchManager.prototype.setTargetManager = function(inTargetManager) {
	this.targetManager = inTargetManager || null;
}

SJSwatchManager.prototype.getImageInfo = function() {
	return this.getImageNode(this.mainImageList,this.getSelectedImageNodeIdxList(this.mainImageList))
};

SJSwatchManager.prototype.setImageInfo = function(inImageInfo) {
	var imageInfo = inImageInfo;
	var parentImageInfo = imageInfo.parentImageInfo;
	while (parentImageInfo != null) {
		for (var i = 0; i < parentImageInfo.imageList.length; i ++) {
			var childImageInfo = parentImageInfo.imageList[i];
			for (var j = 0; j < childImageInfo.swatchList.length; j ++) {
				var swatch = childImageInfo.swatchList[j];
				var key = this.getSwatchKey(swatch);
				var tempkey = this.getSwatchKey(imageInfo.swatchList[j]);
				swatch.selected = (key == tempkey);
			}
		}
		imageInfo = parentImageInfo;
		parentImageInfo = imageInfo.parentImageInfo;
	}
	this.clearSwatches(this.master);
	this.clearSwatches(this.slave);
	this.createSwatches();
	
	
	this.currentImageInfo = inImageInfo;
}

SJSwatchManager.prototype.getSwatchCol = function(inImageInfoList, inRow, inSwatch) {
	var matrix = this.getSwatchMatrix(inImageInfoList);
	for (var col = 0; col < matrix[inRow].length; col ++) {
		var swatch = matrix[inRow][col];
		if (this.getSwatchKey(swatch) == this.getSwatchKey(inSwatch)) {
			return col;
		}
	}
	//alert('Error!!!');
	alert(sj_resource.getResource('%ERROR%!!!'));
}


SJSwatchManager.prototype.getSwatchColList = function(inImageInfoList, inSwatchList) {
	var matrix = this.getSwatchMatrix(inImageInfoList);
	var colList = new Array();
	for (var row = 0; row < matrix.length; row++) {
		colList.push(this.getSwatchCol(inImageInfoList, row, inSwatchList[row]));
	}
	return colList;
}

SJSwatchManager.prototype.initializing = function(inImageInfoList){
	for (var i=0; i<inImageInfoList.length;i++) {
		var imageInfo = inImageInfoList[i];
		for (var row = 0; row < imageInfo.swatchList.length; row ++) {
			var swatch = imageInfo.swatchList[row];
			if (this.getSwatchCol(inImageInfoList, row, swatch) == 0) {
				swatch.selected = true;
			} else {
				swatch.selected = false;
			}
		}
		if (inImageInfoList[i].imageList.length > 0) {
				 this.initializing(inImageInfoList[i].imageList);
		}
	}
}

SJSwatchManager.prototype.getSwatchMatrix = function(inImageInfoList) {
	var swatchMatrix = new Array();
	for (var i = 0; i < inImageInfoList[0].swatchList.length; i ++) {
		var list = new Array();
		var hash = new Object();
		for (var j = 0; j < inImageInfoList.length; j ++) {
			var img = inImageInfoList[j];
			var sw = img.swatchList[i];
			var key = this.getSwatchKey(sw);
			if (!hash[key]) {
				hash[key] = true;
				list.push(sw);
			}
		}
		swatchMatrix.push(list);
	}
	return swatchMatrix;
}

SJSwatchManager.prototype.getSwatchKey = function(inSwatch) {
	if (inSwatch.name != '') {
		return inSwatch.name;
	} else {
		return inSwatch.color;
	}
}

SJSwatchManager.prototype.getImageNode = function(inImageInfoList,inIdxList) {
	if (inImageInfoList.length>0){
		var imageNode = inImageInfoList[inIdxList[0]];
		for (var i = 1; i < inIdxList.length; i ++) {
			imageNode = imageNode.imageList[inIdxList[i]];
		}
		return imageNode;
	}
		return null;
}

SJSwatchManager.prototype.getSelectedImageNodeIdxForLevel = function(inImageInfoList) {
	for (var i = 0; i < inImageInfoList.length; i ++) {
		var imageInfo = inImageInfoList[i];
		var selected = true;
		for (var row = 0; row < imageInfo.swatchList.length; row ++) {
			var swatch = imageInfo.swatchList[row];
			if (!swatch.selected) {
				selected = false;
				break;
			}
		}
		if (selected) {
			return i;
		}
	}
}

SJSwatchManager.prototype.getSelectedImageNodeIdxList = function(inImageInfoList) {
	var imageInfoIdxList = new Array();
	var imageInfoIdx = this.getSelectedImageNodeIdxForLevel(inImageInfoList);
	imageInfoIdxList.push(imageInfoIdx);
	var imageInfo = this.getImageNode(inImageInfoList, imageInfoIdxList);
	while (imageInfo.imageList.length > 0) {
		imageInfoIdx = this.getSelectedImageNodeIdxForLevel(imageInfo.imageList);
		imageInfoIdxList.push(imageInfoIdx);
		imageInfo = this.getImageNode(inImageInfoList, imageInfoIdxList);
	}
	return imageInfoIdxList;
}

SJSwatchManager.prototype.selectSwatch = function(inParent,inImageInfoList, inIndexList, inRow, inCol) {
	if (inIndexList.length > 0) {
		inImageInfoList = this.getImageNode(inImageInfoList, inIndexList).imageList;
	}
	var matrix = this.getSwatchMatrix(inImageInfoList);
	for (var i=0; i<inImageInfoList.length;i++) {
		var imageInfo = inImageInfoList[i];
		var swatch = imageInfo.swatchList[inRow];
		if (this.getSwatchCol(inImageInfoList, inRow, swatch) == inCol) {
			swatch.selected = true;
		} else {
			swatch.selected = false;
		}
		if (!this.swRetainSelection && (imageInfo.imageList != null)) {
			this.initializing(imageInfo.imageList);
		}
	}
	var tempInfo = this.getImageNode(inImageInfoList,this.getSelectedImageNodeIdxList(inImageInfoList));
	this.zviewer.imageLabel(tempInfo.label);
	this.zviewer.setImage(this.zviewer.baseServerUrl+"/"+tempInfo.name, this.swDoReset,tempInfo.dx,tempInfo.dy,tempInfo.iv,tempInfo.pageId);

	if(this.targetManager){
		this.targetManager.setImageInfo(this.getImageNode(inImageInfoList,this.getSelectedImageNodeIdxList(inImageInfoList)));
	}
	if (this.master != this.slave){
		if (inParent == this.master){
			this.clearSwatches(this.master);
			this.clearSwatches(this.slave);
			this.createSwatches_mode(this.master,SJSwatchManager.masterMode);
			this.createSwatches_mode(this.slave,SJSwatchManager.slaveMode);
		}else if (inParent == this.slave){
			this.clearSwatches(this.slave);
			this.createSwatches_mode(this.slave,SJSwatchManager.slaveMode);
		}
	}else if (this.master == this.slave){
			this.clearSwatches(this.master);
			this.createSwatches();
	}
}

SJSwatchManager.prototype.createSwatches = function(){
	if (this.master != this.slave){
		this.createSwatches_mode(this.master,SJSwatchManager.masterMode);
		this.createSwatches_mode(this.slave,SJSwatchManager.slaveMode);
	}else if (this.master == this.slave){
		this.createSwatches_mode(this.master,SJSwatchManager.allMode);
	}
}

SJSwatchManager.allMode = 0;
SJSwatchManager.masterMode = 1;
SJSwatchManager.slaveMode = 2;

SJSwatchManager.prototype.createSwatches_mode = function(parentDiv,mode){
	var obj = null;
	var imageList = this.mainImageList;
	var imageListIndices = new Array();
	var swArray = new Array();
	var maxx=-1;
	var maxy=-1;
	var level=0;
	var x = 0;
	var y = 0;
	var horiz = true;
	var maxCols = 0;
	var maxRows = 0;

	var cellSpacing = this.swCellSpacing;
	if (cellSpacing != null) {
		var tempStr=cellSpacing.split(",");
			swhorizontalSpacing = parseInt(tempStr[0]);
			swverticalSpacing = parseInt(tempStr[1]);
	}

	if (this.swLayout != null) {
		var tempStr=this.swLayout.split(",");
		if (tempStr !=null){
			if(tempStr[0]!=null){
				if(tempStr[0].toLowerCase() == "horiz")
					horiz = true;
				if(tempStr[0].toLowerCase() == "vert")
					horiz = false;
			}
			if(tempStr[1]!=null){
				maxCols = parseInt(tempStr[1]);
			}
			if(tempStr[2]!=null){
				maxRows = parseInt(tempStr[2]);
			}
		}
	}
	
	var xStopPrev = 0;
	var xStopNext = 0;
	var yStopPrev = 0;
	var yStopNext = 0;
	while (imageList.length > 0) {
		var matrix = this.getSwatchMatrix(imageList);
			if ((level != 0) || (mode != SJSwatchManager.slaveMode)){
				for (var row = 0; row < matrix.length; row++) {
					for (var col = 0; col < matrix[row].length; col ++) {
						var sw_imageListIndices = new Array();
						for(var n=0;n<imageListIndices.length;n++){
							sw_imageListIndices.push(imageListIndices[n]);
						}
						if (mode == SJSwatchManager.masterMode){
							var w = this.wp;
							var h = this.hp;
						}else if(mode == SJSwatchManager.slaveMode){
							var w = this.ws;
							var h = this.hs;
						}
						if (((maxCols == 0) || (x < maxCols)) && ((maxRows == 0) || (y < maxRows))) {
							var sw = new SjSwatch(this,parentDiv, matrix[row][col], sw_imageListIndices,level,row, col, matrix[row][col].selected,w,h);
							swArray.push({swatch:sw,x:x,y:y});
							if (x >= maxx) maxx = x;
							if (y >= maxy) maxy = y;
						}

						if (horiz) {
							x ++;
							if ((maxCols != 0) && (x >= maxCols)) {
								x = xStopPrev;
								y ++;
							}
							if (xStopNext < x) {
								xStopNext = x;
							}
						} else {
							y ++;
							if ((maxRows != 0) && (y >= maxRows)) {
								y = yStopPrev;
								x ++;
							}
							if (yStopNext < y) {
								yStopNext = y;
							}
						}
					}
					if (horiz) {
						if (x > 0) {
							x = 0;
							y ++;
						}
						if ((maxRows != 0) && (y >= maxRows)) {
							x = xStopNext;
							y = 0;
						}
						xStopPrev = xStopNext;
					} else {
						if (y > 0) {
							x ++;
							y = 0;
						}
						if ((maxCols != 0) && (x >= maxCols)) {
							y = yStopNext;
							x = 0;
						}
						yStopPrev = yStopNext;
					}
				}
			}
		if (mode == SJSwatchManager.masterMode){
			break;
		}
		level++;
		var selectedIndex = this.getSelectedImageNodeIdxForLevel(imageList);
		imageListIndices.push(selectedIndex);
		var selectedImageInfo = this.getImageNode(imageList, [selectedIndex]);
		imageList = selectedImageInfo.imageList;
	}//end of while

var html='<TABLE id="tbl_'+parentDiv+'" cellpadding="0" cellspacing="0" border="0">';
for (var r = 0; r <= maxy; r++) {
  html+='<TR valign=top>';
  for (var c = 0; c <= maxx; c++){
	   obj = null;
		for (var i = 0; i < swArray.length; i++) {
		  var x = swArray[i].x;
		  var y = swArray[i].y;
		  if ((x == c) && ( y == r)) {
			obj = swArray[i];
			break;
		  }
		}
      if (obj != null){
	    var s = obj.swatch.html;
		var labelStyle = ' font-size:'+this.swTextSize+'pt;font-family:'+this.swTextFontName+';';
		//var strLabel = '<font color=#'+this.swTextColor.substring(2)+'>'+obj.swatch.swatch.label+'</font>';
		var strLabel = '<font color='+this.swTextColor+'>'+obj.swatch.swatch.label+'</font>';
		if (this.swTextBold == "true"){
			strLabel = '<b>' + strLabel + '</b>';
		}
		if (this.swTextPosition.toLowerCase() == 'left'){
			html += '<TD align=right valign="middle" STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'right'){
			html += '<TD align=left valign="middle" STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'none'){
			html += '<TD align=center STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'top'){
			html += '<TD align=center valign="bottom" STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'bottom'){
			html += '<TD align=center valign="top" STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'only'){
			html += '<TD align=left STYLE="word-wrap:break-word;">';
		}else if (this.swTextPosition.toLowerCase() == 'tooltip'){
			html += '<TD align=center STYLE="word-wrap:break-word;">';
		}else{
			html += '<TD align=center STYLE="word-wrap:break-word;">';
		}
		html += '<table cellpadding="0" cellspacing="0" border="0" width="75">';
		html += '<tr><td align=center><img src='+this.zviewer.zviewer.codePath+"images/spacer.gif"+' width='+Math.round(swhorizontalSpacing/2)+' height='+Math.round(swverticalSpacing/2)+'></td><td></td><td></td></tr>';

		if (this.swTextPosition.toLowerCase() == 'left'){
			html += '<tr><td align=left style="'+labelStyle+'">'+ strLabel+'</td>';
		}else{
			html += '<tr><td></td>';
		}
		 if (this.swTextPosition.toLowerCase() == 'top'){
			html += '  <td align=center style="'+labelStyle+'">'+ strLabel + s;
		}else if (this.swTextPosition.toLowerCase() == 'bottom'){
			html += '  <td align=center style="'+labelStyle+'">'+ s + strLabel;
		}else if (this.swTextPosition.toLowerCase() == 'left'){
			html += '  <td align=center>'+ s;
		}else if (this.swTextPosition.toLowerCase() == 'right'){
			html += '  <td align=center>'+ s;
		}else if (this.swTextPosition.toLowerCase() == 'none'){ 
			html += '  <td align=center>'+ s;
		}else if (this.swTextPosition.toLowerCase() == 'tooltip'){ 
			if (obj.swatch.swatch.label != "") {
				html += '  <td title="'+ obj.swatch.swatch.label +'" align=center>'+ s;
			}else{
				html += '  <td align=center>'+ s;
			}
		}else if (this.swTextPosition.toLowerCase() == 'only'){
			html += '  <td align=center><A  style="'+labelStyle+'" id='+obj.swatch.id+' href=#>'+ strLabel+'</A>';
		}else {
			html += '  <td align=center>'+ s;
		}
		html += '  </td>';
		
		if (this.swTextPosition.toLowerCase() == 'right'){
			html += '  <td align=right style="'+labelStyle+'">' + strLabel+'</td></tr>';
		}else{
			html += '  <td></td></tr>';
		}
		html += '<tr><td></td><td></td><td><img src='+this.zviewer.zviewer.codePath+"images/spacer.gif"+' width='+Math.round(swhorizontalSpacing/2)+' height='+Math.round(swverticalSpacing/2)+'></td></tr>';
		html += '</table>' + '<\/TD>';
	  } else {
		html += '<TD align=center STYLE="word-wrap:break-word;">' +'&nbsp;' + '<\/TD>';
	  }
  }
  html+='<\/TR>';
}
html+='</TABLE>';
sjSetLayerHTML(parentDiv,html);

	for (var i = 0; i < swArray.length; i++) {
		var element=document.getElementById(swArray[i].swatch.id);
		if (element){
			element.ww=swArray[i].swatch;
			element.onclick = function (){
										this.ww.swatchManager.selectSwatch(this.ww._parent,this.ww.swatchManager.mainImageList, this.ww.imageListIndices, this.ww.row, this.ww.col);
									return false;
								   }
		}
/*	var elm=document.getElementById(swArray[i].swatch.id+'_img');
	if (elm){
		elm.id=swArray[i].swatch.id+'_img';
		elm.zviewer=this.zviewer;
		elm.onload = function (){
							//alert(this.id);
							fadeIn(this.id,0,this.zviewer.fadeTime);
							return false;
						 }

		}*/
	}
//sjOpacity(parentDiv,0);
//fadeIn(parentDiv,0);
//fadeOut(parentDiv,100);
	if (mode == SJSwatchManager.masterMode){
		sjSetWidth('swatchGrid'+this.suffix,sjGetElement("tbl_"+parentDiv).offsetWidth);
		sjSetHeight('swatchGrid'+this.suffix,sjGetElement("tbl_"+parentDiv).offsetHeight);
	}else if(mode == SJSwatchManager.slaveMode){
		sjSetWidth('swatchGrid2'+this.suffix,sjGetElement("tbl_"+parentDiv).offsetWidth);
		sjSetHeight('swatchGrid2'+this.suffix,sjGetElement("tbl_"+parentDiv).offsetHeight);
	}
}


function fadeIn(objId,opacity,tm) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 10;
			window.setTimeout("fadeIn('"+objId+"',"+opacity+")", tm);
		}
	}
}

function fadeOut(objId,opacity,tm) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity > 0) {
			setOpacity(obj, opacity);
			opacity -= 10;
			window.setTimeout("fadeOut('"+objId+"',"+opacity+")", tm);
		}
	}
}

function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	obj.style.filter = "alpha(opacity:"+opacity+")";
	obj.style.KHTMLOpacity = opacity/100;
	obj.style.MozOpacity = opacity/100;
	obj.style.opacity = opacity/100;
}


SJSwatchManager.prototype.clearSwatches = function (inParent){
	sjSetLayerHTML(inParent,"");
}

function SjSwatch(inSwatchManager,inParent, inSwatch, inImageListIndices,inLevel, inRow, inCol,inSelected,inWidth,inHeight) {
	this.html ="";
	this.swatchManager = inSwatchManager;
	this._parent = inParent;
	this.swatch = inSwatch;
	this.imageListIndices = inImageListIndices;
	this.row = inRow || 0; 
	this.level = inLevel || 0; 
	this.col = inCol || 0;
	this.width = inWidth || 50; 
	this.height = inHeight || 50;
	this.selected = inSelected || false;
	this.id = this._parent+this.level+"x"+this.row+"x"+this.col;//corrected id
	this.html ='<table id='+this.id+' WIDTH='+ this.width +' HEIGHT=' + this.height;
	this.html +=' style="border:'+(this.selected ?inSwatchManager.swHighlightThickness:0)+'px solid '+inSwatchManager.swHighlightColor+';" cellspacing="0" cellpadding="0"'+'>';   
	this.html +='<tr><td><table cellspacing="0" cellpadding="0" style="border:'+inSwatchManager.swBorderThickness+'px solid '+inSwatchManager.swBorderColor+';"';
	this.html +=' WIDTH='+ this.width +' HEIGHT=' + this.height+'>';
		if (this.swatch.name != '') {                         
			this.src = this.swatchManager.zviewer.baseServerUrl+"/" + this.swatch.name;
			this.src += sjPBreak(this.src)+ 'req=tmb&wid=' + this.width + '&hei=' + this.height;
			var version = parseFloat(navigator.appVersion.split("MSIE")[1]);
					this.html +='<TR><TD align=center>';
					this.html +='<img id='+this.id+'_img';
//			if ((version >= 5.5) && (document.body.filters) && (this.src.toUpperCase().indexOf("PNG-ALPHA") != -1)){
//			if ((version >= 5.5) && (version < 8) && (document.body.filters) && (this.swatchManager.zviewer.zviewer.transparency)){
				var haveFilters = null;
				if ((version >= 5.5) && (version < 8)){
						try {
							haveFilters = document.body.filters;
						}catch(e){
							//alert();
						}
				}
				if ((version >= 5.5) && (version < 8) && (haveFilters != null) && (this.swatchManager.zviewer.zviewer.transparency)){
					this.html +=' src='+this.swatchManager.zviewer.zviewer.codePath+"images/spacer.gif";
					this.html +=' width=' + this.width; 
					this.html +=' height=' + this.height; 
					this.html +=' border="0"'; 
					this.html +=' style=visibility:hidden;filter:progid:DXImageTransform.Microsoft.AlphaImageLoader('; 
					this.html +='src="' + this.src + '",sizingMethod="scale");'; 
					this.html +=' onload=(this.style.visibility="inherit") >'; 
			}else{
					this.html +=' src="';
					this.html +='' + this.src+'"'; 
					this.html +=' width=' + this.width; 
					this.html +=' height=' + this.height; 
					this.html +=' border="0"'; 
					this.html +=' style="visibility:hidden;"'; 
					this.html +=' onload=(this.style.visibility="inherit") >'; 
			}
		}else if (this.swatch.color != ''){
			this.html +='<TR><TD align=center BGCOLOR='+this.swatch.color;
			this.html +=' >'; 
		}
	this.html +='</td></tr></table>';
	this.html +='</td></tr></table>';
};
]';/.