// Make jQuery play well with others
$j = jQuery.noConflict();

$j(function(){

	// Form CSS
	$j('input:text').addClass('text');
	$j('input:checkbox').addClass('checkbox');
	$j('input:radio').addClass('radio');
	$j('input:submit').addClass('submit');
	
	// Navigation
	$j('#primary-nav li').each(function(){
		var vertPadding = 0;
		var horPadding = 0;
		
			var pL = $j(this).find('a').css('paddingLeft').replace("px","");
			pL = parseInt(pL);
			horPadding += pL;
			
			var pR = $j(this).find('a').css('paddingRight').replace("px","");
			pR = parseInt(pR);
			horPadding += pR;
			
			var pT = $j(this).find('a').css('paddingTop').replace("px","");
			pT = parseInt(pT);
			vertPadding += pT;
			
			var pB = $j(this).find('a').css('paddingTop').replace("px","");
			pB = parseInt(pB);
			vertPadding += pB;
		
		var theWidth = $j(this).width();
		theWidth -= horPadding;
		
		var theHeight = $j(this).height();
		theHeight -= vertPadding;
		
		$j(this).find('a').css({
			width : theWidth,
			height : theHeight
		});
		$j(this).find('a').append('<span class="pN-tr"></span>');
	});
	
	// Customers
	if ($j('body.pageid-159, body.pageid-125').length > 0){
		$j('body.pageid-159 #wrapper #content table tr, body.pageid-125 #wrapper #content table tr').each(function(){
			$j(this).find('td:first').addClass('firstCol');
			$j(this).find('td:last').addClass('lastCol');
		});
	}
	
	// Sidebar 
	$j('.sidebar li#in-the-news ul li:last').addClass('last');
	
	// Scaling Images
	if ($j('#products-entries li .product-image, #entry-tabs.installation .ui-tabs-panel img').length > 0){
		jQuery.getScript('/wp-content/themes/alteris/js/jquery.jScale.js',function(){ 
			jQuery('.product-image').jScale({w:'285px'});
			jQuery('#entry-tabs.installation .ui-tabs-panel img').jScale({w:'200px'});
		});
	}
	
	// Main Content
	if ($j('#content table').length > 0){
		$j('#content table').each(function(){
			$j(this).find('tr:even').addClass('odd');
		});
	}
	
	// Automatic External Links
	$j('a[href^=http://]').attr('target','_blank').addClass('external');
	$j('a[href^=http://www.alterisinc.com/]').removeClass('external').removeAttr('target','_blank');
	
	// Homepage Specific
	if ($j('body.pageid-57').length > 0){
		$j('.home-how-it-works ul li:first').addClass('first');
		$j('.home-how-it-works ul li:last').addClass('last');
	}
	
	// Special Incentives Form
	if ($j('form.special-incentives').length > 0){
		$j('form.special-incentives').submit(function(){
			var state = $j(this).find('select').val();
			var action = $j(this).attr('action');
			var url = action + state + '/';
			window.location = url;
			return false;
		});
	}
	
	// FAQ's
	if ($j('body.page-template-faqs-php').length > 0){
		$j('body.page-template-faqs-php li').each(function(){
			$j(this).find('.faq-entry :last').addClass('last');
		});
		$j('body.page-template-faqs-php #faqs h2 a').click(function(){
			if($j(this).parent().next().is(':visible')) {
				$j(this).removeClass('active').parent().next().hide();
			} else {
				$j(this).addClass('active').parent().next().show();
			}
			return false;
		});
		// Anchor Functions
		$j.getScript('/wp-content/themes/alteris/js/jquery.url.packed.js',function(){ 
			var hash = jQuery.url.attr('anchor');
			if(hash){
				$j('body.page-template-faqs-php #faqs li#faq-'+ hash +' a').click();
			}
		});
	}
	
	// Installation Process
	if ($j('#entry-tabs.installation').length > 0){
		$j('#entry-tabs-nav li:last').addClass('last');
	}
	
	// Product Pages
	if ($j('#products-entries').length > 0){
		$j('.product-entries-content').each(function(){
			$j(this).find('*:last').addClass('last');
		});
	}
	
	// Careers 
	if ($j('#career-entries').length > 0){
		$j('#career-entries a.toggler').click(function(){
			$j(this).toggleClass('active').next().slideToggle();
			return false;
		});
	}
	
	// About: News & Mission Tables
	if ($j('body.pageid-75, body.pageid-67, body.pageid-1276').length > 0){
		$j('#content table tr').each(function(){
			$j(this).find('td:first').addClass('first-column');
		});
	}
	
	// Headline Nav Box
	if ($j('#headline-nav-box').length > 0){
		$j('#headline-nav-box ul li:last').addClass('last');
	}
	
	// Sidebar: In The News Feed
	if ($j('#in-the-news.solar').length > 0){
		$j("#in-the-news #cURL-container").load("/wp-content/themes/alteris/inc/newsCurl.solar.inc.php #news-table",{},function(){
			$j(this)
				.addClass('loaded')
				.find('tr:eq(1)').addClass('last'); // Second Item is flagged as the "last" item 
			$j(this).find('tr').each(function(){
				$j(this)
					.find('td:first').addClass('first-col');
			});
			// Hide TR's >2
			$j(this).find('tr:gt(1)').hide();
		});
	}
	if ($j('#in-the-news.wind').length > 0){
		$j("#in-the-news #cURL-container").load("/wp-content/themes/alteris/inc/newsCurl.wind.inc.php #news-table",{},function(){
			$j(this)
				.addClass('loaded')
				.find('tr:eq(1)').addClass('last'); // Second Item is flagged as the "last" item 
			$j(this).find('tr').each(function(){
				$j(this)
					.find('td:first').addClass('first-col');
			});
			// Hide TR's >2
			$j(this).find('tr:gt(1)').hide();
		});
	}
	
	// Paginated Testimonials
	if ($j('.paginated-sidenav').length > 0){
		$j('.paginated-sidenav ul').css('background','none');
		$j('.paginated-sidenav li').css('visibility','visible');
		
		// Offset Alteris sidebar to avoid overlap
		paginatedSidenavHeight = $j('.paginated-sidenav').outerHeight();
		contentMarginTop = paginatedSidenavHeight - 73;
		if ($j('body.s-category-videos').length > 0){
			contentMarginTop += 20;
		}
		contentMarginTop = '-'+contentMarginTop+'px';
		$j('#headline').height(paginatedSidenavHeight);
		$j('#content').css('marginTop',contentMarginTop);
	}
	
	// Careers Apply Form
	if ($j('#open-positions').length > 0){
		$j('#open-positions').change(function(){
			if($j(this).val() == 'Other') {
				$j('#dept-dropdown').fadeIn();
			} else {
				$j('#dept-dropdown').fadeOut();
			}
		});
	}
	
});