/**
 * jQuery.ScrollTo - Easy element scrolling using jQuery.
 * Copyright (c) 2007-2008 Ariel Flesler - aflesler(at)gmail(dot)com | http://flesler.blogspot.com
 * Dual licensed under MIT and GPL.
 * Date: 9/11/2008
 * @author Ariel Flesler
 * @version 1.4
 *
 * http://flesler.blogspot.com/2007/10/jqueryscrollto.html
 */
;(function(h){var m=h.scrollTo=function(b,c,g){h(window).scrollTo(b,c,g)};m.defaults={axis:'y',duration:1};m.window=function(b){return h(window).scrollable()};h.fn.scrollable=function(){return this.map(function(){var b=this.parentWindow||this.defaultView,c=this.nodeName=='#document'?b.frameElement||b:this,g=c.contentDocument||(c.contentWindow||c).document,i=c.setInterval;return c.nodeName=='IFRAME'||i&&h.browser.safari?g.body:i?g.documentElement:this})};h.fn.scrollTo=function(r,j,a){if(typeof j=='object'){a=j;j=0}if(typeof a=='function')a={onAfter:a};a=h.extend({},m.defaults,a);j=j||a.speed||a.duration;a.queue=a.queue&&a.axis.length>1;if(a.queue)j/=2;a.offset=n(a.offset);a.over=n(a.over);return this.scrollable().each(function(){var k=this,o=h(k),d=r,l,e={},p=o.is('html,body');switch(typeof d){case'number':case'string':if(/^([+-]=)?\d+(px)?$/.test(d)){d=n(d);break}d=h(d,this);case'object':if(d.is||d.style)l=(d=h(d)).offset()}h.each(a.axis.split(''),function(b,c){var g=c=='x'?'Left':'Top',i=g.toLowerCase(),f='scroll'+g,s=k[f],t=c=='x'?'Width':'Height',v=t.toLowerCase();if(l){e[f]=l[i]+(p?0:s-o.offset()[i]);if(a.margin){e[f]-=parseInt(d.css('margin'+g))||0;e[f]-=parseInt(d.css('border'+g+'Width'))||0}e[f]+=a.offset[i]||0;if(a.over[i])e[f]+=d[v]()*a.over[i]}else e[f]=d[i];if(/^\d+$/.test(e[f]))e[f]=e[f]<=0?0:Math.min(e[f],u(t));if(!b&&a.queue){if(s!=e[f])q(a.onAfterFirst);delete e[f]}});q(a.onAfter);function q(b){o.animate(e,j,a.easing,b&&function(){b.call(this,r,a)})};function u(b){var c='scroll'+b,g=k.ownerDocument;return p?Math.max(g.documentElement[c],g.body[c]):k[c]}}).end()};function n(b){return typeof b=='object'?b:{top:b,left:b}}})(jQuery);;
/*
 *
 * Copyright (c) 2010 Jeff Lyon, Albatross Digital
 * Dual licensed under the MIT and GPL licenses.
 * Uses the same license as jQuery, see:
 * http://docs.jquery.com/License
 *
 * @version 0.1
 *//**
 * Custom JS for STARTERKIT theme
 */

/* New D7 Wrapper --> */(function ($) {


Drupal.behaviors.ifsight = {
  attach: function(context, settings) { //new attach function
    
    if ($('#popup-content').size() > 0){
      $('html').addClass('no-bg');
      $('div.tabs a').attr('target', '_parent');
    } else {
      device = isMobileDevice();
      if(!device){
        $('html').addClass('not-mobile');
      }
      
      if($('body.front').length) {
		    var winW = 630, winH = 460;
		    var firstBreak, secondBreak, thirdBreak, fourthBreak, fifthBreak,
		        firstWidth, secondWidth, thirdWidth, fourthWidth, fifthWidth,
		        firsthHeight, secondHeight, thirdHeight, fourthHeight, fifthHeigh,
		        totalWidth, navHeight, markerWidth;
		    var slider_arr;
			  var device;
			  var $navigation = $('#navigation');
			
			  //first page load
			  if(!$('body.ifsight-processed').length) {
			    getWindowSize();
			    navHeight = $('#navigation').outerHeight();
			    if(device) {
			      if ( window.orientation != undefined ) {
			        window.onorientationchange = function() {
			          getWindowSize();
			          $navigation.css({'top' : (winH-68)+'px', 'padding-bottom' : (winH)+'px'});
			        }
			      }
            else {
			        $(window).resize(function() {
			          getWindowSize();
			          $navigation.css({'top' : (winH-68)+'px', 'padding-bottom' : (winH)+'px'});
			        });
			      }
			      $navigation.css({'padding-bottom' : (winH)+'px', 'position' : 'absolute', 'margin' : '0', 'top' : (winH-68)+'px', 'z-index' : '1000'});
			      document.addEventListener('touchmove', function (e) { 
			        if(!$('body').hasClass('touchmoving')) {
			          $('body').addClass('touchmoving');
			          $navigation.toggle();
			        }
			      });
			      $('#navigation #block-block-1 ul.menu li a, #navigation #block-block-1 h1.title a').click(function() {
			        if(!$('body').hasClass('scrollmoving')) {
			          $('body').addClass('scrollmoving');
			          $navigation.toggle();
			        }
						  $.scrollTo($(this).attr('href'), 1000, {onAfter:function(){
						    if($('body').hasClass('scrollmoving')) {
						      $('body').removeClass('scrollmoving');
						      $navigation.css({'top' : (window.pageYOffset + (winH - 68)) + 'px'});
						      $navigation.toggle();
						    }
						  }});
						  return false;
						});
			    }
			    else {
			      markerWidth = $('#window-gauge').outerWidth();
			      buildSectionHeights();
			      getNavWidths();
			      $(window).resize(function() {
							getWindowSize();
							buildSectionHeights();
							getNavWidths();
					  });
			    	$('#navigation #window-gauge').addClass('hidden');
			    	$('#navigation #block-block-1 ul.menu li a, #navigation #block-block-1 h1.title a').click(function() {
						  $.scrollTo($(this).attr('href'),1000);
						  return false;
						});
			    }
		      $('body').addClass('ifsight-processed');
		    }
		    if(device) {
		      window.onscroll = function() {
			      $navigation.css({'top' : (window.pageYOffset + (winH - 68)) + 'px'});
			      if($('body').hasClass('touchmoving')) {
				      $('body').removeClass('touchmoving');
				      $navigation.toggle();
				    }
		      };
		    }
		    else {
		      $(window).scroll(function() {
		        if(!$('#navigation').hasClass('width-processed')) {
		          getNavWidths();
		          $('#navigation').addClass('width-processed');
		        }
		        var currentBreak, currentHeight, currentWidth, currentLeft = 0, currentPos, currentRight = totalWidth-(markerWidth);
		        if(firstBreak >= $(document).scrollTop()) {
		          if($('#navigation #block-block-1 ul.menu li.research a').hasClass('active')) {
		            $('#navigation #block-block-1 ul.menu li.research a').removeClass('active');
		            $('#navigation #window-gauge').addClass('hidden').toggle();
		          }
		        }
		        //Hit the first break
		        else if(secondBreak >= $(document).scrollTop() && firstBreak < $(document).scrollTop()) {
		          if(!$('#navigation #block-block-1 ul.menu li.research a').hasClass('active')) {
		          	changeActiveHeader($('#navigation #block-block-1 ul.menu li.research a'));
		          }
		          if($('#navigation #window-gauge').hasClass('hidden')) {
		            $('#navigation #window-gauge').removeClass('hidden').toggle();
		          }
		          currentBreak = firstBreak;
		          currentHeight = firstHeight;
		          currentWidth = firstWidth;
		          currentPos = slider_arr[0];
		        }
		        else if(thirdBreak >= $(document).scrollTop() && secondBreak < $(document).scrollTop()) {
		          if(!$('#navigation #block-block-1 ul.menu li.clients a').hasClass('active')) {
		          	changeActiveHeader($('#navigation #block-block-1 ul.menu li.clients a'));
		          }
		          currentBreak = secondBreak;
		          currentHeight = secondHeight;
		          currentWidth = secondWidth;
		          currentPos = slider_arr[1];
		        }
		        else if(fourthBreak >= $(document).scrollTop() && thirdBreak < $(document).scrollTop()) {
		          if(!$('#navigation #block-block-1 ul.menu li.company a').hasClass('active')) {
		            changeActiveHeader($('#navigation #block-block-1 ul.menu li.company a'));
		          }
		          currentBreak = thirdBreak;
		          currentHeight = thirdHeight;
		          currentWidth = thirdWidth;
		          currentPos = slider_arr[2];
		        }
		        else if(fifthBreak >= $(document).scrollTop() && fourthBreak < $(document).scrollTop()) {
		          if(!$('#navigation #block-block-1 ul.menu li.team a').hasClass('active')) {
		          	changeActiveHeader($('#navigation #block-block-1 ul.menu li.team a'));
		          }
		          currentBreak = fourthBreak;
		          currentHeight = fourthHeight;
		          currentWidth = fourthWidth;
		          currentPos = slider_arr[3];
		        }
		        else if(fifthBreak < $(document).scrollTop()) {
		          if(!$('#navigation #block-block-1 ul.menu li.contact a').hasClass('active')) {
		          	changeActiveHeader($('#navigation #block-block-1 ul.menu li.contact a'));
		          }
		          currentBreak = fifthBreak;
		          currentHeight = fifthHeight;
		          currentWidth = fifthWidth;
		          currentPos = slider_arr[4];
		        }
		        $('#window-gauge').css({'right' : ((totalWidth - 30) - (currentWidth)*($(document).scrollTop() - currentBreak)/(currentHeight) - currentPos)+'px'});
		      });
			  }
		  }
		}
		
		function isMobileDevice() {
	    if( navigator.userAgent.match( /(iPod|iPhone)/ ) ) {
	      return 'iPhone';
	    } 
	    else if( navigator.userAgent.match( /(iPad)/i ) ) {
	      return 'iPad';
	    }
	    else if( navigator.userAgent.match( /(Android)/i ) ) {
	      return 'Android';
	    }
	    else if( navigator.userAgent.match( /(Windows; U; Windows NT 6.1; en-US)/i ) ) {
	      return 'GTablet';
	    }
	    else {
	      return false;
	    }
    }
    
    function getWindowSize() {
		  if(!device){
		    if (document.body && document.body.offsetWidth) {
		      winW = document.body.offsetWidth;
		      winH = document.body.offsetHeight;
		    }
		    if (document.compatMode=='CSS1Compat' && document.documentElement && document.documentElement.offsetWidth ) {
		      winW = document.documentElement.offsetWidth;
		      winH = document.documentElement.offsetHeight;
		    }
				if (window.innerWidth && window.innerHeight) {
				  winW = window.innerWidth;
				  winH = window.innerHeight;
				}
			}
			else {
			  winW = window.innerWidth;
			  winH = window.innerHeight ? window.innerHeight : $(window).height();
			}
		  /*if (winW < winH) {
		    winH = winW;
		  }*/
		  $('#header').css({'min-height' : (winH-168)+'px'});
		  $('#header .section').css({'padding-top' : (((winH-68)/2) - 90)+'px'});
		  $('#research, #clients, #company, #team, #contact').css({'min-height' : (winH-218)+'px'});
    }
    
    function buildSectionHeights() {
      /* Get Offset */
      firstBreak = $('#research').offset().top - (winH - navHeight)/2;
      secondBreak = $('#clients').offset().top - (winH - navHeight)/2;
      thirdBreak = $('#company').offset().top - (winH - navHeight)/2;
      fourthBreak = $('#team').offset().top - (winH - navHeight)/2;
      fifthBreak = $('#contact').offset().top - (winH - navHeight)/2;
      /* Get Height */
      firstHeight = $('#research').outerHeight(true);
      secondHeight = $('#clients').outerHeight(true);
      thirdHeight = $('#company').outerHeight(true);
      fourthHeight = $('#team').outerHeight(true);
      fifthHeight = $('#contact').outerHeight(true);
    }
    
    function getNavWidths() {
      firstWidth = $('#navigation .menu li.research').outerWidth(true) + 4;
      secondWidth = $('#navigation .menu li.clients').outerWidth(true) + 4;
      thirdWidth = $('#navigation .menu li.company').outerWidth(true) + 4;
      fourthWidth = $('#navigation .menu li.team').outerWidth(true) + 4;
      fifthWidth = $('#navigation .menu li.contact').outerWidth(true) + 4;
      totalWidth = firstWidth+secondWidth+thirdWidth+fourthWidth+fifthWidth;
      slider_arr = new Array( 0, firstWidth, firstWidth+secondWidth, firstWidth+secondWidth+thirdWidth, firstWidth+secondWidth+thirdWidth+fourthWidth);
      //console.log(firstWidth+', '+secondWidth+', '+thirdWidth+', '+fourthWidth+', '+fifthWidth+','+totalWidth);
    }
	
	  function changeActiveHeader($current) {
	    if(!$current.hasClass('active')) {
         $current.parent().siblings('li').find('a.active').removeClass('active');
         $current.addClass('active');
       }
	  }
	  
	  // popups
	  $('.view-team .views-row a, .view-clients .views-row a, .view-research .views-row a').each(function(){
	    $(this).attr('href', $(this).attr('href') + '?iframe=true');
	    $(this).colorbox({
	      iframe:true, 
	      innerWidth:650, 
	      innerHeight:'60%',
	      onComplete:function(){
	        $('#cboxNext, #cboxPrevious').click(function(){
	          //alert('nx');
	          //console.log($(window.parent.document).find($('.view-team .views-row a.active, .view-clients .views-row a.active, .view-research .views-row a.active')).html());
	        });
	      },
				onClosed:function(){ 
				  //alert('onClosed: colorbox has completely closed');
				  $('#popup-content').remove();
				  $('.view-team .views-row a, .view-clients .views-row a, .view-research .views-row a').removeClass('active-popup');
				 }
	    });
	  }).click(function(){
	    $('.view-team .views-row a, .view-clients .views-row a, .view-research .views-row a').removeClass('active-popup');
	    $(this).addClass('active-popup');
	  });
  }//attach
};//behaviors

/* New D7 Wrapper --> */ })(jQuery);

;

