﻿/*** Begins jQueryUI dialog and DatePicker ***/
$(document).ready(function() {
    // This is the normal Dialog, no iframe content
    $('#dialog').dialog({
        autoOpen: false,
        width: 600,
        buttons: {
            "Ok": function() {
                $(this).dialog("close");
            },
            "Cancel": function() {
                $(this).dialog("close");
            }
        }
    });

    // Dialog Link for normal uses
    $('#dialog_link').click(function() {
        $('#dialog').dialog('open');
        return false;
    });

    $(".openDialog").click(function() {
        $('#dialog').dialog('open');
        return false;
    });

    // Datepicker
    $('#datepicker').datepicker({
        inline: true
    });
});    /*** Ends jQueryUI dialog and DatePicker ***/

/*** Begins jQuery Tools Overlay ***/
//$(function() {

//    // if the function argument is given to overlay, it is assumed to be the onBeforeLoad event listener 
//    $("a[rel]").overlay(function() {

//        // grab wrapper element inside content 
//        var wrap = this.getContent().find("div.wrap");

//        // load only for the first time it is opened 
//        if (wrap.is(":empty")) {
//            wrap.load(this.getTrigger().attr("href"));
//        }
//    });
//});
/*** Ends jQuery Tools Overlay ***/

//$().ready(function() {
//    $("#commentForm").validate({
//        rules: {
//            CommentName: {
//                required: true,
//            },
//            CommentEmail: {
//                required: false,
//                email: true,
//            }
//        },
//        messages: {

//            CommentName: {
//                required: "Please enter a username",
//            },
//            CommentEmail: {
//                minLength: "Please enter a valid email address",
//            }
//        }
//    });
//});

(function($) {
    var youtubeURL = 'http://www.youtube.com/v/';

    var yl2e = {
        onclick: function() {
            if ($(this).next().hasClass('youtubeLinksToEmbed')) { return false; }

            href = youtubeURL + this.href.match(/[a-zA-Z0-9-_]+$/) + '&rel=1';
            wrapper = $('<div class="youtubeLinksToEmbed" style="display:none;"><span>Loading...</span><div style="display:none;"></div></div>');
            player = $('div:first', wrapper);

            player.flash({ src: href, width: 600, height: 380 });
            $(this).after(wrapper);

            wrapper.queue(function() {
                $(this).css('height', 40);
                $(this).dequeue();
                $(this).slideDown();
                $(this).dequeue();
            });

            setTimeout(function() {
                wrapper.queue(function() {
                    $(this).find('span').hide().end();
                    $(this).dequeue();
                    $(this).animate({ height: 400 });
                    $(this).dequeue();
                });
                setTimeout(function() {
                    player.show();
                }, 500);
            }, 1500);

            return false;
        }
    };

    $.fn.youtubeLinksToEmbed = function(options) {
        var opt = $.extend({ autoOpen: false }, options);
        $(this).find('a[href*=youtube.com/watch?v=]')
            .addClass('youtubeLinksToEmbed')
            .each(function() {
                $(this).click(yl2e.onclick);
                if (opt.autoOpen) {
                    $(this).trigger('click');
                }
            });
    };

    $.fn.youtubeInputsToEmbed = function(options) {
        $(this).find('input[value*=youtube.com/watch?v=]')
            .addClass('youtubeLinksToEmbed')
            .each(function() {
                $('<a href="' + $(this).val() + '">watch</a>')
                    .insertAfter(this);
            });
        $(this).youtubeLinksToEmbed(options);
    };
})(jQuery);