///////////////
//private functions and variables
var sjCallbacks=new Object();//!global variable MUST be created !
var sjErrCallbacks=new Object();//!global variable MUST be created !
var parentSwatches = '';



 function setOptions(chosen,ID) {
      s7zoom.setImage(S7ConfigClient.isRoot + company + '/' + chosen);
		document.getElementById("altviews").innerHTML=dumpmyProps(imagset[ID]);
    }
    
    
/*
parseImageSets

A function used to split a comma separated list of image sets into an array for reference to the viewer and json calls

input: a comma seperated list of imagesets referencing only the set (not the company)
output:  none, this function writes to a globally defined object imagset_test
*/                                                                                                                                                                                                                                 

function parseImageSets(sets)  { 
var b = sets;
b += "";  //for some reason JavaScript doesn't like splitting the return from getParam on a comma, but it works fine after performing a string operation on it.

imagset_test = b.split(',');  //split the list of imagesets on each comma and return each individual imagest to an index within the globally declared imagset_test Object

return;
}

/*
initializeJSON

A function used to make the initial calls to load imageset properties via JSON from the image server and perform the necessary steps to parse that data into useful objects for reference by the page

input:  string of comma separated Image Sets
output:  None
usage notes:  In this code the initializeJSON method should be called when declaring the image argument that will be passed to the viewer
				  the function will return a comma separated list of company/imagesets that can then be passed to the viewer, and will as side
				  effect perform the other necessary actions in order to initialize the page with JSON support
*/
             
function initializeJSON(set){

parseImageSets(set);  //Break out comma seperated list of imagesets to the globally defined imagset_test object

var setCount=imagset_test.length-1;  //variable to store how many sets were passed to the viewer

for (var i = 0; i <= setCount; i++) {  //this code makes JSON requests to the image server to retrieve the images for each set passed to the page and load them into the same array for reference by the viewer
    loadimagset(company + '/' + imagset_test[i],i);
 }
 
return;

} //End Initialize JSON


function sjGetResponseLocal(inReq, inImg, inCallback, inErrCallback) {

	var tempi = inImg.indexOf("?");
	var tempmod = "";
	if(tempi >= 0){
		tempmod = inImg.substring(tempi+1);
		inImg = inImg.substring(0,tempi);
	}
	
	var urljson = S7ConfigClient.isRoot + '/' + inImg + '?' + inReq;
	
	if (tempmod){
		urljson += '&' + tempmod;
		
	}
	
	var id = sjHashCode(urljson);
	urljson += '&id=' + id;
	if (typeof inCallback != 'undefined'){
		sjCallbacks[id] = inCallback;
	}
	if (typeof inErrCallback != 'undefined'){
		sjErrCallbacks[id] = inErrCallback;
	}
	document.write("<script language='javascript' src='" + urljson + "'><\/script>");
}

function s7jsonResponse(inArg, inId) {
	sjCallbacks[inId](inArg);
}

function s7jsonError(inArg, inId) {
	if (typeof sjErrCallbacks[inId] != 'undefined'){
		sjErrCallbacks[inId](inArg);
	}else{
		alert(inArg.message);
	}
}

function sjDebug(inPsResponse, inJsonResponse, inPsResponseParserName, inPsRequest) {
}

function sjHashCode(d) {//unix style
	if (!d || d=="") return 1;
	var h=0,g=0;
	for (var i=d.length-1;i>=0;i--) {
		var c=parseInt(d.charCodeAt(i));
		h=((h << 6) & 0xfffffff) + c + (c << 14);
		if ((g=h & 0xfe00000)!=0) h=(h ^ (g >> 21));
	}
	return h;
}

function loadimagset(inURL,isetid) {
	sjGetResponseLocal(
		'req=imageset,json',
		inURL,
		function(inArg) {
			imagset[isetid]=inArg;
		},
		function(inArg) {
			alert('failed loading imageset for [' + inURL + ']: ' + inArg.message);
		}
	);
}

function dumpmyProps(obj) {
	var i ="";
      var x=0;
      var y=0;
      var z=0;
      var altView="";
      var retstr="";
 	for (i in obj){
            x=obj[i].indexOf(";",0);
            y=obj[i].indexOf(",",0);
            if (y==-1) {
                y=obj[i].length;
            }
            while (x>0)
            {  
                altView=obj[i].substring(x+1,y);   
                altView=S7ConfigClient.isRoot + '/' +altView;
                altImageURL[z]=altView + '?resMode=sharp2&op_usm=0.9,1.9,8,0';
                retstr=retstr + "<div class=\"img_unslctd\" id=\"othr_img_" + z + "\"><img src=\"" + altView + "?resMode=sharp2&op_usm=0.9,1.9,8,0&wid=49&hei=49\" border=\"0\" alt=\"\" width=\"49\" height=\"49\" class=\"img_thumb\" onclick=\"javascript:selectImg('"+ z + "');\"/></div>"; 
                x=obj[i].indexOf(";",y);
                y=obj[i].indexOf(",",y+1);
                if (y==-1) {
                    y=obj[i].length;
                } 
                z++;
            }
	} 
	return retstr;
	
}


