$(document).ready(function(){
	$('#hp_col_wrapper .slider').bxSlider({
		nextSelector: '#slider-next',
		prevSelector: '#slider-prev',
		nextText: 'Next',
		prevText: 'Previous'
	});
});

$(function(){
// start menu code
	var $header = $('#header'),
		$nav_tablet = $('.nav_tablet', $header),
		$menu = $('.menu', $nav_tablet),
		$trigger = $('.trigger', $nav_tablet),
		$fact_sheets = $('.fact_sheets'),
		$fact_sheets_ul = $('ul', $fact_sheets),
		$trigger2 = $('.trigger', $fact_sheets),
		$tablet_drop = $('.drop', $nav_tablet),
		$mobile_nav = $('.nav_mobile', $header),
		$mobile_trigger = $('button', $mobile_nav),
		$mobile_drop = $('.drop', $mobile_nav),
		$content = $('#content'),
		$col2 = $('.col2', $content),
		$col2_portlets = $('.portlet', $col2),
		portlet_count = 'portlets_'+$col2_portlets.length,
		$fancy_table = $('#fancy_table'),
		$process = $('#process');


	$col2.addClass(portlet_count);






	$menu.hover(function(){$(this).addClass('hover');},function(){$(this).removeClass('hover');});

	$trigger.click(function(){
		if($menu.hasClass('hover') === true){
			$(this).removeClass('open');
			$tablet_drop.css('display', 'none');
			$menu.removeClass('hover');
		}else{
			if($(this).hasClass('open') === false){
				$(this).addClass('open');
				$tablet_drop.css('display', 'block');
				$menu.addClass('hover');
			}else{
				$(this).removeClass('open');
				$tablet_drop.css('display', 'none');
				$menu.removeClass('hover');
			}
		}
	});

	$mobile_trigger.click(function(){
		if($mobile_nav.hasClass('open') === true){
			$mobile_drop.slideUp();
			$mobile_nav.removeClass('open');
		}else{
			$mobile_drop.slideDown();
			$mobile_nav.addClass('open');
		}
	});

	$trigger2.click(function(){
		if($fact_sheets.hasClass('open') === true){
			$fact_sheets_ul.slideUp('normal',
			function(){$fact_sheets.removeClass('open');});

		}else{
			$fact_sheets_ul.slideDown();
			$fact_sheets.addClass('open');
		}
	});




	// end menu code
	// moved from custom.js and rewritten
	var $vi = $('.cboxElement');

	if($vi.length){
		//calls colorbox on the homepage
		//if ($vi.attr('href') == "") {
		//$vi.colorbox({html:'<h1> &nbsp; Video Coming Soon</h1>',height:'480px',width:'640px'});
		//} else {//$vi.colorbox({iframe:true,height:'480px',width:'640px'});}
			$(".youtube").colorbox({iframe:true, innerWidth:640, innerHeight:390, maxWidth:'95%', maxHeight:'95%'});
			$(".vimeo").colorbox({iframe:true, innerWidth:500, innerHeight:409, maxWidth:'95%', maxHeight:'95%'});
		//}
	}

	var resizeTimer;
	$(window).resize(function(){
		if (resizeTimer) clearTimeout(resizeTimer);
		resizeTimer = setTimeout(function() {
			if ($('#cboxOverlay').is(':visible')) {$.colorbox.load(true);}
		}, 300)
	});

	if($fancy_table.length){
		var rows = $('tr:gt(0)', $fancy_table)
			$ftm = $('#fancy_table_mobile'),
			$ftm_rows = $('tr', $ftm),
			ftm_rows_length = $ftm_rows.length,
			row_count = 2,
			total_loops = $('tr', $fancy_table).length - row_count;

		$(".total td").each(function(index, value){
			var num = index + 2;
			var total = 0;
			rows.children("td:nth-child("+num+")").each(function() {
			    //each time we add the cell to the total
			    total += parseInt($(this).html());
			});

			//then output them to the elements
			$(".total td:nth-child("+num+")").html(total);
		});


		for(var i=0; i<total_loops; i++){
			var the_tr = $('tr:eq('+row_count+')', $fancy_table),
				new_row_count = 0;

			the_tr.find('td').each(function(){
				if($(this).hasClass('tal') === false){$ftm_rows.eq(new_row_count).append('<td>'+$(this).text()+'</td>');}else{$ftm_rows.eq(new_row_count).append('<th>'+$(this).text()+'</th>')}
				new_row_count++;
			});
			 row_count++;
		}

		$('td:last-child',$ftm_rows).addClass('total');
	}

	if($process.length){
		// process - check for big popup
		var $pr_a = $('a', $process),
			$pr_span = $('span',$pr_a);

		$pr_span .each(function(){
			if ($(this).html().length > 750){$(this).parent().addClass('big_popup');}
			else if ($(this).html().length > 500){$(this).parent().addClass('mid_popup');}
		});

		$pr_a.each(function(){
			var len1 = $(this).html().length,
				len2 = $(this).find('span').html().length;

			if (len1 - len2 < 50) {
				// console.log(this.id)
				$(this).css('padding-top','17px');
			}

		});
	}
});