
// Images that get preloaded
var ajaxloadinganim = new Image();
ajaxloadinganim.src = '/images/common/ajax-loader.gif'; 
var hoverbg = new Image();
hoverbg.src = '/images/common/infobox-arrow.gif'; 

//DOM Ready
$(function(){
	//Set the hover that changed the text color on the category items. 
	$('.catthumb img').hover(function(){
			$(this).parent()
			.next('a')
			.addClass('activecatlink');
			//alert('mouseover');
		},function(){
			$(this).parent()
			.next('a')
			.removeClass('activecatlink');
			//alert('mouseout');
		}
	);
});





var ajaxPreLoad = '<div id="ajaxload"><img src="' + ajaxloadinganim.src + '" /></div>';


// -------------------------------------------------
// Product Zoom
// -------------------------------------------------
var switchSku = function(lmnt) {
	skuText = lmnt.options[lmnt.selectedIndex].text;
	skuPrice = skuText.split(" - ");
	$("#price").html(skuPrice[1]);
}
    
//--------------------------------------------------------------------------------------------------------
// cpu / browser speed testing
//--------------------------------------------------------------------------------------------------------
var cpuBrowserBenchmark = function(count) {
	var date1 = new Date()
	var start = date1.getTime()
	for (var i=1; i<=count; i++)  {}
	date1 = new Date()
	var finish = date1.getTime()
	speedmulti = Math.round(((finish-start)/1000)/ 0.084);
	// set minimum multip
	if (speedmulti < 1){
		speedmulti = 1;
	}
}

// -------------------------------------------------
// Product Zoom
// -------------------------------------------------

ZZoom = {
	// init ZZoom 
	init: function(e, object){	
		document.onmousedown = ZZoom.fixff;
		document.onmousemove = ZZoom.dragupdate;
		document.onmouseup = ZZoom.dropupdate;
		// namespace var to carry object
		myobject = object;
		// calculate mouse pointer over drag object offset
		offsetx = ZZoom.coordinates(e).x - ZZoom.dragobjectoffset().xdragoffset;
		offsety = ZZoom.coordinates(e).y - ZZoom.dragobjectoffset().ydragoffset;
		// calculate drag area
		xmin = myobject.parentNode.offsetWidth - myobject.offsetWidth - 3;
		ymin = myobject.parentNode.offsetHeight - myobject.offsetHeight - 3;
	},
	
	// return mouse pointer x y position
	coordinates: function(e){
		if (!e){
			var e = window.event;
		}
			
		if (e){ 
			if (e.pageX || e.pageY){
				return {x: e.pageX, y: e.pageY};
			}
			else { 
				if (e.clientX || e.clientY){
					return {
						x: e.clientX + document.body.scrollLeft + document.documentElement.scrollLeft,
						y: e.clientY + document.body.scrollTop + document.documentElement.scrollTop};
				}
			}
		}
	},
	
	// get position of drag object
	dragobjectoffset: function(){
		return {
			xdragoffset: myobject.offsetLeft,
			ydragoffset: myobject.offsetTop
		};
	},
	
	// move drag object
	dragupdate: function(e){
		xcurrent = ZZoom.coordinates(e).x - offsetx;
		ycurrent = ZZoom.coordinates(e).y - offsety;
		
		if (xcurrent < 1 && xcurrent > xmin){
			myobject.style.left = xcurrent + "px";
		}
		if (ycurrent < 1 && ycurrent > ymin){
			myobject.style.top = ycurrent + "px";
		}
		return false;
	},
	
	// drop drag object
	dropupdate: function(){
		document.onmousemove = null;
	},
	
	// prevent image dragging in FF
	fixff: function(){
		return false;
	}

}

// light box open
var speedmulti = 1;
var image_num_prefix = '0';
var open_product_zoom = function(){
	setprocessingheight();
	document.getElementById('processing').style.display = 'block';
	document.getElementById('processingdisp').style.display = 'block';
	setprocessingheight();
	
	init_loading_sequence(zoomImagesInFolder[image_num_prefix],zoomImagesInFolder[image_num_prefix]);
	
}

// light box close
var close_product_zoom = function(){
	document.getElementById('processing').style.display = 'none';
	document.getElementById('processingdisp').style.display = 'none';
}


// preload zoom image / switch zoom image
var zoom_preload_logic = function(img_src){
	document.getElementById('dragimage').style.left = '0px';
	document.getElementById('dragimage').style.top = '0px';
	document.getElementById('dragimage').style.marginLeft = '287px';
	document.getElementById('dragimage').style.marginTop = '287px';
	document.getElementById('dragimage').src = ajaxloadinganim.src;
	zoom_pan_img = new Image();
	zoom_pan_img.src = img_src;
	if(zoom_pan_img.complete){
		zoom_image_preloaded();
	}
	else {
		zoom_pan_img.onload = zoom_image_preloaded;
	}
}

var zoom_image_preloaded = function(){
	document.getElementById('dragimage').style.margin = 0;
	document.getElementById('dragimage').src = zoom_pan_img.src;
	if (document.getElementById('dragimage').width < 200){
		var surelyloaded = window.setTimeout(function(){zoom_image_preloaded();}, 50);
	}
	else {
		clearTimeout(surelyloaded);
		var centerx = ((document.getElementById('dragimage').width / 2) - 287) * -1; 
		var centery = ((document.getElementById('dragimage').height / 2) - 287) * -1;

		document.getElementById('dragimage').style.left = centerx + 'px';
		document.getElementById('dragimage').style.top = centery + 'px';
	}
}

// zoom thumb switch
var zoom_thumb_active = 'zoom_thumb_0';
var zoom_thumb_switch = function(element, image_num){
	image_num_prefix = image_num;
	if (element.id != zoom_thumb_active){
		element.className = 'zoom_thumb zoom_thumb_active';
		document.getElementById(zoom_thumb_active).className = 'zoom_thumb';
		zoom_thumb_active = element.id;
	}
	eval('zoom_preload_logic("' + image_path + 'full/' + zoomImagesInFolder[image_num_prefix] + '")');
	init_loading_sequence(zoomImagesInFolder[image_num_prefix],zoomImagesInFolder[image_num_prefix]);
}

// switch zoom mode
var active_zoom = 'magnify';
var zoom_switch = function(element, e){
	if (element.id == 'pan' && active_zoom != 'pan'){
		if (e.type == 'click'){
			element.src = '/images/zoom/enlarge-on.gif';
			document.getElementById(active_zoom).src = '/images/zoom/magnify-off.gif';
			active_zoom = 'pan';
			document.getElementById('magnifier_space').style.display = 'none';
			document.getElementById('pan_space').style.display = 'block';
			eval('zoom_preload_logic("'+ image_path + 'full/' + zoomImagesInFolder[image_num_prefix] + '")');
		}
		if (e.type == 'mouseover'){
			element.src = '/images/zoom/enlarge-on.gif';
		}
		if (e.type == 'mouseout'){
			element.src = '/images/zoom/enlarge-off.gif';
		}
	}
	if (element.id == 'magnify' && active_zoom != 'magnify'){
		if (e.type == 'click'){
			element.src = '/images/zoom/magnify-on.gif';
			document.getElementById(active_zoom).src = '/images/zoom/enlarge-off.gif';
			active_zoom = 'magnify';
			document.getElementById('pan_space').style.display = 'none';
			document.getElementById('magnifier_space').style.display = 'block';
			// which image to load, which zoom to load
			init_loading_sequence(zoomImagesInFolder[image_num_prefix],zoomImagesInFolder[image_num_prefix]);
		}
		if (e.type == 'mouseover'){
			element.src = '/images/zoom/magnify-on.gif';
		}
		if (e.type == 'mouseout'){
			element.src = '/images/zoom/magnify-off.gif';
		}
	}
}

//-----------------------------------------------------------------------------
// called when flash intro is complete, hide flash and hand off to slide show
//-----------------------------------------------------------------------------
var postFlashIntro = function() {
  document.getElementById('proddetflashintro').style.display='none';
  setTimeout("ZrProdDetSlide.startPlayPause()",5000);
  ZrProdDetSlide.makeControlVisible = true;
  ZrProdDetSlide.showControl();            
}

//-------------------------------------------------
// OBJECT: ZrProdDetSlide
// Product Details Slide Show Object
//-------------------------------------------------

ZrProdDetSlide = {
  
  images: new Array,
  captions: new Array,
  slides: new Array,
  isPlaying: true,
  isStopped: false,
  slideType: '', 
  curSlideIndex: -1,
  firstRun:true,
  haveCaptions: false,
  slidesCt: 0,
  slidesLd: 0,
  imgsToLoad: -1,
  imgsAllLoaded: false,
  fadeSlides: true,
  playSpeed: 5000,
  makeControlVisible: true,
  controlVisible: false,
  audioPlaying: false,

  //---------------------------
  // initialize slider object
  //---------------------------
  init: function(imgAr,capAr,sType,startPlaying) {
    if (!startPlaying) {
      //if the flash intro is there, we won't start playing until later
      this.isPlaying = false;
      this.isStopped = true;
      this.makeControlVisible = false;
    }
    ZrStatus.init('proddetstatuscontainer',imgAr.length,100,'#333','#999');
    ZrLoadingDotDotDot.setText('Loading slide show');
    this.slideType = sType;
    if ( !isEmpty(this.slideType) && this.slideType == 'slide' ) {
      this.configureSlides(imgAr);
      this.fadeSlides = false;
    }
    this.load(imgAr);
    this.captions = capAr;
    var g = 0;
    for (var i=0; i<capAr.length; i++) {
      if ( ( !isEmpty(this.slideType) && this.slideType != 'slide' ) || !isEmpty(capAr[i])  ) {
        this.captions[g] = capAr[i];
        g++;
      }
      if (!isEmpty(this.captions[i])) {
        this.haveCaptions = true;
      }
    }
  },
  
  //--------------------------------
  // loads all images for slide show
  //--------------------------------
  load: function(imgAr) {
    this.imgsToLoad = imgAr.length;
    for ( var j=0; j < imgAr.length; j++ ) {
      this.images[j] = new Image;
      this.images[j].src = imgAr[j];
      if (this.images[j].complete == true ) {
        this.preloadComplete();
      }
      else {
        this.images[j].onload = this.preloadComplete;
      }
    }
  },  
  
  //----------------------------
  // shows the slide controler
  //----------------------------
  showControl: function() {
    if ( this.imgsAllLoaded ) {
      if ( this.makeControlVisible ) {
        if (!this.controlVisible) {
          document.getElementById('pdslidenav').style.display = 'block';
          zfade(document.getElementById('pdslidenav'),0,100,500,true,'');
          this.controlVisible = true;
        }
      }
    }
  },
  
  //--------------------------------------------
  // rebuilds the meat of the slide controller
  //--------------------------------------------
  rebuildPaging: function(curIdx,imgsCt) {
    var pre_html = "";
    var post_html = "";
    if ( curIdx > 1 ) {
      var prevIdx = curIdx - 1;
      pre_html = '<div class="pdcontarrow"><img src="/images/common/slideshow-controller-graphics/left-arrow.gif" onmouseover="this.src=\'/images/common/slideshow-controller-graphics/left-arrow-on.gif\';" onmouseout="this.src=\'/images/common/slideshow-controller-graphics/left-arrow.gif\';" onclick="ZrProdDetSlide.goto('+prevIdx+');" /></div>';
    }
    else {
      pre_html = '<div class="pdcontarrow"><img src="/images/common/slideshow-controller-graphics/left-arrow-dis.gif" /></div>';
    }
    if ( curIdx < imgsCt ) {
      var nextIdx = curIdx + 1;
      post_html = '<div class="pdcontarrow"><img src="/images/common/slideshow-controller-graphics/right-arrow.gif" onmouseover="this.src=\'/images/common/slideshow-controller-graphics/right-arrow-on.gif\';" onmouseout="this.src=\'/images/common/slideshow-controller-graphics/right-arrow.gif\';" onclick="ZrProdDetSlide.goto('+nextIdx+');" /></div>';
    }
    else {
      post_html = '<div class="pdcontarrow"><img src="/images/common/slideshow-controller-graphics/right-arrow-dis.gif" /></div>';
    }
    paging_logic(curIdx,imgsCt,4,'ZrProdDetSlide.goto','pdslidenavitems',pre_html, post_html);
    document.getElementById("switch"+curIdx).className = "switchactive";
  },

  //--------------------------------------------------------------------
  // sets the current slide, animates to it, and handles all 
  // other ancillary functions associated with going to the next slide  
  //--------------------------------------------------------------------
  goto: function(curIdx,keepPlaying) {
    if ( this.curSlideIdx == curIdx ) {
      //do nothing, we are on this slide
      return; 
    }
    if (!this.firstRun && this.haveCaptions) {
      this.hideCaption(this.captions[curIdx-1]);
    }
    else {
      this.firstRun = false;
    }
    if ( this.slideType == 'traditional' ) {
      this.rebuildPaging(curIdx,this.images.length);
    }
    else {
      this.rebuildPaging(curIdx,this.slidesCt);
    }
    this.fadeCurrent(curIdx, (( this.slideType == 'traditional' ) ? 20 : 0 ));
    this.curSlideIdx = curIdx;

    var curPos = document.getElementById('proddetslidehorizruntrad').offsetLeft;
    var cimg1 = document.getElementById("tradimg"+curIdx);
    if (cimg1 == null) {
      //alert("Next image is null:  tradimg" + curIdx);
      return; 
    }
    var offset = 0;
    var leftWidth = 0;
    if ( this.slideType == 'traditional' ) {
      for ( var f = curIdx-1; f > -1; f--) {
        var cimg = document.getElementById("tradimg"+f);
        leftWidth += cimg.offsetWidth + 10;
      }
      offset = (( 716 - cimg1.offsetWidth + ( 10 * curIdx ) + 10 ) / 2 ) - leftWidth - 5;
    }
    else {
      leftWidth = 721 * (curIdx-1) + 505; 
      offset = leftWidth * -1;
    }
    var forward = false;
    if ( curPos > offset ) {
      forward = true;
    }
    var beChange = Math.abs(curPos - offset);
    var k=0;
    var easeArray  = makeMovementArray(beChange,1.33,true);
    if ( (this.slideType == 'traditional' && this.images.length == 1) || (this.slideType == 'slide' && this.slidesCt == 1)  ) {
      document.getElementById('proddetslidehorizruntrad').style.left=offset+"px";
    }
    else {
      for (var i=0; i<easeArray.length; i++) {
        if (forward) {
          curPos = curPos - easeArray[i];
        }
        else {
          curPos = curPos + easeArray[i];
        }
        setTimeout("document.getElementById('proddetslidehorizruntrad').style.left='"+curPos+"px'",i*33);
        k = i*33;
      }
      setTimeout("document.getElementById('proddetslidehorizruntrad').style.left='"+offset+"px'",k);
    }
    if ( this.slideType == 'traditional' ) {
      setTimeout("zfade(document.getElementById('tradimg"+curIdx+"'), 20, 100, 500, true, '')",k);
      setTimeout("document.getElementById('tradimg"+curIdx+"').style.cursor = 'default'",k);
    }
    else {
      if (this.fadeSlides) {
        //fade in all images of a slide if slide type image fading is turned on
        if (this.slides['slide-'+curIdx] != undefined) {
          if (!isEmpty(this.slides['slide-'+curIdx][0])) {
            for (var z=0; z < this.slides['slide-'+curIdx].length; z++) {
              setTimeout("zfade(document.getElementById('tradimg"+this.slides['slide-'+curIdx][z]+"'), 20, 100, 500, true, '')",k);
            }
          }
        }
      }
    }
    this.isPlaying = ( keepPlaying != undefined && keepPlaying && !this.isStopped);
    if ( this.isPlaying ) {
      var nextIdx = -1;
      var nextWait = k+this.playSpeed;;
      if ( ( this.slideType == 'traditional' && curIdx == this.images.length ) || ( this.slideType == 'slide' && curIdx == this.slidesCt ) ) {
        nextIdx = 1;
        nextWait = nextWait * 2;
      }
      else {
        nextIdx = curIdx + 1;
      }
      setTimeout("ZrProdDetSlide.autoPlay(" + nextIdx + ",true)",nextWait);
      document.getElementById('playpausebutton').src = "/images/common/slideshow-controller-graphics/pause-button-off.gif"
    }
    else {
      document.getElementById('playpausebutton').src = "/images/common/slideshow-controller-graphics/play-button-off.gif"
      this.isStopped = true;
    }
  },
  
  //------------------------------------------------------------
  // does work associated with completion of preloading images
  //------------------------------------------------------------
  preloadComplete: function() {
    ZrProdDetSlide.slidesLd++;
    ZrStatus.step(ZrProdDetSlide.slidesLd);
    if ( ZrProdDetSlide.imgsToLoad == ZrProdDetSlide.slidesLd ) {
      ZrProdDetSlide.imgsAllLoaded = true;
      document.getElementById('proddetslideboxcontainer').style.backgroundColor='#fff';
      ZrProdDetSlide.build();
      zfade(document.getElementById('proddetloading'),100,0,500,true,'document.getElementById(\'proddetloading\').style.display=\'none\'');
      if ( ZrProdDetSlide.slideType == 'traditional' ) {
        if (ZrProdDetSlide.images.length > 1) {
          ZrProdDetSlide.showControl();
        }
      }
      else {
        if (ZrProdDetSlide.slidesCt > 1) {
          ZrProdDetSlide.showControl();
        }
      }
    }
  },
  
  //-----------------------------------------------------------------
  // handles the action of clicking the play or pause toggle button
  //-----------------------------------------------------------------
  startPlayPause: function() {
    if (this.isStopped) {
      document.getElementById('playpausebutton').src = "/images/common/slideshow-controller-graphics/pause-button-off.gif"
      if ( ( ZrProdDetSlide.slideType == 'traditional' && this.curSlideIdx == this.images.length ) || ( this.slideType == 'slide' && pdCurSlideIdx == this.slidesCt ) ) {
        nextIdx = 1;
      }
      else {
        nextIdx = this.curSlideIdx+1
      }
      this.isStopped = false;
      this.goto(nextIdx,true);
    }
    else {
      document.getElementById('playpausebutton').src = "/images/common/slideshow-controller-graphics/play-button-off.gif"
      this.isStopped = true;
    }
  },
  
  //--------------------------------------------------------------------
  // once loading is complete, this use DOM to create all the elements 
  // that make up the structure of the slide show 
  //--------------------------------------------------------------------
  build: function() {
    var cd = document.getElementById('proddetslidehorizruntrad');
    cd.innerHTML = "";
    var d = document.createElement("div");
    d.id = "trad0";
    d.className = "slideends";
    var i = document.createElement("img");
    i.id =  "tradimg0";
    i.src = "/images/common/clear.gif";
    i.style.width = 500 + "px";
    i.style.height = 358 + "px";
    d.appendChild(i);
    cd.appendChild(d);
    var m = 1;
    for ( var j=1; j < ( this.imgsToLoad + 1 ); j++ ) {
      var d = document.createElement("div");
      d.id = "trad" + j;
      var i = document.createElement("img");
      i.id =  "tradimg" + j;
      i.src = this.images[j-1].src;
      if ( this.slideType == 'traditional' ) {
        if(window.addEventListener) { 
          i.setAttribute("onclick","ZrProdDetSlide.goto("+j+")");
        }
        else {
          with ({ "j": j }) {
            i.onclick = function(){ eval("ZrProdDetSlide.goto("+j+")") };
          }
        }
      }
      if ( this.slideType == 'slide' && !this.fadeSlides) {
        i.className = 'act';
      }
      if ( this.slideType == 'traditional' ) {
        i.style.cursor = 'pointer';
      }
      d.appendChild(i);
      cd.appendChild(d);
      m++;
    }
    var d = document.createElement("div");
    d.id = "trad" + m;
    d.className = "slideends";
    var i = document.createElement("img");
    i.id =  "tradimg" + m;
    i.src = "/images/common/clear.gif";
    i.style.width = 500 + "px";
    i.style.height = 358 + "px";
    d.appendChild(i);
    cd.appendChild(d);
    this.goto(1,true);
  },
  
  //-------------------------------------------------------
  // Logic to handle placing individual images into slides 
  // based on trailing letters in the filenames
  //-------------------------------------------------------
  configureSlides: function(imgAr) {
    for (var w=0; w<imgAr.length; w++) {
      var filepath = imgAr[w];
      var filename = filepath.substring(filepath.lastIndexOf('/')+1,filepath.length);
      var slideIndex = filepath.substring(filepath.lastIndexOf('-')+1,filepath.length);
      slideIndex = slideIndex.substring(0,slideIndex.indexOf("."));
      var hasAlpha = false;
      var alphaStart = -1;
      for (var p = 0; p < slideIndex.length; p++) {
        if (!hasAlpha && isNaN(slideIndex.substring(p,p+1))) {
          hasAlpha = true;
          alphaStart = p;
        }
      }
      var al = "";
      var nm = -1;
      if ( hasAlpha && alphaStart != -1 ) {
        al = slideIndex.substring(alphaStart,slideIndex.length);
        nm = slideIndex.substring(0,alphaStart);
        if (al=="a") {
          this.slidesCt++;
          this.slides['slide-'+nm] = new Array;
        }
        this.slides['slide-'+nm][(this.slides['slide-'+nm].length)] = (w+1);
      }
      else {
        nm = slideIndex;
        this.slidesCt++;
        this.slides['slide-'+nm] = new Array;
        this.slides['slide-'+nm][(this.slides['slide-'+nm].length)] = (w+1);
      }
    }
  },

  //------------------------------------------------------------
  // heads to a certain step of the show and continues to play 
  //------------------------------------------------------------
  autoPlay: function(curIdx,keepPlaying) {
    if (!this.isStopped) {
      this.goto(curIdx,keepPlaying);
    }
  }, 

  //------------------------------------------------------------
  // Hides on caption and calls show for the next 
  //------------------------------------------------------------
  hideCaption: function(caption) {
    var cleanCaption = caption.replace(/\"/g, '\\"');
    zfade(document.getElementById("prodfeature"),100,0,500,true,'ZrProdDetSlide.showCaption("'+cleanCaption+'")');    
  },

  //------------------------------------------------------------
  // fades in a caption
  //------------------------------------------------------------
  showCaption: function(caption) {
    document.getElementById("prodfeature").innerHTML = caption;
    zfade(document.getElementById("prodfeature"),0,100,500,true);
  },

  //----------------------------------------------------------------
  // loads and plays an audio file coordinated with the slide show
  //----------------------------------------------------------------
  loadNarration: function(filenm,cuePoints,bgFileNm) {
    if(!swfobject.hasFlashPlayerVersion("9.0.0")) {
      alert("Sorry, you do not have a new enough\nflash player to play this content.");
      return;
    }
    document.getElementById('audiocontrol').innerHTML = "<a href=\"#\" onClick=\"ZrProdDetSlide.playNarration();return false;\"><img src=\"/images/product-detail/audio-pause.gif\" id=\"audioplaypause\" onMouseOver=\"ZrProdDetSlide.audioPlayPauseMO(true);\" onMouseOut=\"ZrProdDetSlide.audioPlayPauseMO(false);\" /></a><a href=\"#\" onClick=\"ZrProdDetSlide.restartNarration();return false;\"><img src=\"/images/product-detail/audio-replay.gif\" onMouseOver=\"this.src='/images/product-detail/audio-replay-on.gif'\" onMouseOut=\"this.src='/images/product-detail/audio-replay.gif'\"  /></a>";
    if(document.getElementById('audio_cue_player') != null) {
      //already loaded
      //ZrProdDetSlide.playNarration();
      //return;
      if (!this.audioPlaying) {
        //this.restartNarration();
      }
      return;
    }
    var d = document.createElement("div");
    d.id = 'proddethiddenaudio';
    d.style.display='none';
    document.body.appendChild(d);

    this.isStopped = true;
    var flashvars = {
      audioFilePath: filenm,
      bkgAudioFilePath: bgFileNm,
      cuePointList: cuePoints,
      startFunction: "ZrProdDetSlide.playNarration",
      cueFunction: "ZrProdDetSlide.goto",
      endFunction: "ZrProdDetSlide.narrationDone"
    };
    var params = {
      bgcolor: "#666666"
    };
    var attributes = {
      id: "audio_cue_player",
      name: "audio_cue_player"
    };
    swfobject.embedSWF("/swf/audio_cue_player.swf", "proddethiddenaudio", "0", "0", "9.0.0","expressInstall.swf", flashvars, params, attributes);
  },

  
  //------------------------------------------------------------
  //
  //------------------------------------------------------------
  audioPlayPauseMO: function(over) {
    if (over) {
      if (this.audioPlaying) { document.getElementById("audioplaypause").src = '/images/product-detail/audio-pause-on.gif'; }
      else { document.getElementById("audioplaypause").src = '/images/product-detail/audio-play-on.gif'; }
    }
    else {
      if (this.audioPlaying) { document.getElementById("audioplaypause").src = '/images/product-detail/audio-pause.gif'; }
      else { document.getElementById("audioplaypause").src = '/images/product-detail/audio-play.gif'; }
    }
  },
  
  //------------------------------------------------------------
  //
  //------------------------------------------------------------
  playNarration: function() {
    //alert("yo"+this.audioPlaying);
    if (document.getElementById("proddethiddenaudio")) {
      //there was a heinous error loading this 
    } 
    else {
      //alert('gothere');
      if (!this.audioPlaying) {
        document.getElementById("audiocontrol").style.backgroundImage = "url(/images/product-detail/audio-control-grey.gif)";
        document.getElementById("audioplaypause").src = "/images/product-detail/audio-pause.gif";
        swfobject.getObjectById("audio_cue_player").playAudio();
        this.audioPlaying = true;
      }
      else {
        document.getElementById("audioplaypause").src = "/images/product-detail/audio-play.gif";
        swfobject.getObjectById("audio_cue_player").pauseAudio();
        this.audioPlaying = false;
      }
    }
  },
  
  //------------------------------------------------------------
  //
  //------------------------------------------------------------
  restartNarration: function() {
    swfobject.getObjectById("audio_cue_player").restartAudio();
  },
    
  //------------------------------------------------------------
  // 
  //------------------------------------------------------------
  narrationDone: function() {
    document.getElementById("audiocontrol").style.backgroundImage = "url(/images/product-detail/audio-control.gif)";
    document.getElementById('audiocontrol').innerHTML = "<span id=\"audiotext\">Audio</span>"
    this.audioPlaying = false;
  },
  
  
  //------------------------------------------------------------
  // stops slide show and fades out the current slide
  //------------------------------------------------------------
  stopAndFadeOut: function(fadePct) {
    this.isStopped = true;
    this.fadeCurrent(999,fadePct); 
  },

  //------------------------------------------------------------
  // fades out the current visible slide or image
  //------------------------------------------------------------
  fadeCurrent: function(curIdx,fadePct) {
    var myFadePct = (!isEmpty(fadePct)) ? fadePct : 20;
    if ( this.curSlideIdx != curIdx && this.curSlideIdx > -1 ) {
      if ( this.slideType == 'traditional' ) {
        var td = document.getElementById("tradimg"+this.curSlideIdx);
        td.style.cursor = 'pointer';
        zfade(td, 100, myFadePct, 100, true, '');
      }
      else {
        if (this.fadeSlides) {
          //fade out all images of a slide if slide type image fading is turned on
          if (this.slides['slide-'+this.curSlideIdx] != undefined) {
            if (!isEmpty(this.slides['slide-'+this.curSlideIdx][0])) {
              for (var z=0; z < this.slides['slide-'+this.curSlideIdx].length; z++) {
                zfade(document.getElementById("tradimg"+this.slides['slide-'+this.curSlideIdx][z]), 100, myFadePct, 100, true, '');
              }
            }
          }
        }
      }
    }
  },
  showCurrent: function(fadePct) {
    var myFadePct = (!isEmpty(fadePct)) ? fadePct : 20;
    if ( this.curSlideIdx > -1 ) {
      if ( this.slideType == 'traditional' ) {
        var td = document.getElementById("tradimg"+this.curSlideIdx);
        td.style.cursor = 'pointer';
        zfade(td, myFadePct, 100, 100, true, '');
      }
      else {
        if (this.fadeSlides) {
          //fades in all images of a slide if slide type image fading is turned on
          if (this.slides['slide-'+this.curSlideIdx] != undefined) {
            if (!isEmpty(this.slides['slide-'+this.curSlideIdx][0])) {
              for (var z=0; z < this.slides['slide-'+this.curSlideIdx].length; z++) {
                zfade(document.getElementById("tradimg"+this.slides['slide-'+this.curSlideIdx][z]), myFadePct, 100, 100, true, '');
              }
            }
          }
        }
      }
    }
  }
  
  //end object
}



var pdShowProductVideo = function(movPath, vidCt, vidIdx) {
  ZrProdDetSlide.isStopped = true;
  var vc = document.getElementById('proddetvideobox')
  vc.innerHTML = '';
  var d = document.createElement("div");
  d.id = 'pdvideoplayer';
  vc.appendChild(d);
  var dl = document.createElement("div");
  dl.id = 'pdvideolinks';
  dl.innerHTML = document.getElementById('pdvideolinkscontent').innerHTML;
  vc.appendChild(dl);
  if ( vidCt > 1 ) {
    var ll = document.getElementById('pdvideolinksleft');
    ll.innerHTML = '';
    var s = document.createElement("span");
    s.innerHTML = "videos:";
    ll.appendChild(s);
    for (var t=1; t<=vidCt; t++) {
      var a = document.createElement("a");
      a.href = '#';
      if ( t == vidIdx ) {
        a.className = 'active';
      }
      if(window.addEventListener) { 
        a.setAttribute("onclick","pdShowProductVideo('"+movPath+"',"+vidCt+","+t+")");
      }
      else {
        with ({ "t": t }) {
          a.onclick = function(){ eval("pdShowProductVideo('"+movPath+"',"+vidCt+","+t+")") };
        }
      }
      a.innerHTML = t;
      ll.appendChild(a);
    }
  }
  vc.style.display='block';
  loadVideo(movPath,'','pdvideoplayer','true');
}
var pdHideProductVideo = function() {
  var vc = document.getElementById('proddetvideobox')
  vc.innerHTML = '';
  vc.style.display = 'none';
  ZrProdDetSlide.showCurrent();
}


//-------------------------------------------------
//OBJECT: ZrCatSlide
//Product slide show on all category pages
//-------------------------------------------------

//clear the object
ZrCatSlide = {}

ZrCatSlide = {

  initStatus: 'not',
  inViewIdx: 0,
  products: new Array,
  pdLabels: new Array,
  pdLinks:  new Array,
  imLoaded: new Array,
  curWidth: 80,
  lastLoaded: -1,
  firstLoad: true,
  isMoving: false,
  playing: true,
  
  
  //---------------------------
  // initialize slider object
  //---------------------------
  init: function(prodList,url,internalCall) {
    if ( this.initStatus == 'complete' ) {
      reshow_light_box();
      return;
    }
    if ( this.initStatus == 'inprocess' || this.initStatus == 'geturl' ) {
      if ( !internalCall ) {
        return;
      }
    }
    this.initStatus = 'inprocess';
    this.inViewIdx = 0;
    this.products =  new Array;
    this.pdLabels = new Array;
    this.pdLinks = new Array;
    this.imLoaded =  new Array;
    this.curWidth = 80;
    this.lastLoaded = -1;
    this.firstLoad = true;
    this.isMoving = false;
    if (isEmpty(prodList)) {
      //get the product list from a url
      if (!isEmpty(url)) {
        this.initStatus = 'geturl';
        this.getList(url);
        return; 
      }
      return;
    }
    else {
      this.products = prodList;
    }
    setprocessingheight();
    document.getElementById('processing').style.display = 'block';
    var sDisplay = document.getElementById('processingdisp');
    sDisplay.innerHTML = '<div id="close_button" onclick="close_product_zoom()"><img src="/images/zoom/close.png" /></div>';
    var cc = document.createElement("div");
    cc.id = 'catslidecontain';
    cc.style.visibility = 'visible';
    sDisplay.appendChild(cc);
    sDisplay.style.padding='0';
    sDisplay.style.display = 'block';
    //load the palette layer, to scroll through products
    var palette = document.createElement("div");
    palette.id = 'catslidepalette';
    palette.innerHTML = '<img src="/images/common/ajax-loader-on-fff.gif" class="catslideimgloading"/>';
    cc.appendChild(palette);
    //load a layer for product names
    var pdLabel = document.createElement("div");
    pdLabel.id = 'catslideprodlabel';
    pdLabel.innerHTML = 'Loading...';
    cc.appendChild(pdLabel);
    var pdNav = document.createElement("div");
    pdNav.id = 'catslidenavcontrol';
    pdNav.innerHTML = "<a href='#' onClick='ZrCatSlide.previous(); return false;'>Prev</a> | <a href='#' onClick='ZrCatSlide.next(); return false;'>Next</a> | <a href='#' onClick='ZrCatSlide.play(); return false;'>Play</a> | <a href='#' onClick='ZrCatSlide.stop(); return false;'>Stop</a>"; 
    cc.appendChild(pdNav);
    setprocessingheight();
    this.load(this.inViewIdx);
  },
  
  //--------------------------------------
  // code to go get the list of products 
  //--------------------------------------
  getList: function(url) {
    //add this in later
    $.post(url+"&r="+Math.random(), '',
      function(response){
        var pattern = /,/; 
        var myProds = response.split(pattern);
        ZrCatSlide.init(myProds,'',true);
      });
  },
  
  //---------------------------
  // initialize slider object
  //---------------------------
  load: function(idx) {
    if ( isEmpty(this.imLoaded['product'+idx]) ) {
      this.imLoaded['product'+idx] = 'notyet';
      this.lastLoaded = idx;
      $.post("/za/RAU/category/includes/category-slide-item.jsp?refId="+this.products[idx], '',
        function(response){
          if (!isEmpty(document.getElementById('catslideloader'))) {
            var ldr = document.getElementById('catslideloader');
            ldr.innerHTML = '';
          }
          else {
            var ldr = document.createElement('div');
            ldr.id = "catslideloader";
            document.body.appendChild(ldr);
            ldr.style.visibility = 'hidden';
          }
          ldr.innerHTML = response;
          if (!document.getElementById('catslidepimg')) {
            ZrCatSlide.inViewIdx = idx+1; 
            ZrCatSlide.load(ZrCatSlide.inViewIdx);
            return;
          }
          var imgPath = document.getElementById('catslidepimg').innerHTML;
          if (!isEmpty(document.getElementById('catslidelabel'))) { 
            var prodLabel = document.getElementById('catslidelabel').innerHTML;
            ZrCatSlide.pdLabels['product'+idx] = "";
            ZrCatSlide.pdLinks['product'+idx] = "";
            if (!isEmpty(document.getElementById('catslidelink'))) {
              ZrCatSlide.pdLinks['product'+idx] = document.getElementById('catslidelink').innerHTML;
							try {
								if (!isEmpty(slideshowQueryParams)) {
                	ZrCatSlide.pdLinks['product'+idx] += "?" + slideshowQueryParams;
              	}
							} catch(error){}
              ZrCatSlide.pdLabels['product'+idx] += "<a href='";
              ZrCatSlide.pdLabels['product'+idx] += ZrCatSlide.pdLinks['product'+idx];
              ZrCatSlide.pdLabels['product'+idx] += "'>";
            }
            ZrCatSlide.pdLabels['product'+idx] += prodLabel;
            if (!isEmpty(document.getElementById('catslidelink'))) {
              ZrCatSlide.pdLabels['product'+idx] += "</a>";
            }
          }
          if (!isEmpty(imgPath)) {
            var imx = new Image;
            imx.id = 'catslidenewimage'+idx;
            imx.src = imgPath;
            ldr.appendChild(imx);
            if (imx.complete == true ) {
              ZrCatSlide.preloadComplete(idx);
            }
            else {
              imx.onload = function() { eval("ZrCatSlide.preloadComplete("+idx+")"); }; 
            }
          }
        }
      );
    }
  },

  //---------------------------
  // initialize slider object
  //---------------------------
  preloadComplete: function(idx) {
    this.imLoaded['product'+idx] = 'loaded';
    var ni = document.getElementById('catslidenewimage'+idx);
    var h = ni.offsetHeight;
    var w = ni.offsetWidth;
    var p = document.getElementById('catslidepalette');
    if (this.firstLoad) {
      var lft = p.offsetLeft;
      var lastWidth = w+10;
      var mv = (lastWidth-ZrCatSlide.curWidth)/2;
      var lastLeft = (900-(w+10))/2;
      var pw = p.offsetWidth;
      var ww = lastWidth - pw;
      var step = ww/20;
      var mstep = mv/20;
      var tnxt = 1;
      for (var k = 1; k<21; k++) {
        tnxt = k * 20;
        pw = pw+step;
        setTimeout("document.getElementById('catslidepalette').style.width='"+Math.round(pw) + "px'",tnxt);
        lft = lft - mstep;
        setTimeout("document.getElementById('catslidepalette').style.left='"+Math.round(lft) + "px'",tnxt);
      }
      setTimeout("document.getElementById('catslidepalette').style.width='"+ lastWidth + "px'",tnxt+20);
      setTimeout("document.getElementById('catslidepalette').style.left='"+lastLeft + "px'",tnxt+20);
      this.showLabel();
    }
    setTimeout("ZrCatSlide.show("+idx+")",tnxt + 20);
  },
  
  //---------------------------
  // places image in slide show
  //---------------------------
  show: function(idx) {
    var imc = document.createElement('div');
    imc.className = 'catslideimgbx';
    imc.id = 'catslideimgbx' + idx;
    var ig = document.getElementById('catslidenewimage' + idx);
    if(window.addEventListener) { 
      ig.setAttribute("onclick","ZrCatSlide.gotoAndStop(" + idx + ")");
    }
    else {
      with ({ "idx": idx }) {
        ig.onclick = function(){ eval("ZrCatSlide.gotoAndStop(" + idx + ")") };
      }
    }
    var igw = ig.offsetWidth;
    imc.style.marginLeft = '5px';
    imc.appendChild(ig);
    var p = document.getElementById('catslidepalette');
    if (this.firstLoad) {
      p.innerHTML = "";
    }
    if (idx > 0) {
      p.style.width = ( p.offsetWidth + igw + 5 ) + "px";
    }
    p.appendChild(imc);
    if (this.firstLoad) {
      zfade(document.getElementById('catslidenewimage'+idx),20,100,500,true,'ZrCatSlide.loadSurrounding()');
      this.firstLoad = false;
    }
    else {
      ZrCatSlide.loadSurrounding();
    }
    if ( idx == 0 && this.initStatus == 'inprocess') {
      this.initStatus = 'complete';
      if (this.playing) {
        setTimeout("ZrCatSlide.playNext("+(idx+1)+")",4000);
      }
    }
  },
  
  //-----------------------------------------------------
  // loads a couple images before and after the current
  //-----------------------------------------------------
  loadSurrounding: function() {
    for (var g=this.inViewIdx+1; g<=(this.inViewIdx+3); g++) { 
      if ( g < this.products.length ) {
        if ( isEmpty(this.imLoaded['product'+g]) ) {
          this.load(g);
          return;
        }
      }
    }
  },

  //--------------------------------------
  // code to go get the list of products 
  //--------------------------------------
  gotoAndStop: function(idx) {
    this.stop();
    this.goto(idx);
  },
  //--------------------------------------
  // code to go get the list of products 
  //--------------------------------------
  goto: function(idx) {
    if ( this.isMoving ) {
      setTimeout("ZrCatSlide.goto("+idx+")",100);
      return;
    }
    this.isMoving = true;
    document.getElementById('catslideprodlabel').innerHTML = '';
    var p = document.getElementById('catslidepalette');
    var cl = p.offsetLeft;
    var cim = document.getElementById("catslidenewimage"+idx);
    var leftWidth = 0;
    for ( var k=0; k<this.products.length; k++) {
      if ( k < idx ) {
        if ( !isEmpty(this.imLoaded['product'+k]) ) {
          if ( this.imLoaded['product'+k] == 'loaded' ) {
            leftWidth += document.getElementById("catslidenewimage"+k).offsetWidth + 5;
          }
        }
      }
    }
    leftWidth += 5;
    var nl =  450 - leftWidth - ( cim.offsetWidth / 2 ); 
    var curPos = p.offsetLeft;
    var beChange = Math.abs(curPos - nl);
    var easeArray  = makeMovementArray(beChange,1.33,true);
    var m = 0;
    for (var i=0; i<easeArray.length; i++) {
      if (idx > this.inViewIdx) {
        curPos = curPos - easeArray[i];
      }
      else {
        curPos = curPos + easeArray[i];
      }
      setTimeout("document.getElementById('catslidepalette').style.left='"+curPos+"px'",i*33);
      m = i*33;
    }
    setTimeout("document.getElementById('catslidepalette').style.left='"+nl+"px'",m);
    setTimeout("ZrCatSlide.fadeIn("+idx+")",m);
  },
  
  //--------------------------------------
  // go to the previous slide  
  //--------------------------------------
  previous: function() {
    this.stop();
    if ( this.inViewIdx > 0 ) {
      this.goto(this.inViewIdx - 1);
    }
  },

  //--------------------------------------
  // go to the next slide  
  //--------------------------------------
  next: function() {
    this.stop();
    if ( this.inViewIdx < this.products.length ) {
      this.goto(this.inViewIdx + 1);
    }
  },
  
  //--------------------------------------
  // fades in the current slide, fades out the last one 
  //--------------------------------------
  fadeIn: function(idx) {
    zfade(document.getElementById('catslidenewimage'+this.inViewIdx),100,20,750,true,'');
    zfade(document.getElementById('catslidenewimage'+idx),20,100,750,true,'ZrCatSlide.showLabel');
    this.inViewIdx = idx;
    this.loadSurrounding();
  },
  
  //--------------------------------------
  // Shows label 
  //--------------------------------------
  showLabel: function() {
    if ( !isEmpty(this.pdLabels['product'+this.inViewIdx]) ) {
      var pdl = document.getElementById('catslideprodlabel');
      pdl.innerHTML = this.pdLabels['product'+this.inViewIdx];
    }
    this.isMoving = false;
  },

  //--------------------------------------
  // plays next
  //--------------------------------------
  playNext: function(idx) {
    if (this.playing) {
      if (idx >= this.products.length) {
        this.goto(0);
        setTimeout("ZrCatSlide.playNext(1)",4000);
      }
      else {
        this.goto(idx);
        setTimeout("ZrCatSlide.playNext("+(idx+1)+")",4000);
      }
    }
  },
  
  //--------------------------------------
  // sets playing
  //--------------------------------------
  play: function() {
    if (!this.playing) {
      this.playing = true;
      this.playNext(this.inViewIdx+1);
    }
  },
  
  //--------------------------------------
  // sets playing
  //--------------------------------------
  stop: function() {
    this.playing = false;
  }
  
}

var zrInsertItemCreateCollection = function(){
	var hasCheckedCollection = false;
	var lastWishlistID = "last";
	if (document.getElementById('existingcollections')){
		var collectionCheckBoxContainer = document.getElementById('existingcollections');
		var collectionCheckBox = collectionCheckBoxContainer.getElementsByTagName('*','input');
		if (collectionCheckBox.length > 0){
			// existing collection
			for (x = 0; x < collectionCheckBox.length; x++){
				if (collectionCheckBox[x].checked == true) {
				  if (collectionCheckBox[x].name == 'zwl_save') {
  					hasCheckedCollection = true;
  	        var wlid = "#wishlistid" + collectionCheckBox[x].value;
  	        lastWishlistID = $(wlid).val();
				  }
				}
			}
		}
	}
	if (hasCheckedCollection == true || (document.getElementById('collectionname').value != '' && document.getElementById('collectionname').value != '')){
		document.getElementById('collectionsubmit').style.display = 'none';
		document.getElementById('collectionsubmitloading').style.display = 'inline';
		var myformelements = $("#collectionsForm").serialize();
		
		$.post("/", myformelements,
			function(response){
				zrCollectionItemAdded(lastWishlistID);
				return false;
			}
		);
	}
	else {
		alert ('Please check a existing Collection or enter collection name and collection description.');
		return false;
	}
}

var zrCollectionItemAdded = function(lastWishlistID){
	document.getElementById('addtocollectionlogic').innerHTML = "We have added this item to your collection(s).<br><br><a href=\"/account/my-collections-view.jsp?wishlistid=" + lastWishlistID + "\">View Collection(s)</a>";
}

