﻿var $j = jQuery.noConflict();

//Runs when document is ready to be manipulated
$j(document).ready(function () {
    //code to set last link width in main nav
    //    if ($j('.main-nav').length) {
    //        var li = $j('#mainnav').children();
    //        for (var i = 0; i < li.length; i++) {
    //            var a = $j(li[i]).children().filter('a');
    //            if ($j(a).hasClass('last')) {
    //                var ulWidth = $j('#mainnav').width();
    //                var siteWidth = $j('#Wrapper').width();
    //                var difference = (siteWidth - ulWidth) - 1;
    //                var aWidth = $j(a).width();
    //                $j(a).css("width", aWidth + difference + "px");
    //                $j(a).css("text-align", "center");
    //            }
    //        }
    //    }

    //Function to remove "Search" text from site search button
    if ($j('#Site-Search').length) {
        $j('.searchButton').val('');
        $j('.searchField').val('search site');
    }

    //Function to remove "Sign Up" text from newsletter sign up button
    if ($j('#Newsletter').length) {
        $j('.signupButton').val('Sign Up');
        $j('.signupField').val('enter email');
    }

    //Code to grab the top nav and through it into the bottom nav section
    if ($j('#Bottom-Nav').length) {
        $j('.main-nav').clone().prependTo('#Bottom-Nav');
    }

    //Removes border from last li element in the toplevel main nav
    if ($j('.main-nav').length) {
        var li = $j('.main-nav > ul').find('li:last');

        $j(li).css('border', '0px');
    }

    //Sets the content header section text to that of the current 
    //page section title.
    if ($j('#Default').length) {
        var links = $j('#mainnav').children().find('a.current');

        if ($j(links).length > 0) {
            $j('#Section-Title').text($j(links[0]).text());
        }
    }

    $j('input.datepicker[type=text]').datepicker();


    //Changing "Our Price" and "MSRP" on product details page to
    //"Retail" and "Sale"
    if ($j('.product-name').length) {
        var salePrice = $j('.our-price').text().split(":");
        var retailPrice = $j('.msrp-price').text().split(":");
        var productName = $j('.product-name > h1').text();

        retailPrice[0] = 'Retail';
        salePrice[0] = 'Ministry Price';

        $j('.our-price').text(salePrice[0] + ':' + salePrice[1]);
        $j('.msrp-price').text(retailPrice[0] + ':' + retailPrice[1]);

        $j('#Section-Title').text(productName);
        //console.log(retailPrice);
        //console.log(salePrice);
        //console.log(productName);
    }

    //Code to remove member register from login page.
    if ($j('.member-register').length) {
        $j('.member-register').css('display', 'none');
    }

    //Initiates jQuery tools tabs plugin
    if ($j('.slidetabs').length) {
        $j('.slidetabs').tabs(".images > div", {
            effect: 'fade',
            fadeOutSpeed: 'slow',
            rotate: true
        }).slideshow({ interval: 8000, clickable: false });

        setTimeout(playerState, 8000);

    }

    if ($j('#Video-Container').length) {

        //Embeds flash with swfObject
        var params = { allowScriptAccess: "always", wmode: "transparent" };
        var atts = { id: "myytplayer" };
        swfobject.embedSWF("http://www.youtube.com/v/eOsq3xe3PSI&amp;hl=en_US&amp;fs=1?enablejsapi=1&playerapiid=ytplayer", "YT-Video", "460", "259", "8", null, null, params, atts);

        $j('#Video-Overlay').click(function (e) {
            e.preventDefault();

            play();
            playerState();

            return false;
        });
    }

    if ($j('#Cart-Totals').length) {
        var ul = $j('#Cart-Totals ul');

        $j('li:first', ul).nextAll().css('display', 'none');
    }


    //Handles showing and hiding of FAQ's
    if ($j('.faq-template').length) {
        var allQuestions = $j('.faq-template');
        var count = $j(allQuestions).length - 1;
        var lastQuestion = $j(allQuestions[count]).find('.faq-next-question a');

        $j(lastQuestion).addClass('last-question');

        $j('.question a').click(function (e) {
            e.preventDefault();

            var answer = $j(this).closest('.faq-template');

            $j('.faq-template').fadeOut(500, function () {
                $j(answer[0]).fadeIn(300, function () {
                    $j('.faq-answer', answer[0]).slideDown(600);
                });
            });

            $j('.faq-question').fadeOut(300);

            return false;

        });

        $j('.faq-answer-back-to-list a').click(function (e) {
            e.preventDefault();

            var currentAnswer = $j(this).closest('.faq-answer');

            $j(currentAnswer).slideUp(500, function () {
                $j('.faq-template').fadeIn(300, function () {
                    $j('.faq-question').fadeIn(300);
                    $j('.template-content').fadeIn(300);
                });
            });

            return false;
        });

        $j('.faq-next-question a').click(function (e) {
            e.preventDefault();

            var currentQuestion = $j(this).closest('.template-content');
            var currentAnswer = $j(this).closest('.faq-answer');
            var nextQuestion = $j(currentQuestion).next().children().filter('.faq-template');

            if (!$j(this).hasClass('last-question')) {
                $j(currentAnswer).slideUp(500, function () {
                    $j(currentQuestion).fadeOut(500, function () {
                        $j(nextQuestion).fadeIn(300, function () {
                            $j('.faq-answer', nextQuestion).slideDown(600);
                        });
                    });
                });
            }


            return false;
        });
    }

    //Adds text to the login page.
    if ($j('.member-login').length) {
        $j('.member-login p').append('<br/> Your user name and password are on the right hand side of the Getting Started Guide in the Church Experience Kit. If you need more help, call 800-727-3480.');
    }
});

//Runs when document is fully loaded including images.
$j(window).load(function () {
    //Set cart items to same height
    if ($j('#Store').length || $j('.store-category-display').length) {
        var items = $j('.product-list-item');
        var maxHeight = 0;

        $j.each(items, function () {
            var itemHeight = $j(this).height();

            if (itemHeight > maxHeight) {
                maxHeight = itemHeight;
            }
        });

        $j.each(items, function () {
            $j(this).height(maxHeight);
        });
    }

    //Checks if side nav is there if not hide it and slide content under it up
    if (!$j('#subnav').length) {
        $j('.side-nav').css('display', 'none');
    }

    //Addes IE6 popup to site.
    $j('body').append("<div id='IE6'><a href='#' class='ie6close'></a><span class='warning'></span> <span>Wow, you're using IE6!</span><br /><p>The browser you're using is very out of date. We beg you to consider upgrading. This website will work for you, but some things may not work as intended because, well, your browser is really old. Upgrade now and be a happier web surfer.</p><a href='http://www.microsoft.com/nz/windows/internet-explorer/default.aspx' class='ie6'></a><a href='http://www.google.com/chrome' class='chrome'></a><a href='http://www.mozilla.com/en-US/firefox/upgrade.html' class='firefox'></a></div>");

    //IE 6 browser detection
    if ($j.browser.msie) {
        var version = $j.browser.version;

        if (version == '6.0') {
            var cookie = $j.cookie('IE6');

            if (cookie != 'set') {
                $j.cookie('IE6', 'set');
                $j('#IE6').show().stop().animate({
                    top: 0
                }, 1000);
            }

            $j('.ie6close').click(function () {
                $j('#IE6').fadeOut(500);
            });
        }
    }

    //Sets up the counter for the churches
    if ($j('#Counter-PP').length) {
        var Count = $j('#Counter-PP .count').text();
        var Start = setCounterStart(Count);

        $j('#Counter-PP').jOdometer({
            counterStart: Start,
            counterEnd: Start,
            numbersImage: 'SiteFiles/501196/images/counter_numbers.png',
            spaceNumbers: 2,
            heightNumber: 30,
            widthNumber: 25
        });
    }

    function setCounterStart(Count) {
        var length = Count.length;
        var Start = 0;

        switch (length) {
            case 1:
                Start = '00000' + Count;
                break;
            case 2:
                Start = '0000' + Count;
                break;
            case 3:
                Start = '000' + Count;
                break;
            case 4:
                Start = '00' + Count;
                break;
            case 5:
                Start = '0' + Count;
                break;
            case 6:
                Start = Count;
                break;
            default:
                Start = 000000;
                break;
        }

        return Start;
    }

});

function onYouTubePlayerReady(playerId) {
    ytplayer = document.getElementById("myytplayer");
    //console.log(state);
}

function playerState() {
    if (ytplayer) {
        state = ytplayer.getPlayerState();

        switch (state) {
            case -1:
                startSlideShow();
                break;
            case 0:
                startSlideShow();
                break;
            case 1:
                stopSlideShow();
                break;
            case 2:
                break;
            case 3:
                stopSlideShow();
                break;
            case 5:
                startSlideShow();
                break;
        }
    }
}

function play() {
    if (ytplayer) {
        ytplayer.playVideo();
    }
}

function stop() {
    if (ytplayer) {
        ytplayer.stopVideo();
    }
}

function startSlideShow() {
    stop();
    $j(".slidetabs").data("slideshow").play();
}

function stopSlideShow() {
    $j(".slidetabs").data("slideshow").stop();
}

