/*

@@@BUILDINFO@@@ kuler5.jsf 3.0.0.12 16-September-2009

*/




//Point Product: FW



// constants used in script

var CONST_STROKE = 1;

var CONST_FILL 	= 2;



//############ PARSING FUNCTIONS ###################

var trim = function (str) {

    return str.replace(/^\s*/, "").replace(/\s*$/, "");

}

var removeNewLine = function(str){

		return str.replace(new RegExp( "\\n", "g" ), " ");

}



var getTabValue = function(strXML, strTab, bUnTrimed){

	var openTag = "<"+strTab+">";

	var closeTag = "</"+strTab+">";

	var len = strXML.indexOf(closeTag ) - strXML.indexOf(openTag) - 2 - strTab.length;

	var strValue;

	if('undefined' == typeof bUnTrimed || !bUnTrimed )

		strValue = trim(strXML.substr(strXML.indexOf(openTag)+2+strTab.length, len));

	else

		strValue = strXML.substr(strXML.indexOf(openTag)+2+strTab.length, len);

	return strValue;

}

var getColorFromXML = function(strXML){

	var col = {};

	strXML = strXML.toLowerCase();

	col.r = Math.round(parseFloat (getTabValue(strXML, "r")) * 255);

	col.g = Math.round(parseFloat (getTabValue(strXML, "g")) * 255);

	col.b = Math.round(parseFloat (getTabValue(strXML, "b")) * 255);

	return col;

}



var getSwatches = function(strXML){

	return trim(getTabValue(strXML, "swatches"));

}

var getName = function(strXML){

	return trim(getTabValue(strXML, "label"));

}

var getSwatch = function(strSwatches, iIndx){

	// Skipping iIndx-1 swatches

	for(var iCnt=0; iCnt<iIndx; iCnt++){

		strSwatches = trim(strSwatches);

		strSwatches = strSwatches.substring(getTabValue(strSwatches, "swatch", true).length + "<swatch>".length + "</swatch>".length);

	}

	return getTabValue(strSwatches, "swatch");

}



var getRGBColorFromSwatch = function(strSwatch){

	var col = {};

	var strHexValue = getTabValue(strSwatch,"hexColor");

	if(null != strHexValue){

		col.r = parseInt(strHexValue.substring(0,2), 16);

		col.g = parseInt(strHexValue.substring(2,4), 16);

		col.b = parseInt(strHexValue.substring(4,6), 16);

	}else{

		col.r=0; col.g=0; col.b=0;

	}	

	return col;

}



//############ PARSING FUNCTIONS - END ###################

//parses encoded strings passed from the SWF

var kulerParse = function(fileBinary)

{

	var val = fileBinary;

	var charmark = 4;

	var retStr = new String();

	var i = 0;

	for(i=0; i<val.length; i+=4){

		var onebyte = ((val.charCodeAt(i) - 65) << 12) | ((val.charCodeAt(i+1) - 65)<<8) | ((val.charCodeAt(i+2) - 65)<<4) | (val.charCodeAt(i+3) - 65);

		var onechar = String.fromCharCode(onebyte);	

		retStr += onechar;

	}

	return retStr;

}



// function to add color values to Swatches in FW

var kulerAddToSwatch = function(strData)

{

	var strResult = '<object><property id="bSuccess">';

	var bSwatchAddSuccess = false;

	try{

		

		if('undefined' != typeof fw) {

			var arrColor = new Array();

			var strXML = kulerParse(strData);

			

			var strLabel = getName(strXML);

			var strSwatches = removeNewLine(getSwatches(strXML));

			

			var iCnt = 0;

			var strSwatch = getSwatch(strSwatches, iCnt);

			while('undefined' != typeof strSwatch && null != strSwatch && "" != strSwatch)

			{

				// Valid Swatch tab found.. Take out the color values and add it to the array

				var col = getRGBColorFromSwatch(strSwatch);

				if(null != col && "" != col){

					var red = col.r;

					var green = col.g;

					var blue = col.b;

					arrColor[iCnt] = fw.makeRGBColor(red, green, blue);

				}

				else{

					break;

				}

				strSwatch = getSwatch(strSwatches, ++iCnt)

			}

			if( (iCnt != 0) && fw.addSwatches(strLabel, arrColor)){

				strResult += '<true/></property>';

				strResult += '<property id="strError"><string></string></property>';

				bSwatchAddSuccess = true;

			}

		}

	}catch(err){ bSwatchAddSuccess = false; }



// If not able to add to Swatches.

	if(!bSwatchAddSuccess){

		strResult = '<object><property id="bSuccess">';

		strResult += '<false/></property>';

		strResult += '<property id="strError"><string>Not Able to Add Swatches to the panel</string></property>';

	}



	strResult += '</object>';

	return strResult;

}



// Used as a check to see if any document is open or not.

var kulerGetDocColorSpace = function()

{

	var strXMLResult = '<object><property id="bSuccess">';

	var bSuccess = false;

	try{

		if('undefined' != typeof fw && null != fw.getDocumentDOM() ){

			strXMLResult += '<true/></property>';

			strXMLResult += '<property id="strError"><string></string></property>';

			strXMLResult += '<property id="strDocColorSpace"><string>rgb</string></property>';

			bSuccess = true;

		}

	}catch(err){ 

		bSuccess = false; 

	}

	

	if(!bSuccess){

		strXMLResult = '<object><property id="bSuccess">';

		strXMLResult += '<false/></property>';

		strXMLResult += '<property id="strError"><string>Unable to retrieve Doc Info</string></property>';

	}

	strXMLResult += '</object>';

	return strXMLResult;

}



// Private function to get Fill/Stroke color from FW

var getFillStrokeColor = function(iColType){

	var strXMLResult = '<object><property id="bSuccess">';

	var bsuccess = false;

	try{

		if('undefined' != typeof fw){

			var doc = fw.getDocumentDOM();

			if(null != doc)

			{

				var pathAttrs = doc.pathAttributes;

				if(null != pathAttrs)

				{

					var col = null;



					if(iColType == CONST_FILL){

					 if(null != pathAttrs.fill && "fc_Solid" == pathAttrs.fill.category){

					   col = pathAttrs.fillColor;						// Case Fill Color

					 }

					}else if(iColType == CONST_STROKE){

					 if(null != pathAttrs.brush){

					   col = pathAttrs.brushColor;					  // Case Stroke Color

					 }

					}

					if(null != col)

					{

						var r = fw.getRed(col)/255.0;

						var g = fw.getGreen(col)/255.0;

						var b = fw.getBlue(col)/255.0;

						strXMLResult += '<true/></property>';

						strXMLResult += '<property id="strError"><string></string></property>';

						strXMLResult += '<property id="color"><object>';

						strXMLResult += '<property id="r"><number>' +r+ '</number></property>';

						strXMLResult += '<property id="g"><number>' +g+ '</number></property>';

						strXMLResult += '<property id="b"><number>' +b+ '</number></property>';

						strXMLResult += '</object></property>';

						bsuccess = true;

					}

				}

			}

		}

	}

	catch(err){ ; /* Do Nothing.. bSuccess will be false */ }

	

	if(!bsuccess){

		strXMLResult = '<object><property id="bSuccess">';

		strXMLResult += '<false/></property>';

		if(iColType == CONST_FILL){

			strXMLResult += '<property id="strError"><string>FillWrongSwatch</string></property>';

		}else{

			strXMLResult += '<property id="strError"><string>StrokeWrongSwatch</string></property>';

      	}

	}

	strXMLResult += '</object>';

	return strXMLResult;

}	



var kulerGetFillForeColor = function()

{

	return getFillStrokeColor(CONST_FILL);

}	



var kulerGetStrokeBackColor = function()

{

	return getFillStrokeColor(CONST_STROKE);

}	



var kulerSetActiveColor = function(strData)

{

	var strResult = '<object><property id="bSuccess">';

	var bSuccess = false;

	try{

		if('undefined' != typeof strData && 'undefined' != typeof fw){

		

			var col = getColorFromXML(kulerParse(strData));

			if(fw.setActiveColor(fw.makeRGBColor(col.r, col.g, col.b))){

				strResult += '<true/></property>';

				strResult += '<property id="strError"><string></string></property>';

				bSuccess = true;

			}

		}

	}catch(err){ 

		bSuccess=false; 

	}



	if(!bSuccess){

		strResult = '<object><property id="bSuccess">';

		strResult += '<false/></property>';

		strResult += '<property id="strError"><string>Not Able to Set The Active Color</string></property>';

	}

	strResult += '</object>';

	return strResult;

}

