$(document).ready(function() {
    
    // initialize modules
    initPodcasts();
    initUpdates();
    initVideos();
    initMiniCal();
    initDonate();
    
    if( $("#thegallery").size() > 0 ) {
        initPhotoCarousel("thegallery");
    }
    
    if( $("#header div.video").size() > 0 ) {
        initMenuFlashHide();
    }
    
});




// prepping the podcasts picker
function initPodcasts() {
    $("#podcasts_mod form").addClass("init");
    
    $("#podcasts_mod select").change( function() {
        var action = $(this).parents("form").attr("action");
        var selected = $("#podcasts_mod option:selected").val();
        window.location=action + "/" + selected;
    });
}


// prepping the email subscriber
function initUpdates() {
    $("#updates_mod form").addClass("init");
    
    function checkUpdatesInput() {
        var address = $("#updates_mod #update_email").val();
        if( address == "" ) {
            $("#updates_mod input").parent().find(".hint").show();
        } else {
            $("#updates_mod input").parent().find(".hint").hide();
        }
    }
    checkUpdatesInput();
    
    $("#updates_mod input").focus(function() {
        $(this).parent().find(".hint").hide();
    });
    
    $("#updates_mod input").blur(function() {
        checkUpdatesInput();
    });
}


// prepping the video carousel
function initVideos() {
    $("#videos_mod .mod_content").addClass("init");
    initCarousel("videos_mod");
    
}


// prepping/adding to the mini calendar
function initMiniCal() {
    $("#minical tr").each( function() {
        $(this).children("th:eq(0)").addClass("sun");
        $(this).children("td:eq(0)").addClass("sun");
        $(this).children("th:eq(6)").addClass("sat");
        $(this).children("td:eq(6)").addClass("sat");
    });
    
    // clicks on calendar dates
    $("#minical td.event a").click( function(e) {
        e.preventDefault();
        
        var index = $("#minical td.event a").index(this);
        var modHeight = $(this).parents(".mod_content").height();
        
        $("#minical td.event").removeClass("chosen");
        $("#minical td.event:eq(" + index + ")").addClass("chosen");
        
        $(this).parents(".mod_content").css("height",modHeight);
        $(".events_list .event_day").css("position","absolute").fadeOut();
        $(".events_list .event_day:eq(" + index + ")").fadeIn().queue( function() {
            $(this).css("position","relative");
            $(this).parents(".mod_content").css("height","auto");
            $(this).dequeue();
        });
    });
    
    // "click" the next upcoming event (if it exists)
    $("#minical td.today").prev().nextAll("td.event").find("a").click();
//    $("#minical td.event a:eq(0)").click();
}


// prepping the donator
function initDonate() {
    $("#donate_mod form").addClass("init");
    
    $("#donate_mod input").focus(function() {
        $(this).parent().find(".hint").hide();
    });
    
    $("#donate_mod input").blur(function() {
        var address = $(this).val();
        if( address == "" ) {
            $(this).parent().find(".hint").css("display","inline");
        }
    });
}




// Set up videos carousel
function initCarousel(index) {

    /* ----- procedural setup >>>> ----- */
    var root = index;
    var horseCount = $("#"+root+" .horses li").size();
    var currentHorse = $("#"+root).attr("class").replace(/module /, "");

    // get the width of the wrapper window
    var trackSize = $("#"+root+" .track").width();
    // get the height of an individual item
    var horseSize = $("#"+root+" .horses li:first").width();
    // figure out how many items fit in the window
    var horsesPerPage = Math.floor(trackSize / horseSize);
    var pagesOfHorses = Math.ceil(horseCount / horsesPerPage);
    // set the height of the list of shows
    var horsesSize = trackSize * pagesOfHorses;
    $("#"+root+" .horses").css("width",horsesSize+"px");

    // set the page to that of the current show
    //var startHorse = $("#"+root).attr("class").replace(/module /, "");
    var startHorse = $("#"+root+" .horses li").index( $("#"+root+" .horses li.select") );
    if( startHorse < 0 ) { startHorse = 0; }
    var startPage = Math.floor(
        ( $("#"+root+" .horses li").index( $("#"+root+" .horses li:eq("+startHorse+")") ) ) / horsesPerPage
    );
    $("#"+root+" .horses").css({ left: "-"+(startPage * trackSize)+"px" });
    manuSlide(startPage);
    var currentIndex = startPage;

    // set the thumbnail of the current show
    $("#"+root+" .horses li:eq("+startHorse+")").addClass("select");

    /* ----- <<<< procedural setup ----- */

    /* ----- general functions >>>> ----- */

    // manual click on a particular horse
    function manuSlide(page) {
		oldIndex = currentIndex;
		currentIndex = page;
        slideShowsList(currentIndex);
        $("#"+root+" .controls .index").eq(oldIndex).removeClass("select");
        $("#"+root+" .controls .index").eq(currentIndex).addClass("select");
	}

    // automatically move from one horse to another
    function autoSlide(direction) {
        oldIndex = currentIndex;

        // move forward
        if( direction == "next" ) {
            currentIndex = (oldIndex + 1) % pagesOfHorses;
        }

        // move backward
        else if( direction == "prev" ) {

            // if the departing horse is not the first one
            if( oldIndex > 0 ) {
                currentIndex = (oldIndex - 1) % pagesOfHorses;
            }

            // if the departing horse is the first one
            else if( oldIndex == 0 ) {
                currentIndex = pagesOfHorses - 1;
            }
        }

        slideShowsList(currentIndex);
        $("#"+root+" .controls .index").eq(oldIndex).removeClass("select");
        $("#"+root+" .controls .index").eq(currentIndex).addClass("select");
    }


    // slide the carousel
    function slideShowsList(slideTo) {
        $("#"+root+" .horses").animate({ left: "-"+(slideTo * trackSize)+"px" }, 500 );

        // check to see if we're at the end and treat that arrow accordingly
        if( slideTo == pagesOfHorses-1 ) {
            $("#"+root+" .controls .next a").hide();
        } else {
            $("#"+root+" .controls .next a").show();
        }
        
        // check to see if we're at the beginning and treat that arrow accordingly
        if( slideTo == 0 ) {
            $("#"+root+" .controls .prev a").hide();
        } else {
            $("#"+root+" .controls .prev a").show();
        }
    }

    // set wrapper classes
    function setHorse(leaving,coming) {
        jQuery("#"+root).removeClass(leaving);
        jQuery("#"+root).addClass(coming);
    }

    /* ----- <<<< general functions ----- */


    /* ----- interactive buttons >>>> ----- */

    // click on the down button
    jQuery("#"+root+" .controls .next a").click(function() {
        autoSlide("next");
    });

    // click on the up button
    jQuery("#"+root+" .controls .prev a").click(function() {
        autoSlide("prev");
    });

    // click on an index button
    jQuery("#"+root+" .controls .index").click(function() {
        var thisPage = $("#"+root+" .controls .index").index(this);
        manuSlide(thisPage);
    });

    /* ----- <<<< interactive buttons ----- */
}
// END initCarousel




// Set up photo gallery carousel
function initPhotoCarousel(index) {

    /* ----- procedural setup >>>> ----- */
    var root = index;
    var horseCount = $("#"+root+" .horses li").size();
//    var currentHorse = $("#"+root).attr("class").replace(/module /, "");

    // get the width and height of the wrapper window
    var trackWidth = $("#"+root+" .track").width();
    var trackHeight = $("#"+root+" .track").height();
    
    // get the width and height of an individual item
    var horseWidth = $("#"+root+" .horses li:first").width();
    var horseHeight = $("#"+root+" .horses li:first").height();
    
    // figure out how many items fit in the window
    var horsesPerPage = ( Math.floor(trackWidth / horseWidth) * Math.floor(trackHeight / horseHeight) );
    
    // figure out how many pages total
    var pagesOfHorses = Math.ceil(horseCount / horsesPerPage);
    
    // create index links for each page
    for(i=1; i<=pagesOfHorses; i++) {
        $("#"+root+" .controls li.up").before("<li class=\"index index" + i + "\"><a title=\"Go to page " + i +"\">" + i + "</a></li>");
    }

    // set the height of the list of shows
    var horsesSize = trackHeight * pagesOfHorses;
    $("#"+root+" .horses").css("height",horsesSize+"px");

    // set the page to that of the current show
    //var startHorse = $("#"+root).attr("class").replace(/module /, "");
    var startHorse = $("#"+root+" .horses li").index( $("#"+root+" .horses li.select") );
    if( startHorse < 0 ) { startHorse = 0; }
    var startPage = Math.floor(
        ( $("#"+root+" .horses li").index( $("#"+root+" .horses li:eq("+startHorse+")") ) ) / horsesPerPage
    );
    $("#"+root+" .horses").css({ top: "-"+(startPage * trackHeight)+"px" });
    manuSlide(startPage);
    var currentIndex = startPage;

    // set the thumbnail of the current show
    $("#"+root+" .horses li:eq("+startHorse+")").addClass("select");

    /* ----- <<<< procedural setup ----- */

    /* ----- general functions >>>> ----- */

    // manual click on a particular horse
    function manuSlide(page) {
		oldIndex = currentIndex;
		currentIndex = page;
        slideShowsList(currentIndex);
        $("#"+root+" .controls .index").eq(oldIndex).removeClass("select");
        $("#"+root+" .controls .index").eq(currentIndex).addClass("select");
	}

    // automatically move from one horse to another
    function autoSlide(direction) {
        oldIndex = currentIndex;

        // move forward
        if( direction == "next" ) {
            currentIndex = (oldIndex + 1) % pagesOfHorses;
        }

        // move backward
        else if( direction == "prev" ) {

            // if the departing horse is not the first one
            if( oldIndex > 0 ) {
                currentIndex = (oldIndex - 1) % pagesOfHorses;
            }

            // if the departing horse is the first one
            else if( oldIndex == 0 ) {
                currentIndex = pagesOfHorses - 1;
            }
        }

        slideShowsList(currentIndex);
        $("#"+root+" .controls .index").eq(oldIndex).removeClass("select");
        $("#"+root+" .controls .index").eq(currentIndex).addClass("select");
    }


    // slide the carousel
    function slideShowsList(slideTo) {
        $("#"+root+" .horses").animate({ top: "-"+(slideTo * trackHeight)+"px" }, 500 );

        // check to see if we're at the end and treat that arrow accordingly
        if( slideTo == pagesOfHorses-1 ) {
            $("#"+root+" .controls .down a").hide();
        } else {
            $("#"+root+" .controls .down a").show();
        }
        
        // check to see if we're at the beginning and treat that arrow accordingly
        if( slideTo == 0 ) {
            $("#"+root+" .controls .up a").hide();
        } else {
            $("#"+root+" .controls .up a").show();
        }
    }

    // set wrapper classes
    function setHorse(leaving,coming) {
        jQuery("#"+root).removeClass(leaving);
        jQuery("#"+root).addClass(coming);
    }
    
    
    // set showcase image
    function setShowcase(photo) {
        $("#"+root+" #showcase img").hide();
        
        var url = location.href;
        var sliceat = url.lastIndexOf("photos/");
        var short_name = url.slice(sliceat+7);
        
        $("#"+root+" #showcase img").load( function() {
            
            var showcasePhotoHeight = $("#"+root+" #showcase img").height();
            var showcaseHeight = $("#"+root+" #showcase").height();
            if( showcaseHeight > showcasePhotoHeight ) {
                var offset = Math.floor( (showcaseHeight-showcasePhotoHeight)/2 );
            } else {
                var offset = 0;
            }
            $("#"+root+" #showcase img").css({
                top: offset + "px"
            });
            
            $("#"+root+" #showcase img").show();
            
        });
        
        $("#"+root+" #showcase a").attr({
            href: "/images/photo_galleries/" + short_name + "/" + photo + ".jpg"
        });
        
        $("#"+root+" #showcase img").attr({ 
            src: "/images/photo_galleries/" + short_name + "/" + photo + "_medium.jpg"
        });
    }

    /* ----- <<<< general functions ----- */


    /* ----- interactive buttons >>>> ----- */

    // click on the down button
    jQuery("#"+root+" .controls .down a").click(function() {
        autoSlide("next");
    });

    // click on the up button
    jQuery("#"+root+" .controls .up a").click(function() {
        autoSlide("prev");
    });

    // click on an index button
    jQuery("#"+root+" .controls .index").click(function() {
        var thisPage = $("#"+root+" .controls .index").index(this);
        manuSlide(thisPage);
    });
    
    
    // click on a thumbnail
    $("#"+root+" .thumbs li a").click( function(e) {
        e.preventDefault();
        
        var photo = $(this).find("img").attr("longdesc");
        var photo = photo.replace(/.jpg/i, "");
        setShowcase(photo);
    });

    /* ----- <<<< interactive buttons ----- */
    
    
    // set the enlarged image to the big version of the startHorse
    $("#"+root+" .horses li.select a").click();
    
    $("#"+root+" #showcase a").prettyPhoto({
		animationSpeed: 'normal', /* fast/slow/normal */
		padding: 40, /* padding for each side of the picture */
		opacity: 0.6, /* Value betwee 0 and 1 */
		showTitle: false, /* true/false */
		allowresize: true, /* true/false */
		counter_separator_label: '/', /* The separator for the gallery counter 1 "of" 2 */
		theme: 'light_square' /* light_rounded / dark_rounded / light_square / dark_square */
	});
}
// END initPhotoCarousel


// Handle menu hovers that need to hide the flash
function initMenuFlashHide() {
    $("body.home #header .nav").hover(
        function() {
            $("div.video *").hide();
        },
        function() {
            $("div.video *").show();
        }
    );
}
