﻿var ytplayer;

function onYouTubePlayerReady(eleid) {
    lastwatchedid = "myytplayer" + eleid;
    ytplayer = document.getElementById(lastwatchedid);
    //ytplayer.mute();
    if (typeof(ytpausefirst) == "undefined") {
        ytplayer.playVideo();
    }
}


function loadmovie(vid, srctype, vwidth, vheight) {

    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/embed.asmx/embedcode",
        data: "{id:" + vid + "}",
        dataType: "json",
        success: function(htmlresults) {

            if (srctype == 1) {
                var params = { allowScriptAccess: "always", allowFullScreen: "true", wmode: "transparent" };
                var atts = { id: "myytplayer" + vid };

                if (!(vwidth && vheight)) {
                    vwidth = "640";
                    vheight = "385";
                }

                swfobject.embedSWF("http://www.youtube.com/v/" + htmlresults.d + "&enablejsapi=1&fs=1&cc_load_policy=1&playerapiid=" + vid,
                       "watch" + vid, vwidth, vheight, "8", null, null, params, atts);
            }
            else {
                $("#watch" + vid).html(htmlresults.d);
            }

            $('#closeme' + vid).css('display', 'inline');
            $("#moviecontainer" + vid).css('display', 'block');
            if (document.getElementById('scrollwatch' + vid)) {
                setTimeout(function() { $.scrollTo('#scrollwatch' + vid, { duration: 250 }) }, 800);
            }
        }
    });

    $.ajax({});
}


function closemovie(vid) {
    $("#moviecontainer" + vid).css('display', 'none');
    $("#watch" + vid).html("");
    ytplayer.stopVideo();
}

function seektime(secs) {
    ytplayer.seekTo(secs, true);
    setTimeout(function() { $.scrollTo('#titleanchor', { duration: 250 }) }, 200);
}

function openinlink(lnkid, vid) {
    var gotourl = false;
    var thelink = $('a#' + lnkid);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/embed.asmx/exlink",
        data: "{id:" + vid + "}",
        dataType: "json",
        async: false,
        timeout: 3000,
        success: function(axres) {

            thelink.attr("href", axres.d);
            $.ajax({});
            gotourl = true;
        }
    });

    return gotourl;
}


function openexlink(lnkid, vid) {
    var gotourl = false;
    var thelink = $('a#' + lnkid);
    $.ajax({
        type: "POST",
        contentType: "application/json; charset=utf-8",
        url: "/embed.asmx/exlink",
        data: "{id:" + vid + "}",
        dataType: "json",
        async: false,
        timeout: 3000,
        success: function(axres) {

            thelink.attr("href", axres.d);
            $.ajax({});
            gotourl = true;
        }
    });

    return gotourl;
}

jQuery.fn.wcenter = function(yoffset) {
    this.css("position", "absolute");
    oh = this.outerHeight();
    ow = this.outerWidth();
    if (yoffset) {
        this.css("top", (yoffset + $(window).scrollTop()) + "px");
    }
    else {
        this.css("top", (($(window).height() - oh) / 2 + $(window).scrollTop()) + "px");
    }
    this.css("left", (($(window).width() - ow) / 2 + $(window).scrollLeft()) + "px");
    return this;
}

function wordpopfor(theword) {

    $("#wordpopword").html(theword);

    $.ajax({
        url: "http://api.wordnik.com/api/word.json/" + theword + "/examples",
        data: { api_key: '953a7f74cd4f76221400702eb1503b75e49dee6236e8971d2', count: 3, callback: '?' },
        dataType: "jsonp",
        async: false,
        success: function(data) {
            examplist = $.map(data, function(item, cnt) {
                var e_v = new RegExp(theword, "gi");
                return item.display.replace(e_v, "<b>" + theword + "</b>");
            });
            displaylist = examplist ? "<ul><li>" + examplist.join("</li><li>") + "</li></ul>" : "<i>No example sentences found for \"" + theword + ".\"</i>";
            $("#wordpopexample").html(displaylist);
        },
        error: function(xrequest, status, ex) {
            var tempy = ex;
        }
    });

    $.ajax({
        url: "http://api.wordnik.com/api/word.json/" + theword + "/related",
        data: { api_key: '953a7f74cd4f76221400702eb1503b75e49dee6236e8971d2', count: 5, type: 'synonym', callback: '?' },
        dataType: "jsonp",
        async: false,
        success: function(data) {
            var synlist;
            $.each(data, function(idx, val) {
                if (val.relType == "synonym") synlist = val.wordstrings;
            });

            displaylist = synlist ? synlist.join(", ") : "<i>No synonyms found for \"" + theword + ".\"</i>";
            $("#wordpoprelated").html(displaylist);
        },
        error: function(xrequest, status, ex) {
            var tempy = ex;
        }
    });

    /*
    $.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: "http://api.wordnik.com/api/word.json/" + theword + "/examples?api_key=953a7f74cd4f76221400702eb1503b75e49dee6236e8971d2",
    //data: "{api_key: 953a7f74cd4f76221400702eb1503b75e49dee6236e8971d2}",
    dataType: "json",
    async: false,
    timeout: 3000,
    success: function(axres) {
    var tempx = axres;
    },
    error: function(xrequest, status, ex) {
    var tempy = ex;
    }
    });
    */

    $("#wordpop").css("top", ($(window).scrollTop() + 20) + "px");
    $("#wordpop").show(400);

}