
//+++++ - After document has loaded - ++++//
$(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");
			$("#updates").corner("bl 12px");
			$("#rounded").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

			Cufon.replace('#peel h1,#updates h1,h1.blue,h1.bblue-head', { fontFamily: 'TitilliumText14L' }); //Cufon font replacement
			
			$("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("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(images/sprites.png)','backgroundPosition':'0px -115px'});
					$(i).css({'background-image':'url(images/sprites.png)','backgroundPosition':'-52px -115px'});
} 
	
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;
			});
}
			
});