/*
    tabSlideOUt v1.1
    
    By William Paoli: http://wpaoli.building58.com

    To use you must have an image ready to go as your tab
    Make sure to pass in at minimum the path to the image and its dimensions:
    
    example:
    
        $('.slide-out-div').tabSlideOut({
                tabHandle: '.handle',                         //class of the element that will be your tab -doesnt have to be an anchor
                pathToTabImage: 'images/contact_tab.gif',     //relative path to the image for the tab *required*
                imageHeight: '133px',                         //height of tab image *required*
                imageWidth: '44px',                           //width of tab image *required*    
        });

    
*/


(function($){



  // iPad detect - Maedi
  jQuery.extend(jQuery.browser,
  {SafariMobile : navigator.userAgent.toLowerCase().match(/iP(hone|ad)/i) }
  );

    $.fn.tabSlideOut = function(callerSettings) {
        var settings = $.extend({
            tabHandle: '.handle',
            speed: 300, 
            action: 'click',
            tabLocation: 'left',
            topPos: '200px',
            leftPos: '20px',
            fixedPosition: false,
            positioning: 'absolute',
            pathToTabImage: null,
            imageHeight: null,
            imageWidth: null                       
        }, callerSettings||{});

        settings.tabHandle = $(settings.tabHandle);
        var obj = this;
        if (settings.fixedPosition === true) {
            settings.positioning = 'fixed';
        } else {
            settings.positioning = 'absolute';
        }
        
        //ie6 doesn't do well with the fixed option
        if (document.all && !window.opera && !window.XMLHttpRequest) {
            settings.positioning = 'absolute';
        }
        
        //set initial tabHandle css
        settings.tabHandle.css({ 
            'display': 'block',
            'width' : settings.imageWidth,
            'height': settings.imageHeight,
            'textIndent' : '-99999px',
            'background' : 'url('+settings.pathToTabImage+') no-repeat',
            'outline' : 'none',
            'position' : 'absolute'
        });
        
        obj.css({
            'line-height' : '1',
            'position' : settings.positioning
        });

        
        var properties = {
                    containerWidth: parseInt(obj.outerWidth(), 10) + 'px',
                    containerHeight: parseInt(obj.outerHeight(), 10) + 'px',
                    tabWidth: parseInt(settings.tabHandle.outerWidth(), 10) + 'px',
                    tabHeight: parseInt(settings.tabHandle.outerHeight(), 10) + 'px'
                };

        //set calculated css
        if(settings.tabLocation === 'top' || settings.tabLocation === 'bottom') {
            obj.css({'left' : settings.leftPos});
            settings.tabHandle.css({'right' : 0});
        }
        
        if(settings.tabLocation === 'top') {
            obj.css({'top' : '-' + properties.containerHeight});
            settings.tabHandle.css({'bottom' : '-' + properties.tabHeight});
        }

        if(settings.tabLocation === 'bottom') {
            obj.css({'bottom' : '-' + properties.containerHeight, 'position' : 'fixed'});
            settings.tabHandle.css({'top' : '-' + properties.tabHeight});
            
        }
        
        if(settings.tabLocation === 'left' || settings.tabLocation === 'right') {
            obj.css({
                'height' : properties.containerHeight,
                'top' : settings.topPos
            });
            
            settings.tabHandle.css({'top' : 0});
        }
        
        if(settings.tabLocation === 'left') {
            obj.css({ 'left': '-' + properties.containerWidth});
            settings.tabHandle.css({'right' : '-' + properties.tabWidth});
        }

        if(settings.tabLocation === 'right') {
            obj.css({ 'right': '-' + properties.containerWidth});
            settings.tabHandle.css({'left' : '-' + properties.tabWidth});
            
            $('html').css('overflow-x', 'hidden');
        }

        //functions for animation events
        
        settings.tabHandle.click(function(event){
            event.preventDefault();
        });
        
        var slideIn = function() {
        
          // iPad / iPHone only hide
          if($.browser.SafariMobile){

            $('#block-cck_blocks-field_sidebar_overlay .content').css({
          
              'display' : 'none'
            });
            
          };
          
                      
          if (settings.tabLocation === 'top') {
              obj.animate({top:'-' + properties.containerHeight}, settings.speed).removeClass('open');
          } else if (settings.tabLocation === 'left') {
              obj.animate({left: '-' + properties.containerWidth}, settings.speed).removeClass('open');
          } else if (settings.tabLocation === 'right') {
              obj.animate({right: '-' + properties.containerWidth}, settings.speed).removeClass('open');
          } else if (settings.tabLocation === 'bottom') {
              obj.animate({bottom: '-' + properties.containerHeight}, settings.speed).removeClass('open');
          }    
            
        };
        
        var slideOut = function() {
         
          // iPad show
          $('#block-cck_blocks-field_sidebar_overlay .content').css({
          
            'display' : 'block'
          });
          
          

            
            if (settings.tabLocation == 'top') {
                obj.animate({top:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'left') {
                obj.animate({left:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'right') {
                obj.animate({right:'-3px'},  settings.speed).addClass('open');
            } else if (settings.tabLocation == 'bottom') {
                obj.animate({bottom:'-3px'},  settings.speed).addClass('open');
            }
        };

        var clickScreenToClose = function() {
            obj.click(function(event){
                event.stopPropagation();
            });
            
            $(document).click(function(){
                slideIn();
            });
        };
        
        var clickAction = function(){
            settings.tabHandle.click(function(event){
                if (obj.hasClass('open')) {
                    slideIn();
                } else {
                    slideOut();
                }
            });
            
            clickScreenToClose();
        };
        
        var hoverAction = function(){
            obj.hover(
                function(){
                    slideOut();
                },
                
                function(){
                    slideIn();
                });
                
                settings.tabHandle.click(function(event){
                    if (obj.hasClass('open')) {
                        slideIn();
                    }
                });
                clickScreenToClose();
                
        };
        
        //choose which type of action to bind
        if (settings.action === 'click') {
            clickAction();
        }
        
        if (settings.action === 'hover') {
            hoverAction();
        }
    };
})(jQuery);
;
$(document).ready(function(){
  
  // Fading image replacment
  $('.img-hover').imghover({suffix: '-over', fade: 'true', fadeSpeed: '200'});

  // Click to Reveal content     
  
  // Hide definitions
  $('dl.reveal dd').css('display','none');      
  
  // Click terms to reveal definitions
  $('dl.reveal dt').click(function() {         
    if ($(this).hasClass('reveal-open')) {
      $(this).removeClass('reveal-open');      
    } else {      
      $(this).addClass('reveal-open');
    }
    $(this).next('dd').slideToggle('fast',function() {      
      // animation complete
    });
  });
    
  // Sidebar overlay block
  
  $('#block-cck_blocks-field_sidebar_overlay').tabSlideOut({
    tabHandle: '#block-cck_blocks-field_sidebar_overlay .block-title',                     //class of the element that will become your tab
    //pathToTabImage: '/sites/www.hydro.com.au/themes/hydro/images/additional-info-tab.png', //path to the image for the tab //Optionally can be set using css
    pathToTabImage: '/sites/hydro.digitalink.com.au/themes/hydro/images/additional-info-tab.png',
    imageHeight: '186px',                     //height of tab image           //Optionally can be set using css
    imageWidth: '36px',                       //width of tab image            //Optionally can be set using css
    tabLocation: 'right',                      //side of screen where tab lives, top, right, bottom, or left
    speed: 300,                               //speed of animation
    action: 'click',                          //options: 'click' or 'hover', action to trigger animation
    topPos: '330px',                          //position from the top/ use if tabLocation is left or right
    leftPos: '300px',                          //position from left/ use if tabLocation is bottom or top
    fixedPosition: false                      //options: true makes it stick(fixed position) on scroll
  });

  // Region Accordian  
  
  // Hide definitions
  $('.accordion-item .def').css('display','none');      
  
  // Click terms to reveal definitions
  $('.accordion-item .term').click(function(event) { 
  
    event.preventDefault();
   
    // add and remove reveal-open class    
    if ($(this).hasClass('reveal-open')) {    
      $(this).removeClass('reveal-open'); 
    } else {    
      $(this).addClass('reveal-open');
    }
      
    // only toggle if not already opened by region map    
    if (!$(this).hasClass('map-revealed')) {
      $(this).next('.def').slideToggle('600',function() {      
        // animation complete
      });    
    }    
  });  
  
  // Automatically open accordians when their ID is in the URL
  
  if(window.location.hash) {
    $('html,body').animate({
      scrollTop: $(window.location.hash).offset().top
      }, 1000);
  }
  
  setTimeout( function() {
    if(window.location.hash) {
      $(window.location.hash + ' .term').click();
    }    
  }, 500);    

}); // End $(document).ready();
/*
  x
  start 143.67
  end 148.42
  difference 4.75

  y
  start -39.55
  end -43.52
  difference 3.97
  
  input 145
  start to input 1.33
  percentage of grid = (input - 143.67 ) / 4.75
  
  hobart y -42.875964
  ((42.875964 -39.55 ) / 3.97 ) * 594 = 497px;
  hobart x 147.315674
  ((147.315674 - 143.67 ) / 4.75 ) * 525 = 402px;
  
*/
  
function xToPx(x) {

  var lat = Math.abs(parseFloat(x));
  var start_lat = 143.67;
  var width = 525 - 45; // - random number to conform to graphic / real-lat-start-point inconsistencies
      
  // find the distance from the start latitude to the input latitude
  // then divide by the total distance between the start and stop lat   
  // then convert this decimal percentage to pixels 
  
  return result = ((lat - start_lat ) / 4.75 ) * width;  	
}

function yToPx(y) {

  var lon = Math.abs(parseFloat(y)); // positive number even though always negative
  var start_lon = 39.55;
  var height = 594 - 35; // - random number to conform to graphic
  
  return (( lon - start_lon ) / 3.97 ) * height;	
}


$(document).ready( function() {


  // PROJECT PINS
  
  var current = 0,
  map = $('.map-canvas'),
  pins = $('.map-canvas .map-pin');

  // for each pin, plot coordinates
  pins.each(function () {
  
    $(this).css({
      left: '' + xToPx($('.longitude', this).text()) + 'px',
      top: '' + yToPx($('.latitude', this).text()) + 'px'
    });
    
  });
  
  pins.show();


  // PROJECT POPUPS
  
  $('.map-canvas .map-pin').each(function () {
  
	// options
	var distance = 10;
	var time = 250;
	var hideDelay = 500;
	
	var hideDelayTimer = null;
	
	// tracker
	var beingShown = false;
	var shown = false;
	
	var trigger = $('.pin', this);
	var popup = $('.popup', this).css('opacity', 0);
	
	// set the mouseover and mouseout on both element
	$([trigger.get(0), popup.get(0)]).mouseover(function () {
	  // stops the hide event if we move from the trigger to the popup element
	  if (hideDelayTimer) clearTimeout(hideDelayTimer);
	
	  // don't trigger the animation again if we're being shown, or already visible
	  if (beingShown || shown) {
	    return;
	  } else {
	    beingShown = true;
	
	    // reset position of popup box
	    popup.css({
	      top: -50,
	      left: -33,
	      display: 'block' // brings the popup back in to view
	    })
	
	    // (we're using chaining on the popup) now animate it's opacity and position
	    .animate({
	      top: '-=' + distance + 'px',
	      opacity: 1
	    }, time, 'swing', function() {
	      // once the animation is complete, set the tracker variables
	      beingShown = false;
	      shown = true;
	    });
	  }
	}).mouseout(function () {
	  // reset the timer if we get fired again - avoids double animations
	  if (hideDelayTimer) clearTimeout(hideDelayTimer);
	  
	  // store the timer so that it can be cleared in the mouseover if required
	  hideDelayTimer = setTimeout(function () {
	    hideDelayTimer = null;
	    popup.animate({
	      top: '-=' + distance + 'px',
	      opacity: 0
	    }, time, 'swing', function () {
	      // once the animate is complete, set the tracker variables
	      shown = false;
	      // hide the popup entirely after the effect (opacity alate doesn't do the job)
	      popup.css('display', 'none');
	    });
	  }, hideDelay);
	});
 }); 
 
  
  // REGION POPUPS
  
  $('.region').each(function () {
  
	// options
	var distance = 10;
	var time = 250;
	var hideDelay = 500;
	
	var hideDelayTimer = null;
	
	// tracker
	var beingShown = false;
	var shown = false;
	
	var trigger = $(this);
	var popup = $('.popup', this);
	var regionID = popup.parent().attr('id');
	
	
	// popup position
	var top = 0;
	var left = -10;
	
	
	// IE7 Z-INDEX FIX
	if($.browser.msie && parseInt($.browser.version, 10) == 7) {

    // move popups away from overlaying regions
    switch(regionID) {
      case 'derwent': 				left = 50; 						break;
      case 'great-lake': 			left = 50; top = -20; break;
      case 'mersey-forth':		top = -120; 					break;
      case 'anthony-pieman':	left = -155; 					break;
      case 'mersey-forth':		top = -90; 						break;
      case 'king-yolande':		left = -140;					break;
    }
  }
	

	// set the mouseover and mouseout on both element
	$([trigger.get(0), popup.get(0)]).mouseover(function () {
	  // stops the hide event if we move from the trigger to the popup element
	  if (hideDelayTimer) clearTimeout(hideDelayTimer);
	
	  // don't trigger the animation again if we're being shown, or already visible
	  if (beingShown || shown) {
	    return;
	  } else {
	    beingShown = true;

	    // reset position of popup box
	    popup.css({
	      top: top,
	      left: left,
	      display: 'block'
	    })
	    // (we're using chaining on the popup) now animate it's opacity and position
	    .animate({
	      top: '-=' + distance + 'px',
	      opacity: 1 // required to fix dissapearing popup bug in IE
	    }, time, 'swing', function() {
	      // once the animation is complete, set the tracker variables
	      beingShown = false;
	      shown = true;
	    });
	    
	  }
	}).mouseout(function () {
	  // reset the timer if we get fired again - avoids double animations
	  if (hideDelayTimer) clearTimeout(hideDelayTimer);
	  
	  // store the timer so that it can be cleared in the mouseover if required
	  hideDelayTimer = setTimeout(function () {
	    hideDelayTimer = null;
	    popup.animate({
	      top: '-=' + distance + 'px',
	      opacity: 0
	    }, time, 'swing', function () {
	      // once the animate is complete, set the tracker variables
	      shown = false;
	      // hide the popup entirely after the effect (opacity alone doesn't do the job)
	      popup.css('display', 'none');
	    });
	  }, hideDelay);
	});
  });
 
 
 
});
;

