/// <reference path="jquery-1.3.2.min.js" />

function previewYoutubeVideo() {

    var name = $("#clipName").val();
    var url = $("#clipUrl").val();

    //alert($("#clipName"));
    //alert($("#clipUrl"));

    //alert('Name: ' + name);
    //alert('Url: ' + url);

    //var youtubeID = 'KbSPPFYxx3o';

    if (name.length > 0 && url.length > 0) {

        var re = new RegExp("v=([\\S]+)");
        var matches = re.exec(url);

        if (matches == null || matches.length == 0) {
            //alert(matches);
            alert('The video URL is invalid');
            $("#clipUrl").focus();
            return;
        }

        var youtubeID = matches[1];

        $("#youtubePreview").html(
            "<object width='425' height='344'>" +
            "<param name='movie' value='http://www.youtube.com/v/" + youtubeID + "&hl=en&fs=1'></param>" +
            "<param name='allowFullScreen' value='false'></param>" +
            "<embed src='http://www.youtube.com/v/" + youtubeID + "&hl=en&fs=1' type='application/x-shockwave-flash' allowfullscreen='false' width='425' height='344'></embed></object>"
            );

        //alert($("#youtubePreview").html());

        $("#divPreview").show();
    }
}

function addYoutubeVideo() {
 
    var name = $("#clipName").val();
    var url = $("#clipUrl").val();
    var tags = $("#tags").val();

    var youtubeID = 'KbSPPFYxx3o';

    //alert('hello');

    $.ajax({
        type: "POST",
        url: "/Services/ContentServices.asmx/AddYoutubeVideo",
        data: "{name: '" + name + "', youtubeId: '" + youtubeID + "', tags: '" + tags + "' }",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(msg) {
            window.location = "/videos"
        },
        error: function(xhr, ajaxOptions, thrownError) {
            alert(xhr.status + ' \n\r ' + xhr.statusText + '\n\r' + xhr.responseText);
        }
    });
}
