var imgSrc, zoom;
var portHeight, portWidth;
var defaultPlayerName = 'flex/player/zoomplayer_1_5.swf';
var defaultNavName = 'flex/player/zoomnav3.swf';
var defaultLinkName = '/flex/player/zoomlink.swf';
var baseDomain = "yourzoom.com";
var downloadHost = "cdn";
var dataHost = "yzos.s3.amazonaws.com";
var downloadThreads = 4;

function writeTarget( channel, imageDescription, imageUrl, imageWidth, imageHeight, t, b, l, r, initialImageName, initialImageFitWidth, initialImageFitHeight ) {
	document.write( '<a href="#" onclick="loadTarget(\'' + channel + '\', \'' + imageUrl + '\',' 
			+ imageWidth + ', ' + imageHeight + ','
			+ t + ',' + b + ',' + l + ',' + r 
			+ ",\'" + initialImageName +'\',' + initialImageFitWidth + ',' + initialImageFitHeight
			+ ' );return false;">' );
	document.write( ' <img alt="" style="border-color: #D1D1D1; border-width: 1; border-style: solid" src="' + imageUrl + '/[crop,top=' + t + ',bottom=' + b 
	  + ',left=' + l + ',right=' + r + '&size=90w].jpg">' ); 
	document.write( '</a>' ); 
}	

function writeAlternateView( channel, imageName, imageUrl, imageWidth, imageHeight, initialImageName, initialImageFitWidth, initialImageFitHeight ) {
	document.write( '<a href="#" onclick="loadTarget(\'' + channel + '\', \'' + imageUrl + '\', ' + imageWidth + ', ' + imageHeight + ', null, null, null, null,\'' + initialImageName +'\',' + initialImageFitWidth + ',' + initialImageFitHeight
		  + ' );return false;">' );
		  
	document.write( ' <img alt="" style="border-color: #D1D1D1; border-width: 1; border-style: solid" src="' + imageUrl + '/[fit=100w100h].jpg">' ); 
	document.write( '</a>' ); 
}	

/*
 * Writes the flash player plugin code, where:
 *  imagePath: full or relative path to the reimg directory
 *  vw: viewer width
 *  vh: viewer height
 *  playerPath: full or relative path to the zoomplayer swf.  Defaults to ../player-1.0/*.swf
 */
function writeZoomWindow( channel, autostart, imagePath, imageWidth, imageHeight, vw, vh, playerPath ) {
	var imgUrl;
	if (playerPath == null || playerPath == '') {
	  playerPath = imagePath + "/" + defaultPlayerName;
	}
	if (imagePath != null && imagePath != "") {
		vars = "channel=" + channel + "&baseDomain=" + baseDomain + "&downloadThreads=" + downloadThreads + "&downloadHost=" + downloadHost + "&dataHost=" + dataHost + "&autostart="+autostart+"&imageName=" 
		+ imagePath + "&imageWidth=" + imageWidth + "&imageHeight=" + imageHeight ;
	}
	else {
		vars = "channel=" + channel + "&baseDomain=" + baseDomain + "&downloadThreads=" + downloadThreads + "&downloadHost=" + downloadHost + "&dataHost=" + dataHost + "&autostart="+autostart;
	}
	flashEmbed( 'zoomimg', playerPath, vw, vh, vars);
}

function writeZoomNav( channel, imagePath, vw, vh, navPath ) {
	var imgUrl;
	var vars;
	if (navPath == null || navPath == '') {
	  navPath = imagePath + "/" + defaultNavName;
	}
	
	if (imagePath != null && imagePath != "") {
		vars = "channel=" + channel + "&imageName=" + imagePath;
	}
	else {
		vars = "channel=" + channel;
	}
	flashEmbed('zoomNav', navPath, vw, vh, vars);
}

function initZoom( zoom, 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", "channel=" + channel + "&method=tweenTo&p=" + p + "&cx=" + cx + "&cy=" + cy );
}

function zoomToFit( channel, top, bottom, left, right ) {
	sendFlashMethod( "zoom", "channel=" + channel + "&method=zoomToFit&left=" + left + "&top=" + top + "&right=" + right + "&bottom=" + bottom );
}

function loadImage( channel, imageUrl ) {
	showZoom();
	sendFlashMethod( "zoom", "channel=" + channel + "&method=loadImage&imgName=" + imageUrl  );
}

function loadTarget( channel, imageUrl, imageWidth, imageHeight, top, bottom, left, right, initialImageName, initialImageFitWidth, initialImageFitHeight ) {
	showZoom();
	sendFlashMethod( "zoom", "channel=" + channel + "&method=loadTarget&imgName=" + imageUrl
	  + "&width=" + imageWidth + "&height=" + imageHeight 
		+ "&left=" + left + "&top=" + top + "&right=" + right + "&bottom=" + bottom
		+ "&initialImageName=" + initialImageName + "&initialImageFitWidth=" + initialImageFitWidth + "&initialImageFitHeight=" + initialImageFitHeight );
}

function writeMovieWindow( channel, movieUrl, vw, vh, playerPath ) {
	vars = "channel=" + channel; //  + movieUrl;
  flashEmbed( 'yzmovie', playerPath, vw, vh, vars);
}

function writeLoadMovie( channel, movieUrl, thumbUrl, length, position ) {
  document.write( '<a href="#" onclick="loadMovie(\'' + channel + '\', \'' + movieUrl + '\', ' + position + ' );return false;">' );
  document.write( ' <img alt="" style="border-color: #D1D1D1; border-width: 1; border-style: solid" src="' + thumbUrl + '">' );
  document.write( '</a>' );
  document.write( '<div class="notes">' + formatMillis(position) + '/' + formatMillis(length) + '</div>' );
}
	
function loadMovie( channel, movieUrl, position ){
  showMovie();
  sendFlashMethod( "yzmovie", "channel=" + channel + "&method=loadMovie&movieUrl=" + movieUrl + "&position=" + position  );
}

function formatMillis(millis) {
      var t = Math.floor((millis/10) % 100);
      var secs = Math.floor(millis/1000);
      var s = secs % 60;
      var m = Math.floor((secs % 3600 ) / 60);
      var h = Math.floor(secs / (60*60));
      var hs = (h<10 ? "0"+h : h) +":";
      if (h==0) hs="";
      var ms = (m<10 ? "0"+m : m) + ":";
      if (m==0) ms="";
      var ss = (s<10 ? "0"+s : s ) + ".";
      var hns = "" + (t<10 ? "0"+t : t );
      return hs+ms+ss+hns;
}

function showMovie() {
  document.getElementById("movieCell").style.top='0px';
  document.getElementById("zoomCell").style.top='-10000px';
}
function showZoom() {
  document.getElementById("movieCell").style.top='-10000px';
  document.getElementById("zoomCell").style.top='0px';
}

function flashEmbed(id, movie, width, height, flashVars) {
  document.write( '<OBJECT  classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=8,0,0,0" WIDTH="' + width + '" HEIGHT="' + height + '" id="' + id + '" ALIGN="">');
  document.write( '   <PARAM NAME="movie" VALUE="' + movie + '">' );
  document.write( '   <PARAM NAME="quality" VALUE="high">' );
  document.write( '   <PARAM NAME="allowScriptAccess" VALUE="always">' );
  document.write( '   <PARAM NAME="FlashVars" VALUE="' + flashVars + '">' );
  document.write( '   <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + movie + '" FlashVars="' + flashVars + '" quality="high" bgcolor="#FFFFFF" width="' + width + '" height="' + height +'" name="' + id + '" allowScriptAccess="always" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer">' );
  document.write( '</OBJECT>' );
}

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 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;
		if(!document.getElementById(divcontainer)){
			var divholder = document.createElement("div");
			divholder.id = divcontainer;
			window.document.body.appendChild(divholder);
		}
		
		var divinfo = "<embed src='" + defaultLinkName + "' FlashVars='" + flashquery +"' width='0' height='0' type='application/x-shockwave-flash'></embed>";
		window.document.getElementById(divcontainer).innerHTML = divinfo;
	}
}

function replaceImg() {
	// do nothing
}


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



