jQuery(document).ready( function ()
{

	if(jQuery('.acc_ctrl').length > 0){ //if the element exisits

	///BEGIN SIDEBAR ACCORDION SCRIPTS //
	//all first child nodes within #accordion need the class .acc_ctrl
	//default opened controller needs the class .opened
									
	var controller="";
	var my_counter=0;
	var lock_out=false;
	var featureCounter={};//create an object literal to hold our counters for each LI
	var controllerChildrenCount={};//create an object literal to hold the number of child LIs for each controller
	var feature_height = jQuery("#accordion .acc_ctrl ul").css("height");//get the CSS set height of the UL to be shown
	if(feature_height){		
		feature_height=feature_height.replace("px", ""); //get rid of px
	}else{
		feature_height=1;
	}
	
	
									
	//set up buttons to manually look through features. 
	//clicking buttons stop the playhead until a new controller is hovered over
	jQuery(".acc_rev").click(function(event) {
		event.preventDefault();
		animate_features("rev", "fast")
		
		
		clearInterval(accordion_playhead);
		
	});
									  
	jQuery(".acc_fwd").click(function(event) {
		event.preventDefault();
		animate_features("fwd", "fast")
		
		clearInterval(accordion_playhead);
		
	});
									
	jQuery("#accordion .acc_ctrl").each(function() {//loop through each LI with the class acc_ctrl (first child LIs) 
		jQuery(this).attr("id","ctrl" + my_counter);//and assign a unique ID
		childCountHold = jQuery(this).find("li").length;//get the number of child LIs
		if (jQuery(this).hasClass("opened")){//if this LI has the class "opened", 
			controller=jQuery(this).attr("id"); //set this as the controller
			jQuery(this).find("ul").stop().slideDown(700).animate({height: feature_height}, 700); // slide down this UL to display the first feature
			jQuery(this).stop().animate({ backgroundColor: "drk_gray" }, 700);
			jQuery(this).find(".acc_btns").show();
		}
		featureCounter["ctrl" + my_counter]=1;//set the LI IDs in out featureCounter object to 1 to show the first feature LI
		controllerChildrenCount["ctrl" + my_counter]=childCountHold;//set the ControllerChildrenCount with the number of LIs within each controller
		my_counter++;
		jQuery(this).find("li:nth-child(1)").show(); //show the first feature LI for each controller
										
	});
									
	//takes the arguments dir:"rev" to move backwards through the features
	function animate_features(dir, speed){
		li_count=controllerChildrenCount[controller];//get the number of LIs under the controller ID
		if(li_count < 2){
		
			clearInterval(accordion_playhead);
			return false;
		};
		
		currFeatureNum=featureCounter[controller];//gets the current feature number the controller was left at
		if(speed=="fast"){
			fade_speed="fast"
		}else{
			fade_speed="slow"
		}
		
		if(dir=="rev"){ //if the reverse button was clicked
			if (currFeatureNum == 1){
				newFeatureNum=li_count;
			}else{
				 newFeatureNum=currFeatureNum -1;//the new feature will be 
			}
		}else{
			if (currFeatureNum < li_count){//if the number of the currently displayed feature is less than the number of feature LIs
				newFeatureNum=currFeatureNum +1;//the new feature will be 
			}else{
				 newFeatureNum=1;
			}
		 }	
		 //console.log("currFeatureNum= " + currFeatureNum); console.log("newFeatureNum= " + newFeatureNum);
		featureCounter[controller]=newFeatureNum;//set the current feature LI number for the current controller LI
		jQuery("#" + controller + " li:nth-child(" + currFeatureNum + ")").fadeOut(fade_speed, function(){
			jQuery("#" + controller + " li:nth-child(" + newFeatureNum + ")").fadeIn(fade_speed)
			});
		 }
		jQuery("#accordion .acc_ctrl").mouseover(function(){
			if (controller!=jQuery(this).attr("id")){//if we're not hovering over the same ID that we are already set to
				if(jQuery(this).find("ul").length > 0){ //if the UL holding the content for each category exists		 		
				jQuery("#" + controller).find("ul").stop().animate({height: '0'}, 200)
				jQuery("#" + controller).stop().animate({ backgroundColor: "lt_gray" }, 200);
				jQuery("#" + controller).find(".acc_btns").hide();
				jQuery(this).find("ul").stop().slideDown(200).stop().animate({height: feature_height}, 200) 
				jQuery(this).stop().animate({ backgroundColor: "drk_gray" }, 200, function(){
				//if callback is needed, it goes here
					});
				jQuery(this).find(".acc_btns").show();
				controller=jQuery(this).attr("id");//set the new controller to the ID that is being hovered upon
				clearInterval(accordion_playhead);
				start_playhead();
					}//end if el exisits
				}
			});
		function start_playhead(){
			try{
                accordion_playhead=setInterval(animate_features, 5000);
            }catch (e){
            
            }
		}
	start_playhead();
	///END SIDEBAR ACCORDION SCRIPTS //
	
	}
	
	
	
	//begin sidebar sign up clear out and return
	
	if(jQuery('.follow_newsletter').length > 0){
		var initval= jQuery('.follow_newsletter').val();
		
		jQuery('.follow_newsletter').focus(function(){
			if (jQuery(this).val()==initval){
				jQuery(this).val('')
			}
			jQuery(this).blur(function(){
				if (jQuery(this).val() == ''){
					jQuery(this).val(initval);
				}			  
			})
		});
	}
	//end sidebar sign up clear out and return
	
	
});
