// $Id:$

/**
 * @file
 */
(function ($) {
	
    tmsLocationMenu = function(){
      var pub = {};
      
      var themePath = '/sites/all/themes/tms/';
      var loaderImgPath = themePath + 'images/location_menu_loading.gif';
      var bgActiveImgPath = themePath + 'images/location_menu_bg_active.png';
      
      /**
       * Setup adds classes, clones the top element and adds the event listener
       */
      pub.setup = function() {
        // Temp fix for shanghai
        pub.doShanghaiFix();
        
        // Add our custom class to the loation dropdown
        $(".menu-block-3 ul").addClass('location-menu');
        $('ul.location-menu li').not('.active-trail').hide();

        // Move the active trail item to the top of the list
        $selectedLoc = $('ul.location-menu li.active-trail').clone();
        if ($selectedLoc.length == 0) {
        	$selectedLoc = $('ul.location-menu li:first').clone();
        	$selectedLoc.addClass('active-trail');
        	$selectedLoc.show();
        }
        $selectedLoc.addClass('selected-location');
        $selectedLoc.removeClass('last');
        $('ul.location-menu').prepend($selectedLoc);

        // Add our event listener
        $('ul.location-menu li a').click(pub.handleClick);
        
        // Preload images
        var pic1 = new Image(16,16); 
        pic1.src = loaderImgPath;
        var pic2 = new Image(126,28); 
        pic2.src = bgActiveImgPath; 
      };
    
      /**
       * Temp fix to add Shanghai as an alias for Hong Kong if it doesn't already exist
       */ 
      pub.doShanghaiFix = function() {
        $menuBlockUL = $(".menu-block-3 ul");
        $existingLink = $("li a[href='/shanghai']", $menuBlockUL)
        // Make sure shanghai didn't already come back at a content level
        if ($existingLink.length) {
          return;
        }
        // Otherwise, we create a fake one
        else {
          // Find sao-paulo so we can put this link below that one
          $targetLink = $("li a[href='/sao-paulo']", $menuBlockUL);
          if ($targetLink.length) {
            // Get the li container
            $targetLI = $("li a[href='/sao-paulo']", $menuBlockUL).closest("li");
            if ($targetLI.length) {
              // Write to the page
              $targetLI.after('<li class="leaf"><a href="/hong-kong">Shanghai</a></li>');
            }
          }
        }
      }
    
      /**
       * Handle the main click event
       */
      pub.handleClick = function(e) {
        // Get our parent elements
        var $ul = $(this).closest('ul.location-menu');
        var $li = $(this).closest('li');
       // If we are clicking on an exapended element, then either collapse or process the link
        if ($ul.hasClass('expanded')) {
          var ret = pub.clickExpanded($(this), $ul);
          if (ret) {
            return true;
          }
        }
        // Otherwise, expand the list
        else {
          pub.expand($ul);
        }
        return false;
      };
    
      /**
       * Expand the list
       */
      pub.expand = function($ul) {
        // Note that we are expanded
        $ul.addClass('expanded');
        // Show all list elements
        $('ul.location-menu li').show();
        // Add an event listener to the rest of the page to collapse
        $(document).one("click", function() {
          pub.collapse();
        });
      };
    
      /**
       * Handle a click into an already expanded list
       */
      pub.clickExpanded = function($clicked, $ul) {
        // Collapse
    	pub.collapse();
        // If the user clicked on the parent dropdown selector link, assume we are closing the menu and return false
    	var $li = $clicked.closest('li');
        if ($li.hasClass('selected-location')) {
          return false;
        }
    	// Otherwise proceed to process the location change, eventually returning true
        else {
          // Perform some sort of "working" feedback
          $selectedA = $('ul.location-menu li.selected-location a');
          $selectedA.html('<img src="'+ loaderImgPath +'" alt="loading" />');
          $selectedA.addClass('now-loading');
          setTimeout(function(){
              location.href = $clicked.attr('href');
            }, 100);
          return false;
        }
        
      };
      
      /**
       * 
       */
      pub.collapse = function() {
        // Remove the expanded class
        $('ul.location-menu').removeClass('expanded');
        // Hide all of those that are not in the active trail
        $('ul.location-menu li').not('.selected-location').hide();
      }
      
      return pub;
    }();

    
    // TODO: Move this into the Drupal namespace
    $(document).ready(function() {
  	// Call setup
      tmsLocationMenu.setup();
    });
})(jQuery);;
// $Id:$

/**
 * @file
 * Alters case study and solution teasers to create the hover effect on images.
 */
(function ($) {
	
	// Attach our behavior to the drupal namespace
    Drupal.behaviors.tms_teaser_hover = {
      attach: function(context, settings) {
    	// Call our function
    	tms_teaser_hover.init();
      }
    };
    
    var tms_teaser_hover = function(){
        var pub = {};
        
        var teaserWrapperSelector = '.teaser-img-wrapper';
        var teaserImgDivSelector = '.case-study-teaser-img';
        
        /**
         * Init function
         */
        pub.init = function() {
          pub.debug('called tms_teaser_hover.init', '');
          var $teaserWrappers = $(teaserWrapperSelector);
          if ($teaserWrappers) {
            pub.addOverOp($teaserWrappers);
            pub.setListeners($teaserWrappers);
          }
        };
        
        /**
         * Add the overlay opacity effect
         */
        pub.addOverOp = function($teaserWrappers) {
          var overlayDiv = '<div class="teaser-over-op" style="position: absolute; z-index: 10; top: 0; width: 100%; height: 100%; background-color: #FFFFFF;"></div>';
          $imgDiv = $(teaserImgDivSelector, $teaserWrappers);
          $imgDiv.prepend(overlayDiv);
          $(".teaser-over-op").css('opacity', 0.4);
        };
        
        /**
         * Set listeners
         */
        pub.setListeners = function($teaserWrappers) {
          // Set the hover listener
          $teaserWrappers.hover(
            // mouse over
            function(e) {
              // update the cursor to show that this is clickable
              $(this).css('cursor','pointer');
              // Hide the overlay opacity div
              $(".teaser-over-op", $(this)).hide();
            },
            // mouse out
            function(e) {
              // Change the cursor back
              $(this).css('cursor','auto');
              // Show the overlay opacity div again
              $(".teaser-over-op", $(this)).show();
            }
          );
          
          // Set the click event
          $teaserWrappers.click(function() {
            $imgLink = $(teaserImgDivSelector +" a", $(this));
            location.href = $imgLink.attr('href');
            return false; // just in case (avoids any double load of the page)
          });
        };
        
        /**
         * Debug wrapper
         */
        pub.debug = function(sMessage, oObject) {
          if (typeof console !== 'undefined' && 'log' in console) {
            console.log(sMessage, oObject);
          }
        };
        
        return pub;
    }();

})(jQuery);
;

