// JavaScript Document

function ismaxlength(obj){
	var mlength=obj.getAttribute? parseInt(obj.getAttribute("maxlength")) : ""
	if (obj.getAttribute && obj.value.length>mlength)
	obj.value=obj.value.substring(0,mlength);
	jQuery('#chars_left').text( '('+ (mlength-obj.value.length)+' characters remaining)');
}

//universal close popup function
function closePopup(){
	if(jQuery('.popup').css('opacity') > 0 ){
		jQuery('.popup').animate({opacity: 0}, 500,
			function(){
				jQuery(this).css({'display' : 'none'});
			}
		);
	}
}

jQuery( function($) {	
	//draggable popups
	$('.popup').draggable();
	$('.showPopup, .removeHref').removeAttr('href');
	$('input:radio[@name=news_filter]').click( function() {
		if( $(this).attr('id') == "type_all" ){
			$('.news_filter p').show(500, function(){
				$('html, body').animate({
					scrollTop: $("#top").offset().top
				}, 250);
			});
		} else {
			var theType = $(this).attr('id');
			var totalContainers = $(".news_filter > p").size();
			var containerCounter = 0;
			$('.news_filter p').each( function(){
				containerCounter++;
				if( $(this).attr('class').indexOf(theType) < 0 ){
					$(this).hide(500);
				} else {
					$(this).show(500);
				}
				if(containerCounter == totalContainers){
					$('html, body').animate({
						scrollTop: $("#top").offset().top
					}, 250);
				}				
			});			
		}
	});
	
	$('.gotoPageAnchor').click( function(){
		$('html, body').animate({ scrollTop: $("."+$(this).attr('id').split('-')[0]).offset().top},$(this).attr('id').split('-')[1]);
	});
	
	//close popup button
	$('.popup_close').click( function() {
		zIndex--;
		$(this).parent().parent().animate({opacity: 0}, 500,
			function(){
				$(this).css({'display' : 'none'});
			}
		);
	});
	
	//nutrition popups
	$('#product_column .popup').css({'opacity' : 0});
	$('#left_column .popup').css({'display' : 'block'});
	var zIndex = 2;
	var doneAnimating = false;
	$('.showPopup').click( function() { 
		doneAnimating = false;
		var showPopup = "#" + $(this).attr('id') + "-box";
		$('#product_column .popup').animate({opacity: .5}, 250, 
			function(){
				$(showPopup).css({'display' : 'block' , 'zIndex' : zIndex++}).animate({opacity: 1 , top : 0, left : 0}, 250,
						function(){
							doneAnimating = true;
						}
				);
			}
		);
	});
	//nutrition popup hovering
	$('#product_column .popup').hover( function() {
		zIndex++;
		if(doneAnimating == true && $(this).css('opacity') < 1 ){
			doneAnimating = false;
			$('#product_column .popup').animate({opacity: .5}, 250);
			$(this).css({'zIndex' : zIndex++}).animate({opacity: 1}, 250,
				function(){
					doneAnimating = true;
				}
			);			
		}
	});
	
	
	/* show / hide product image and description */
	$('.product_thumbs').click( function() {
		var productID = "#" + $(this).attr('id');
		var productClass = "." + $(this).attr('id');
			$("a", productID).animate({color : '#EB6E1F'}, 500);
			$('#product_image, #product_descriptions').animate({opacity: 0}, 500, 
				function(){
				   $("#product_image div, #product_descriptions div").css({'display' : 'none'});
				   $(productClass).css({'display' : 'block'});
				   $('#product_image, #product_descriptions').animate({opacity: 1}, 500 );
				}
			);	
		$('#selected').removeAttr('id').animate({color : '#333'}, 500, 
			function(){
				$(productID + ' a').attr('id', 'selected');
			}
		);
	});
	
	/* show post navigation pages */
	$('#post_pagination.single li a').removeAttr('href');
	$('#post_pagination.single li a').click( function() {
		closePopup();
		$('.form_submit input').addClass('single');
		var pageID = $(this).attr('class');
		var pageIDStr = "#"+$(this).attr('class');
		var pageNum = $(this).attr('class').split('-')[1];
		$('#pageNum').attr('value' , pageNum);
		$('.post-page').each( function(){
		   if($(this).attr('id') != pageID){
			   	$(this).hide(500, function(){
					$('html, body').animate({
					scrollTop: $(".single_post").offset().top
					}, 250, function(){
						$('.form_submit input').removeClass('single');
					});				
				});
		   } else {
				$(this).show(500, function(){
					$('html, body').animate({
					scrollTop: $(".single_post").offset().top
					}, 250, function(){
						$('.form_submit input').removeClass('single');	
					});				
				});   
		   }
		});
		$('#post_pagination.single li a#active').animate({color : '#333'}, 500, 
			function(){
				$('#post_pagination.single li a#active').removeAttr('id');
			}
		);
		$(this).animate({color : '#EB6E1F'}, 500, 
			function(){
				$(this).attr('id', 'active');
			}
		);
	});	
	
	//State filter
	$('#state_filter').click( function(){
		if($('#filterByState').text() != "View all"){
			$('#filterByState').text("View all").addClass("gray");			
		}
	}).change( function(){
		var stateFilter = $(this).val();
		if($(this).val() == 0){
			$('#retail_container .outer_retail_container').show(500);
		} else {
			$('#retail_container .outer_retail_container').each( function() {
				if($(this).hasClass(stateFilter)){
					$(this).show(500);
				} else {
					$(this).hide(500);
				}
			});
		}
	});
	
	$('input:submit, .remove_button').addClass('cart_button');
	$('.adjustform input:text').addClass('cart_field');
});	
