var ce = {
    /**
     * Setup elements used on every page
     */
    initPage: function() {
        ce._initNavigation();
        ce._initBackground();
        ce._initSlideshow();
        ce._initMarketData();
    },

    /**
     * Setup contact page
     */
    initPageContact: function() {
        // Add onFocus effects to contact form
        jQuery("#contact-form input[type=text], #contact-form textarea").each(function(index, el) {
            var $el = jQuery(el);

            if ($el.data('default') == undefined) {
                $el.data('default', $el.val());
            }

            $el.focus(function() {
                if ($el.val() == $el.data('default')) {
                    $el.val('');
                }
            }).blur(function() {
                if ($el.val() == '') {
                    $el.val($el.data('default'));
                }
            });
        });
    },

    /**
     * Setup drop-down menus for main navigation
     */
    _initNavigation: function() {
        jQuery("#nav-main > ul > li").hover(function() {
            jQuery(this).find("ul").show();
            jQuery(this).children("a").addClass('hover');
        }, function() {
            jQuery(this).find("ul").hide();
            jQuery(this).children("a").removeClass('hover');
        });
    },

    /**
     * Add the body bg image and make it scale with the page
     */
    _initBackground: function() {
        // Pad content if it's too short so the white bg always reaches the bottom of the browser
        var viewportHeight = jQuery(window).height();
        var contentHeight = jQuery("#wrapper").height();

        if (contentHeight < viewportHeight) {
            jQuery("#footer > .inner").css('padding-bottom', parseInt(jQuery("#footer > .inner").css('padding-bottom')) + (viewportHeight - contentHeight) + 'px');
        }

        if (typeof urlBg != 'undefined') {
            jQuery.backstretch(urlBg);
        }
    },

    /**
     * Setup the slideshow located below the header on every page
     */
    _initSlideshow: function() {
        /*var urlImgCollapse = '/assets/images/btnCollapse.png';
        var urlImgExpand = '/assets/images/btnExpand.png';
        var $btnCollapse = jQuery("#slideshow .collapse a");
        var collapsedHeight = 16;*/

        var startingSlide = 0;

        // If this page has defined a slide variable
        // we'll start the slideshow on that slide'
        if (typeof slide != 'undefined' && jQuery("#slide-" + slide)) {
            jQuery(".slide").each(function(index, el) {
                el = jQuery(el);
                if (el.attr('id') == 'slide-' + slide) {
                    startingSlide = index;
                    return;
                }
            });
        }

        // Setup the scrolling slides
        jQuery('#slideshow .slides').cycle({
            fx:     'scrollDown',
            speed: 500,
            delay: 3000,
            timeout: 9000,
            pager: '#nav-slideshow',
            cleartype: false,
            startingSlide: startingSlide
        });

        // Expand/collapse slideshow
        /*jQuery("#slideshow .collapse a").click(function(e) {
            e.preventDefault();

            $sh = jQuery("#slideshow");

            if (!$sh.data('originalHeight')) {
                $sh.data('originalHeight', $sh.height());
            }

            // Collapse
            if (parseInt($sh.css('height')) >= $sh.data('originalHeight')) {
                jQuery("#slideshow .slides").cycle('pause');

                $sh.animate({
                        height: collapsedHeight + 'px',
                        marginTop: ($sh.data('originalHeight') - collapsedHeight) + 'px'
                    },
                    500,
                    function() {
                        jQuery("#slideshow .slides").hide();
                    }
                );

                $btnCollapse.css('background-image', 'url(' + urlImgExpand + ')');
            }
            // Expand
            else {
                jQuery("#slideshow .slides").show();

                $sh.animate({
                        height: $sh.data('originalHeight') + 'px',
                        marginTop: 0
                    },
                    500,
                    function() {
                        jQuery("#slideshow .slides").cycle('resume');
                    }
                );

                $btnCollapse.css('background-image', 'url(' + urlImgCollapse + ')');
            }
        });*/
    },

    /**
     * Setup the market data scroller
     */
    _initMarketData: function() {
        jQuery.ajax({
            url: '/marketData.json',
            dataType: 'json',

            //error: function() { jQuery("#market-data .data").html('An error occurred loading recent market data.');}
            error: function() { jQuery("#market-data .data").html('');}

            // Debug helping code
            // Comment out existing error proto and use this one for a help figuring problems.
            //error:function (xhr, ajaxOptions, thrownError){alert(xhr.responseText);alert(thrownError); }
            ,
            success: function(data) {
                var items = [];
                var i = 0;

                items.push('<li>' + data[0].Exchange);
                items.push('&nbsp;closing&nbsp;prices&nbsp;' + data[0].Symbol);
                items.push('&nbsp;::&nbsp</li>');

                data = data.slice(1);  //Slice off the first item in the jQuery object. We used it above.

                jQuery.each(data, function(key, val) {

                    var liClass = '';

                    if (val.Change > 0) {
                        liClass = 'positive';
                    } else if (val.Change < 0) {
                        liClass = 'negative';
                    }
                    items.push('<li class="' + liClass + '">');
                    items.push('<span class="symbol">' + val.Symbol + '</span>');
                    items.push('<span class="price">&nbsp;$' + val.Price + '</span>');
                    items.push('<span class="percent-change">&nbsp;(' + val.Change + ')</span>');
                    items.push('&nbsp;.&nbsp;.&nbsp;.&nbsp;.&nbsp;');
                    items.push('</li>');

                    i++;
                });


                jQuery("#market-data .data").html(jQuery('<ul/>', {
                    id: 'scroller',
                    html: items.join('')
                }));

                jQuery("#scroller").liScroll({
					travelocity: .03
                    //travelocity: .93
                });
            }
        });
    }
};

jQuery(document).ready(function() {
    ce.initPage();
});
