///////////////
//private functions and variables
var sjCallbacks=new Object();//!global variable MUST be created !
var sjErrCallbacks=new Object();//!global variable MUST be created !


 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

//dynamically generate parent swatch images based on number of sets passed to the page in the query string
  
 for (var i = 0; i <= setCount; i++) {  //For loop that executes as many times as there are imagesets declared to the viewer
   
   /*
     Generate the proper image in the page with a javascript call to the function that changes out the image through the setColor function
   */
   
   parentSwatches += '<a href="javascript:setOptions(' + "'" + imagset_test[i] + "'," + i + ')">' + '<img src="' + S7ConfigClient.isRoot + company + '/' + imagset_test[i] + '?wid=50&hei=50&crop=1000,1000,50,50" border=0 alt=""/>' + '</a>';

}

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)
            {  
                z++;
                altView=obj[i].substring(x+1,y);   
                altView=S7ConfigClient.isRoot + '/' +altView;
                altImageURL[z]=altView;
               // retstr=retstr + '<a href="javascript:s7zoom.setImage(altImageURL[' + z + '])">';
			    retstr=retstr + '<a class="MainImgThumb" href="javascript:changeMainImg('+ z + ')">';
                retstr=retstr + '<img src="' + altView + '?wid=85" border=0 alt=""/>'; 
                retstr=retstr + '</a>'; 

                x=obj[i].indexOf(";",y);
                y=obj[i].indexOf(",",y+1);
                if (y==-1) {
                    y=obj[i].length;
                }
            }
	} 
	return retstr;
}

/*function dumpmyPropsMain(obj, inImgURL) {
	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)
            {  
                z++;
                altView=obj[i].substring(x+1,y);   
                altView=S7ConfigClient.isRoot + '/' +altView;
                altImageURL[z]=altView;

				retstr=retstr + '<div class="ImageDetailLinkContainer" id="ImageDetailLink' + z + '"> ';
				if(z == 1)
					retstr=retstr + 'style="display:block;">'; 
				else
					retstr=retstr + 'style="display:none;">';
					
			  	retstr=retstr + '<a href="' + altView + '?wid=800&hei=800" class="ImageDetail" title="Image Detail" style="text-align:center;margin-left:auto;margin-right:auto;"><img src="' + inImgURL + altView + '?$ProductPageMain$&1" /></a>';	
    
				retstr=retstr + '</div>';
	
				x=obj[i].indexOf(";",y);
                y=obj[i].indexOf(",",y+1);
                if (y==-1) {
                    y=obj[i].length;
                }
            }
	} 
	return retstr;
}*/

function dumpmyPropsMain2(obj, inImgURL) {
	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)
		{  
			z++;
			altView=obj[i].substring(x+1,y);   
			altView=S7ConfigClient.isRoot + '/' +altView;
			altImageURL[z]=altView;

			retstr=retstr + '<a href="' + altView + '?wid=800&hei=679" id="' + altView + '" class="ImageDetail" title="Image Detail" style="text-align:center;margin-left:auto;margin-right:auto;"><img src="' + altView + '?$ProductPageMain$&1" /></a>';	

			x=obj[i].indexOf(";",y);
			y=obj[i].indexOf(",",y+1);
			if (y==-1) {
				y=obj[i].length;
			}
		}
	} 
	return retstr;
}