﻿
App = function () {

    /* Private */

    /* Properties */

    var cmp = {};





    /* Defaults */

    TVI.debug = false;





    /* Methods */

    var init = function () {


        /* Nice #link scrolling */

        $('a[href^=#]').live('click', function () {

            if ($(this).attr('href') == '#') {
                return true;
            }

            if (location.pathname.replace(/^\//, '') == this.pathname.replace(/^\//, '') && location.hostname == this.hostname) {

                var $target = $(this.hash);

                $target = $target.length && $target || $('[name=' + this.hash.slice(1) + ']');

                if ($target.length) {

                    var targetOffset = $target.offset().top;

                    $('html,body').animate({ scrollTop: targetOffset }, 1000, 'easeOutQuint', function () {
                        return false;
                    });

                }
            }
        });


        // CUFON
        Cufon.replace('h2');

        // Search Form
        cmp.searchForm = new TVI.Form({

            ID: 'searchForm',
            validate: false,
            local: true,
            success: function () {
                window.location.href = '/search.aspx?keywords=' + cmp.searchForm.field('keyword').val();
            }

        });      

        /* TVI-BUTTON */
        TVI.Components.Button.styles.defaultStyle = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 4,
            rightPadding: 5,
            hoverClass: 'buttonHover',
            activeClass: 'buttonActive'
        };

        TVI.Components.Button.styles.blueLong = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 32,
            rightPadding: 5,
            hoverClass: 'buttonHoverBlueLong',
            activeClass: 'buttonActiveBlueLong'
        };

        TVI.Components.Button.styles.greyLong = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 4,
            rightPadding: 5,
            hoverClass: 'buttonHoverGreyLong',
            activeClass: 'buttonActiveGreyLong'
        };

        TVI.Components.Button.styles.greyReserve = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 32,
            rightPadding: 5,
            hoverClass: 'buttonHoverGreyReserve',
            activeClass: 'buttonActiveGreyReserve'
        };

        TVI.Components.Button.styles.blueWishlist = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 32,
            rightPadding: 5,
            hoverClass: 'buttonHoverBlueWishlist',
            activeClass: 'buttonActiveBlueWishlist'
        };

        TVI.Components.Button.styles.blueCheckout = {
            leftEndWidth: 4,
            leftPadding: 5,
            rightEndWidth: 34,
            rightPadding: 5,
            hoverClass: 'buttonHoverBlueCheckout',
            activeClass: 'buttonActiveBlueCheckout'
        };

        /* Custom DDL */
        TVI.Components.CustomDDL.styles.defaultStyle = {
            buttonLeftEndWidth: 2,
            buttonLeftPadding: 9,
            buttonRightEndWidth: 22,
            buttonRightPadding: 0,
            panelBorderWidth: 1,
            hoverClass: 'TVI-customDDL-hover'
        };


        TVI.Components.initialize($('BODY'));

        var logo = $('#logo');

        // Set the logo background starting position
        setLogoBG(logo);

        //Start the logo background loop
        setInterval(function () { App.scrollBg(logo); }, 70);

    };


    // Top Menu Nav
    var menu = function (name) {
        TVI.ready(function () {

            $('#nav' + name).children('a').addClass('current');

        });
    };


    function get_cookie(cookie_name) {
        var results = document.cookie.match('(^|;) ?' + cookie_name + '=([^;]*)(;|$)');

        if (results)
            return (unescape(results[2]));
        else
            return null;
    }

    var current = 0; //The current position
    var restartPosition = -(847 - 128); //The pixel row where to start a new loop (background width - logo width)

    var setLogoBG = function (logo) {
        var cookieCurrent = get_cookie('logoposition');
        if (cookieCurrent != null) {
            current = cookieCurrent;
            logo.css("background-position", current + "px 0");
            var expDate = new Date();
            expDate.setDate(expDate.getDate() - 1);
            document.cookie = 'logoposition=' + current + '; expires=' + expDate.toGMTString();

        }
    }

    var cookie = "";

    var scrollBg = function (logo) {

        //Set scroll speed (pixels per interval)
        current -= 1;

        //If at the end of the image go to the beginning.
        if (current < restartPosition) {
            current = 0;
        }

        //Set the CSS of the header.
        logo.css("background-position", current + "px 0");

        if (current % 20 == 0) {
            var expDate = new Date();

            //  Expire previous cookie
            expDate.setDate(new Date().setYear(1970));
            document.cookie = cookie + expDate.toGMTString();

            // Set new cookie
            expDate.setDate(expDate.getDate() + 5);
            document.cookie = 'logoposition=' + current + '; expires=' + expDate.toGMTString();

            // Remember cookie so that we can expire it next time this runs!
            cookie = 'logoposition=' + current + '; expires=';

        }

    }

    /* Public */

    TVI.apply(cmp, {

        menu: function (name) {

            menu(name);

        },
        scrollBg: function (logo) {

            scrollBg(logo);

        }

    });


    TVI.ready(init);


    return cmp;


} ();
