﻿/// <reference path='jquery-1.6.2.min.js' />
/// <reference path='swfobject.js' />
/// <reference path='dateFormat-1.2.js' />
/*
* M3MTube Lib
* (c) 2011-2012 Marvin Martins Mendes <marvin_mendes@yahoo.com.br>
* 
* M3MTube is distributed under the MIT License
* Read more about the MIT License --> http://www.opensource.org/licenses/mit-license.php
*
* jQuery 1.4.* or higher required
*
*/


(function ($) {
    var apiSingleVideo = "http://www.youtube.com/v/";
    var apiFeedVideo = "http://gdata.youtube.com/feeds/api/";
    var mm_options = null;
    var mm_feedContainer = null;

    //o - Params, f = funciton
    $.youTubeFeed = function (o, f) {
        var options = {
            ytId: false,
            feedResults: 25
        };

        options = $.extend(options, o);
        if (!options.ytId) return null;
        var url = apiFeedVideo + "playlists/" + options.ytId + "?v=2&alt=jsonc&max-results=" + options.feedResults + "&callback=?";
        $.getJSON(url, f);
    },

    $.fn.youTubeVideo = function (o) {
        var options = {
            ytId: false,
            fromPlayList: false,
            width: "580",
            height: "380",
            fullScreen: false,
            autoPlay: false,
            playerMode: "normal",
            urlParams: false
        };
        options = $.extend(options, o);

        if (!options.ytId) return null;
        var p = options;

        var url = [apiSingleVideo]
        url.push(p.ytId + "?");
        url.push("playerMode=" + p.playerMode);
        if (p.fullScreen) url.push("&fs=1");
        if (p.autoPlay) url.push("&autoplay=1");
        if (p.urlParams) url.push("&" + p.urlParams);

        this.html("<div id='" + this.attr("id") + "-video-holder" + "'></div>");
        var params = { allowScriptAccess: "always", allowfullscreen: "true" };
        var atts = { id: this.attr("id") + "-video" };
        swfobject.embedSWF(url.join(""), this.attr("id") + "-video-holder", p.width, p.height, "8", null, null, params, atts);
    },

    $.fn.youTubePlayList = function (o) {
        var options = {
            ytId: false,
            showList: true,
            listGroupSize: 999,
            imageSize: "small",
            feedResults: 25,
            dateFormat: "dd/mm/yyyy",
            callback: null,
            template: "<div class='video-item'><div class='video-image'>{image}</div><div class='video-desc'>Data Pub: {uploaded} | Titulo: {title} | Vistos: {viewCount} </div></div>",
            playerId: false,
            playerWidth: "580",
            playerHeight: "380",
            playerFullScreen: true,
            playerAutoPlay: false,
            playerAutoLoad: true
        };
        options = $.extend(options, o);
        if (!options.ytId) return false;
        mm_feedContainer = this;
        mm_options = options;

        $.youTubeFeed(o,
            function (json) {
                if (json == undefined) return false;
                var vs = json.data.items;
                var mo = mm_options;

                if (mo.showList) {
                    var ci = 0;
                    var cl = 0;
                    var li = 0;

                    mm_feedContainer.html("<ul class='video-list categoria'></ul>");
                    for (; ci < vs.length; ci++) {
                        var p = mm_options;
                        var c = mm_feedContainer.children(".video-list").eq(li);
                        var item = vs[ci];
                        var v_img = "";
                        if (p.playerId) v_img += "<a class='videoLink' rel='prettyPhoto' title='teste' href='#'>";
                        if (p.imageSize == "small")
                            v_img += "<img src='" + item.video.thumbnail.sqDefault + "' alt='" + item.video.id + "' />";
                        else
                            v_img += "<img src='" + item.video.thumbnail.hqDefault + "' alt='" + item.video.id + "' />";
                        if (p.playerId) v_img += "</a>";

                        var r = new RegExp("\\{image\\}", "gm");
                        var content = p.template.replace(r, v_img);

                        r = new RegExp("\\{title\\}", "gm");
                        content = content.replace(r, item.video.title);

                        r = new RegExp("\\{uploaded\\}", "gm");
                        var y = item.video.uploaded.substring(0, 4);
                        var m = item.video.uploaded.substring(5, 7);
                        var d = item.video.uploaded.substring(8, 10);
                        var dt = new Date(y, m, d);
                        var t = dateFormat(dt, p.dateFormat);
                        content = content.replace(r, t);

                        r = new RegExp("\\{viewCount\\}", "gm");
                        content = content.replace(r, item.video.viewCount);

                        r = new RegExp("\\{description\\}", "gm");
                        content = content.replace(r, item.video.description);

                        c.append(content);

                        cl++;

                        if (cl >= p.listGroupSize && ci < vs.length -1) {
                            mm_feedContainer.append("<ul class='video-list categoria'></ul>");
                            li++;
                            cl = 0;
                        }
                    }
                }

                if (mo.playerId && mm_options.showList) {
                    $(".videoLink").click(function (e) {
                        e.preventDefault();
                        var vid = $(this).children("img").attr("alt");
                        $(mm_options.playerId).youTubeVideo({
                            ytId: vid,
                            width: mm_options.playerWidth,
                            height: mm_options.playerHeight,
                            fullScreen: mm_options.playerFullScreen,
                            autoPlay: mm_options.playerAutoPlay
                        });
                    });
                }

                if (mo.playerAutoLoad && vs.length > 0) {
                    $(mo.playerId).youTubeVideo({
                        ytId: vs[0].video.id,
                        width: mm_options.playerWidth,
                        height: mm_options.playerHeight,
                        fullScreen: mm_options.playerFullScreen,
                        autoPlay: mm_options.playerAutoPlay
                    });
                }

                if (mo.callback != null) mo.callback(json.data);
            }
        );
    }

    })(jQuery);

    jQuery.fn.listVideo = function (o) {
        var options = {
            playListId: null,
            feedResuts: 5,
            width: "580",
            height: "380",            
            fullScreen: true,
            autoPlay: false,
            playerMode: "normal",
            flashParams: { allowScriptAccess: "always", allowfullscreen: "true" },
            urlParams: "playerMode=normal&autoplay=0&fs=1&v=3"
        };

        options = jQuery.extend(options, o);
        //check playlistID before proceed
        if (options.playListId == null) return $(this);
        var $this = $(this);
        //Options to load the feed
        var feedOptions = {
            ytId: options.playListId,
            feedResults: options.feedResuts
        };

        jQuery.youTubeFeed(feedOptions, function (json) {
            if (json == undefined || json == null) return $this;
            var vs = json.data.items;
            jQuery.each(vs, function (index, item) {
                var url = ["http://www.youtube.com/v/"];
                url.push(item.video.id);
                if (options.urlParams != null && options.urlParams != undefined) {
                    url.push("?" + options.urlParams);
                }

                var itemId = "video-" + index;

                $this.append("<div class='video-holder'><div id='" + itemId + "'></div></div>");

                var atts = { id: itemId };
                swfobject.embedSWF(url.join(""), itemId, options.width, options.height, "8", null, null, options.flashParams, atts);
            });
        });

    };

