var current_index = 0;
var nav_classes = ['what','how','why','benefits'];

function update_nav(index){
	for (var i = nav_classes.length - 1; i >= 0; i--){
		jQuery('.slidernav').removeClass(nav_classes[i]);
	};
	jQuery('.slidernav').addClass(nav_classes[index]);
}

function load_slide(index){
	update_nav(index);
	targetleft = 0 - ( ( jQuery('#process .panel').width() * index ) + ( index * 50 ) ); // 50px left+right padding on .panel
	jQuery('#panel-wrapper').animate({
		left: targetleft
	}, 400, function(){
	});
	current_index = index;
}



jQuery(window).load(function() {
	// ==========
	// = STICKY =
	// ==========
	if(jQuery('#calculator-helper').length>0&&jQuery('#content-helper').height()>jQuery('#calculator').height()){
		var Sticky = function( $obj, opts ){

		jQuery(window).scroll( 
			function(e){
				Sticky.onScroll(e, $obj, opts );
			});
		}

		Sticky.onScroll = function( e, $o, opts ){

			var iScrollTop = jQuery(window).scrollTop();
			var sClass = "sticky";

			//set original data
			if( !$o.data(sClass) ){
				$o.data(sClass, {css:{position:$o.css('position'),top:$o.css('top')},offset:$o.offset(),height:$o.height(),heightref:jQuery('#content-helper').height()} );
			}
			var oOrig = $o.data(sClass);
			var bIsSticky = $o.hasClass(sClass);

			if( oOrig.heightref - iScrollTop - oOrig.height + oOrig.offset.top - 10 < 0 ){
				toofar = true;
			}else{
				toofar = false;
			}

			if( iScrollTop > oOrig.offset.top && !bIsSticky && !toofar ){
				$o.css({position:'fixed',top:0}).addClass(sClass);
			}else if(iScrollTop < oOrig.offset.top && bIsSticky && !toofar){
				$o.css(oOrig.css).removeClass(sClass);
			}else if( toofar ){
				newtop = oOrig.heightref - iScrollTop - oOrig.height + oOrig.offset.top;
				$o.css({position:'fixed',top:newtop + 'px'}).addClass(sClass);
			}else if( iScrollTop > oOrig.offset.top ){
				$o.css({position:'fixed',top:0}).addClass(sClass);
			}

		}
		Sticky( jQuery('#calculator') );
	}
});




jQuery(document).ready(function() {
	
	
	
	
	// =======================
	// = NEW JERSEY LIGHTBOX =
	// =======================
	jQuery('#trigger-nj-disclaimer').click(function() {
	    jQuery('#disclaimer-wrapper').lightbox_me({
	        centered: true, 
	        onLoad: function() { 
	            
	            }
	        });
	    return false;
	});
	
	
	
	// ================
	// = FOOTER LINKS =
	// ================
	jQuery('#footer a, #company-history-icons a').click(function(){
		window.open(this.href);
		return false;
	});
	
	
	
	
	// =================
	// = ENTRY MARGINS =
	// =================
	jQuery('.entry h1>img').parent().css('margin',0);
	jQuery('.entry h2>img').parent().css('margin',0);
	jQuery('.entry h3>img').parent().css('margin',0);
	jQuery('.entry h4>img').parent().css('margin',0);
	jQuery('.entry h5>img').parent().css('margin',0);
	jQuery('.entry h6>img').parent().css('margin',0);
	
	
	
	// =======
	// = FAQ =
	// =======
	if(jQuery('.ie7').length==0&&jQuery('.ie6').length==0){
		jQuery('.faq').each(function() {
			answer = jQuery(this).find('.answer');
			answer.data('initial_height',answer.height());
			answer.hide().css('height',0);
		});
		jQuery('.faq h3 span').css('cursor','pointer');
		jQuery('.faq h3').click(function() {
			answer = jQuery(this).parent().find('.answer');
			height = answer.data('initial_height');
			if(answer.is(':visible')){
				answer.parent().removeClass('active');
				answer.animate({
					height:0
					},{
						duration:500,
						complete:function(){
							answer.hide();
						}
					});
			} else {
				answer.parent().addClass('active');
				answer.show().animate({
					height:height
					},{
						duration:500
					});
			}
		});
	}
	
	
	
	
	// ===============
	// = SCROLL PANE =
	// ===============
	jQuery('#client-roster').jScrollPane({
		showArrows: true,
		dragMinHeight: 90,
		dragMaxHeight: 90,
		scrollbarWidth: 18,
		arrowSize: 22
		});
	
	
	// =======
	// = MAP =
	// =======
	jQuery('#flash-map').flash({ 
		src: om_theme_path + '/flash/map.swf',
		width: 700,
		height: 506,
		wmode: 'transparent',
		flashvars: { xmlvar: om_theme_path + '/flash/states.xml' }
	},
	{ version: 8 });
	jQuery('#flash-map div.alt').hide();

	
	
	
	
	// ====================
	// = HOME PAGE SLIDER =
	// ====================
	
	jQuery('#process').addClass('enhanced');
	
	// add buttons
	jQuery('#process').append('<p class="control left"><a href="#">&lt;</a></p>');
	jQuery('#process').append('<p class="control right"><a href="#">&gt;</a></p>');
	
	min_height = 100;
	
	jQuery('#process .panel').each(function() {
		if((jQuery(this).height()+25)>min_height){
			min_height = jQuery(this).height()+25;
		}
	});
	
	jQuery('#panel-wrapper').width( ( jQuery('#process .panel').length + 1 ) * jQuery('#process .panel').width() );
	
	jQuery('.scroll-container').height(min_height);
	jQuery('#panel-wrapper').height(min_height);
	
	// set our 'default' class
	update_nav(current_index);
	
	
	jQuery('.slidernav li a').click(function() {
		load_slide(jQuery(this).parent().prevAll().length);
		return false;
	});
	
	jQuery('.control.left a').click(function() {
		if(current_index==0){
			load_slide(3);
		} else {
			load_slide(current_index-1);
		}
		return false;
	});
	
	jQuery('.control.right a').click(function() {
		if(current_index==3){
			load_slide(0);
		} else {
			load_slide(current_index+1);
		}
		return false;
	});
	
	
	
	// ====================================
	// = CALCULATOR MONTHLY/YEARLY TOGGLE =
	// ====================================
	var on_display = 0;
	jQuery('#bestpass-savings').addClass('enhanced');
	jQuery('#bestpass-savings li:eq(1)').addClass('hidden');
	jQuery('#bestpass-savings li span.label').click(function() {
		jQuery('#bestpass-savings li').addClass('hidden');
		if(on_display==0){
			jQuery('#bestpass-savings li:eq(1)').removeClass('hidden');
			on_display=1;
		}else{
			jQuery('#bestpass-savings li:eq(0)').removeClass('hidden');
			on_display=0;
		}
	});
	
	
});