/*--------------------------------------------------------------------------*
 * 
 * Alternate Photos Quick Preview (Diversion)
 * 
 * Version 1.0.0
 * 
 * Copyright (C) 2009 Brand Labs LLC
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or any later version.
 * 
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  
 * 02110-1301  USA
 *  
 *--------------------------------------------------------------------------*/

var DiversionSettings={ENABLED:true,PHOTOS_PATH:'/v/vspfiles/photos',MAIN_PHOTO_ID:'product_photo',ALTERNATE_PHOTO_ID_TEMPLATE:new Template('alternate_product_photo_#{photoIndex}'),NOT_EQUAL_SIZE_IGNORE:false,FORCE_QUICK_PREVIEW_WINDOW:false,QUICK_PREVIEW_WINDOW_OFFSET_TOP:0,QUICK_PREVIEW_WINDOW_OFFSET_LEFT:0,PHOTO_SIZE_NAME:'T'};var Diversion={load:function(){if(!DiversionSettings.ENABLED){return;}
if(location.pathname.toLowerCase()=='/productdetails.asp'||location.pathname.toLowerCase().indexOf('-p/')!=-1||location.pathname.toLowerCase().indexOf('_p/')!=-1){Diversion.setup();}},setup:function(){var aa=2;var ba=null;var ca=new Array();var da;var ea;var fa=true;var ga=null;var ha=null;try{ga=Diversion.getProductCodeFromURL(window.location.href);if(ga==null){return;}
ha=$(DiversionSettings.MAIN_PHOTO_ID);if(ha==null){return;}
ba=$(DiversionSettings.ALTERNATE_PHOTO_ID_TEMPLATE.evaluate({photoIndex:aa}));if(ba==null||!ba.getWidth||!ba.getHeight){return;}
da=ba.getWidth();ea=ba.getHeight();while($(DiversionSettings.ALTERNATE_PHOTO_ID_TEMPLATE.evaluate({photoIndex:aa}))!=null){ba=$(DiversionSettings.ALTERNATE_PHOTO_ID_TEMPLATE.evaluate({photoIndex:aa}));ca.push(ba);if(ba.getWidth()!=da||ba.getHeight()!=ea){fa=false;}
aa++;}
if(DiversionSettings.FORCE_QUICK_PREVIEW_WINDOW){fa=false;}
ca.each(function(ia,ja){if(fa){new DiversionMainImageQuickPreview(ga,(ja+2),ia,ha);}
else{if(!DiversionSettings.NOT_EQUAL_SIZE_IGNORE){new DiversionQuickPreview(ga,(ja+2),ia);}}});}
catch(e){}},getProductCodeFromURL:function(ka){var la=null;if(ka==null){return null;}
la=ka.match(/\/productdetails\.asp\?(?:[\&]?.*\=.*)*productcode=([^\&\#]+)/i);if(la!=null&&la.length>=2){return unescape(la[1]);}
la=ka.match(/(?:_p|-p)\/(.+)\.htm/i);if(la!=null&&la.length>=2){return unescape(la[1]);}
return null;}};Event.observe(window,'load',Diversion.load);var DiversionQuickPreview=Class.create({initialize:function(ma,na,oa){var pa;var qa;try{pa=$$('body').first();}
catch(e){}
if(pa==null){return;}
if(oa==null){return;}
this.productCode=ma;this.alternateElement=oa;this.photoNumber=na;this.fileExtension=this.extractFileExtension(oa.src);this.fileExtension=this.fileExtension==null?'jpg':this.fileExtension;this.thumbnailElement=new Element('div',{id:'diversion_quick_preview_'+na});this.thumbnailElement.addClassName('diversion_quick_preview');this.thumbnailElement.setStyle({position:'absolute',display:'none',zIndex:102});qa=new Element('img',{src:this.getThumbnailFileName(),alt:this.alternateElement.alt,title:this.alternateElement.title});this.thumbnailElement.update(qa);pa.insert(this.thumbnailElement);Event.observe(oa,'mouseover',this.mouseOver.bindAsEventListener(this));Event.observe(oa,'mouseout',this.mouseOut.bindAsEventListener(this));},mouseOver:function(){var ra;if(this.alternateElement==null||this.thumbnailElement==null){return;}
ra=this.alternateElement.positionedOffset();this.thumbnailElement.setStyle({top:(ra.top+DiversionSettings.QUICK_PREVIEW_WINDOW_OFFSET_TOP)+'px',left:(ra.left+this.alternateElement.getWidth()+DiversionSettings.QUICK_PREVIEW_WINDOW_OFFSET_LEFT)+'px'});this.showElement(this.thumbnailElement);},mouseOut:function(){if(this.thumbnailElement==null){return;}
this.hideElement(this.thumbnailElement);},getThumbnailFileName:function(){var sa;sa=DiversionSettings.PHOTOS_PATH+"/"+this.productCode+"-"+this.photoNumber+DiversionSettings.PHOTO_SIZE_NAME+'.'+this.fileExtension;return sa;},extractFileExtension:function(ta){var ua;try{if(ta==null||!ta.lastIndexOf){return null;}
ua=ta.lastIndexOf('.');if(ua<0){return null;}
return ta.substring(ua+1);}
catch(e){}
return null;},showElement:function(va){va.show();},hideElement:function(wa){wa.hide();}});var DiversionMainImageQuickPreview=Class.create(DiversionQuickPreview,{initialize:function(xa,ya,za,Aa){if(Aa==null||za==null){return;}
this.productCode=xa;this.photoNumber=ya;this.mainElement=Aa;this.fileExtension=this.extractFileExtension(za.src);this.fileExtension=this.fileExtension==null?'jpg':this.fileExtension;this.originalFileName=Aa.src;Event.observe(za,'mouseover',this.mouseOver.bindAsEventListener(this));Event.observe(za,'mouseout',this.mouseOut.bindAsEventListener(this));Event.observe(za,'click',this.click.bindAsEventListener(this));},mouseOver:function(){if(this.mainElement==null){return;}
this.originalFileName=this.mainElement.src;this.mainElement.src=this.getThumbnailFileName();},mouseOut:function(){if(this.mainElement==null){return;}
this.mainElement.src=this.originalFileName;},click:function(){this.originalFileName=this.getThumbnailFileName();}});
