/// <reference path="jquery.js" />

$(document).ready(function () {

    // Remove no-js class

    $('html').removeClass('no-js');


    // login box functionality

    $('.sign_in').click(function (event) {
        event.preventDefault();
        $(this).addClass('highlight');
        $('.login').show();
        $('#fade').fadeIn(1400);
        return false;
    })

    $('#fade').click(function () {
        $('.sign_in').removeClass('highlight');
        $('.login').hide();
        $('#fade').fadeOut(800);
        return false;
    })

    var hash = this.location.hash;
    if (hash == '#signin') {
        $('.sign_in').trigger('click');
    }

    if ($('.sign_in').hasClass('active')) {
        $('.sign_in').trigger('click');
        $('.sign_in').removeClass('active');
    }

    // topics landing functionality
    $('.topicSummary').hide();

    // load up the topic from the location hash
    var hash = window.location.hash.substring(1);
    if (hash.length > 0) {
        var topic = $('.' + hash, 0, 0);
        showTopic(topic);
    }
    else {
        $('.topicSummary').not(':eq(0)').hide();
        showTopic($('.topicSelect li:eq(0)'), 0, 0);
    }

    $('.topicSelect li').click(function () {
        showTopic($(this), 100, 600);
        return false;
    })

    function showTopic(topic, fadeOut, fadeIn) {
        $('.topicSelect li').removeClass('active');
        topic.addClass('active');
        Cufon.refresh('h2:not(.wine_of_week h2,#header h2, .blog h2, .buying_wine h2, .rightCol h2, .discussions h2, .search_disc h2, .post h2)')
        $('.topicSummary').fadeOut(fadeOut);
        var topicNumber = topic.index();
        $('.topicSummary').eq(topicNumber).fadeIn(fadeIn);
    }
    // wine topics functionality

    $('.wine_topics li').each(function (index) {

        vHeight = $(this).find('p').height();
        $(this).find('p').css('height', '0');

        $(this).hover(
          function () {
              $(this).find('p').animate({
                  height: '34px',
                  paddingTop: '+=2',
                  paddingBottom: '+=5'
              }, 200, function () {
              });

              $(this).find('div').animate({
                  bottom: '-=10'
              }, 200, function () {
              });
          },
          function () {
              $(this).find('p').animate({
                  height: '0',
                  paddingTop: '0',
                  paddingBottom: '0'
              }, 200, function () {
              });

              $(this).find('div').animate({
                  bottom: '27px'
              }, 200, function () {
              });
          }
        );
    })


    // homepage discussion list functionality

    $('.discussion_list table tr').each(function (index) {

        $(this).hover(
          function () {
              $(this).addClass('hover');
          },
          function () {
              $(this).removeClass('hover');
          }
        );

        $(this).click(function (event) {
            event.preventDefault();

            if (typeof ($(this).find('a').attr('href')) == 'undefined') return false;

            thisHref = $(this).find('a').attr('href');
            window.location.href = thisHref;

            return false;
        })

    });


    // discussions functionality

    $('ul.discussions .discussions_table').hide();

    $('.discussions h2').click(function (event) {
        event.preventDefault();

        $(this).parents('li').addClass('clicked');

        $('.discussions li').each(function () {
            if ($(this).hasClass('clicked') == false) {
                $(this).removeClass('highlight');
                $(this).find('.discussions_table').slideUp(400);
            }
        })

        $(this).parents('li').toggleClass('highlight');
        $(this).parents('li').find('.discussions_table').slideToggle(400);
        $(this).parents('li').removeClass('clicked');

        return false;

    });

    $('.discussions_table table tr, .discussions table tr').each(function (index) {

        $(this).hover(
          function () {
              $(this).addClass('hover');
          },
          function () {
              $(this).removeClass('hover');
          }
        );

        $(this).click(function (event) {
            event.preventDefault();

            if (typeof ($(this).find('a').attr('href')) == 'undefined') return false;

            thisHref = $(this).find('a').attr('href');
            window.location.href = thisHref;

            return false;
        })

    });



    // reply

    $('.reply').hide();

    $('.reply_btn').click(function (event) {
        if (!$(this).hasClass('disabled')) {
            event.preventDefault();
            $('.reply').slideDown(400);
            $.scrollTo('.reply', 600);
            return false;
        }

    });

    $('.close_btn').click(function (event) {
        event.preventDefault();
        $('.reply').slideUp('slow');
        return false;
    });


    // ads

    $('.ad').click(function (event) {
        event.preventDefault();

        if (typeof ($(this).find('a').attr('href')) == 'undefined') return false;

        thisHref = $(this).find('a').attr('href');
        window.location.href = thisHref;

        return false;
    })


    // Blank inputs on focus

    var formText = ''

    $('input.text , textarea').focus(function () {

        // get default value
        formText = this.defaultValue;

        //if current value is default value, blank it
        if ($(this).val() == formText) {
            $(this).val('');
        }
    });

    $('input.text , textarea').blur(function () {

        //if the value is blank or is the default value, restore default value or set to completed
        if ($(this).val() == '' || $(this).val() == formText) {
            $(this).val(formText);
        }
    });


    /* Breadcrumb */

    $('.breadcrumb li:last-child').addClass('last');

    /* Twitter */
    $('.twitterFeedPanel').tweet({
        username: "TescoWines",
        template: "{text}"
    });
});

