// page init
$(function(){
/*XXX as we know where these are used, the initializations
    should be moved into the views which render these, using
    the headStuffer protocol to stuff the inits into the html
    head */
        //initSlideShow();

    $('#univ-tools .title').click(function (e) {$('.drop-down', e.target.parentNode).slideToggle();e.preventDefault();return false;});

});

function initGallery() {
    // settings
    var _activeClass = 'active';
    var _minWidth = 1000;
    var _fadeSpeed = 100;
    var _speed = 650;

    $('div#gallery').each(function(){
        // gallery options
        var _holder = $(this);
        var _btnUp = _holder.find('a.arrow-up');
        var _btnDown = _holder.find('a.arrow-down');
        var _slidesHolder = _holder.find('div.gallery-holder');
        var _tooltips = _holder.find('div.more-info > div.infobox').hide();
        var _slider = _slidesHolder.find('>div.slider');
        var _slides = _slider.children();
        var _stepsCount = _slides.length;
        var _slideHeight = _slidesHolder.height();
        var _currentIndex = 0;
        var _oldIndex = 0;
        var _direction;
        
        ///Updated "click-ability" to the "infobox-content" class in the gallery //erm--2011-05-23@2:59pm
    $('.infobox-content',_holder).click(function(){
        window.location = $(this).find('a[href]').attr('href');
        return false;
    });

    // gallery control
    _btnUp.click(function(){
        prevSlide();
        return false;
    });
    _btnDown.click(function(){
        nextSlide();
        return false;
    });

    // gallery init
    if(_currentIndex<0) _currentIndex=0;
    _slider.css({
        position:'relative',
        height:_slideHeight
    });
    _slides.hide().eq(_currentIndex).show();
    _slides.css({
        position:'absolute',
        top:0
    });

    // gallery animation
    function prevSlide() {
        _oldIndex = _currentIndex;
        if(_currentIndex > 0) _currentIndex--;
        else _currentIndex = _stepsCount-1;
        _direction=0;
        switchSlide();
    }
    function nextSlide() {
        _oldIndex = _currentIndex;
        if(_currentIndex < _stepsCount-1) _currentIndex++;
        else _currentIndex = 0;
        _direction=1;
        switchSlide();
    }
    function switchSlide() {
        var _prevSlide = _slides.eq(_oldIndex);
        var _nextSlide = _slides.eq(_currentIndex);
        _nextSlide.css({top:_slideHeight*(_direction ? 1 : -1)}).show();
        _prevSlide.animate({top:_slideHeight*(_direction ? -1 : 1)},{duration:_speed, queue:false});
        _nextSlide.animate({top:0},{duration:_speed, queue:false});
    }

    // gallery tooltips
    _slides.children().each(function(_ind){
        var _opener = $(this);
        var _popup = _tooltips.eq(_ind);
        var _timer;
        _opener.hover(function(){
            if(_timer) clearTimeout(_timer);
            _timer = setTimeout(function(){
                _popup.fadeIn(_fadeSpeed);
            },_fadeSpeed);
        },function(){
            if(_timer) clearTimeout(_timer);
            _timer = setTimeout(function(){
                _popup.fadeOut(_fadeSpeed);
            },_fadeSpeed)
        });

    _popup.hover(function(){
        if(_timer) clearTimeout(_timer);
    },function(){
        _timer = setTimeout(function(){
            _popup.fadeOut(_fadeSpeed);
        },_fadeSpeed);
    });
});
});
}
function initGalleryBig() {
    // settings
    var _activeClass = 'active';
    var _minWidth = 1000;
    var _fadeSpeed = 100;
    var _speed = 650;

    $('div#gallerybig').each(function(){
        // gallery options
        var _holder = $(this);
        var _btnUp = _holder.find('a.arrow-up');
        var _btnDown = _holder.find('a.arrow-down');
        var _slidesHolder = _holder.find('div.gallery-holder');
        var _tooltips = _holder.find('div.more-info > div.infobox').hide();
        var _slider = _slidesHolder.find('>div.slider');
        var _slides = _slider.children();
        var _langs = _holder.find('div.slider .text');
        var _stepsCount = _slides.length;
        var _rowHeight = $(_slides[0]).height()
        var _slideHeight = _slidesHolder.height();
        var _currentIndex = 0;
        var _oldIndex = 0;
        var _direction;
        var _animationsInProgress = [];

    ///Updated "click-ability" to the "infobox-content" class in the gallery //erm--2011-05-23@2:59pm
    $('.infobox-content',_holder).click(function(){
        window.location = $(this).find('a[href]').attr('href');
        return false;
    });

    // gallery control
    _btnUp.click(function(){
        prevSlide();
        return false;
    });
    _btnDown.click(function(){
        nextSlide();
        return false;
    });

    // gallery init
    if(_currentIndex<0) _currentIndex=0;
    _slider.css({
        position:'relative',
        height:_slideHeight
    });

    /* hide all slide rows except the currentIndex and the two after it */
    _slides.each(function(index, element) {
        var el = $(element);
        if (index >= _currentIndex && index < _currentIndex + 3) {
            el.show();
        }
        var relativeLoc = index - _currentIndex;
        /* move the slide to the "correct" location */
        el.css({
            position:'absolute',
            top:relativeLoc * _rowHeight
        });
    });

    /* only show the text in the middle row */
    _langs.hide().each(function(index, element) {
        var el = $(element);
        var start = _currentIndex + 3;
        var stop = start + 2;
        if (index >= start && index <= stop) {
            el.fadeIn('slow');
        }
    });

    // gallery animation
    function prevSlide() {
        if (_animationsInProgress.length) { return; }
        _oldIndex = _currentIndex;
        if(_currentIndex > 0) _currentIndex--;
        else _currentIndex = _stepsCount-1;
        _direction=0;
        switchSlide();
    }
    function nextSlide() {
        if (_animationsInProgress.length) { return; }
        _oldIndex = _currentIndex;
        if(_currentIndex < _stepsCount-1) _currentIndex++;
        else _currentIndex = 0;
        _direction=1;
        switchSlide();
    }
    function switchSlide() {
        var _nextSlide;
        var slidesInView = [];
        var nextHeight;

    if (_direction) { /* down arrow pressed, moving up (negative) */
        slidesInView.push(_slides.eq(_oldIndex));
        slidesInView.push(_slides.eq(_currentIndex));
        // using %3 will cause these to wrap around to the front,
        // effectively simulating a "ring buffer"
        slidesInView.push(_slides.eq((_currentIndex+1)%_stepsCount));
        /* put the next slide at the end (since it is coming in from
            the bottom) */
        _nextSlide = _slides.eq((_currentIndex+2)%_stepsCount);
        slidesInView.push(_nextSlide);
        nextHeight = _slideHeight;
    } else { /* up arrow pressed, moving down (positive) */
        _nextSlide = _slides.eq(_currentIndex);
        slidesInView.push(_nextSlide);
        slidesInView.push(_slides.eq(_oldIndex));
        slidesInView.push(_slides.eq((_oldIndex+1)%_stepsCount));
        slidesInView.push(_slides.eq((_oldIndex+2)%_stepsCount));
        nextHeight = _rowHeight;
    }
    /* Since the slides are changing position vertically, we must
        adjust the CSS classes of the hover slides */
    _slides.eq(_currentIndex).children('li').each(function(index, slide) {
        $('#' + $(this).attr('name')).toggleClass('position-top', true);
        $('#' + $(this).attr('name')).toggleClass('position-bottom', false);
    });
    _slides.eq((_currentIndex+1)%_stepsCount).children('li').each(function(index, slide) {
        $('#' + $(this).attr('name')).toggleClass('position-top', false);
        $('#' + $(this).attr('name')).toggleClass('position-bottom', false);
            });
    _slides.eq((_currentIndex+2)%_stepsCount).children('li').each(function(index, slide) {
        $('#' + $(this).attr('name')).toggleClass('position-top', false);
        $('#' + $(this).attr('name')).toggleClass('position-bottom', true);
    });

    /* position the next slide either above or below the box 
        (depending on the direction of the slider */
    _nextSlide.css({
        top:nextHeight*(_direction ? 1 : -1)
    }).show();

    /* initialize the animations */
    jQuery.each(slidesInView, function(index, slide) {
        var curTop = parseInt(slide.css('top').replace(/px/,''));
        _animationsInProgress.push(slide);
        slide.animate(
            {top:curTop + (_rowHeight*(_direction ? -1 : 1))},
            {duration: _speed, queue: false,
                complete: function() {
                    /* remove one item from the animation slides in progress.
                        when this is empty, another slide can begin */
                    _animationsInProgress.pop();
                }
            }
        );
        var langs = slide.find('.text');
        /* fade in / out the branding text.  only the middle visible row
            should have text displayed */
        if ((_direction && index == 2) || (!_direction && index == 1)) {
            jQuery.each(langs, function(index, text) {
                $(text).fadeIn('slow');
            });
        } else {
            jQuery.each(langs, function(index, text) {
                $(text).fadeOut('slow');
            });
        }
    });
}

    // gallery tooltips
    _slides.children('.slide').each(function(_ind){
        var _opener = $(this);
        var _popup = _tooltips.eq(_ind);
        var _timer;
        _opener.hover(function(){
            if(_timer) clearTimeout(_timer);
            _timer = setTimeout(function(){
                _popup.fadeIn(_fadeSpeed);
            },_fadeSpeed);
        },function(){
            if(_timer) clearTimeout(_timer);
            _timer = setTimeout(function(){
                _popup.fadeOut(_fadeSpeed);
            },_fadeSpeed)
        });

    _popup.hover(function(){
        if(_timer) clearTimeout(_timer);
    },function(){
        _timer = setTimeout(function(){
            _popup.fadeOut(_fadeSpeed);
        },_fadeSpeed);
    });
});
});
}
// slideshow init
function initSlideShow() {
    $('div.photo-gallery').fadeGallery({
        slideElements:'ul.slideset > li',
        pagerLinks:'div.carousel li',
        autoRotation:false,
        autoHeight:false,
        switchTime:5000,
        duration:300
    });

    $('div.photo-gallery').scrollGallery({
        sliderHolder: 'div.carousel > div',
        btnPrev:'a.link-prev',
        btnNext:'a.link-next',
        circleSlide:true,
        pauseOnHover:true,
        autoRotation:false,
        stopAfterClick:false,
        easing:'swing',
        switchTime:5000,
        duration:650
    });
}

// slideshow plugin
jQuery.fn.fadeGallery = function(_options){
    var _options = jQuery.extend({
        slideElements:'div.slideset > div',
        pagerLinks:'div.pager a',
        btnNext:'a.next',
        btnPrev:'a.prev',
        btnPlayPause:'a.play-pause',
        btnPlay:'a.play',
        btnPause:'a.pause',
        pausedClass:'paused',
        disabledClass: 'disabled',
        playClass:'playing',
        activeClass:'active',
        currentNum:false,
        allNum:false,
        startSlide:null,
        noCircle:false,
        pauseOnHover:true,
        autoRotation:false,
        autoHeight:false,
        switchTime:3000,
        duration:650,
        event:'click'
    },_options);

    return this.each(function(){
        // gallery options
        var _this = jQuery(this);
        var _slides = jQuery(_options.slideElements, _this);
        var _pagerLinks = jQuery(_options.pagerLinks, _this);
        var _btnPrev = jQuery(_options.btnPrev, _this);
        var _btnNext = jQuery(_options.btnNext, _this);
        var _btnPlayPause = jQuery(_options.btnPlayPause, _this);
        var _btnPause = jQuery(_options.btnPause, _this);
        var _btnPlay = jQuery(_options.btnPlay, _this);
        var _pauseOnHover = _options.pauseOnHover;
        var _autoRotation = _options.autoRotation;
        var _activeClass = _options.activeClass;
        var _disabledClass = _options.disabledClass;
        var _pausedClass = _options.pausedClass;
        var _playClass = _options.playClass;
        var _autoHeight = _options.autoHeight;
        var _duration = _options.duration;
        var _switchTime = _options.switchTime;
        var _controlEvent = _options.event;
        var _currentNum = (_options.currentNum ? jQuery(_options.currentNum, _this) : false);
        var _allNum = (_options.allNum ? jQuery(_options.allNum, _this) : false);
        var _startSlide = _options.startSlide;
        var _noCycle = _options.noCircle;

    // gallery init
    var _hover = false;
    var _prevIndex = 0;
    var _currentIndex = 0;
    var _slideCount = _slides.length;
    var _timer;
    if(!_slideCount) return;

    _prevIndex = _slides.index(_slides.filter('.'+_activeClass));
    if(_prevIndex < 0) _prevIndex = _currentIndex = 0;
    else _currentIndex = _prevIndex;
    if(_startSlide != null) {
        if(_startSlide == 'random') _prevIndex = _currentIndex = Math.floor(Math.random()*_slideCount);
        else _prevIndex = _currentIndex = parseInt(_startSlide);
    }
    _slides.hide().eq(_currentIndex).show();
    if(_autoRotation) _this.removeClass(_pausedClass).addClass(_playClass);
    else _this.removeClass(_playClass).addClass(_pausedClass);

    // gallery control
    if(_btnPrev.length) {
        _btnPrev.bind(_controlEvent,function(){
            prevSlide();
            return false;
        });
    }
    if(_btnNext.length) {
        _btnNext.bind(_controlEvent,function(){
            nextSlide();
            return false;
        });
    }
    if(_pagerLinks.length) {
        _pagerLinks.each(function(_ind){
            jQuery(this).bind(_controlEvent,function(){
                if(_currentIndex != _ind) {
                    _prevIndex = _currentIndex;
                    _currentIndex = _ind;
                    switchSlide();
                }
                return false;
            });
        });
    }

    // play pause section
    if(_btnPlayPause.length) {
        _btnPlayPause.bind(_controlEvent,function(){
            if(_this.hasClass(_pausedClass)) {
                _this.removeClass(_pausedClass).addClass(_playClass);
                _autoRotation = true;
                autoSlide();
            } else {
                _autoRotation = false;
                if(_timer) clearTimeout(_timer);
                _this.removeClass(_playClass).addClass(_pausedClass);
            }
            return false;
        });
    }
    if(_btnPlay.length) {
        _btnPlay.bind(_controlEvent,function(){
            _this.removeClass(_pausedClass).addClass(_playClass);
            _autoRotation = true;
            autoSlide();
            return false;
        });
    }
    if(_btnPause.length) {
        _btnPause.bind(_controlEvent,function(){
            _autoRotation = false;
            if(_timer) clearTimeout(_timer);
            _this.removeClass(_playClass).addClass(_pausedClass);
            return false;
        });
    }

    // gallery animation
    function prevSlide() {
        _prevIndex = _currentIndex;
        if(_currentIndex > 0) _currentIndex--;
        else {
            if(_noCycle) return;
            else _currentIndex = _slideCount-1;
        }
        switchSlide();
    }
    function nextSlide() {
        _prevIndex = _currentIndex;
        if(_currentIndex < _slideCount-1) _currentIndex++;
        else {
            if(_noCycle) return;
            else _currentIndex = 0;
        }
        switchSlide();
    }
    function refreshStatus() {
        if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentIndex).addClass(_activeClass);
        if(_currentNum) _currentNum.text(_currentIndex+1);
        if(_allNum) _allNum.text(_slideCount);
        _slides.eq(_prevIndex).removeClass(_activeClass);
        _slides.eq(_currentIndex).addClass(_activeClass);
        if(_noCycle) {
            if(_btnPrev.length) {
                if(_currentIndex == 0) _btnPrev.addClass(_disabledClass);
                else _btnPrev.removeClass(_disabledClass);
            }
            if(_btnNext.length) {
                if(_currentIndex == _slideCount-1) _btnNext.addClass(_disabledClass);
                else _btnNext.removeClass(_disabledClass);
            }
        }
    }
    function switchSlide() {
        _slides.eq(_prevIndex).fadeOut(_duration);
        _slides.eq(_currentIndex).fadeIn(_duration);
        if(_autoHeight) _slides.eq(_currentIndex).parent().animate({height:_slides.eq(_currentIndex).outerHeight(true)},{duration:_duration,queue:false});
        refreshStatus();
        autoSlide();
    }

    // autoslide function
    function autoSlide() {
        if(!_autoRotation || _hover) return;
        if(_timer) clearTimeout(_timer);
        _timer = setTimeout(nextSlide,_switchTime+_duration);
    }
    if(_pauseOnHover) {
        _this.hover(function(){
            _hover = true;
            if(_timer) clearTimeout(_timer);
        },function(){
            _hover = false;
            autoSlide();
        });
    }
    refreshStatus();
    autoSlide();
});
}

// scrolling gallery plugin
jQuery.fn.scrollGallery = function(_options){
    var _options = jQuery.extend({
        sliderHolder: '>div',
        slider:'>ul',
        slides: '>li',
        pagerLinks:'div.pager a',
        btnPrev:'a.link-prev',
        btnNext:'a.link-next',
        activeClass:'active',
        disabledClass:'disabled',
        generatePagination:'div.pg-holder',
        circleSlide:true,
        pauseOnHover:true,
        autoRotation:false,
        stopAfterClick:false,
        switchTime:5000,
        duration:650,
        easing:'swing',
        event:'click',
        vertical:false,
        step:false
    },_options);

    return this.each(function(){
        // gallery options
        var _this = jQuery(this);
        var _sliderHolder = jQuery(_options.sliderHolder, _this);
        var _slider = jQuery(_options.slider, _sliderHolder);
        var _slides = jQuery(_options.slides, _slider);
        var _btnPrev = jQuery(_options.btnPrev, _this);
        var _btnNext = jQuery(_options.btnNext, _this);
        var _pagerLinks = jQuery(_options.pagerLinks, _this);
        var _generatePagination = jQuery(_options.generatePagination, _this);
        var _pauseOnHover = _options.pauseOnHover;
        var _autoRotation = _options.autoRotation;
        var _activeClass = _options.activeClass;
        var _disabledClass = _options.disabledClass;
        var _easing = _options.easing;
        var _duration = _options.duration;
        var _switchTime = _options.switchTime;
        var _controlEvent = _options.event;
        var _step = _options.step;
        var _vertical = _options.vertical;
        var _circleSlide = _options.circleSlide;
        var _stopAfterClick = _options.stopAfterClick;

    // gallery init
    if(!_slides.length) return;
    var _currentStep = 0;
    var _sumWidth = 0;
    var _sumHeight = 0;
    var _hover = false;
    var _stepWidth;
    var _stepHeight;
    var _stepCount;
    var _offset;
    var _timer;

    _slides.each(function(){
        _sumWidth+=$(this).outerWidth(true);
        _sumHeight+=$(this).outerHeight(true);
    });

    // calculate gallery offset
    function recalcOffsets() {
        if(_vertical) {
            if(_step) {
                _stepHeight = _slides.eq(_currentStep).outerHeight(true);
                _stepCount = Math.ceil((_sumHeight-_sliderHolder.height())/_stepHeight)+1;
                _offset = -_stepHeight*_currentStep;
            } else {
                _stepHeight = _sliderHolder.height();
                _stepCount = Math.ceil(_sumHeight/_stepHeight);
                _offset = -_stepHeight*_currentStep;
                if(_offset < _stepHeight-_sumHeight) _offset = _stepHeight-_sumHeight;
            }
        } else {
            if(_step) {
                _stepWidth = _slides.eq(_currentStep).outerWidth(true);
                _stepCount = Math.ceil((_sumWidth-_sliderHolder.width())/_stepWidth)+1;
                _offset = -_stepWidth*_currentStep;
            } else {
                _stepWidth = _sliderHolder.width();
                _stepCount = Math.ceil(_sumWidth/_stepWidth);
                _offset = -_stepWidth*_currentStep;
                if(_offset < _stepWidth-_sumWidth) _offset = _stepWidth-_sumWidth;
            }
        }
    }

    // gallery control
    if(_btnPrev.length) {
        _btnPrev.bind(_controlEvent,function(){
            if(_stopAfterClick) stopAutoSlide();
            prevSlide();
            return false;
        });
    }
    if(_btnNext.length) {
        _btnNext.bind(_controlEvent,function(){
            if(_stopAfterClick) stopAutoSlide();
            nextSlide();
            return false;
        });
    }
    if(_generatePagination.length) {
        _generatePagination.empty();
        recalcOffsets();
        var _list = $('<ul />');
        for(var i=0; i<_stepCount; i++) $('<li><a href="#">'+(i+1)+'</a></li>').appendTo(_list);
        _list.appendTo(_generatePagination);
        _pagerLinks = _list.children();
    }
    if(_pagerLinks.length) {
        _pagerLinks.each(function(_ind){
            jQuery(this).bind(_controlEvent,function(){
                if(_currentStep != _ind) {
                    if(_stopAfterClick) stopAutoSlide();
                    _currentStep = _ind;
                    switchSlide();
                }
                return false;
            });
        });
    }

    // gallery animation
    function prevSlide() {
        recalcOffsets();
        if(_currentStep > 0) _currentStep--;
        else if(_circleSlide) _currentStep = _stepCount-1;
        switchSlide();
    }
    function nextSlide() {
        recalcOffsets();
        if(_currentStep < _stepCount-1) _currentStep++;
        else if(_circleSlide) _currentStep = 0;
        switchSlide();
    }
    function refreshStatus() {
        if(_pagerLinks.length) _pagerLinks.removeClass(_activeClass).eq(_currentStep).addClass(_activeClass);
        if(!_circleSlide) {
            _btnPrev.removeClass(_disabledClass);
            _btnNext.removeClass(_disabledClass);
            if(_currentStep == 0) _btnPrev.addClass(_disabledClass);
            if(_currentStep == _stepCount-1) _btnNext.addClass(_disabledClass);
        }
    }
    function switchSlide() {
        recalcOffsets();
        if(_vertical) _slider.animate({marginTop:_offset},{duration:_duration,queue:false,easing:_easing});
        else _slider.animate({marginLeft:_offset},{duration:_duration,queue:false,easing:_easing});
        refreshStatus();
        autoSlide();
    }

    // autoslide function
    function stopAutoSlide() {
        if(_timer) clearTimeout(_timer);
        _autoRotation = false;
    }
    function autoSlide() {
        if(!_autoRotation || _hover) return;
        if(_timer) clearTimeout(_timer);
        _timer = setTimeout(nextSlide,_switchTime+_duration);
    }
    if(_pauseOnHover) {
        _this.hover(function(){
            _hover = true;
            if(_timer) clearTimeout(_timer);
        },function(){
            _hover = false;
            autoSlide();
        });
    }
    recalcOffsets();
    refreshStatus();
    autoSlide();
});
}

/* this is the left and right quotation callouts in body text */
$(document).ready(function() { 
    $('#content-main span.pull-right').each(function(index) { 
        var $parentParagraph = $(this).parent('p'); 
        $parentParagraph.css('position', 'relative'); 
        $(this).clone() 
            .addClass('pulled-right')
            .prependTo($parentParagraph)
            .prepend("<span class='begin-quotation-mark'>&#8220;</span>")
            .append("<span class='end-quotation-mark'>&#8221;</span>");
    }); 

    $('#content-main span.pull-left').each(function(index) { 
        var $parentParagraph = $(this).parent('p'); 
        $parentParagraph.css('position', 'relative'); 
        $(this).clone() 
            .addClass('pulled-left') 
            .prependTo($parentParagraph)
            .prepend("<span class='begin-quotation-mark'>&#8220;</span>")
            .append("<span class='end-quotation-mark'>&#8221;</span>");
    });    

}); 

// Left navigation menu
function left_nav_display(li_id) {
    // This method is used for the arrow in the left navigation menu
    // When the user clicks the arrow, the menu should open up without
    // going to the page.

    // get the first (and only direct child) 'ul' element
    var li_elem = document.getElementById(li_id);
    var ul_list = li_elem.getElementsByTagName("ul");
    var ul = "";
    if (0 < ul_list.length) {
        ul = ul_list[0];
    }
    if (ul.style.display == 'none') {
        // make the sublist visible
        ul.style.display = '';
        li_elem.className += " open";

    // need to remove the css class closed if present
    li_elem.className = li_elem.className.replace("closed", "");
}
else if (ul.style.display == '') {
    // make the sublist hidden
    ul.style.display = 'none';
    li_elem.className = "plus";

    // if this is an active item then need to add the css class closed
    var a_list = li_elem.getElementsByTagName("a");
    // get the second 'a' element
    if (1 < a_list.length) {
        var a_class = a_list[1].className;
        if (a_class.indexOf("active") != -1) {
            li_elem.className += " closed";
        }
    }   
}
} 


