// ***************************************
// NOTICE OF COPYRIGHT
//
// Permission is granted to use and
// distribute this work in conjunction
// with licensed server software from DepLabs, Inc.
//
// Except as permitted above no part of
// this work may be reproduced in whole
// or in part in any manner without the
// permission of the copyright owner.
//
// Copyright (C) 2004 By DepLabs, Inc.
// ***************************************

/**
 * Creates the appropriate viewer by reading the querystring and/or detecting
 * browser capabilities.
 *
 * viewerWidth : width of the viewer on the page
 * viewerHeight: height of the viewer on the page
 * imgSrc: full path to the initial image to be shown in the viewer
 * imgWidth: width of the initial image
 * imgHeight: height of the initial image
 *
 *  ili: instant-load img name (optional)
 *  ild: instant-load dimensions in the format '123w456h' (optional, but required if ili is passed)
 */
function createViewer( viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ,channel ) {
    var qs = querystring("flash");

    if (qs && qs.toLowerCase() == "yes" ) {
        return new FlashViewer(viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ,channel);
    }
    else if (qs && qs.toLowerCase() ==  "no" ) {
        return new JavaScriptViewer(viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild );
    }

    // need to detect
    if (browserInfo.flashVersion >= 6) {
        return new FlashViewer(viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ,channel);
    }
    else {
        return new JavaScriptViewer(viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ,channel );
    }

}


/**
 * Encapsulates information about the viewer
 *
 * viewerWidth : width of the viewer on the page
 * viewerHeight: height of the viewer on the page
 * imgSrc: full path to the initial image to be shown in the viewer
 * imgWidth: width of the initial image
 * imgHeight: height of the initial image
 *
 *  ili: instant-load img name (optional)
 *  ild: instant-load dimensions in the format '123w456h' (optional, but required if ili is passed)
 */
function FlashViewer(  viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ) {
    this.viewerWidth = viewerWidth;
    this.viewerHeight = viewerHeight;

    this.imgSrc = imgSrc;
	

    this.imgWidth = imgWidth;
    this.imgHeight = imgHeight;

    this.ili = ili;
    this.ild = ild;
}

/**
 * Encapsulates information about the viewer
 *
 * viewerWidth : width of the viewer on the page
 * viewerHeight: height of the viewer on the page
 * imgSrc: full path to the initial image to be shown in the viewer
 * imgWidth: width of the initial image
 * imgHeight: height of the initial image
 *
 *  ili: instant-load img name (optional)
 *  ild: instant-load dimensions in the format '123w456h' (optional, but required if ili is passed)
 */
function FlashViewer(  viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ,channel) {
    this.viewerWidth = viewerWidth;
    this.viewerHeight = viewerHeight;
    
	//for testing
    if(imgWidth <=0)
		imgWidth =1100;

     if(imgHeight <=0)
		imgHeight =1413;

    this.imgSrc = imgSrc;
    this.imgWidth = imgWidth;
    this.imgHeight = imgHeight;

    this.ili = ili;
    this.ild = ild;
    this.channel =channel;
    if(channel ==null) this.channel ="zoom";
}


/*
 * Writes the flash player plugin code.  Required values are defined in
 * the constructor
 */
FlashViewer.prototype.writeZoomWindow = function() {
	var imgUrl = '/reimg/zoom/zoomimg.swf?channel=' + this.channel + '&imageName=' + this.imgSrc
	    + '&imageWidth=' + this.imgWidth
	    + '&imageHeight=' + this.imgHeight
	    + ((this.ili!=null && this.ili != '') ? ('&ili=' + this.ili + '&ild='+this.ild) : '' );

	// alert( "imgUrl = " + imgUrl );
    //document.write( "<div style='border-color: #D1D1D1; border-width: 1; border-style: solid;' >" );
	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="' + this.viewerWidth + '" HEIGHT="' + this.viewerHeight + '" id="zoomimg" ALIGN="">');
    document.write( '     <PARAM NAME="movie" VALUE="' + imgUrl + '">' );
    document.write( '     <PARAM NAME="quality" VALUE="high">' );
	document.write( '     <PARAM NAME="wmode" VALUE="transparent">' );
    document.write( '     <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + imgUrl + '" quality="high" bgcolor="#FFFFFF" width="' + this.viewerWidth + '" height="' + this.viewerHeight +'" name="zoomimg" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="transparent" >' );
    document.write( '  </OBJECT>' );
	//document.write( "</div");
}

/*
 * Writes the flash player plugin code.  Required values are defined in
 * the constructor
 */
FlashViewer.prototype.writePDZoomWindow = function() {
	var imgUrl = '/reimg/zoom/pdzoomimg.swf?channel=' + this.channel + '&imageName=' + this.imgSrc
	    + '&imageWidth=' + this.imgWidth
	    + '&imageHeight=' + this.imgHeight
	    + ((this.ili!=null && this.ili != '') ? ('&ili=' + this.ili + '&ild='+this.ild) : '' );

	// alert( "imgUrl = " + imgUrl );
    //document.write( "<div style='border-color: #D1D1D1; border-width: 1; border-style: solid;' >" );
	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="' + this.viewerWidth + '" HEIGHT="' + this.viewerHeight + '" id="zoomimg" ALIGN="">');
    document.write( '     <PARAM NAME="movie" VALUE="' + imgUrl + '">' );
    document.write( '     <PARAM NAME="quality" VALUE="high">' );
	document.write( '     <PARAM NAME="wmode" VALUE="transparent">' );
    document.write( '     <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + imgUrl + '" quality="high" bgcolor="#FFFFFF" width="' + this.viewerWidth + '" height="' + this.viewerHeight +'" name="zoomimg" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="transparent">' );
    document.write( '  </OBJECT>' );
	//document.write( "</div");
}

FlashViewer.prototype.writeTarget = function ( url, width, height, top, bottom, left, right ) {
    var zoom = Math.min( this.viewerHeight/(bottom-top), this.viewerWidth/(right-left) );
    //alert( "zoom is: " + zoom );
	document.write( '<a href="#" onclick="zoomTo( \'' + url +'\',' + width + ',' + height + ','
	  + zoom + ',' + top + ',' + bottom + ',' + left + ',' + right + ' );return false;">' );
	document.write( ' <img class=bordertrends alt="" border=0 src="' + url + '?crop,top=' + top + ',bottom=' + bottom
	  + ',left=' + left + ',right=' + right + '&size=90w">' );
	document.write( '</a>' );
}

FlashViewer.prototype.writeInstructions = function () {
	//document.write( 'Click on tools to the right to zoom in, zoom out and reset the image. ' );
	//document.write( 'Click and drag on the image to pan. ' );
	document.write( '<img src="/reimg/zoom/clickdragzoom.gif" border=0>')
}

FlashViewer.prototype.writeNavigation = function () {
	var imgUrl = '/reimg/zoom/zoomnav4.swf?channel=' + this.channel;
	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="160" HEIGHT="23" id="zoomnav" ALIGN="">' );
	document.write( ' <PARAM NAME="movie" VALUE="' + imgUrl +'">' );
	document.write( ' <PARAM NAME="wmode" VALUE="transparent">' );
	document.write( '  <PARAM NAME="quality" VALUE="high">' );
	document.write( '  <PARAM NAME="bgcolor" VALUE="#FFFFFF"><embed src="' + imgUrl +'" quality="high" bgcolor="#FFFFFF" width="160" height="23" name="zoomnav" align="" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"  wmode="transparent">' );
	document.write( '</OBJECT>' );
}
/**
 * Zooms to the the target area as defined.  If the source image is different
 * it is automatically swapped out.
 */
FlashViewer.prototype.zoomTo = function( imgSrc, imgWidth, imgHeight,
		zoom, top, bottom, left, right )
{
	var p, cx, cy
	
	p = zoom * 100;
	cx = Math.round( left + (right-left)/2 );
	cy = Math.round( top + (bottom-top)/2);

	sendFlashMethod( this.channel, "channel=" + this.channel + "&method=loadTarget&imgName=" + imgSrc
            + "&w=" + imgWidth + "&h=" + imgHeight
            + "&p=" + p + "&cx=" + cx + "&cy=" + cy );

//  alert( "channel=zoom&method=loadTarget&imgName=" + initImgSrc
//            + "&w=" + initImgWidth + "&h=" + initImgHeight
//            + "&p=" + p + "&cx=" + cx + "&cy=" + cy );
}

/**
 * Encapsulates information about the viewer
 *
 * viewerWidth : width of the viewer on the page
 * viewerHeight: height of the viewer on the page
 * imgSrc: full path to the initial image to be shown in the viewer
 * imgWidth: width of the initial image
 * imgHeight: height of the initial image
 *
 *  Not used for javascript viewer -------
 *  ili: instant-load img name (optional)
 *  ild: instant-load dimensions in the format '123w456h' (optional, but required if ili is passed)
 */
function JavaScriptViewer(  viewerWidth, viewerHeight, imgSrc, imgWidth, imgHeight, ili, ild ) {
    this.viewerWidth = viewerWidth;
    this.viewerHeight = viewerHeight;

    this.imgSrc = imgSrc;
    this.imgWidth = imgWidth;
    this.imgHeight = imgHeight;

    // Not used
    // this.ili = ili;
    // this.ild = ild;
}


/*
 * Writes the player code.
 * For JavaScript this is just the appropriately sized image.
 */
JavaScriptViewer.prototype.writeZoomWindow = function() {
	var imgUrl = this.imgSrc
	    + "?fit=" + this.viewerWidth + "w" + this.viewerHeight + "h";

	// alert( "imgUrl = " + imgUrl );
    // document.write( "<div style='border-color: #D1D1D1; border-width: 1; border-style: solid; width: 269; vertical-align: middle; height: 346'  >" );
	document.write( "  <img class=bordertrends id='zoomImg' src='" + imgUrl + "' />");
    // document.write( "</div");
}

JavaScriptViewer.prototype.writeTarget = function ( url, width, height, top, bottom, left, right ) {
    var zoom = Math.min( this.viewerHeight/(bottom-top), this.viewerWidth/(right-left) );
    //alert( "zoom is: " + zoom );
	document.write( '<a href="#" onclick="zoomTo( \'' + url +'\',' + width + ',' + height + ','
	  + zoom + ',' + top + ',' + bottom + ',' + left + ',' + right + ' );return false;">' );
	document.write( ' <img class=bordertrends alt="" border=0 src="' + url + '?crop,top=' + top + ',bottom=' + bottom
	  + ',left=' + left + ',right=' + right + '&size=90w">' );
	document.write( '</a>' );
}

JavaScriptViewer.prototype.writeInstructions = function () {
	document.write( 'Click the images across the top to select the images. ' );
	document.write( 'Click the images on the left to view detail (if applicable). ' );
}

JavaScriptViewer.prototype.writeNavigation = function () {
	document.write( '<span id="reimg.zoom.nav">View products with our exciting Flash viewer (requires Flash 6.0 or higher). ' );
	document.write( ' <a onclick="changeZoomNavMessage()" target="_blank" href="http://www.macromedia.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash">Click here</a> for the free Flash download.</span>');
}

function changeZoomNavMessage() {
    document.getElementById( "reimg.zoom.nav" ).innerHTML =
        "Once you have installed the Flash viewer, " +
        "<a href='javascript:window.location.reload()'>Click here</a> to refresh window.";
}

/**
 * Zooms to the the target area as defined.  If the source image is different
 * it is automatically swapped out.
 */
JavaScriptViewer.prototype.zoomTo = function( imgSrc, imgWidth, imgHeight,
		zoom, top, bottom, left, right )
{
	var imgUrl = imgSrc
        + "?crop,top=" + top + ",bottom=" + bottom + ",left=" + left + ",right=" + right
	    + "&fit=" + this.viewerWidth + "w" + this.viewerHeight + "h";

    var zoomImg = document.getElementById( "zoomImg" );
    zoomImg.src = imgUrl;
}


// 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 );		
	}
}

