﻿$(function() {


    // Twitter
    $(".twitter").twitterfeed({
        hashtag: "#RW12",
        searchContainer: ".latest_tweet",
        searchTweetCount: 5,
        retweet: false
    });

    // Facebook markup for AddThis
    $('.addthis_button_facebook_like').attr('fb:like:layout', 'button_count');

    // Register Module
    $('.register #txtEmail').val('');
    $('.register #txtEmail').hover(function() {
        $('.register label').css({ 'color': '#999' })
    }, function() {
        $('.register label').css({ 'color': '#333' })
    });
    $('.register #txtEmail').focus(function() {
        $('.register label').fadeOut();
    });
    $('.register #txtEmail').blur(function() {
        if ($(this).val() == '') {
            $('.register label').fadeIn();
        }
    });

    // Article zebra colouring
    $('.article_list article').each(function(i) {
        var bg_color = "";
        switch (i % 3) {
            case 0:
                bg_color = "#bfe3cb";
                break;
            case 1:
                bg_color = "#afdfd9";
                break;
            case 2:
                bg_color = "#eae29b";
                break;
            default:
                bg_color = "#bfe3cb";
        }
        $(this).css({ 'background-color': bg_color });
    });

    // Lineup zebra colouring
    $('.lineup article ul ul').each(function() {
        $(this).find('li:odd').css({ 'background-color': '#FFF' });
    });

    // Homepage Carousel
    $('#carousel .carousel').ln_carousel();

    // Accordian
    $('.accordian').each(function() {
        var _accord = $(this);
        $(_accord).find('article').each(function() {
            var _article = $(this);
            $(_article).find('div').hide();
        });
        $(_accord).find('article:eq(0) div').show();

        $(_accord).find('h2').click(function() {
            var _div = $(this).parent().find('div');
            var _h2 = $(this);
            if (!$(_div).is(":visible")) {
                $(_accord).find('article div').slideUp();
                $(_div).slideDown('slow');
            } else {
                $(_div).slideUp('slow');
            }
            return false;
        });
    });

    // Background stretch (1-7)
    var rand_bg = Math.ceil(Math.random() * 7);
    $.backstretch("/_Resources/img/backgrounds/RW12_backgrounds_" + rand_bg + ".jpg");


    // Gallery Page Lightbox
    var sTog = "";
    $(".fancybox").click(function() {
        if ($(this).find('img').attr('title') != '') {
            sTog = "<h2>" + $(this).attr('title') + "</h2><p>Photographer: " + $(this).find('img').attr('title') + "</p>";
        } else {
            sTog = "<h2>" + $(this).attr('title') + "</h2>";
        }
    });
    $(".fancybox").fancybox({fitToView: true});
});

// Global functions
function goToByScroll(cssSelector) {
    $('html,body').animate({ scrollTop: $(cssSelector).offset().top }, 'slow');
}
