// JavaScript Document


/***************************/
// Services Tabs JS					
/***************************/

$(document).ready(function(){
	$(".menu > li").click(function(e){
		switch(e.target.id){
			case "cmc":
				//change status & style menu
				$("#cmc").addClass("active");
				$("#cc").removeClass("active");
				$("#cm").removeClass("active");
				//display selected division, hide others
				$("div.cmc").fadeIn();
				$("div.cc").css("display", "none");
				$("div.cm").css("display", "none");
			break;
			case "cc":
				//change status & style menu
				$("#cmc").removeClass("active");
				$("#cc").addClass("active");
				$("#cm").removeClass("active");
				//display selected division, hide others
				$("div.cc").fadeIn();
				$("div.cmc").css("display", "none");
				$("div.cm").css("display", "none");
			break;
			case "cm":
				//change status & style menu
				$("#cmc").removeClass("active");
				$("#cc").removeClass("active");
				$("#cm").addClass("active");
				//display selected division, hide others
				$("div.cm").fadeIn();
				$("div.cmc").css("display", "none");
				$("div.cc").css("display", "none");
			break;
		}
		//alert(e.target.id);
		return false;
	});
});


/***************************/
// Clients Tabs JS					
/***************************/

$(document).ready(function(){
	$(".our-clients > li").click(function(e){
		switch(e.target.id){
			case "consumer-retail":
				//change status & style menu
				$("#technology-logos").removeClass("active");
				$("#business-services").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#consumer-retail").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.consumer-retail").fadeIn();
				$("div.technology-logos").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.natural-resources").css("display", "none");
			break;
			case "business-services":
				//change status & style menu
				$("#technology-logos").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#business-services").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.business-services").fadeIn();
				$("div.technology-logos").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.natural-resources").css("display", "none");
			break;
			case "digital-media":
				//change status & style menu
				$("#technology-logos").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#business-services").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#digital-media").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-reseources").removeClass("active");
				//display selected division, hide others
				$("div.digital-media").fadeIn();
				$("div.technology-logos").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.natural-resources").css("display", "none");
			break;
			case "healthcare-bio":
				//change status & style menu
				$("#technology-logos").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#business-services").removeClass("active");
				$("#healthcare-bio").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.healthcare-bio").fadeIn();
				$("div.technology-logos").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.natural-resources").css("display", "none");
			break;
			case "clean-technology":
				//change status & style menu
				$("#business-services").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#technology-logos").removeClass("active");
				$("#clean-technology").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.clean-technology").fadeIn();
				$("div.technology-logos").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("#natural-resources").removeClass("active");
			break;
			case "technology-logos":
				//change status & style menu
				$("#business-services").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#technology-logos").addClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.technology-logos").fadeIn();
				$("div.clean-technology").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.natural-resources").css("display", "none");
			break;
			case "testimonials":
				//change status & style menu
				$("#business-services").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#technology-logos").removeClass("active");
				$("#featured").removeClass("active");
				$("#testimonials").addClass("active");
				$("#clean-technology").removeClass("active");
				$("#natural-resources").removeClass("active");
				//display selected division, hide others
				$("div.technology-logos").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.natural-resources").css("display", "none");
				$("div.testimonials").fadeIn();
			break;
			case "featured":
				//change status & style menu
				$("#business-services").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#technology-logos").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#featured").addClass("active");
				$("#natural-resources").removeClass("active");
				$("#clean-technology").removeClass("active");
				//display selected division, hide others
				$("div.technology-logos").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.natural-resources").css("display", "none");
				$("div.featured").fadeIn();
			break;
			case "natural-resources":
				//change status & style menu
				$("#business-services").removeClass("active");
				$("#consumer-retail").removeClass("active");
				$("#digital-media").removeClass("active");
				$("#healthcare-bio").removeClass("active");
				$("#technology-logos").removeClass("active");
				$("#testimonials").removeClass("active");
				$("#featured").removeClass("active");
				$("#natural-resources").addClass("active");
				$("#clean-technology").removeClass("active");
				//display selected division, hide others
				$("div.technology-logos").css("display", "none");
				$("div.business-services").css("display", "none");
				$("div.consumer-retail").css("display", "none");
				$("div.digital-media").css("display", "none");
				$("div.healthcare-bio").css("display", "none");
				$("div.testimonials").css("display", "none");
				$("div.clean-technology").css("display", "none");
				$("div.featured").css("display", "none");
				$("div.natural-resources").fadeIn();
			break;
		}
		//alert(e.target.id);
		return false;
	});
});



/***************************/
// Smooth Scrolling					
/***************************/
var ss = {
  fixAllLinks: function() {
    // Get a list of all links in the page
    var allLinks = document.getElementsByTagName('a');
    // Walk through the list
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if ((lnk.href && lnk.href.indexOf('#') != -1) && 
          ( (lnk.pathname == location.pathname) ||
	    ('/'+lnk.pathname == location.pathname) ) && 
          (lnk.search == location.search)) {
        // If the link is internal to the page (begins in #)
        // then attach the smoothScroll function as an onclick
        // event handler
        ss.addEvent(lnk,'click',ss.smoothScroll);
      }
    }
  },

  smoothScroll: function(e) {
    // This is an event handler; get the clicked on element,
    // in a cross-browser fashion
    if (window.event) {
      target = window.event.srcElement;
    } else if (e) {
      target = e.target;
    } else return;

    // Make sure that the target is an element, not a text node
    // within an element
    if (target.nodeName.toLowerCase() != 'a') {
      target = target.parentNode;
    }
  
    // Paranoia; check this is an A tag
    if (target.nodeName.toLowerCase() != 'a') return;
  
    // Find the <a name> tag corresponding to this href
    // First strip off the hash (first character)
    anchor = target.hash.substr(1);
    // Now loop all A tags until we find one with that name
    var allLinks = document.getElementsByTagName('a');
    var destinationLink = null;
    for (var i=0;i<allLinks.length;i++) {
      var lnk = allLinks[i];
      if (lnk.name && (lnk.name == anchor)) {
        destinationLink = lnk;
        break;
      }
    }
    if (!destinationLink) destinationLink = document.getElementById(anchor);

    // If we didn't find a destination, give up and let the browser do
    // its thing
    if (!destinationLink) return true;
  
    // Find the destination's position
    var destx = destinationLink.offsetLeft; 
    var desty = destinationLink.offsetTop;
    var thisNode = destinationLink;
    while (thisNode.offsetParent && 
          (thisNode.offsetParent != document.body)) {
      thisNode = thisNode.offsetParent;
      destx += thisNode.offsetLeft;
      desty += thisNode.offsetTop;
    }
  
    // Stop any current scrolling
    clearInterval(ss.INTERVAL);
  
    cypos = ss.getCurrentYPos();
  
    ss_stepsize = parseInt((desty-cypos)/ss.STEPS);
    ss.INTERVAL =
setInterval('ss.scrollWindow('+ss_stepsize+','+desty+',"'+anchor+'")',20);
  
    // And stop the actual click happening
    if (window.event) {
      window.event.cancelBubble = true;
      window.event.returnValue = false;
    }
    if (e && e.preventDefault && e.stopPropagation) {
      e.preventDefault();
      e.stopPropagation();
    }
  },

  scrollWindow: function(scramount,dest,anchor) {
    wascypos = ss.getCurrentYPos();
    isAbove = (wascypos < dest);
    window.scrollTo(0,wascypos + scramount);
    iscypos = ss.getCurrentYPos();
    isAboveNow = (iscypos < dest);
    if ((isAbove != isAboveNow) || (wascypos == iscypos)) {
      // if we've just scrolled past the destination, or
      // we haven't moved from the last scroll (i.e., we're at the
      // bottom of the page) then scroll exactly to the link
      window.scrollTo(0,dest);
      // cancel the repeating timer
      clearInterval(ss.INTERVAL);
      // and jump to the link directly so the URL's right
      location.hash = anchor;
    }
  },

  getCurrentYPos: function() {
    if (document.body && document.body.scrollTop)
      return document.body.scrollTop;
    if (document.documentElement && document.documentElement.scrollTop)
      return document.documentElement.scrollTop;
    if (window.pageYOffset)
      return window.pageYOffset;
    return 0;
  },

  addEvent: function(elm, evType, fn, useCapture) {
    // addEvent and removeEvent
    // cross-browser event handling for IE5+,  NS6 and Mozilla
    // By Scott Andrew
    if (elm.addEventListener){
      elm.addEventListener(evType, fn, useCapture);
      return true;
    } else if (elm.attachEvent){
      var r = elm.attachEvent("on"+evType, fn);
      return r;
    } else {
      alert("Handler could not be removed");
    }
  } 
}

ss.STEPS = 25;

ss.addEvent(window,"load",ss.fixAllLinks);// JavaScript Document



var INSTRUMENT = {
    
    common: {
        
        init: function(){
            Cufon.replace('.knockout_27', { fontFamily: 'Knockout 27 Junior Bantamwt' });
            Cufon.replace('.knockout_29', { fontFamily: 'Knockout 29 Junior Ltweight' });
            Cufon.now();
        }
        
    },
    
    
    /*- About Page Methods
    ------------------------------------------------------------------*/
    
    about: {
        
        init: function() {
            // added > into slector to ignore child ul and li items
            var members = $('#team > ul > li').not('#profilebox'),
                containerWidth = $('#team ul').width(),
                halfContainerWidth = containerWidth / 2;
            
            // simplified the logic by itterating through members and 
            // testing offset against half of the container width
            for (var i = 0; i < members.length; i++) {
                var xPosition = members[i].offsetLeft;
                if (xPosition >= halfContainerWidth) {
                    $(members[i]).addClass('balloon-left');
                }
            }
        },
        
        list: function(){}
        
    },
    
    /*- Blog Page Methods
    ------------------------------------------------------------------*/
    
    blog: {
        
        init: function(){
          Cufon.replace('blockquote', { fontFamily: 'Knockout 27 Junior Bantamwt' });
          $('#pattern_bar').addClass('pattern_'+Math.floor(Math.random()*4));
        },
        
        list: function(){},
        
        detail: function(){}
        
    },
    
    /*- Contact Page Methods
    ------------------------------------------------------------------*/
    
    contact: {
        
        init: function(){
            INSTRUMENT.contact.ss = new Slideshow("#slideshow_1", true);
        },
        
        list: function(){}
        
    },
    
    /*- Jobs Page Methods
    ------------------------------------------------------------------*/
    
    jobs: {
        
        init: function(){
            
        },
        
        list: function(){},
        
        detail: function(){}
        
    },
    
    /*- Merger Page Methods
    ------------------------------------------------------------------*/
    
    merger: {
        
        init: function(){
            
            var current_page = '#page_1';
            
            $('a.arrow').click(function (e) {
                e.preventDefault();
                $('html, body').animate({ scrollTop: $($(this).attr('href')).position().top }, 1500, 'easeInOutExpo');
                return false;
            });
            
            $('#merger').bind('mousewheel', function(event, delta) {
                $('#merger').stop();
            });
            
        },
        
        list: function(){
            
        }
        
    }
    
};

/*- UTIL
----------------------------------------------------------------------*/

var UTIL = {
    
    exec: function( controller, action ) {
        var ns = INSTRUMENT;
        
        action = ( action === undefined ) ? "init" : action;
        
        if ( controller !== "" && ns[controller] && typeof ns[controller][action] == "function" ) {
            ns[controller][action]();
        }
    },
    
    init: function() {
        var body = document.body,
            controller = body.getAttribute( "data-controller" ),
            action = body.getAttribute( "data-action" );
        
        UTIL.exec( "common" );
        UTIL.exec( controller );
        UTIL.exec( controller, action );
    }
    
};

// kick it all off here 
$( document ).ready( UTIL.init );

