/* * Accordion 1.1 - jQuery menu widget*/
jQuery.fn.nextUntil = function(expr, include) {
	var match = [];
	include = include ? true : false;
	jQuery(this).each(function(){
		for( var i = this.nextSibling; i; i = i.nextSibling ) {
			if ( i.nodeType != 1 ) continue;
			if ( include ) {
				match.push( i );
			}
			if ( jQuery.filter( expr, [i] ).length ) break;
			if (! include ) {
				match.push( i );
			}
		}
	});
	return jQuery(this).pushStack( match, arguments );
};

// create private scope with $ alias for jQuery
(function($) {
	// save reference to plugin method
	var plugin = $.fn.Accordion = function(settings) {
		// setup configuration
		settings = $.extend({}, plugin.defaults, {
			// define context defaults
			
			header: $(':first-child', this).eq(0).tagName // take first childs tagName as header
		}, settings);

		// calculate active if not specified, using the first header
		var container = this,
			active = settings.active ? $(settings.active, this) : settings.active === false ? $("<div>") : $(settings.header, this).eq(0),
			running = 0;

		container.find(settings.header)
			.not(active && active[0] || "")
			.nextUntil(settings.header)
			.hide();
		active.addClass(settings.selectedClass);

		var clickHandler = function(event) {
			// get the click target
			var clicked = $(event.target);
			
			var clickedActive = clicked[0] == active[0];
			
			// if animations are still active, or the active header is the target, ignore click
			if(running || (settings.alwaysOpen && clickedActive) || !clicked.is(settings.header))
				return;

			// switch classes
			active.removeClass(settings.selectedClass);
			clicked.addClass(settings.selectedClass);

			// find elements to show and hide
			var toShow = clicked.nextUntil(settings.header),
				toHide = active.nextUntil(settings.header),
				data = [clicked, active, toShow, toHide];
			active = clicked;
			// count elements to animate
			running = toHide.size() + toShow.size();
			var finished = function() {
				if(--running)
					return;

				// trigger custom change event
				container.trigger("change", data);
			};
			// TODO if hideSpeed is set to zero, animations are crappy
			// workaround: use hide instead
			// solution: animate should check for speed of 0 and do something about it
			if(!settings.alwaysOpen && clickedActive) {
				toShow.slideToggle(settings.showSpeed, finished);
				finished();
			} else {
				toHide.filter(":hidden").each(finished).end().filter(":visible").slideUp(settings.hideSpeed, finished);
				toShow.slideDown(settings.showSpeed, finished);
			}

			return false;
		};
		var activateHandlder = function(event, index) {
			// call clickHandler with custom event
			clickHandler({
				target: $(settings.header, this)[index]
			});
		};

		return container
			.click(clickHandler)
			.bind("activate", activateHandlder);
	};
	// define static defaults
	plugin.defaults = {
		selectedClass: "selected",
		showSpeed: 'slow',
		hideSpeed: 'fast',
		alwaysOpen: true
	};

	// shortcut for trigger, nicer API and easily to document
	$.fn.activate = function(index) {
		return this.trigger('activate', [index || 0]);
	};

})(jQuery);


(function( $ ){
	
	var $scrollTo = $.scrollTo = function( target, duration, settings ){
		$(window).scrollTo( target, duration, settings );
	};

	$scrollTo.defaults = {
		axis:'xy',
		duration: parseFloat($.fn.jquery) >= 1.3 ? 0 : 1
	};

	// Returns the element that needs to be animated to scroll the window.
	// Kept for backwards compatibility (specially for localScroll & serialScroll)
	$scrollTo.window = function( scope ){
		return $(window)._scrollable();
	};

	// Returns the real elements to scroll (supports window/iframes, documents and regular nodes)
	$.fn._scrollable = function(){
		return this.map(function(){
			var elem = this,
				isWin = !elem.nodeName || $.inArray( elem.nodeName.toLowerCase(), ['iframe','#document','html','body'] ) != -1;

				if( !isWin )
					return elem;

			var doc = (elem.contentWindow || elem).document || elem.ownerDocument || elem;
			
			return $.browser.safari || doc.compatMode == 'BackCompat' ?
				doc.body : 
				doc.documentElement;
		});
	};

	$.fn.scrollTo = function( target, duration, settings ){
		if( typeof duration == 'object' ){
			settings = duration;
			duration = 0;
		}
		if( typeof settings == 'function' )
			settings = { onAfter:settings };
			
		if( target == 'max' )
			target = 9e9;
			
		settings = $.extend( {}, $scrollTo.defaults, settings );
		// Speed is still recognized for backwards compatibility
		duration = duration || settings.speed || settings.duration;
		// Make sure the settings are given right
		settings.queue = settings.queue && settings.axis.length > 1;
		
		if( settings.queue )
			// Let's keep the overall duration
			duration /= 2;
		settings.offset = both( settings.offset );
		settings.over = both( settings.over );

		return this._scrollable().each(function(){
			var elem = this,
				$elem = $(elem),
				targ = target, toff, attr = {},
				win = $elem.is('html,body');

			switch( typeof targ ){
				// A number will pass the regex
				case 'number':
				case 'string':
					if( /^([+-]=)?\d+(\.\d+)?(px|%)?$/.test(targ) ){
						targ = both( targ );
						// We are done
						break;
					}
					// Relative selector, no break!
					targ = $(targ,this);
				case 'object':
					// DOMElement / jQuery
					if( targ.is || targ.style )
						// Get the real position of the target 
						toff = (targ = $(targ)).offset();
			}
			$.each( settings.axis.split(''), function( i, axis ){
				var Pos	= axis == 'x' ? 'Left' : 'Top',
					pos = Pos.toLowerCase(),
					key = 'scroll' + Pos,
					old = elem[key],
					max = $scrollTo.max(elem, axis);

				if( toff ){// jQuery / DOMElement
					attr[key] = toff[pos] + ( win ? 0 : old - $elem.offset()[pos] );

					// If it's a dom element, reduce the margin
					if( settings.margin ){
						attr[key] -= parseInt(targ.css('margin'+Pos)) || 0;
						attr[key] -= parseInt(targ.css('border'+Pos+'Width')) || 0;
					}
					
					attr[key] += settings.offset[pos] || 0;
					
					if( settings.over[pos] )
						// Scroll to a fraction of its width/height
						attr[key] += targ[axis=='x'?'width':'height']() * settings.over[pos];
				}else{ 
					var val = targ[pos];
					// Handle percentage values
					attr[key] = val.slice && val.slice(-1) == '%' ? 
						parseFloat(val) / 100 * max
						: val;
				}

				// Number or 'number'
				if( /^\d+$/.test(attr[key]) )
					// Check the limits
					attr[key] = attr[key] <= 0 ? 0 : Math.min( attr[key], max );

				// Queueing axes
				if( !i && settings.queue ){
					// Don't waste time animating, if there's no need.
					if( old != attr[key] )
						// Intermediate animation
						animate( settings.onAfterFirst );
					// Don't animate this axis again in the next iteration.
					delete attr[key];
				}
			});

			animate( settings.onAfter );			

			function animate( callback ){
				$elem.animate( attr, duration, settings.easing, callback && function(){
					callback.call(this, target, settings);
				});
			};

		}).end();
	};
	
	// Max scrolling position, works on quirks mode
	// It only fails (not too badly) on IE, quirks mode.
	$scrollTo.max = function( elem, axis ){
		var Dim = axis == 'x' ? 'Width' : 'Height',
			scroll = 'scroll'+Dim;
		
		if( !$(elem).is('html,body') )
			return elem[scroll] - $(elem)[Dim.toLowerCase()]();
		
		var size = 'client' + Dim,
			html = elem.ownerDocument.documentElement,
			body = elem.ownerDocument.body;

		return Math.max( html[scroll], body[scroll] ) 
			 - Math.min( html[size]  , body[size]   );
			
	};

	function both( val ){
		return typeof val == 'object' ? val : { top:val, left:val };
	};

})( jQuery );


/** Synoptek site code */
$(function(){

	
	
	$("#nav_links_list li").hover(function(){$(this).addClass("sfhover");}, function(){$(this).removeClass("sfhover");});
	$('ul.teamlist').Accordion({header:'a.h', active: false, showSpeed: 0, hideSpeed: 0});
	$("#news_archive").click(function(){ $('.news_archive').fadeIn(); $(this).hide(); return false; 	});
});
$(document).ready(function() {	//Side tabs logic, uses ScrollTo plugin



var navSet = '#panel-'; /* = $.cookie('nav_selected');
    if (navSet == null) { navSet = 'navdiv1' }
    setDiv = function(div_id) { $.cookie('nav_selected', div_id) }*/
	
	var tparts = window.location.pathname.split("/");
	switch(tparts[1].toLowerCase()){
	case "plan":
	navSet+=1;
	break;
	case "build":
	navSet+=2;
	break;
	case "operate":
	navSet+=3;
	break;
	default:
	navSet+= 1;
	break;
	}

		var panel = $(navSet);


	//Get the height of the first item
	$('#mask').css({'height':panel.height()});
	
	$('#scroller-body').css('visibility','visible');
	$('#scroller-header a[href='+navSet+']').addClass('selected');
	
	
	//Calculate the total width - sum of all sub-panels width
	//Width is generated according to the width of #mask * total of sub-panels
	$('#panel').width(parseInt($('#mask').width() * $('#panel div').length));
	
	//Set the sub-panel width according to the #mask width (width of #mask and sub-panel must be same)
	$('#panel div').width($('#mask').width());
	
	
	$('#mask').scrollTo(navSet,0);
	//Get all the links with rel as panel
	$('a[rel=panel]').click(function () {
	
	//try{
	//pageTracker._trackEvent('SideNav', 'Interact', $(this).text()); 
	//} catch(e){ };
	
		//Get the height of the sub-panel
		var panelheight = $($(this).attr('href')).height();
		
		//Set class for the selected item
		$('a[rel=panel]').removeClass('selected');
		$(this).addClass('selected');
		
		//Resize the height
		$('#mask').animate({'height':panelheight},{queue:false, duration:500});			

		//Scroll to the correct panel, the panel id is grabbed from the href attribute of the anchor
		$('#mask').scrollTo($(this).attr('href'), 800);		
		
		//Discard the link default behavior
		return false;
	}).mouseover(function(){ $('#mask').stop(true);$(this).click(); });
	
	
	
});