﻿/// <reference path="http://ajax.microsoft.com/ajax/jquery/jquery-1.3.2.min.js" />

$(document).ready(function() {
    // Tabbed Content
    $('.tabbedContent div.tabContent').hide();
    $('.tabbedContent div.tabContent:first').show();
    $('.tabbedContent ul.tabs li:first').addClass('active first');
    $('.tabbedContent ul.tabs li a').click(function() {
        $('.tabbedContent ul.tabs li').removeClass('active');
        $(this).parent().addClass('active');
        var currentTab = $(this).attr('href');
        $('.tabbedContent div.tabContent').hide();
        $(currentTab).show();
        return false;
    });

    // Style select boxes
    $("select.styled").sSelect({ ddMaxHeight: '200px' });

    // Thinking About Section
    var $thinkingAboutFields = $("#thinkingAbout .remainingFields");
    $thinkingAboutFields.append('<div class="close">Close</div>');
    $thinkingAboutFields.children(".close").click(function() {
        $(this).parent().hide();
    });
    $thinkingAboutFields.hide();
    $("#thinkingAbout input")
      .click(function() { $thinkingAboutFields.show(); })
    //.mouseleave(function() { $thinkingAboutFields.hide(); });

    $('#mainNavigation li').hoverIntent({
        sensitivity: 3,
        interval: 1,
        timeout: 400,
        over: function() {
            $(this).children('ul').slideDown();
        },
        out: function() {
            $(this).children('ul').slideUp();
        }
    });

    // Load youtube video
    $('a.loadYouTubeVideo').click(function() {
        var href = $(this).attr("href") || this.getAttribute('href');
        var youTubeId = href.replace('#', '');
        var id = $(this).attr('rel');
        loadYouTubeVideo(id, youTubeId);
        return false;
    });

    // Character counter
    $("[class^='count[']").each(function() {
        var elClass = $(this).attr('class');
        var minWords = 0;
        var maxWords = 0;
        var countControl = elClass.substring((elClass.indexOf('[')) + 1, elClass.lastIndexOf(']')).split(',');
        var numChars = $(this).val().length;
        var minMaxString = "";
        
        if (countControl.length > 1) {
            minWords = countControl[0];
            maxWords = countControl[1];
            var minMaxString = "(Min " + minWords + " words, Max " + maxWords + " characters)";
        } else {
            maxWords = countControl[0];
            var minMaxString = "(Max " + maxWords + " characters)";
        }
        $(this).after('<div class="wordCount"><strong>' + numChars + '</strong> Characters <small>' + minMaxString + '</small></div>');
        if (minWords > 0) {
            $(this).siblings('.wordCount').addClass('error');
        }

        $(this).bind('keyup click blur focus change paste', function() {
            var numWords = $(this).val().length; //jQuery.trim($(this).val()).split(' ').length;
            if ($(this).val() === '') {
                numWords = 0;
            }
            $(this).siblings('.wordCount').children('strong').text(numWords);

            if (numWords < minWords || (numWords > maxWords && maxWords != 0)) {
                $(this).siblings('.wordCount').addClass('error');
            } else {
                $(this).siblings('.wordCount').removeClass('error');
            }
        });
    });

    (function() {
        $('.labelInsideInput label').labelInsideTextarea();
    })();

});
function loadYouTubeVideo(containerId, youTubeId) {
    var $container = $('#' + containerId);
    if ($container) {
        var width = $container.width();
        var height = $container.innerHeight();
        var url = 'http://www.youtube.com/v/' + youTubeId + '&hl=en_US&fs=1&rel=1&border=0&fs=1';
        swfobject.embedSWF(
            url,
            containerId,
            width,
            height,
            '9.0.0',
            false,
            false,
            { allowfullscreen: 'true', wmode: 'transparent' }
        );
    }
}

jQuery.fn.labelInsideTextarea = function() {
    return this.each(function() {
        var forAttr = $(this).attr('for');
        var $field = $('#' + $(this).attr('for'));
        var $label = $(this);
        if ($field) {
            if ($field.val()) {
                $label.hide();
            }
            $field.focus(function() {
                $label.hide();
            }).blur(function() {
                if (!$(this).val()) $label.show();
            });
        }
    });
};

