(function($) {

    $(function() {

        // Rotators
        (function() {

            // Product Rotator

            // The transition fx.
            var fx = 'scrollLeft';

            // The inactive and active dot urls.
            var inactiveDotUrl = WP.template_url + "/images/inactive-dot.png",
                activeDotUrl = WP.template_url + "/images/active-dot.png";

            var dotContainer = $(".dot-container");
            var slides = $(".sub-header #slides");

            // Create as many dots as there are slides.
            slides.children().each(function() {
                $("<img>", {
                    src: inactiveDotUrl,
                    alt: "Dot"
                }).appendTo($("<a>", {
                    href: "#"
                }).appendTo(dotContainer));
            });

            // Find all the created dots.
            var dots = dotContainer.find("img");

            // Moves the dot to correspond with the current slide.
            var dotHandler = function(current, next) {
                var offset = $(next).prevAll().size();
                dots.attr("src", inactiveDotUrl);
                dots.eq(offset).attr("src", activeDotUrl);
            };

            // Start the cycle plugin.
            slides.cycle({
                fx: fx,
                after: dotHandler,
                cleartypeNoBg: true
            });

            // This is for the awesomeness that is IE 7.
            slides.cycle("pause");
            slides.children().eq(0)
            .css({
                width: 810,
                height: 220
            });
            slides.cycle("resume");

            // Bind navigation controls to each dot.
            dotContainer.find("a").click(function() {
                var offset = $(this).prevAll().size();
                slides.cycle("stop");
                slides.cycle({
                    fx: fx,
                    after: dotHandler,
                    startingSlide: offset,
                    cleartypeNoBg: true
                });
                slides.cycle("stop");
                return false;
            });

            // Client Rotator
            var clients = $(".stripe-content #clients-rotator");
            clients.cycle({
                fx: 'fade',
                cleartypeNoBg: true
            });

            // iTAM Link Landing Page Rotator
            var itamlinkSlides = $("#itamlink-slides-rotator");
            itamlinkSlides.cycle({
                fx: 'fade',
                cleartypeNoBg: true
            });
        
        })();

        // Contact Us
        (function() {

            var DEFAULT_NAME = 'Your Name',
                DEFAULT_COMPANY = 'Your Company',
                DEFAULT_EMAIL_ADDRESS = 'Email Address',
                DEFAULT_PHONE_NUMBER = 'Phone Number',
                DEFAULT_HEAR_ABOUT_US = 'How Did You Hear About Us?',
                DEFAULT_COMMENTS = 'Comments';

            var form = $('#contact-box-container form'),
                name = form.find('input[name=contact-name]'),
                company = form.find('input[name=company]'),
                emailAddress = form.find('input[name=email-address]'),
                phoneNumber = form.find('input[name=phone-number]'),
                hearAboutUs = form.find('select[name=how-did-you-hear-about-us]'),
                comments = form.find('textarea[name=comments]'),
                submit = form.find('input[type=submit]');            

            var handleDefaultValue = function(el, defaultValue) {
                el.focus(function() {
                    var el = $(this);
                    if (el.val() === defaultValue) el.val('');
                }).blur(function() {
                    var el = $(this);
                    if (el.val() === '') el.val(defaultValue);
                });
            };

            handleDefaultValue(name, DEFAULT_NAME);
            handleDefaultValue(company, DEFAULT_COMPANY);
            handleDefaultValue(emailAddress, DEFAULT_EMAIL_ADDRESS);
            handleDefaultValue(phoneNumber, DEFAULT_PHONE_NUMBER);            
            handleDefaultValue(comments, DEFAULT_COMMENTS);
            
            var validateValue = function(el, defaultValue) {
                if (el.val() === defaultValue || el.val() === '') {
                    el.addClass("highlight");
                    return false;
                } else {
                    el.removeClass("highlight");
                    return true;
                }
            };

            form.submit(function() {                
                var validated = !!(1
                    & validateValue(name, DEFAULT_NAME)
                    & validateValue(company, DEFAULT_COMPANY)
                    & validateValue(emailAddress, DEFAULT_EMAIL_ADDRESS)
                    & validateValue(phoneNumber, DEFAULT_PHONE_NUMBER)
                    & validateValue(hearAboutUs, DEFAULT_HEAR_ABOUT_US));

                if (!validated) return false;
                
                submit.fadeTo('normal', 0.3);
                form.find(":input").attr("readonly", "readonly");
                $.post(WP.url + "/contact-ajax", form.serialize(), function() {
                    alert("Submission complete.");
                    form.find(':input').val('').removeAttr("readonly").blur();
                    submit.clearQueue().fadeTo('normal', 1);
                });

                return false;
            });

        })();

        // Comparision Table
        (function() {

            $("table")
                .find("tr:odd").addClass("odd").end()
                .find("tr:even").addClass("even");

            $("table.comparison td").each(function() {
                var cell = $(this);
                switch (cell.text().toUpperCase()) {
                    case "YES": cell.html("&#10003;").addClass("check-or-x"); break;
                    case "NO": cell.html("&#10005;").addClass("check-or-x"); break;
                }
            });

            $("table th.featured").each(function() {
                var header = $(this);
                var offset = header.prevAll().size() + 1;
                var parent = header.parents("table");
                var column = parent.find("tr > :nth-child(" + offset + ")")
                column.addClass("featured");
            });

        })();

        // FAQ
        (function() {

            // Determine if this is a FAQ page.
            var title = $('.page-title').text().toLowerCase();            
            if (title.indexOf("faq") < 0 && title.indexOf("f.a.q.") < 0
                && title.indexOf("frequently asked questions") < 0) {                
                return;
            }
            
            var orderedList = $("ol").eq(0);
            var tableOfContents = $("<ol class='toc'>").insertBefore( orderedList );

            var items = orderedList.find("li");
            items
                .wrapInner($("<a>"))
                .find("a")
                .each(function(i) {
                    $(this).attr("name", "question_" + (i + 1));
                });

            items.each(function(i) {
                $("<li>")
                    .append($("<a>", {
                        href: "#question_" + (i + 1),
                        text: $(this).find("strong").text()
                    }))
                    .appendTo( tableOfContents );
            });

        })();

    });

})(jQuery);


