var slideshowRun = true;

$.fn.infiniteCarousel = function () {

    function repeat(str, num) {
        return new Array( num + 1 ).join( str );
    }
  
    return this.each(function () {
        var $wrapper = $('> div', this).css('overflow', 'hidden'),
            $slider = $wrapper.find('> ul'),
            $items = $slider.find('> li'),
            $single = $items.filter(':first'),
            
            singleWidth = $single.outerWidth(), 
            visible = Math.ceil($wrapper.innerWidth() / singleWidth), // note: doesn't include padding or border
            currentPage = 1,
            pages = Math.ceil($items.length / visible);            


        // 1. Pad so that 'visible' number will always be seen, otherwise create empty items
        if (($items.length % visible) != 0) {
            $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
            $items = $slider.find('> li');
        }

        // 2. Top and tail the list with 'visible' number of items, top has the last section, and tail has the first
        $items.filter(':first').before($items.slice(- visible).clone().addClass('cloned'));
        $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
        $items = $slider.find('> li'); // reselect
        
        // 3. Set the left position to the first 'real' item
        $wrapper.scrollLeft(singleWidth * visible);
        
        // 4. paging function
        function gotoPage(page) {
            var dir = page < currentPage ? -1 : 1,
                n = Math.abs(currentPage - page),
                left = singleWidth * dir * visible * n;
            
            $wrapper.filter(':not(:animated)').animate({
                scrollLeft : '+=' + left
            }, 500, function () {
                if (page == 0) {
                    $wrapper.scrollLeft(singleWidth * visible * pages);
                    page = pages;
                } else if (page > pages) {
                    $wrapper.scrollLeft(singleWidth * visible);
                    // reset back to start position
                    page = 1;
                } 

                currentPage = page;
            });                
            
            return false;
        }
        
        $wrapper.after('<a class="arrow back">&lt;</a><a class="arrow forward">&gt;</a>');
        
        // 5. Bind to the forward and back buttons
        $('a.back', this).click(function () {
            return gotoPage(currentPage - 1);                
        });
        
        $('a.forward', this).click(function () {
            return gotoPage(currentPage + 1);
        });
        
        // create a public interface to move to a specific page
        $(this).bind('goto', function (event, page) {
            gotoPage(page);
        });
    });  
};

$(document).ready(function(){

	addEvent(window, 'load', initCorners);

	if (document.getElementById("homeslides")){
	 	$('.slideshow').cycle({
			fx: 'scrollUp',
			speed: 1500,
			random: 1,
			timeout: 6000
		});
		// pause/play slideshow	
		$('#slideshow-controls a').click(function(e){
			e.preventDefault();
			
			if(slideshowRun == true) {
				pauseSlideshow();
			} else {
				$('.slideshow').cycle('resume');
				$(this).removeClass('active');
				slideshowRun = true;
			}
		});
	}
	
	if (document.getElementById("gallery")){
	 	$('.galleryslides').cycle({
			fx:     'fade', 
    		speed:  'fast', 
   			timeout: 0,
			next: 	'#nextbtn', 
    		prev: 	'#prevbtn'
		});
	}
	if (document.getElementById("egallery")){
	
		$('div.edetails').hide();
	
	 	$('.equipslides').cycle({
			fx:     'fade', 
    		speed:  'fast', 
   			timeout: 0,
			next: 	'#nextbtn', 
    		prev: 	'#prevbtn',
    		after:	equipDetails
		});
	}
	
	function equipDetails(curr, next, opts) {
	
		var index = opts.currSlide+1;
		//$('#prevbtn')[index == 0 ? 'hide' : 'show']();
		//$('#nextbtn')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		
		//alert('show equip details' + index);
		$('#prevbtn')[index == 1 ? 'hide' : 'show']();
		$('#nextbtn')[index == opts.slideCount ? 'hide' : 'show']();
		$('div.edetails').hide();
		$('#equip' + index + '-details').fadeIn();
		
	}
	/*function onAfter(curr, next, opts) {
		    var index = opts.currSlide;
		    $('#prevbtn')[index == 0 ? 'hide' : 'show']();
		    $('#nextbtn')[index == opts.slideCount - 1 ? 'hide' : 'show']();
		}*/
	
	// toggle
	
	//Hide (Collapse) the toggle containers on load
	$(".toggle_container").hide(); 

	//Switch the "Open" and "Close" state per click
	$("h3.trigger").toggle(function(){
		$(this).addClass("active");
		}, function () {
		$(this).removeClass("active");
	});

	//Slide up and down on click
	$("h3.trigger").click(function(){
		$(this).next(".toggle_container").slideToggle("normal");
	});
	
	// Expand all
	
	var expand = false;
	
	$("#expandall").click(function(){
		
		$(".toggle_container").toggle();
		if(expand == true) {
			$(this).text("Expand All");
			expand = false;
		} else {
			$(this).text("Collapse All");
			expand = true;
		}
	});


	if (document.getElementById("meet-teachers")){
	
		$('.infiniteCarousel').infiniteCarousel();
		
		var teacherbios = $('div.teacher-bio');
			//teacherbios.hide().filter(':first').show();
			teacherbios.hide();
			
		$('#bios ul li a').hover(function() {
		
			$('#bios ul li').removeClass('hover');
			
			if(!$(this).parent('li').hasClass('active')) {
	   			$(this).parent('li').addClass('hover');
	   		}
		});
		$('#bios ul li a').mouseout(function() {
			$('#bios ul li').removeClass('hover');
		});
		
		$('#bios ul li a').click(function(e) {
			e.preventDefault();
		
		if(!$(this).parent('li').hasClass('active')) {	
	   		$('#bios ul li').removeClass('active');
	   		$(this).parent('li').addClass('active');
	   		
	   		//var activetab = this.hash;
			//	activetab = activetab.replace("#ti","tb");
	   		
	   		//alert(activetab);
	   		
	   		var tmpid = $(this).attr("id");
	   			tmpid = tmpid.replace("tb","tc");
	   		
	   		teacherbios.hide().filter('#'+tmpid).fadeIn();
	   	}
	   		
	    });
	}
});

function pauseSlideshow() {

	$('.slideshow').cycle('pause');
	slideshowRun = false;
		
	var e = document.getElementById('slideshow-controls').getElementsByTagName('a')[0];
		e.setAttribute('class', 'active');
		e.setAttribute('className', 'active');
}


function initCorners() {

    var settings = {
      tl: { radius: 3 },
      tr: { radius: 3 },
      bl: { radius: 3 },
      br: { radius: 3 },
      antiAlias: true
    }
    var cbox = {
      tl: { radius: 6 },
      tr: { radius: 6 },
      bl: { radius: 6 },
      br: { radius: 6 },
      antiAlias: true
    }
    var togglebox = {
      tl: { radius: 10 },
      tr: { radius: 10 },
      bl: { radius: 10 },
      br: { radius: 10 },
      antiAlias: true
    }

    /*
    Usage:

    curvyCorners(settingsObj, selectorStr);
    curvyCorners(settingsObj, Obj1[, Obj2[, Obj3[, . . . [, ObjN]]]]);

    selectorStr ::= complexSelector [, complexSelector]...
    complexSelector ::= singleSelector[ singleSelector]
    singleSelector ::= idType | classType
    idType ::= #id
    classType ::= [tagName].className
    tagName ::= div|p|form|blockquote|frameset // others may work
    className : .name
    selector examples:
      #mydiv p.rounded
      #mypara
      .rounded
    */
    curvyCorners(settings, "#sitenav li");
    curvyCorners(togglebox, ".toggle_container");
    curvyCorners(togglebox, ".teacher-bio");
    
    if (document.getElementById("slideshow-controls")){
    	//curvyCorners(settings, "#slideshow-controls a");
    	}
    //curvyCorners(settings, "#rate-packages li");

  }


/*
if(document.getElementById("promos")){
	
		var promomods = $('#promos > div.promo');
			promomods.hide().filter('#promo-' + cdate).show();
		var promotabs = $('#promo-nav > li');
	    	promotabs.filter('#tab-' + cdate).addClass('selected');
	   
		$('ul#promo-nav li a').hover(function () {
			
			$('ul#promo-nav li').removeClass('hover');
			
			if(!$(this).parent('li').hasClass('selected')) {
	   			$(this).parent('li').addClass('hover');
	   		}
	    });
	    $('ul#promo-nav li a').mouseout(function () {		
	   			$(this).parent('li').removeClass('hover');
	    });
	    
		$('ul#promo-nav li a').click(function () {
			
			var activetab = this.hash;
				activetab = activetab.replace("#","promo-");
			
			//alert(activetab);
			
			promomods.hide().filter('#' + activetab).show();
			
			var qu = $(this).attr("id");
		    var an = "#" + qu.replace("gb","gid");
		    var gc = "#" + qu.replace("gb","gc");*/
			
			//alert(this.hash);
			// switch chart
	//		var seasonID = this.hash;
	//			seasonID = seasonID.replace("#","");
	 //  		switchchart(seasonID);
		
			//alert(this.hash);
			//chartdetails.hide().filter(this.hash + '-detail').show();
			
	   		// remove selected
	   		//$('ul#promo-nav li').removeClass('hover');
	   		//$('ul#promo-nav li').removeClass('selected');
	   		// add selected to this
	   		//$(this).parent('li').addClass('selected');
	   		
	   		//return false;
	   		
	  //  });


