
/* Fix MSIE6 Image flicker */
var m = document.uniqueID /*IE*/
                && document.compatMode /*>=IE6*/
                && !window.XMLHttpRequest /*<=IE6*/
                && document.execCommand;

try { if (!!m) { m("BackgroundImageCache", false, true) /* = IE6 only */ } } catch (oh) { };



(function($) {

    $.landing = function(options) {
        return $.landing.impl.init(options);
    };

    $.fn.landing = function(options) {
        return $.landing.impl.init(this, options);
    };

    /*
    * default options
    */
    $.landing.defaults = {

};

$.landing.impl = {

    /*
    * options
    */
    opts: null,

    /*
    * helper
    */
    helper: {},

    /*
    * scroll
    */
    scroll: false,

    /*
    * Initialize the page
    */
    init: function(options) {

        var self = this;

        this.opts = $.extend({}, $.landing.defaults, options);

        // helper objects
        self.helper.container = $('div.page-landing');
        self.helper.tray = self.helper.container.find('.tray-container');
        self.helper.backgrounds = self.helper.container.find('ul.background-collection');
        self.helper.list = self.helper.tray.find('ul.trayitem-collection');
        
        // background
        self.background();

        
        return self;
    },
    
    /*
    * Background
    */
    background: function () {
        
        var self = this;
        
        // select random background image
        var count = self.helper.backgrounds.children().length;
        var i = Math.floor(Math.random()*count)
        var $bg = $(self.helper.backgrounds.children()[i]);
        var src = $bg.find('img').attr('src');
        
        $(new Image()).load(function() {
            
            $bg
                .css({
                    display: 'none',
                    opacity: '1.00'
                }) // safari 3.2 hack
                .fadeIn(3000)
                ;
            
            self.initTray();
            
        }).attr('src', src);
        
        return;
    },

    /*
    * Initialize the tray
    */
    initTray: function() {

        var self = $.landing.impl;
        var trayHeight = 200;
        var b = 0;

        self.scroll = !(parseInt($(document).height() - $(window).height()) < $('#footer').outerHeight());

        if (self.scroll) {
            // set the position
            b = self.helper.container.outerHeight() + $('#header').outerHeight() - $(window).height();

            self.helper.tray
		            .css({
		                bottom: b + 'px'
		            })
		            ;
        }

        // show the tray
        self.helper.tray
		        .animate({
		            height: trayHeight + 'px'
		        }, 3500, 'easeOutExpo', initScrollFollow)
		        ;

        // show the tray items
        setTimeout(show, 250);
        function show() {

            self.helper.tray.find('ul.trayitem-collection')
		            .fadeIn(3000, function() {

		                self.events();

		            })
		            ;

        }

        // init the scroll follow
        function initScrollFollow() {

            // skip it if screen is big enough
            if (!self.scroll)
                return;

            self.helper.tray
                    .css({
                        top: self.helper.container.outerHeight() - b - trayHeight + 'px',
                        bottom: 'auto'
                    })
                    .scrollFollow({
                        relativeTo: 'bottom',
                        speed: 1000,
                        easing: 'easeOutExpo',
                        offset: $('#header').outerHeight()
                    })
                    ;

        }

        return;
    },

    /*
    * Events
    */
    events: function() {

        var self = this;

        self.helper.list.children()
		        .hover(
		            function() {
		                $(this).siblings()
		                    .queue('fx', [])
		                    .fadeTo(300, '0.30')
		                    ;
		            },
		            function() {
		                $(this).siblings()
		                    .queue('fx', [])
		                    .fadeTo(300, '1.00')
		                    ;
		            }
		        )
		        ;

        return;
    }

};
})(jQuery);
