/*--------------------------------------------------------------------------*
 * 
 * Category Preview (Thames)
 * 
 * Version 1.0.2
 * 
 * Copyright (C) 2008 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 (at your option) 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 Thames={load:function(){if(location.pathname.toLowerCase()=='/searchresults.asp'||location.pathname.toLowerCase().indexOf('-s/')!=-1||location.pathname.toLowerCase().indexOf('_s/')!=-1){if(ThamesCategoryPreviewSettings.ENABLED){ThamesCategoryPreview.load();}}}};Event.observe(window,'load',Thames.load);var ThamesCategoryPreviewSettings={ENABLED:false,PHOTOS_PATH:'/v/vspfiles/photos',MORE_INFO_FILE_NAME:'Bullet_MoreInfo.gif',ADD_TO_CART_URL_TEMPLATE:new Template('/ShoppingCart.asp?ProductCode=#{productCode}'),GET_PHOTOS_URL_TEMPLATE:new Template('/v/thames_get_photos.asp?productcode=#{productCode}&type=T'),CONTENT_AREA_ID:'content_area',NO_PHOTO_FILE_NAME:'nophoto.gif'};var ThamesCategoryPreview={body:null,ieAppVersion:null,load:function(){var aa=null;var ba=null;try{ThamesCategoryPreview.body=$$('body').first();aa=$(ThamesCategoryPreviewSettings.CONTENT_AREA_ID);if(aa==null||ThamesCategoryPreview.body==null){return;}
ba=aa.select('a');if(ba==null||ba.size<1){return null;}
if(Prototype.Browser.IE){var ca=navigator.userAgent;var da=null;da=ca.match(/MSIE (\d+\.\d+);/);if(da!=null&&da.length>=2){ThamesCategoryPreview.ieAppVersion=new Number(da[1]);new Element('img',{src:'/v/thames/images/preview.gif'});}}
new Element('img',{src:'/v/thames/images/preview.png'});ba.each(function(ea){var fa=null;var ga=null;var ha=null;var ia=null;ha=ThamesCategoryPreview.getProductCodeFromURL(ea.href);if(ha==null){return;}
fa=ea.select('img');if(fa==null||fa.size()!=1){return;}
if(fa.first().src.indexOf(ThamesCategoryPreviewSettings.MORE_INFO_FILE_NAME)!=-1){return;}
if(fa.first().src.indexOf(ThamesCategoryPreviewSettings.NO_PHOTO_FILE_NAME)!=-1){return;}
new ThamesCategoryPreviewInitiator(ha,fa.first());});}
catch(e){return;}},getProductCodeFromURL:function(ja){var ka=null;if(ja==null){return null;}
ka=ja.match(/\/productdetails\.asp\?(?:[\&]?.*\=.*)*productcode=([^\&\#]+)/i);if(ka!=null&&ka.length>=2){return ka[1];}
ka=ja.match(/(?:_p|-p)\/(.+)\.htm/i);if(ka!=null&&ka.length>=2){return ka[1];}
return null;}};var ThamesCategoryPreviewInitiator=Class.create({initialize:function(la,ma){if(la==null||ma==null){return;}
this.imageElement=ma;this.imageElementPosition=ma.positionedOffset();this.productCode=la;this.mouseMoveEventListener=null;this.clickEventListener=null;this.overlayElement=null;this.previewWindow=null;Event.observe(ma,'mouseover',this.mouseOver.bindAsEventListener(this));},mouseOver:function(na){var oa=null;if(this.overlayElement!=null){return;}
oa=this.createOverlay();if(oa==null||oa.main==null|oa.button==null){return null;}
this.overlayElement=oa.main;this.buttonElement=oa.button;this.clickEventListener=this.click.bindAsEventListener(this);Event.observe(this.overlayElement,'click',this.clickEventListener);ThamesCategoryPreview.body.insert(this.overlayElement);this.overlayElement.clonePosition(this.imageElement,{setHeight:false,setWidth:false});this.showOverlay();this.mouseMoveEventListener=this.mouseMove.bindAsEventListener(this);Event.observe(ThamesCategoryPreview.body,'mousemove',this.mouseMoveEventListener);},showOverlay:function(){if(this.overlayElement==null){return;}
this.overlayElement.show();},hideOverlayAndRemove:function(pa){if(pa==null){return;}
pa.hide();pa.remove();},createOverlay:function(){var qa=null;var ra=null;var sa='/v/thames/images/preview.png';try{if(Prototype.Browser.IE){if(ThamesCategoryPreview.ieAppVersion<7.0){sa='/v/thames/images/preview.gif';}}}
catch(e){}
qa=new Element('div');qa.addClassName('thames_category_quick_preview_overlay');qa.setStyle({position:'absolute',zIndex:100,display:'none',cursor:'pointer'});ra=new Element('img',{src:sa,alt:'Preview',title:'Preview'});ra.addClassName('thames_category_quick_preview_overlay_btn');qa.insert(ra);ra.setStyle({cursor:'pointer'});return{main:qa,button:ra};},mouseMove:function(ta){var x=Event.pointerX(ta);var y=Event.pointerY(ta);if(this.overlayElement==null){return;}
if(this.boundsCheck(x,y,this.imageElement,this.imageElementPosition)){return;}
if(this.mouseMoveEventListener!=null){Event.stopObserving(ThamesCategoryPreview.body,'mousemove',this.mouseMoveEventListener);}
this.mouseMoveEventListener=null;this.mouseOut();},mouseOut:function(){var ua=this.overlayElement;this.overlayElement=null;if(ua!=null){this.hideOverlayAndRemove(ua);}},boundsCheck:function(x,y,va,wa){var xa=wa;var ya={};var za=true;var Aa=true;var Ba=true;var Ca=true;if(va==null){return false;}
if(arguments[3]){ya=arguments[3];}
za=ya['excludeLeftEdge']?(x>xa.left):(x>=xa.left);Aa=ya['excludeRightEdge']?(x<(xa.left+va.getWidth())):(x<=(xa.left+va.getWidth()));Ba=ya['excludeTopEdge']?(y>xa.top):(y>=xa.top);Ca=ya['excludeBottomEdge']?(y<(xa.top+va.getHeight())):(y<=(xa.top+va.getHeight()));if(za&&Aa){if(Ba&&Ca){return true;}}
return false;},click:function(Da){var Ea=this;var Fa=null;if(this.clickEventListener!=null&&this.overlayElement!=null){Event.stopObserving(this.overlayElement,'click',this.clickEventListener);}
this.clickEventListener=null;if(this.previewWindow!=null){this.mouseOut();this.previewWindow.load();return;}
Fa=ThamesCategoryPreviewSettings.GET_PHOTOS_URL_TEMPLATE.evaluate({productCode:escape(this.productCode)});new Ajax.Request(Fa,{method:'get',evalJSON:true,sanitizeJSON:true,onCreate:function(){Ea.startShowingWait();},onSuccess:function(Ga){var Ha=Ga.responseJSON;Ea.mouseOut();Ea.stopShowingWait();if(Ha==null||Ha.size()<=0){return;}
Ea.previewWindow=new ThamesCategoryPreviewWindow(Ea.productCode,Ea.imageElement,Ha);Ea.previewWindow.load();},onFailure:function(e){Ea.stopShowingWait();}});},startShowingWait:function(){if(this.imageElement!=null){this.imageElement.setStyle({cursor:'wait'});}
if(this.overlayElement!=null){this.overlayElement.setStyle({cursor:'wait'});this.buttonElement.setStyle({cursor:'wait'});}},stopShowingWait:function(){if(this.imageElement!=null){this.imageElement.setStyle({cursor:'auto'});}
if(this.overlayElement!=null){this.overlayElement.setStyle({cursor:'pointer'});this.buttonElement.setStyle({cursor:'pointer'});}}});var ThamesCategoryPreviewWindow=Class.create({initialize:function(Ia,Ja,Ka){var La=null;if(Ia==null||Ja==null||Ka==null||Ka.size()<=0){return;}
this.imageElement=Ja;this.productCode=Ia;this.data=Ka;this.elements=null;this.currentPhotoPosition=0;this.imageElements=[];this.maxPhotoWidth=this.data.max(function(Ma){return Ma.width;});this.maxPhotoHeight=this.data.max(function(Na){return Na.height;});},load:function(){var Oa;var Pa;var Qa=null;var Ra=this;var Sa=null;if(this.elements!=null){return;}
this.elements=this.createWindow();Qa=this.elements.main;ThamesCategoryPreview.body.insert(Qa);Sa=this.imageElement.positionedOffset();if(this.isTooWide(Sa.left,Qa.getWidth())){var Ta=(Sa.left+this.imageElement.getWidth())-Qa.getWidth();Qa.setStyle({top:Sa.top+'px',left:Ta+'px'});}
else{Qa.clonePosition(this.imageElement,{setHeight:false,setWidth:false});}
this.data.each(function(Ua){var Va=null;var Wa=Ra.imageElement.alt;var Xa=Ra.imageElement.title;Va=new Element('img',{src:ThamesCategoryPreviewSettings.PHOTOS_PATH+'/'+escape(Ua.fileName),width:Ua.width,height:Ua.height,alt:Wa,title:Xa});Va.addClassName('thames_category_quick_preview_window_photo');Va.setStyle({width:Ua.width+'px',height:Ua.height+'px'});Event.observe(Va,'click',Ra.goToProductDetail.bindAsEventListener(Ra));Ra.imageElements.push(Va);});this.updatePhoto(this.currentPhotoPosition);Event.observe(this.elements.close,'click',this.closeWindow.bindAsEventListener(this));if(this.elements.addToCart!=null){Event.observe(this.elements.addToCart,'click',this.addToCart.bindAsEventListener(this));}
if(this.data.size()==1){this.elements.left.remove();this.elements.right.remove();}
else{Event.observe(this.elements.left,'click',this.previousPhoto.bindAsEventListener(this));Event.observe(this.elements.right,'click',this.nextPhoto.bindAsEventListener(this));}
this.showWindow();},showWindow:function(){if(this.elements==null||this.elements.main==null){return;}
Effect.Appear(this.elements.main,{queue:'end',duration:0.4});},hideWindowAndRemove:function(){if(this.elements==null||this.elements.main==null){return;}
Effect.Fade(this.elements.main,{queue:'end',duration:0.4,afterFinish:function(Ya){if(Ya.element!=null){Ya.element.remove();}}});},closeWindow:function(){if(this.elements==null||this.elements.main==null){return;}
this.hideWindowAndRemove();this.elements=null;this.imageElements=[];},nextPhoto:function(){if(this.elements==null||this.imageElements==null||this.elements.photoContainer==null){return;}
this.currentPhotoPosition++;this.currentPhotoPosition=Math.abs(this.currentPhotoPosition%this.imageElements.size());this.updatePhoto(this.currentPhotoPosition);},previousPhoto:function(){if(this.elements==null||this.imageElements==null||this.elements.photoContainer==null){return;}
this.currentPhotoPosition--;if(this.currentPhotoPosition<0){this.currentPhotoPosition=this.imageElements.size()-1;}
this.currentPhotoPosition=Math.abs(this.currentPhotoPosition%this.imageElements.size());this.updatePhoto(this.currentPhotoPosition);},updatePhoto:function(Za){if(this.elements==null||this.elements.photoContainer==null){return;}
if(this.imageElements==null||Za>=this.imageElements.size()){return;}
this.elements.photoContainer.update(this.imageElements[Za]);},createWindow:function(){var $a=null;var ab=null;var bb=null;var cb=null;var db=null;var eb=null;var fb=null;var gb=null;var hb=null;var ib=null;var jb=null;var kb=null;$a=new Element('div');$a.addClassName('thames_category_quick_preview_window');$a.setStyle({position:'absolute',zIndex:101,opacity:0,display:'none'});ab=new Element('div');ab.addClassName('thames_category_quick_preview_window_top');bb=new Element('div');bb.addClassName('thames_category_quick_preview_window_top_left');cb=new Element('div');cb.addClassName('thames_category_quick_preview_window_top_right');kb=new Element('img',{src:'/v/thames/images/drag.gif',alt:'Drag',title:'Drag'});kb.addClassName('thames_category_quick_preview_window_drag_btn');jb=new Element('input',{type:'image',src:'/v/thames/images/close.gif',alt:'Close',title:'Close'});jb.addClassName('thames_category_quick_preview_window_close_btn');bb.insert(kb);cb.insert(jb);ab.insert(bb);ab.insert(cb);eb=new Element('div');eb.addClassName('thames_category_quick_preview_window_middle');db=new Element('div');db.setStyle({textAlign:'center',width:this.maxPhotoWidth +6+'px',height:this.maxPhotoHeight +6+'px'});eb.insert(db);fb=new Element('div');fb.addClassName('thames_category_quick_preview_window_bottom');gb=new Element('input',{type:'image',src:'/v/thames/images/previous.gif',alt:'Previous Photo',title:'Previous Photo'});gb.addClassName('thames_category_quick_preview_window_left_btn');hb=new Element('input',{type:'image',src:'/v/thames/images/add_to_cart.gif',alt:'Add To Cart',title:'Add To Cart'});hb.addClassName('thames_category_quick_preview_window_add_btn');ib=new Element('input',{type:'image',src:'/v/thames/images/next.gif',alt:'Next Photo',title:'Next Photo'});ib.addClassName('thames_category_quick_preview_window_right_btn');fb.insert(gb);fb.insert(hb);fb.insert(ib);$a.insert(ab);$a.insert(eb);$a.insert(fb);new Draggable($a,{starteffect:null,endeffect:null});return{main:$a,photoContainer:db,close:jb,left:gb,right:ib,addToCart:hb};},isTooWide:function(x,lb){var mb=document.viewport.getWidth();if(mb<lb){return false;}
if((x+lb)>mb){return true;}
return false;},addToCart:function(){var nb=ThamesCategoryPreviewSettings.ADD_TO_CART_URL_TEMPLATE;var ob=null;ob=nb.evaluate({productCode:escape(this.productCode)});window.location.href=ob;},goToProductDetail:function(){var pb=this.imageElement.up();if(pb!=null&&pb.href){window.location.href=pb.href;}}});