//+++++ - After document has loaded - ++++//
jQuery(document).ready(function($) {

//+++++ - Function to initialize effects - ++++//
initEffects(); 

//+++++ - Function for starting the slideshow on the homepage - ++++//
startSliding("#but a",7000); // takes list of items to slide and persistance time in milliseconds (for how long the slide will be displayed)

//+++++ - Function to control the navigation for the slideshow buttons - ++++//
controlButtons("#but a"); // takes as input a list of items

//+++++ - Function to add rounded corners - ++++//
function addRoundedCorners() {
			$("#footer").corner("bl 12px").corner("br 12px"); 
			$("#topContent").corner("tl 12px");
			$("#updates").corner("bl 12px");
			$("#rounded").corner("br 12px").corner("tr 12px");
			$(".roundit").corner("12px");
			$(".roundit2").corner("12px");
}

//+++++ - Function to hide slides - ++++//
function hideSlides() {

			$('#rounded div .featured_text, #rounded div .featured_image').hide();
}

function initEffects() {

			hideSlides(); //make sure all slides are hidden at the beginning
			
            addRoundedCorners();
			
			firstSlide(1); //pick which slide is showed first; 1 through 5 (1-5); replace as needed
	
			$("ul.sf-menu").superfish({                   //initialize the menu options
			animation:   {opacity:'show',height:'show'},  // fade-in and slide-down animation 
            speed:       'fast',                          // faster animation speed 			
            autoArrows:  false,                           // disable generation of arrow mark-up 
            dropShadows: false                            // disable drop shadows 
			});  
						
			$('tr:nth-child(even)').addClass('alt'); // alternating table row color
}

//+++++ - Function for showing a certain slide - ++++//
function firstSlide(i) {
	       $('#featured'+i+' '+'.featured_text').show(); //this constructs a css selector; for i=1, the css selector is: #featured1 .featured_text
		   $('#featured'+i+' '+'.featured_image').show();
		   switchBackgrounds('._'+i);
}

function startSliding($buttons, $transition_time) {	

			period = setInterval(slideEach, $transition_time);
			
			$($buttons).click(function($eventobj, $auto) {
				if (period && !$auto) clearInterval(period);
			});
			
			var i = 1;
			
			function slideEach() {	
				$($buttons).filter(":eq("+i+")")
				           .trigger('click',[true]); //simulate click here for each slidable item	   
				i+1 < $($buttons).length ? i++ : i = 0; //have all the items been slid?
			}			
}

//+++++ - Apply some effects here - ++++//
function doEff(x,y,z) {
	                hideSlides();
                    $(x+y).show();
					$(x+z).fadeIn(1500); //speed in milliseconds, change if you want
					$("#rounded").corner("br 12px").corner("tr 12px");//re-add rounded corners

}

//+++++ - Function switches backgrounds on links with class ._i; for i=1, the affected link clas is ._1 - ++++//
function switchBackgrounds(i) {
	                $('#but ul li a').css({'background-image':'url(wp-content/themes/octofirst/images/nb.png)','backgroundPosition':'0 0'});
					$(i).css({'background-image':'url(wp-content/themes/octofirst/images/nb.png)','backgroundPosition':'-52px 0'});
} 
	
function controlButtons($buttons) {
             
			 var totalLinks = 5;
		 
			$($buttons).click(function(event) {
									   
				for (var i=1; i<=totalLinks;i++) {
					if ($(event.target).is('._'+i)) {
					doEff('#featured'+i+' ', '.featured_text','.featured_image');
                    switchBackgrounds('._'+i);
				    }
				}				
			return false;
			});
}

function adjustDimensions() {

	      $('.roundit2,.roundit').each(function() {

				$(this).width($(this).find('img').attr('width'));
				$(this).height($(this).find('img').attr('height'));

			});
}

adjustDimensions();

$(".menu a, .sf-menu a").removeAttr("title"); //remove title attributes from the header menu
$('.sf-menu li:last-child a').css({'padding-right':'0px','margin-right':'0px'}); //fix for IE6
$('#sidebar ul li ul li:last-child').css({'padding-bottom':'0px','margin-bottom':'0px'}); //spacing fix

$("#commentform").validate();

$('a.top').click(function(){
     $('html, body').animate({scrollTop: '0px'}, 300);
     return false;
});


});