/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
var SegeriusBruce ={}

SegeriusBruce.gallery = {

    scrollGallery:0 ,

    init: function() {

        // only do init if carousel exists
        if ($('#gallery')) {
            
            //remove height & width attribiutes
            $('#gallery .wrapper img').removeAttr('width').removeAttr('height');
                        
            // Add click handlers
            $('#gallery .controls .left a').live('click', SegeriusBruce.gallery.scrollPrev);
            $('#gallery .controls .right a').live('click', SegeriusBruce.gallery.scrollNext);
            $("#gallery .controls .item a").live('click',SegeriusBruce.gallery.clickHandler);
            $("#gallery .controls .play a").live('click',SegeriusBruce.gallery.playpause);
            $("#gallery .controls .stop a").live('click',SegeriusBruce.gallery.playpause);

            //initiate the autoscroll
            $("#gallery .controls .play a").click();

            //click hte first item
            $("#gallery .controls .item:first a").click();

        }
    },

    scrollNext:function () {

        var $next;
        if($("#gallery .controls .item.active").next('.item').length != 0 ){
            $next = $("#gallery .controls .item.active").next('.item');
        }
        else{
            $next = $("#gallery .controls .item:first");
        }

        $next.find('a').click();
        return false;

    },

    scrollPrev:function () {

        var $next;
        if($("#gallery .controls .item.active").prev().length != 0 ){
            $next = $("#gallery .controls .item.active").prev();
        }
        else{
            $next = $("#gallery .controls .item:last");
        }

        $next.find('a').click();

        return false;

    },

    playpause:function () {

        $(this).die('click');

        if(SegeriusBruce.gallery.scrollGallery!=0){            
            $(this).parent().removeClass('stop');
            $(this).parent().addClass('play');
            clearInterval(SegeriusBruce.gallery.scrollGallery);
            SegeriusBruce.gallery.scrollGallery = 0;
        }
        else
        {
            $(this).parent().removeClass('play');
            $(this).parent().addClass('stop');
            SegeriusBruce.gallery.scrollGallery = setInterval(SegeriusBruce.gallery.scrollNext, 5000);           
        }

        $("this").live('click',SegeriusBruce.gallery.playpause);
               
        return false;

    },
   
    clickHandler: function() {

        //reset the auto scrolling
        if(SegeriusBruce.gallery.scrollGallery!=0){
            clearInterval(SegeriusBruce.gallery.scrollGallery);
            SegeriusBruce.gallery.scrollGallery = setInterval(SegeriusBruce.gallery.scrollNext, 5000);
        }        

        //Set Variables
        var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
        var imgSrc = $(this).find('img').attr("src"); //Get Main Image URL

        //If the list item is active/selected, then...
        if ($(this).parent().is(".active")) {
            // Don't click through - Prevents repetitive animations on active/selected list-item
            return false;
        } else {
            //Animate
            $('#gallery .wrapper img.front').animate({
                opacity:'hide',
                height:'show'
            },'slow', function() {
                // Animation complete.
                $(this).attr({
                    src: imgSrc ,
                    alt: imgAlt
                });
                $('#gallery .wrapper img.front').animate({
                    opacity:'show',
                    height:'show'
                },'slow');
            });

        }

        //set the background imag eon the ul to make the fade transition smoother
        $('#gallery .wrapper img.back').attr({
            src: imgSrc ,
            alt: imgAlt
        });


        //dim all the thumbnails
        $("#gallery .controls .item").removeClass('active');

        //highlight the item clicked
        $(this).parent().addClass('active');

        return false;

    }

}


$.fn.equalHeights = function() {
    $(this).each(function(){
        var currentTallest = 0;
        $(this).children().each(function(i){
            if ($(this).height() > currentTallest) {
                currentTallest = $(this).height();
            }
        });

        // for ie6, set height since min-height isn't supported
        if ($.browser.msie && $.browser.version == 6.0) {
            $(this).children().css({
                'height': currentTallest
            });
        }
        $(this).children().css({
            'height': currentTallest
        });
    });
    return this;
};

$(document).ready(function() {

    SegeriusBruce.gallery.init();

    //set the  heights on the grid columns & revoce the height on the hero
    //$('.loop ul').not('.loop ul ul').not('.sidebaox .loop ul').equalHeights();
    
});
