﻿/* SIGN IN */
$(function () {

    if ($.cookie("email")) {
        $("#login_username").val($.cookie("email"));
    }

    $("li.login-button a").click(function (e) {
        e.preventDefault();
        var login = $(".login")
        if (login.is(":visible")) {
            login.hide(0);
            $(".btn_sign_in").removeClass("active");
        } else {
            login.show(0);
            $(".btn_sign_in").addClass("active");
        }

        return false;
    });


    $(".form_sign_in").click(function () {
        if ($("#login_username").val() == "[Email]") {
            $("#login_error").text("Please enter your email address")
        } else if ($("#login_password").val() == "[Password]") {
            $("#login_error").text("Please enter your password")
        } else {
            $("#login_error").text("")
            return true;
        }
        return false;
    })

    $(".btn_close").click(function () {
        $(".login").hide(0);
        $(".btn_sign_in").removeClass("active");
    });
});

/* SIGN IN INPUTS - TEXT REPLACEMENT */
$(function () {
    $("#login_username").focus(function () {
        if ($(this).val() == "[Email]") {
            $(this).val("");
            $(this).addClass("active");
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val("[Email]");
            $(this).removeClass("active");
        }
    });

    $("#login_password").focus(function () {
        if ($(this).val() == "[Password]") {
            $(this).val("");
            $(this).addClass("active");
        }
    }).blur(function () {
        if ($(this).val() == "") {
            $(this).val("[Password]");
            $(this).removeClass("active");
        }
    });
})

/* HOMEPAGE CAROUSEL */
function mycarousel_initCallback(carousel) {
    jQuery('.jcarousel-control a').bind('click', function() {
        carousel.scroll(jQuery.jcarousel.intval(jQuery(this).text()));
           $('.jcarousel-control a').removeClass("on");
           $(this).addClass("on");
        return false;
    });
 
    jQuery('.jcarousel-scroll select').bind('change', function() {
        carousel.options.scroll = jQuery.jcarousel.intval(this.options[this.selectedIndex].value);
        return false;
    });
    
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
 
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function setDot(carousel, li, index, state) {
    // removes the active dot class
    $('.jcarousel-control a').removeClass('item-on');
    // fi
    var selected = (index - 1) % 4
    $('.jcarousel-control a:eq('+selected+')').addClass('item-on');
}
 
// Ride the carousel...
jQuery(document).ready(function() {
    jQuery("#mycarousel").jcarousel({
        // This sets the slide transition time
        auto: 8,
        //This scrolls 1 slide
        scroll: 1,
        wrap: 'circular',
        initCallback: mycarousel_initCallback,
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null,
        // This sets the active slide dot
        itemVisibleInCallback: setDot
    });
});



// FAQ - EXPAND / CONTRACT
$(function () {
    /*$('.faq-see-all').toggle(
    function () {
    $('.faq-see-all').html('Hide All Answers<img src="/images/arrow-red-up.gif" />');
    $('.faqs h3').next('div').slideDown();
    $('.faqs h3').siblings('img').attr('src', '/images/faq-answer-close.gif');
    },
    function () {
    $('.faq-see-all').html('Show All Answers<img src="/images/arrow-red-down.gif" />');
    $('.faqs h3').next('div').slideUp();
    $('.faqs h3').siblings('img').attr('src', '/images/faq-answer.gif');
    }
    );

    $('.faqs h3').next('div').hide();

    $('.faqs img').toggle(
    function () {
    $(this).prev('div').slideDown();
    $(this).attr('src', '/images/faq-answer-close.png');
    },
    function () {
    $(this).prev('div').slideUp();
    $(this).attr('src', '/images/faq-answer.png');
    }
    );

    $('.faqs h3').toggle(
    function () {
    $(this).next('div').slideDown();
    $(this).siblings('img').attr('src', '/images/faq-answer-close.png');
    },
    function () {
    $(this).next('div').slideUp();
    $(this).siblings('img').attr('src', '/images/faq-answer.png');
    }
    );
    */
    if ($('.faqs div').length > 0) { $("<img>", { 'src': '/images/faq-answer-close.png' }).hide().appendTo('body'); }

    $('.faq-see-all').data('sesame', true);
    $('.faqs div').each(function () { $(this).data('sesame', true); $(this).find('div:first').hide(); });

    $('.faqs div img').click(function () { ToggleFAQ($(this).parent()) });

    $('.faqs div h3').click(function () {
        ToggleFAQ($(this).parent());
    });

    $('.faq-see-all').click(function () {
        if ($(this).data('sesame') === true) {
            $(this).data('sesame', false).html('Hide All Answers<img src="/images/arrow-red-up.gif" />');

            $('.faqs div').each(function () {
                OpenFAQ($(this));
            });
        } else {
            $(this).data('sesame', true).html('Show All Answers<img src="/images/arrow-red-down.gif" />');
            $('.faqs div').each(function () {
                CloseFAQ($(this));
            });
        }
    });
});
    

    function ToggleFAQ(FAQ) {
        if (FAQ.data('sesame') == true) {
            OpenFAQ(FAQ);
        } else {
            CloseFAQ(FAQ);
        }
    }

    function OpenFAQ(FAQ) {
        if (FAQ.data('sesame', false).find('div:first').css('display') == 'none') {
            FAQ.find('div:first').slideDown();
        }
        FAQ.find('img').attr('src', '/images/faq-answer-close.png');
    }

    function CloseFAQ(FAQ) {
        if (FAQ.data('sesame', true).find('div:first').css('display') == 'block') {
            FAQ.find('div:first').slideUp();
        }
        FAQ.find('img').attr('src', '/images/faq-answer.png');
    }


$(function () {
    if ($('.public .submenu').find('li').length > 0) {
        $('.public .submenu').find('li:last a').css('background', 'none');
    }
});

/* SET UP POP-UP CONTENT */
$(function () {
    $(".popup-link").click(function (e) {
        var linkedDiv = "#" + $(this).attr('rel');
        $.fancybox({
            'centerOnScroll': true,
            'titleShow': false,
            'title': 'inline',
            'content': $(linkedDiv).html()
        });
    }).show();

    $('a[rel=youtube]').click(function(e){
        e.preventDefault();
        $(this).fancybox({
            'width': '800',
            'height':'488',
            'centerOnScroll': true,
            'titleShow': false,
            'title': 'inline',
            'href': this.href,
            'padding':'0',
            'type': 'swf',
            'swf': { 'wmode': 'transparent', 'allowfullscreen': true,'width':'800','height':'488' }
        });
    });
});

/* TWITTER */
$(function () {
    $('#users li').click(function () {
        if ($(this).hasClass('active') !== true) {
            _active = $('#users li.active');
            _activate = $(this);

            _active.removeClass('active');
            $('ul.tweets.' + _active.attr('id')).fadeOut(function () {
                _activate.addClass('active');
                 $('ul.tweets.' + _activate.attr('id')).fadeIn();
            });
        }
    });
});

