/*
 * 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 (jQuery('#portfolio-gallery')) {

            // Add controls
            jQuery('#portfolio-content .gallery a:first')
            .before('<span class="slideshow_control">play</span>')
            
            jQuery('#portfolio-gallery div.container')
            .before('<span class="button left"><span>Navigate to prev item</span></span>')
            .after('<span class="button right"><span>Navigate to next item</span></span>')

            //Dim all the scroll buttons
            $("span.slideshow_control").css({
                opacity: 0.5
            });

            //Dim all the scroll buttons
            $("span.button").css({
                opacity: 0.5
            });

            //remove height & width attribiutes
            $('.gallery img').removeAttr('width').removeAttr('height');

            //Dim all the thumbs
            $(".gallery a").css({
                opacity: 0.5
            });

            //Add Hover handlers
            $(".gallery a").hover(SegeriusBruce.gallery.hoverInHandler, SegeriusBruce.gallery.hoverOutHandler);
            $("span.button").hover(SegeriusBruce.gallery.hoverInHandler, SegeriusBruce.gallery.hoverOutHandler);
            $("span.slideshow_control").hover(SegeriusBruce.gallery.hoverInHandler, SegeriusBruce.gallery.hoverOutHandler);


            // Add click handlers
            jQuery('#portfolio-gallery span.left').bind('click', SegeriusBruce.gallery.scrollPrev);
            jQuery('#portfolio-gallery span.right').bind('click', SegeriusBruce.gallery.scrollNext);
            jQuery(".gallery a").bind('click',SegeriusBruce.gallery.clickHandler);
            jQuery("span.slideshow_control").bind('click',SegeriusBruce.gallery.playpause);

            //initiate the autoscroll
            jQuery("span.slideshow_control").click();

            //click hte first thumbnail
            $(".gallery a:first").click();

        }
    },

    hoverInHandler:function(){
        //Change the opacity on hover
        $(this).animate({
            opacity: 1
        });
    },

    hoverOutHandler:function(){
        //restore the opacity on hover
        if (!$(this).hasClass('active')){
            $(this).animate({
                opacity: 0.5
            });
        }
    },

    scrollNext:function () {

        var $next;
        if($(".gallery a.active").next().length != 0 ){
            $next = $(".gallery a.active").next();
        }
        else{
            $next = $(".gallery a:first");
        }

        $next.click();
        return false;

    },

    scrollPrev:function () {

        var $next;
        if($(".gallery a.active").prev().length != 0 ){
            $next = $(".gallery a.active").prev();
        }
        else{
            $next = $(".gallery a:last");
        }

        $next.click();

        return false;

    },

    playpause:function () {

        if(SegeriusBruce.gallery.scrollGallery!=0){
            $(this).text('play');
            clearInterval(SegeriusBruce.gallery.scrollGallery);
            SegeriusBruce.gallery.scrollGallery =0;
            $("span.button").fadeIn('fast');
        }
        else
        {
            $(this).text('pause');
            SegeriusBruce.gallery.scrollGallery = setInterval(SegeriusBruce.gallery.scrollNext, 5000);
            $("span.button").fadeOut('fast');
        }
               
        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).is(".active")) {
            // Don't click through - Prevents repetitive animations on active/selected list-item
            return false;
        } else {
            //Animate
            $('#portfolio-gallery .container img').fadeOut('slow', function() {
                // Animation complete.
                $(this).attr({
                    src: imgSrc ,
                    alt: imgAlt
                });
                $('#portfolio-gallery .container img').fadeIn('slow', function(){
                    //resize the prev/next buttons to the same height as the image
                    //var $height = $('#portfolio-gallery .container img').height()-20;
                    //console.log('height',$height);
                    //$("span.button").height($height);
                    });
            });

        }

        //set the background imag eon the ul to make the fade transition smoother
        $('#portfolio-gallery .container').css('background','url("' + imgSrc + '") top left');


        //dim all the thumbnails
        $(".gallery a").removeClass('active').animate({
            opacity: 0.5
        });

        //highlight the thumb clicked
        $(this).addClass('active').animate({
            opacity: 1
        });
        return false;

    }

}

SegeriusBruce.carousel = {

    scrollCarousel:0 ,

    init: function() {

        // only do init if carousel exists
        if ($('#carousel')) {

            // Add controls
            $('#carousel div.container ul')
            .before('<span class="button left"><span>Navigate to prev item</span></span>');
            $('#carousel div.container ul')
            .after('<span class="button right"><span>Navigate to next item</span></span>');

            // Add click handlers
            $('#carousel span.right').bind('click', SegeriusBruce.carousel.scrollNext);
            $('#carousel span.left').bind('click', SegeriusBruce.carousel.scrollPrev);

            SegeriusBruce.carousel.scrollCarousel = setInterval(SegeriusBruce.carousel.scrollNext, 5000);

            //Hide all items
            $('#carousel ul li').hide();
            $('#carousel ul li:first').addClass('active').show();

        }
    },
  
    scrollNext:function () {

        clearInterval(SegeriusBruce.carousel.scrollCarousel);
        
        var $this = $("#carousel ul li.active")
        var $next;
        
        if($this.next().length != 0 ){
            $next = $this.next();
        }
        else{
            $next = $("#carousel ul li:first");
        }

      
        //dim all the items
        $this .removeClass('active').fadeOut('slow');
              
        //highlight the thumb clicked
        $next.addClass('active').delay(400).fadeIn();

        var $img =  $next.find('img').attr('src');
        //set the background imag eon the ul to make the fade transition smoother
        $('#carousel ul').css('background','url("' + $img + '") top left');

        SegeriusBruce.carousel.scrollCarousel = setInterval(SegeriusBruce.carousel.scrollNext, 5000);

        return false;

    },

    scrollPrev:function () {

        clearInterval(SegeriusBruce.carousel.scrollCarousel);
       
        var $prev;
        var $this = $("#carousel ul li.active")

        if($this.prev().length != 0 ){
            $prev = $this.prev();
        }
        else{
            $prev = $("#carousel ul li:last");
        }

        //dim all the items
        $this .removeClass('active').fadeOut('slow');

        //highlight the thumb clicked
        $prev.addClass('active').delay(400).fadeIn();

        var $img =  $prev.find('img').attr('src');
        //set the background imag eon the ul to make the fade transition smoother
        $('#carousel ul').css('background','url("' + $img + '") top left');

        SegeriusBruce.carousel.scrollCarousel = setInterval(SegeriusBruce.carousel.scrollPrev, 5000);

        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;
};

jQuery(document).ready(function() {

    SegeriusBruce.carousel.init();
    SegeriusBruce.gallery.init();

    //set the  heights on the grid columns & revoce the height on the hero
    $('.post-excerpt').equalHeights();
    var $panel = $('.post-excerpt li.grid_12')
    $panel.css('height','');



    //Accordian
     //Portfolio accordian
        lastBlock = $("#accordian li:first-child a");
        maxWidth = 695;
        minWidth = 45;

        lastBlock.animate({width: maxWidth+"px"}, { queue:false, duration:400});

        $("#feature-content #accordian li a").hover(
        function(){
            $(lastBlock).animate({width: minWidth+"px"}, { queue:false, duration:400});
            $(this).animate({width: maxWidth+"px"}, { queue:false, duration:400});
            lastBlock = this;
        });

});
