$(document).ready( function() {
	
	var activeDt = "";
	var targetFeatureSummary = '';
	var activeLi = "";
	var targetSpecSubCategoryIndex = '';
	
	$('dt.article-summary').each(function(i,n) {
		$(n).click(function() {
			activeDt = $(this).attr('id');
			targetFeatureSummary = activeDt.substr(8);
			var targetFeatureDescription = "description-"+targetFeatureSummary;
			var currentState = $('#'+targetFeatureDescription).css('display');
			
			if(currentState == "none")
			{
				$('#'+targetFeatureDescription).slideDown("slow");
				$(this).addClass("feature-summary-active");
				$(this).addClass('active');
			} else
			{
				$('#'+targetFeatureDescription).slideUp("slow");
				$(this).removeClass("feature-summary-active");
				$(this).removeClass('active');
			}
			
		});
	});
	
	gradeSpecSelector();
	
	$('p.spec-title').each(function(i,n) {
		
		$(n).click(function() {
			var activeUl = ($(this).parent().parent()).attr('class').substr(10,16);
			activeLi = $(this).attr('id');
			targetSpecSubCategoryIndex = activeLi.substr(5);
			var targetSpecSubcategory = "spec-detail-"+targetSpecSubCategoryIndex;
			if(activeUl != 'active-spec-list'){
				var currentState = $('#'+targetSpecSubcategory).css('display');
				if(currentState == "none")
				{
					$('#'+targetSpecSubcategory).slideDown('slow');
					$(this).addClass('active');
				} else
				{
					$('#'+targetSpecSubcategory).slideUp('slow');
					$(this).removeClass('active');
					$(this).addClass('not-active'); // used for the expand all functionality
				}
			}else{
				var currentState = $('ul.active-spec-list #'+targetSpecSubcategory).css('display');
				if(currentState == "none")
				{
					$('ul.active-spec-list #'+targetSpecSubcategory).slideDown('slow');
					$(this).addClass('active');
				} else
				{
					$('ul.active-spec-list #'+targetSpecSubcategory).slideUp('slow');
					$(this).removeClass('active');
					$(this).addClass('not-active'); // used for the expand all functionality
				}
			}
			
			
		});
	});
	
	$('.color-trim-a h4').click(function(){
		var tableStatus = $('#color-availability').attr('class');
		if(tableStatus == 'active')
		{	
			$('.color-trim-a #color-availability').slideUp('slow');
			$('#color-availability').removeClass('active');
			$('#color-availability').addClass('nonactive');
			$('.color-trim-table').removeClass('active');
		} else
		{
			$('.color-trim-a #color-availability').slideDown('slow');
			$('#color-availability').removeClass('nonactive');
			$('#color-availability').addClass('active');
			$('.color-trim-table').addClass('active');
		}
	});
	
	
	/* close all functionality */
	$('li.close-all a[rel="close"]').click(function() {
		$('.spec-subcategory').each(function() {
			
			/* get the accordion cat title p tag, we use this to check what class it has on it */
			var AccordionCategoryTitle = $('p', this).parent();
			
			/* makes it a little more efficient, but still slow... */
			if($(AccordionCategoryTitle.hasClass('active')))
			{
				$(this).slideUp('slow');
				$('p.spec-title').removeClass('active');
			}
		});
	});
	
	/* expand all functionality */
	$('li.expand-all a[rel="expand"]').click(function() {
		$('.spec-subcategory').each(function() {
			
			/* get the accordion cat title p tag, we use this to check what class it has on it */
			var AccordionCategoryTitle = $('p', this).parent();
			
			/* makes it a little more efficient, but still slow... */
			if($(AccordionCategoryTitle.hasClass('not-active')))
			{
				$(this).slideDown('slow');
				$('p.spec-title').addClass('active');
			}
		});
	});

});

function gradeSpecSelector() {
	var currentGrade = null;
	currentGrade = $('select[name="grade"] option:selected').val();
	if (currentGrade != null) {
		$('ul.spec-list').hide();
		$('ul.spec-list[rel="' + currentGrade + '"]').show();
		$('ul.spec-list[rel="' + currentGrade + '"]').addClass('active-spec-list');
		$('select[name="grade"]').change(function(){
			currentGrade = $('select[name="grade"] option:selected').val();
			$('ul.spec-list').hide();
			$('ul.spec-list').removeClass('active-spec-list');
			$('ul.spec-list[rel="' + currentGrade + '"]').show();
			$('ul.spec-list[rel="' + currentGrade + '"]').addClass('active-spec-list');
		});	
	}	
}



//get position of the clicked available button
function getBtnPositionOnScreen(btnObj){
	var popUpObj = jQuery(btnObj);
	var coordinates = popUpObj.offset();
	var btnHeightGenerated = 0;
	
	if(isFirefox){
		btnHeightGeneratedModif = 13;
		btnReplacementHeightModif = 3;
	}else if(isSafari){
		btnHeightGeneratedModif = 10;
		btnReplacementHeightModif = 5;
	}else{
		btnHeightGeneratedModif = 16;
		btnReplacementHeightModif = 0;
	}
	
	xCoord = coordinates.left;
	yCoord = coordinates.top - btnReplacementHeightModif;
	
	xCoordBtn = coordinates.left;
	yCoordBtn = coordinates.top + btnHeightGeneratedModif; //13 is the button height, popup needs to go below the button
}

