
// PIPER

var piper = {
    
    vars:       {
        slideshow : false
    },
    
    elements:   {},
    
    init: function()
    {
        this.setupVars();
        this.setupElements();
        this.setupImages();
        this.slideshowInit();
    },
    
    setupVars: function()
    {
    },
    
    setupElements: function()
    {
        this.elements.bkgImages = $("#background_image img");
        //alert(this.elements.bkgImages.length);
    },
    
    setupImages: function()
    {
        piper.resizeImages();
        
        $(window).resize(function(){
            piper.resizeImages();
        });
    },
    
    resizeImages: function()
    {
        var body        = $("body");
        var bodyWidth   = $(body).width();
        var bodyHeight  = $(body).height();
        
        for(var i = 0; i < this.elements.bkgImages.length; i++)
        {
            var image = $(this.elements.bkgImages[i]);
            //var imageHeight = $(image).height();
            var imageHeight = $(image).data("originalHeight");
                
            if(typeof imageHeight == "undefined")
            {
                imageHeight = $(image).height();
                $(image).data("imageHeight", imageHeight);
            }
            
            var currentHeight = (imageHeight >= bodyHeight)? imageHeight : bodyHeight;
            $(image).css("height", currentHeight);
            
            if(imageHeight > bodyHeight)
            {
               var top = Math.floor(((imageHeight - bodyHeight) / 2) * -1);
               $(image).css("top", top);
            }
        }
    },
    
    slideshowInit: function()
    {
        if(this.vars.slideshow && this.elements.bkgImages.length > 1)
        {
            this.vars.slideshowLoop = new Loop(0, this.elements.bkgImages.length - 1);
            for(var i = 0; i < this.elements.bkgImages.length; i++)
            {
                if(i > 0)
                {
                    $(this.elements.bkgImages[i]).css('visibility', 'hidden');
                }
            }
            piper.slideshowSetTimeout();
        }
    },
    
    slideshowSetTimeout: function()
    {
        setTimeout(function(){
            piper.slideshowToggleImages();
        }, 6000);
    },
    
    slideshowToggleImages: function()
    {
        var index = this.vars.slideshowLoop.index;
        var nextIndex = this.vars.slideshowLoop.next(false);
        $(this.elements.bkgImages[index]).css("visibility", "hidden");
        $(this.elements.bkgImages[nextIndex]).css("visibility", "visible");
        this.vars.slideshowLoop.next();
        this.slideshowSetTimeout();
    }
    
}

// PHOTOGALLERY

var photogallery = {
    
    vars            : {
        currentIndex    : null
    },
    
    elements        : {},
    
    init: function()
    {
        
        // images
        this.elements.images = $('img.photogallery');
        //this.alignImages();
        
        // thumbs
        this.elements.thumbs = $('img.photogallery_thumb');
        $(this.elements.thumbs).css("opacity", .5);
        if(this.elements.thumbs.length > 0)
        {
            this.toggleImages("photogallery_thumb_0");
        }
        
        // events
        $(window).resize(function(){photogallery.alignImages()});
        $(this.elements.thumbs).click(function(){ photogallery.toggleImages($(this).attr("id")) });
        
    },
    
    alignImages: function()
    {
        this.vars.documentHeight = $(document).height();
        for(var i = 0; i < this.elements.images.length; i++)
        {
            //$(this.elements.images).css("");
            var imageHeight = $(this.elements.images[i]).height();
            if(imageHeight > this.vars.documentHeight)
            {
                var difference = imageHeight - this.vars.documentHeight;
                var marginTop = Math.round(difference / 2) * -1;
                $(this.elements.images[i]).css("top", marginTop);
            }
        }
    },
    
    toggleImages: function(id)
    {
        index = parseInt(id.replace("photogallery_thumb_", ""));
        $("#"+id).css({"opacity": 1, "border-color" : "#666"});
        if(index != this.vars.currentIndex)
        {
            $(this.elements.images[index]).css({"opacity" : 1, "z-index": 1});
            if(typeof this.vars.currentIndex == "number")
            {
                $(this.elements.images[this.vars.currentIndex]).css({"opacity" : 1, "z-index": 0});
                $(this.elements.thumbs[this.vars.currentIndex]).css({"opacity": .5, "border-color" : "#333"});
            };
        };
        this.vars.currentIndex = index;
    }
    
}

// cufon

Cufon.replace('h2');
Cufon.replace('h3');
Cufon.replace('#language', {"hover" : true});

// preload

var bkgImages = [];
var bkgImagesPhotogallery = false;
var loadedImages = 0;

$(document).ready(function(){
    
    Cufon.now();
    
    //for(image in bkgImages)
    for(var i = 0; i < bkgImages.length; i++)
    {
        var attributes = (bkgImagesPhotogallery)? {'src' : bkgImages[i], 'class' : 'photogallery'} : {'src' : bkgImages[i]};
        $('<img />')
            .attr(attributes)
            //.click(function(){alert("ok")})
            .css('opacity', 0)
            .load(function(){
                $('#background_image').append( $(this) );
                $(this).animate({'opacity' : 1}, {"duration" : 500, "easing" : "swing"});
                
                loadedImages++;
                if(loadedImages == bkgImages.length)
                {
                    piper.init();
                }
            });
    }
    
});
