//+++++ - After document has loaded - ++++//
jQuery(document).ready(function($) {

//+++++ - Function to initialize effects - ++++//
initEffects(); 

//+++++ - Function for starting the slideshow on the homepage - ++++//
startSliding("#but a",5000); // 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");
			$("#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();
			$('#rounded .hideit').css({'width':'0','height':'0'});
}

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 to start the sliding - ++++//
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();
					$('#rounded .hideit').css({'width':'auto','height':'auto'});
					$(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) {

                     if ($('#but').hasClass("notnumb")) {
	                $('#but.notnumb ul li a').css({'background-image':'url(wp-content/themes/octofirst-lightweight/images/sprites.png)','backgroundPosition':'-84px -185px'});
					$('#but.notnumb ul li a.'+i).css({'background-image':'url(wp-content/themes/octofirst-lightweight/images/sprites.png)','backgroundPosition':'-110px -185px'});
                    }
					
					else {
					$('#but ul li a').css({'background-image':'url(wp-content/themes/octofirst-lightweight/images/sprites.png)','backgroundPosition':'0px -115px'});
					$(i).css({'background-image':'url(wp-content/themes/octofirst-lightweight/images/sprites.png)','backgroundPosition':'-52px -115px'});
					}
					
} 

//+++++ - Function to control slider buttons - ++++//	
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 to automatically set width and height for rounded containers - ++++//
function adjustDimensions() {

	      $('.roundit2,.roundit').each(function() {

				$(this).width($(this).find('img').attr('width'));
				$(this).height($(this).find('img').attr('height'));

			});
}

adjustDimensions();

//+++++ - Function to equalize heights of containers - ++++//
function equalizeHeights() {

var maxHeight = 0;

$(".fst,.photoContainer").each(function(){
   if ($(this).height() > maxHeight) { maxHeight = $(this).height(); }
});

$(".fst,.photoContainer").height(maxHeight);

}

equalizeHeights();

//+++++ - Various fixed and improvements - ++++//
$(".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(); //initiliaze jQuery Validate

$('a.top').click(function(){  //scroll to top
     $('html, body').animate({scrollTop: '0px'}, 300);
     return false;
});

});