jQuery.fn.exists = function () {
    return jQuery(this).length > 0;
}

// JavaScript Document
/* plugin */
jQuery.fn.dwFadingLinks = function (settings) {
    settings = jQuery.extend({
        color: '#ff8c00',
        duration: 500
    }, settings);
    return this.each(function () {
        var original = $(this).css('color');
        $(this).mouseover(function () {
            $(this).animate({
                color: settings.color
            }, settings.duration);
        });
        $(this).mouseout(function () {
            $(this).animate({
                color: original
            }, settings.duration);
        });
    });
};






$(window).load(function () {


    $('#navbar a').tipsy({
        fade: true
    });


    $('.event_box a[title]').tipsy({
        fade: true,
        html: true,
        gravity: 'n'
    });

    $('.eventList a[title]').tipsy({
        fade: true,
        html: true,
        gravity: 'n'
    });



    if ($('#loading').exists()) {
		var sections = $("a[id]");
		var currentSection = $("#loading");
		var currentSection2 = $("#loading");
		
		sections.click(function (e){
			e.preventDefault();
			
			switch (e.target.id){
				case "fnl":
					
					currentSection.hide();
					$("#divisionInfo #fnl").fadeIn();
					currentSection = $("#divisionInfo #fnl");
					
					currentSection2.hide();
					$("#divUpcomingInfo #fnl").fadeIn();
					currentSection2 = $("#divUpcomingInfo #fnl");
					break;
					
				case "mc":
					
					currentSection.hide();
					$("#divisionInfo #mc").fadeIn();
					currentSection = $("#divisionInfo #mc");
					
					currentSection2.hide();
					$("#divUpcomingInfo #mc").fadeIn();
					currentSection2 = $("#divUpcomingInfo #mc");
					break;
					
				case "rt":
					
					currentSection.hide();
					$("#divisionInfo #rt").fadeIn();
					currentSection = $("#divisionInfo #rt");
					
					currentSection2.hide();
					$("#divUpcomingInfo #rt").fadeIn();
					currentSection2 = $("#divUpcomingInfo #rt");
					break;
					
				case "sns":
					
					currentSection.hide();
					$("#divisionInfo #sns").fadeIn();
					currentSection = $("#divisionInfo #sns");
					
					currentSection2.hide();
					$("#divUpcomingInfo #sns").fadeIn();
					currentSection2 = $("#divUpcomingInfo #sns");
					break;
					
				case "sen":
					
					currentSection.hide();
					$("#divisionInfo #sen").fadeIn();
					currentSection = $("#divisionInfo #sen");
					
					currentSection2.hide();
					$("#divUpcomingInfo #sen").fadeIn();
					currentSection2 = $("#divUpcomingInfo #sen");
					break;
					
				case "se":
					
					currentSection.hide();
					$("#divisionInfo #se").fadeIn();
					currentSection = $("#divisionInfo #se");
					
					currentSection2.hide();
					$("#divUpcomingInfo #se").fadeIn();
					currentSection2 = $("#divUpcomingInfo #se");
					break;
					
				case "tnc":
					
					currentSection.hide();
					$("#divisionInfo #tnc").fadeIn();
					currentSection = $("#divisionInfo #tnc");
					
					currentSection2.hide();
					$("#divUpcomingInfo #tnc").fadeIn();
					currentSection2 = $("#divUpcomingInfo #tnc");
					break;
					
				case "ln":
					
					currentSection.hide();
					$("#divisionInfo #ln").fadeIn();
					currentSection = $("#divisionInfo #ln");
					
					currentSection2.hide();
					$("#divUpcomingInfo #ln").fadeIn();
					currentSection2 = $("#divUpcomingInfo #ln");
					break;
					
				default:
					break;
						
			}
			
			$.scrollTo("#divisionInfo",800);
			
			$('#divUpcomingInfo a[title]').tipsy({
				fade:true,
				html:true,
				gravity: 'n'
			});
			
		});
    }


    if ($('#weekSlider').exists()) {
		
		$curWeek = 1;
		
		
		$curWeek =  parseInt($('#weekSlider').attr("week"));
		
		
		
        $('#weekSlider').movingBoxes({
            startPanel: $curWeek,
            // start with this panel
            width: 748,
            // overall width of movingBoxes - 743
            panelWidth: .31,
            //.34,
            // current panel width adjusted to 50% of overall width - .7
            imageRatio: 1 //16/9 // Image ratio set to 16:9
        });
        $('.left').tipsy({
            fade: true,
            html: true,
            gravity: 'w'
        });
        $('.right').tipsy({
            fade: true,
            html: true,
            gravity: 'e'
        });
    }



    if ($('#calendar').exists()) {
		
		var date = new Date();
  var d = date.getDate();
  var m = date.getMonth();
  var y = date.getFullYear();
  
	  $('#calendar').fullCalendar({
	    events: $.fullCalendar.gcalFeed(
		"http://www.google.com/calendar/feeds/fn07k70o3trindrlpj1kk5aqne6e612p%40import.calendar.google.com/public/basic",
        {
            editable:        false,
            currentTimezone: 'US/Eastern'
        }
    )		
});

    }



    if ($('#thumbs').exists()) {

        // We only want these styles applied when javascript is enabled
        $('div.navigation').css({
            'width': '300px',
            'float': 'left'
        });
        $('div.content').css('display', 'block');

        // Initially set opacity on thumbs and add
        // additional styling for hover effect on thumbs
        var onMouseOutOpacity = 0.67;
        $('#thumbs ul.thumbs li').opacityrollover({
            mouseOutOpacity: onMouseOutOpacity,
            mouseOverOpacity: 1.0,
            fadeSpeed: 'fast',
            exemptionSelector: '.selected'
        });

        // Initialize Advanced Galleriffic Gallery
        var gallery = $('#thumbs').galleriffic({
            delay: 2500,
            numThumbs: 18,
            preloadAhead: 10,
            enableTopPager: true,
            enableBottomPager: true,
            maxPagesToShow: 7,
            imageContainerSel: '#slideshow',
            controlsContainerSel: '#controls',
            captionContainerSel: '#caption',
            loadingContainerSel: '#loading',
            renderSSControls: true,
            renderNavControls: true,
            playLinkText: 'Play Slideshow',
            pauseLinkText: 'Pause Slideshow',
            prevLinkText: '&lsaquo; Previous Photo',
            nextLinkText: 'Next Photo &rsaquo;',
            nextPageLinkText: 'Next &rsaquo;',
            prevPageLinkText: '&lsaquo; Prev',
            enableHistory: true,
            autoStart: false,
            syncTransitions: true,
            defaultTransitionDuration: 900,
            onSlideChange: function (prevIndex, nextIndex) {
                // 'this' refers to the gallery, which is an extension of $('#thumbs')
                this.find('ul.thumbs').children().eq(prevIndex).fadeTo('fast', onMouseOutOpacity).end().eq(nextIndex).fadeTo('fast', 1.0);
            },
            onPageTransitionOut: function (callback) {
                this.fadeTo('fast', 0.0, callback);
            },
            onPageTransitionIn: function () {
                this.fadeTo('fast', 1.0);
            }
        });
		
		/**** Functions to support integration of galleriffic with the jquery.history plugin ****/
 
				// PageLoad function
				// This function is called when:
				// 1. after calling $.historyInit();
				// 2. after calling $.historyLoad();
				// 3. after pushing "Go Back" button of a browser
				function pageload(hash) {
					// alert("pageload: " + hash);
					// hash doesn't contain the first # character.
					if(hash) {
						$.galleriffic.gotoImage(hash);
					} else {
						gallery.gotoIndex(0);
					}
				}
 
				// Initialize history plugin.
				// The callback is called at once by present location.hash. 
				$.historyInit(pageload, "advanced.html");
 
				// set onlick event for buttons using the jQuery 1.3 live method
				$("a[rel='history']").live('click', function(e) {
					if (e.button != 0) return true;
					
					var hash = this.href;
					hash = hash.replace(/^.*#/, '');
 
					// moves to a new page. 
					// pageload is called at once. 
					// hash don't contain "#", "?"
					$.historyLoad(hash);
 
					return false;
				});
    }



    if ($('#slider').exists()) {
        $('#slider').nivoSlider({
            effect: 'sliceDown',
            //Specify sets like: 'sliceDown, sliceDownLeft, sliceUp, sliceUpLeft, sliceUpDown, sliceUpDownLeft, fold, fade, random
            slices: 15,
            animSpeed: 500,
            pauseTime: 6500,
            startSlide: 0,
            //Set starting Slide (0 index)
            directionNav: true,
            //Next & Prev
            directionNavHide: true,
            //Only show on hover
            controlNav: false,
            //1,2,3...
            controlNavThumbs: false,
            //Use thumbnails for Control Nav
            controlNavThumbsFromRel: false,
            //Use image rel for thumbs
            controlNavThumbsSearch: '.jpg',
            //Replace this with...
            controlNavThumbsReplace: '_thumb.jpg',
            //...this in thumb Image src
            keyboardNav: true,
            //Use left & right arrows
            pauseOnHover: true,
            //Stop animation while hovering
            manualAdvance: false,
            //Force manual transitions
            captionOpacity: 0.94
            //Universal caption opacity
        });

        

    }
	
	
	if($('#events').exists()){
		//make the upcoming events scroll
        $('#events').jCarouselLite({
            vertical: true,
            visible: 3,
            auto: 3000,
            speed: 1000,
            pauseOnHover: true
        });	
	}
	
	if($('.membersListUL').exists()){
		$('.membersListUL li').click(function(e){
			var x = '.underside#' + $(this).attr('id');
			
			$(this).flip({
				direction:'lr',
				content:$(x),
				color:'#E7E7E7'
			});
		
		$(this).unbind('click');
		$(this).click(function(e){
			$(this).revertFlip();
			$(this).css('background','');
		});
	});
	}

    $('#navbar a').tipsy({
        fade: true
    });




    $('.event_box a[title]').tipsy({
        fade: true,
        html: true,
        gravity: 'n'
    });

    $('.eventList a[title]').tipsy({
        fade: true,
        html: true,
        gravity: 'n'
    });
	
	

    //make links fade color
    $('.fade').dwFadingLinks({
        //color: '#26639c',
        color: '#f26522',
        duration: 150
    });

    $('#footer a').dwFadingLinks({
        //color: '#26639c',
        color: '#f26522',
        duration: 150
    });
	



});
