var hfs = {

  rolloverSuffix: '_on',
  fileExtension: '.gif',
  dataUrl : 'http://www.hfs-clinics.co.uk/hotspots/data/',
  obj:{},
  labelClass : 'hotspot_label',

  currentImage: 'http://www.hfs-clinics.co.uk/images/homepage/man-back.jpg', 
  currentData: false,
  data: [
      {
        alt : 'Your Body',
        image :'http://www.hfs-clinics.co.uk/images/homepage/man-back.jpg',
        hotspots : [
              
                   { 'start' : {
                           'left' : 270,
                           'top' : 125,
                           'width' : 10,
                           'height' : 10
                     },
                      'end' : {
                           'left' : 160,
                           'top' : 125,
                           'width' : 120,
                           'height' : 100
                     },
                     'styles' : {
                          'background' : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/right-left.png) top right no-repeat'
                     },   
                     'txt' : {
                          'start' : {
                              'left' : -200,
                              'top' : 85
                          },  
                          'end' : {
                              'left' : 20,
                              'top' : 85
                          },
                          'content' : 'Exercise rehabilitation to ensure your problems stay away'
                     }
                },
             
                   { 'start' : {
                           'left' : 540,
                           'top' : 265,
                           'width' : 10,
                           'height' : 10
                     },
                      'end' : {
                           'left' : 540,
                           'top' : 265,
                           'width' : 110,
                           'height' : 10
                     },
                     'styles' : {
                          'background' : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     'txt' : {
                          'start' : {
                              'left' : 930,
                              'top' : 250
                          },  
                          'end' : {
                              'left' : 670,
                              'top' : 250
                          },
                          'content' : 'All our Physiotherapists are trained in Osteopathic techniques as well as traditional Physiotherapy'
                     }
                }       
           ]},
{
        alt : 'Healthy Feet',
        image : 'http://www.hfs-clinics.co.uk/images/homepage/healthy-feet-home.jpg',
        hotspots : [
              
                   { start : {
                           'left' : 450,
                           'top' : 240,
                           'width' : 10,
                           'height' : 10
                     },
                      'end' : {
                           'left' : 450,
                           'top' : 240,
                           'width' : 80,
                           'height' : 10
                     },
                     'styles' : {
                          'background' : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     'txt' : {
                          'start' : {
                              'left' : 930,
                              'top' : 235
                          },  
                          'end' : {
                              'left' : 560,
                              'top' : 235
                          },
                          'content' : 'Expert analysis of your running technique and orthotic prescription'
                     }
                },
             
                   { start : {
                           'left' : 486,
                           'top' : 120,
                           'width' : 10,
                           'height' : 10
                     },
                      'end' : {
                           'left' : 486,
                           'top' : 120,
                           'width' : 125,
                           'height' : 10
                     },
                     'styles' : {
                          'background' : 'transparent  url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     'txt' : {
                          'start' : {
                              'left' : 1050,
                              'top' : 110
                          },  
                          'end' : {
                              'left' : 625,
                              'top' : 110
                          },
                          'content' : 'All aspects of general foot care treated'
                     }
                }       
           ]},
 {
        alt : 'Working Smart',
        image : 'http://www.hfs-clinics.co.uk/images/homepage/work-smart-home.jpg',
        hotspots : [
              
                   { 'start' : {
                           left : 310,
                           top : 125,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 200,
                           top : 125,
                           width : 120,
                           height : 100
                     },
                     styles : {
                          background : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/right-left.png) top right no-repeat'
                     },   
                     txt : {
                          start : {
                              left : -200,
                              top : 100
                          },  
                          end : {
                              left : 20,
                              top : 100
                          },
                          content : 'Our ergonomic assessments are not just the basic DSE checks'
                     }
                },
             
                   { 'start' : {
                           left : 545,
                           top : 125,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 545,
                           top : 125,
                           width : 125,
                           height : 10
                     },
                     styles : {
                          background : 'transparent  url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     txt : {
                          start : {
                              left : 930,
                              top : 100
                          },  
                          end : {
                              left : 680,
                              top : 100
                          },
                          content : 'Correct work station set up is vital for long term relief of spinal pain and headaches'
                     }
                }       
           ]},
 {
        alt : 'Massage',
        image : 'http://www.hfs-clinics.co.uk/images/homepage/massage-home.jpg',
        hotspots : [
              
                   { start : {
                           left : 360,
                           top : 238,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 360,
                           top : 238,
                           width : 125,
                           height : 10
                     },
                     styles : {
                          background : 'transparent  url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     txt : {
                          start : {
                              left : 930,
                              top : 230
                          },  
                          end : {
                              left : 500,
                              top : 230
                          },
                          content : 'Treat your muscles to sports massage delivered by experts at HFS.  This can be a great way to relieve stress and improve muscle performance.'
                     }
                }       
           ]},
 {
        alt : 'Pilates',
        image : 'http://www.hfs-clinics.co.uk/images/homepage/pilates-home.jpg',
        hotspots : [
              
                   { start : {
                           left : 590,
                           top : 279,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 590,
                           top : 279,
                           width : 80,
                           height : 10
                     },
                     styles : {
                          background : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     txt : {
                          start : {
                              left : 930,
                              top : 250
                          },  
                          end : {
                              left : 685,
                              top : 250
                          },
                          content : 'Better posture and control of movement means less pain and better performance.'
                     }
                },
             
                   { start : {
                           left : 400,
                           top : 50,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 400,
                           top : 50,
                           width : 80,
                           height : 10
                     },
                     styles : {
                          background : 'transparent url(http://www.hfs-clinics.co.uk/images/homepage/left-right.png) top left no-repeat'
                     },   
                     txt : {
                          start : {
                              left : 930,
                              top : 40
                          },  
                          end : {
                              left : 520,
                              top : 40
                          },
                          content : 'Pilates is often not effective when you are in pain.  You may well need some Physiotherapy initially to get the most out of it.'
                     }
                },
             
                   { start : {
                           left : 360,
                           top : 184,
                           width : 10,
                           height : 10
                     },
                      end : {
                           left : 260,
                           top : 184,
                           width : 110,
                           height : 10
                     },
                     styles : {
                          background : 'transparent  url(http://www.hfs-clinics.co.uk/images/homepage/right-left.png) top right no-repeat'
                     },   
                     txt : {
                          start : {
                              left : -200,
                              top : 157
                          },  
                          end : {
                              left : 85,
                              top : 157
                          },
                          content : 'We only use experienced physiotherapists to deliver this service.'
                     }
                }       
           ]}
], 

  preload: function( img ){
    var src = img.src.replace(hfs.fileExtension, hfs.rolloverSuffix + hfs.fileExtension);
    var i = new Image();
    i.src = src;
  },

  getElementBySrc: function(src){
      var ret;
    $$('#main_header_img img').each(function(img){
       if(img.src == src) ret = img;
   });
    return ret;
  },

  swapOn: function(){
     if( this.retrieve('rollover') == 'inactive'){return;}
      
       this.src = this.src.replace(hfs.fileExtension, hfs.rolloverSuffix + hfs.fileExtension);
     
  },

  swapOff: function(){
           if( this.retrieve('rollover') == 'inactive'){return;}
         this.src = this.src.replace(hfs.rolloverSuffix, '');
  },

  findData: function(el){
     for(var i=0; i<hfs.data.length; i++){
       if( hfs.data[i].alt == el.alt ){
            return i;
       }
     }
  },

  makeAllActive: function(){
     $$('#home_nav img').each( function(el){
         el.store('rollover', 'active');
         hfs.swapOff.attempt('', el);
     });
    },

  
    showLabel: function(){
		hfs.hideAllLabels();
        var obj = this.retrieve('show');
        var el = $(this.retrieve('txt') );
        var elObj = el.retrieve('show');
        el.morph( elObj );
        this.morph( obj );
         // this.getElement('p').fade('in');
      
            
     },
    
	//# Added on 
	showMyLabels: function()
	{
		var p, hot ;
		 hfs.currentData.hotspots.each(function(h, i){
			// create text container
			 p = new Element('div');
			 p.setStyles({ 'position' : 'absolute', 'overflow' : 'hidden', width:190, height:140 }); 
			 p.setStyles( h.txt.start );
			 p.set('html', h.txt.content );
			 p.addClass(hfs.labelClass);
			 p.store('hide', h.txt.start);
			 p.store('show', h.txt.end);         
			 p.id = hfs.currentData.alt.toLowerCase().replace(' ', '_') + i;
			 // create arrow container
			 hot = new Element('div');
			 hot.setStyles({'position':'absolute', 'overflow':'hidden' });
			 hot.setStyles(h.start);
			 hot.setStyles(h.styles);         
			 hot.store('hide', h.start);
			 hot.store('show', h.end);
			 hot.store('txt', p.id); 
			 hot.addEvent('mouseover', hfs.showLabel); 
			 p.inject('main_header_img');
			 hot.inject('main_header_img');

			// Call the hotspots
			var obj = hot.retrieve('show');
			var el = $(hot.retrieve('txt') );
			var elObj = el.retrieve('show');
			el.morph( elObj );
			hot.morph( obj );		 

			 
		 });
		 $('main_header_img').addEvent('mouseleave', hfs.hideAllLabels);
	},

    hideLabel: function(){
           var obj = this.retrieve('hide');
        var el = $(this.retrieve('txt') );
        var elObj = el.retrieve('hide');
        el.morph( elObj );
        this.morph( obj );
      
         
     }, 
     
     hideAllLabels: function(){
        $$('#main_header_img div').each(function(d){
            hfs.hideLabel.attempt('', d);
        });
    },
        
    removeLabels: function(){
        $$('#main_header_img div').each(function(d){d.destroy();});
    },

  // RUBEL on 16 Jan 2008
  /*addTextBoxes: function(){	 
     var p, hot ;
     hfs.currentData.hotspots.each(function(h, i){
        // create text container
         p = new Element('div');
         p.setStyles({ 'position' : 'absolute', 'overflow' : 'hidden', width:170, height:100 }); 
         p.setStyles( h.txt.start );
         p.set('html', h.txt.content );
         p.addClass(hfs.labelClass);
         p.store('hide', h.txt.start);
         p.store('show', h.txt.end);         
         p.id = hfs.currentData.alt.toLowerCase().replace(' ', '_') + i;
        // create arrow container
         hot = new Element('div');
         hot.setStyles({'position':'absolute', 'overflow':'hidden' });
         hot.setStyles(h.start);
         hot.setStyles(h.styles);         
         hot.store('hide', h.start);
         hot.store('show', h.end);
         hot.store('txt', p.id); 
         hot.addEvent('mouseover', hfs.showLabel); 
         p.inject('main_header_img');
         hot.inject('main_header_img');
         
     });
     $('main_header_img').addEvent('mouseleave', hfs.hideAllLabels);
  },*/

  gotoImage: function(){
      $('first_image_text_loader').setStyle('display', 'none');
     var obj = hfs.data[this.retrieve('index')];
     hfs.makeAllActive();
     hfs.swapOn.attempt('', this);
     this.store('rollover', 'inactive');
     hfs.removeLabels();
     if( !$chk(obj) ){ alert('data not found!'); return; }
     if( hfs.currentData ) hfs.currentImage = hfs.getElementBySrc( hfs.currentData.image ); 
     
     hfs.currentData = obj;
     var newImg = hfs.getElementBySrc( obj.image );
	 hfs.showMyLabels();
	 hfs.hideLoadingText();
	 /*	
     hfs.addTextBoxes();	
	 */
     hfs.currentImage.fade('out');
     newImg.fade('in');    
  }, 
  hideLoadingText: function(){$('first_image_text').fade('out');},
  hideAll: function(){
        $$('#main_header_img img').each(function(img){
            if(img != hfs.currentImage) img.setOpacity(0);
         });
   },

  addData: function(alt, i){
      new Request.JSON({url:hfs.dataUrl + alt, link:'chain', onSuccess:function(json, txt){hfs.data[i].hotspots.push(json);}, 
       }).get();

  },

  createData: function(){
       $$('#main_header_img img').each(function(img){
           hfs.data.push( {alt:img.alt, image:img.src, hotspots:[]} );
    });
     hfs.data.each(function(obj, i){
         hfs.addData( obj.alt.toLowerCase().replace(' ', '_'), i);
    }); 
  },
  init:function(){
  
     $$('#home_nav img').each( function(el){
        hfs.preload( el );
        el.addEvent('mouseover', hfs.swapOn);
        el.addEvent('mouseout', hfs.swapOff);
        el.store('rollover', 'active');
        el.store( 'index', hfs.findData(el) );
        el.addEvent( 'click', hfs.gotoImage);
       
     });
     hfs.currentImage = hfs.getElementBySrc(hfs.currentImage);
     hfs.hideAll();
     
  }
}

window.addEvent('domready', hfs.init);   
   