/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

(function($) {
    var defaults = {};

    $.fn.postgallery = function(settings) {
        // Initialize the effect
        $.extend(this, defaults, settings);

        //if there are more than 4 images, then move them into the gallery
        //otherwise hide the gallery
        if($('img.gallery', this).size()<=4 ){
            $('#sidebox-gallery').hide();
            $('#post-gallery').hide();

            $('img', this).each(function() {
                $(this).removeAttr('width')
                .removeAttr('height')
                .css('max-width','600px')

            });
        }
        else {
            
            //clean all <br> tags
            //$('.post-content br').remove();
                     
            //move images to sidebar
            //remove height & width attribiutes
            //wrap in anchor tag with thumb class
            $('img.gallery', this).each(function() {
                $(this).appendTo('#sidebox-gallery ul')
                .removeAttr('width')
                .removeAttr('height')
                .wrap('<li><a href="' + $(this).attr('src') + '"></a><div class="block">' +  $(this).attr('title')  + '</div></li>')
            });

            // Show the Gallery
            $('#sidebox-gallery').show();
            $('#post-gallery').show();

            //Show the Slideshow
            $(".main_image .desc").show(); //Show Banner
            $(".main_image .block").animate({
                opacity: 0.33
            }, 1 ); //Set Opacity

            //set the default opacity to 75%
            $(".image_thumb ul li").animate({
                opacity: 0.5
            });

            //Change the opacity on hover
            $(".image_thumb ul li").hover(
                function () {
                    $(this).addClass("hover");
                    $(this).animate({
                        opacity: 1
                    });
                },
                function () {
                    if (!$(this).hasClass('active')){
                        $(this).animate({
                            opacity: 0.5
                        });
                    }
                }
                );
  
            //Load the image on click
            $(".image_thumb ul li").click(function(){
                //Set Variables
                var imgAlt = $(this).find('img').attr("alt"); //Get Alt Tag of Image
                var imgTitle = $(this).find('a').attr("href"); //Get Main Image URL
                var imgDesc = $(this).find('.block').html();  //Get HTML of the "block" container
                
                if ($(this).is(".active")) {  //If the list item is active/selected, then...
                    return false; // Don't click through - Prevents repetitive animations on active/selected list-item
                } else { //If not active then...
                    //Animate the Description
                    $(".main_image .block").animate({
                        opacity: 0
                    }, 'slow' , function() { //Pull the block down (negative bottom margin of its own height)
//                        Show the caption block
//                        $(".main_image .block").html(imgDesc).animate({
//                            opacity: 0.33,
//                            marginBottom: "-60px"
//                        }, 'slow' ); //swap the html of the block, then pull the block container back up and set opacity

                        $('.main_image img').fadeOut('slow', function() {
                            // Animation complete.
                            $(".main_image img").attr({
                                src: imgTitle ,
                                alt: imgAlt
                            }); //Switch the main image (URL + alt tag)
                        });

                        $('.main_image img').fadeIn('slow');
                        
                    });
                }
                //Show active list-item
                $(".image_thumb ul li").removeClass('active').animate({
                    opacity: 0.5
                }); //Remove class of 'active' on all list-items
                $(this).addClass('active').animate({
                    opacity: 1
                });
                return false;

            }) .hover(function(){ //Hover effects on list-item
                $(this).addClass('hover'); //Add class "hover" on hover
            }, function() {
                $(this).removeClass('hover'); //Remove class "hover" on hover out
            });


            $(".main_image img").click(function(){
                $(".image_thumb ul li.active").next().click();
            });
             
            $(".image_thumb ul li:first").click();
        }
        return this;
    };
})(jQuery);

$.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;
};


