var imgSrc, zoom;
var portHeight, portWidth;
var zoomPlayerName = '../player-1.0/zoomimg.swf';
var zoomNavName = '../player-1.0/zoomnav3.swf';

function writeTarget( i, w, h, t2z, t2t, t2b, t2l, t2r ) {
	document.write( '<a href="#" onclick="initZoom( \'' + i +'\',' + w + ',' + h + ',' 
	  + t2z + ',' + t2t + ',' + t2b + ',' + t2l + ',' + t2r + ' );replaceImg();return false;">' );
	document.write( ' <img alt="" border=0 src="' + i + '?crop,top=' + t2t + ',bottom=' + t2b 
	  + ',left=' + t2l + ',right=' + t2r + '&size=90w">' ); 
	document.write( '</a>' ); 
}	

/*
 * Writes the flash player plugin code, where:
 * 	i : imageName of full sized source img
 *  w : width of full sized source img
 *  h : height of full sized source img
 *  vw: viewer width
 *  vh: viwer height
 *  ili: instant-load img name (optional)
 *  ild: instant-load dimensions in the format '123w456h' (optional, but required if ili is passed)
 */
function writeZoomWindow( path, vw, vh ) {
	var imgUrl;
	if (path != null && path != "") {
		imgUrl = path + "/" + zoomPlayerName + "?imageName=" + path;
	}
	else {
		imgUrl = zoomPlayerName;
	}
	//alert( "imgUrl = " + imgUrl );
	document.write( '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' + vw + '" HEIGHT="' + vh + '" id="zoomimg" ALIGN="">');
    document.write( '   <PARAM NAME="movie" VALUE="' + imgUrl + '">' );
    document.write( '   <PARAM NAME="quality" VALUE="high">' );
    document.write( '   <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + imgUrl + '" quality="high" bgcolor="#FFFFFF" width="' + vw + '" height="' + vh +'" name="zoomimg" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">' );
    document.write( '</OBJECT>' );
}

function writeZoomNav( path, vw, vh ) {
	var imgUrl;
	if (path != null && path != "") {
		imgUrl = path + "/" + zoomNavName + "?imageName=" + path;
	}
	else {
		imgUrl = zoomNavName;
	}
	//alert( "imgUrl = " + imgUrl );
	document.write( '<OBJECT classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" WIDTH="' + vw + '" HEIGHT="' + vh + '" id="zoomimg" ALIGN="">');
    document.write( '   <PARAM NAME="movie" VALUE="' + imgUrl + '">' );
    document.write( '   <PARAM NAME="quality" VALUE="high">' );
    document.write( '   <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + imgUrl + '" quality="high" bgcolor="#FFFFFF" width="' + vw + '" height="' + vh +'" name="zoomimg" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">' );
    document.write( '</OBJECT>' );
}

function initZoom( initImgSrc, initImgHeight, initImgWidth, 
		initZoom, initCropTop, initCropBottom, initCropLeft, initCropRight )
{
	var p, cx, cy;
	
	p = initZoom * 100;
	cx = Math.round( initCropLeft + (initCropRight-initCropLeft)/2 );
	cy = Math.round( initCropTop + (initCropBottom-initCropTop)/2);

	sendFlashMethod( "zoom", "method=tweenTo&p=" + p + "&cx=" + cx + "&cy=" + cy );
	
}

// copied/modified from http://www.mustardlab.com/developer/flash/jscommunication/
// -----------------------------------------------------------
// Univeral method for javascript->flash setvariable
// -----------------------------------------------------------

/* -----------------------------------------------------------
----------Supporting Browsers----------
PC:
IE 5 and higher
Netscape 6 and higher
Moz/Firebird all
Opera 7 and higher

Mac OSX:
IE 5.2
Safari all
Netscape 6 and higher
Moz/Firebird/Camino all
Opera 6 and higher

Linux:
Konqueror assumed
----------------------------------------------------------- */

// -----------------------------------------------------------
// Detection snippet from http://www.dithered.com/javascript/browser_detect/index.html
// -----------------------------------------------------------

var ua        = navigator.userAgent.toLowerCase(); 
var is_pc_ie  = ( (ua.indexOf('msie') != -1 ) && ( ua.indexOf('win') != -1 ) && ( ua.indexOf('opera') == -1 ) && ( ua.indexOf('webtv') == -1 ) );

/* -----------------------------------------------------------
function setFlashVariables(movieid, flashquery)

movieid: id of object tag, name of movieid passed in through FlashVars
flashquery: querystring of values to set. example( var1=foo&var2=bar )
----------------------------------------------------------- 
*/

function sendFlashMethod(movieid, flashquery){
	var i,values;
	// disable ie specific - at least for now
	if(false && is_pc_ie){
		//alert( "not done yet" );
		var chunk = flashquery.split("&");
		for(i in chunk){
			values = chunk[i].split("=");
			document[movieid].SetVariable(values[0],values[1]);
		}
	}else{
		var divcontainer = "flash_setvariables_"+movieid;
		//alert( "divcontainer is: " + divcontainer );
		if(!document.getElementById(divcontainer)){
			//alert( "creating divcontainer: " + divcontainer );
			var divholder = document.createElement("div");
			divholder.id = divcontainer;
			//alert( "divholder.id = " + divholder.id);
			//alert( "doc.innerHTML = " + window.document.body.innerHTML);
			window.document.body.appendChild(divholder);
		}
		//window.document.getElementById(divcontainer).innerHTML = "foo!";
		//alert( "divcontainer.innerHTML: " + window.document.getElementById(divcontainer).innerHTML );		
		
		var divinfo = "<embed src='/reimg/zoom/zoomlink.swf' FlashVars='" + flashquery +"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
		window.document.getElementById(divcontainer).innerHTML = divinfo;
		//alert( "divcontainer.innerHTML: " + window.document.getElementById(divcontainer).innerHTML );		
	}
}

function replaceImg() {
	// do nothing
}


function openYourZoomWindow(winURL,winName,winTitle) {
	var ieIncrement = ((navigator.appName+"").indexOf("Netscape") == -1)? 15:0;
	var width = 560, height = 550;
	var newWin;
	newWin = window.open(winURL, "", "resizable=yes,scrollbars=yes,width=" + (width + ieIncrement) + ",height=" + (height + ieIncrement) + ",top=5,left=75");
	newWin.focus();
};

/*
 * Client-side tools for parsing the querystring 
 *
 * Usage:
 * 1. Include this file (must be before the function below is used)
 * 2. To read querystring parameter 'paramX' into variable 'varX' call as:
 *		var varX = querystring("paramX");
 */

function querystring(key) 
{ 
	var value = null; 
	for (var i=0;i<querystring.keys.length;i++)  { 
		if (querystring.keys[i]==key) { 
			value = querystring.values[i]; 
			//alert( key + " = " + value );
			break; 
		} 
	} 
	return value; 
} 

querystring.keys = new Array(); 
querystring.values = new Array(); 

function querystring_Parse() { 
	var query = window.location.search.substring(1); 
	var pairs = query.split("&"); 

	for (var i=0;i<pairs.length;i++) { 
		var pos = pairs[i].indexOf('='); 
		if (pos >= 0) { 
			var argname = pairs[i].substring(0,pos); 
			var value = pairs[i].substring(pos+1); 
			querystring.keys[querystring.keys.length] = argname; 
			querystring.values[querystring.values.length] = value; 
		} 
	} 
} 
querystring_Parse(); 


