//-------------------------------------------------
//		youtube playlist jquery plugin
//		Created by dan@geckonm.com
//		www.geckonewmedia.com
//
//		v1.9 - updated to allow html5
//           - modified by Ceasar Feijen www.cfconsultancy.nl
//-------------------------------------------------

jQuery.fn.ytplaylist = function (options) {
    var options = jQuery.extend({
        holderId: 'ytvideo',
        playerHeight: '385',
        playerWidth: '640',
        addThumbs: false,
        thumbSize: 'small',
        autoPlay: true,
        playOnLoad: false,
        playfirst: 0,
        start: 0,
        showRelated: false,
        showInfo: true,
        wmode: true,
        iv_load_policy: true,
        hd: false,
        autoHide: true,
        modestbranding: true,
        theme: '&theme=dark&color=red',
        customtitle: '',
        html5: true,
        controls: true,
        playerColor: 'ffffff',
        playerversion: '&version=2',
        allowFullScreen: true
    }, options);
    var selector = $(this);
    var autoPlay = "";
    var showRelated = "&rel=0";
    var showInfo = "&showinfo=0";
    var wmode = "";
    var customtitle = "";
    var autoHide = "";
    var theme = "";
    var modestbranding = "";
    var fullScreen = "";
    var iv_load_policy = "";
    var hd = "";
    var controls = "&controls=0";
    var playerversion = "";
    if (options.showRelated) showRelated = "&rel=1";
    if (options.showInfo) showInfo = "&showinfo=1";
    if (options.wmode) wmode = "&wmode=opaque";
    if (options.autoHide) autoHide = "&autohide=1";
    if (options.allowFullScreen) fullScreen = "&fs=1";
    if (options.iv_load_policy) iv_load_policy = "&iv_load_policy=3";
    if (options.modestbranding) modestbranding = "&modestbranding=1";
    if (options.hd) hd = "&hd=1";
    if (options.controls) controls = "&controls=1";

    function play(id) {
        if (options.autoPlay && options.playOnLoad) autoPlay = "&autoplay=1";
        options.playOnLoad = true;

        var html = '';
        if (id == false) {
        	return html
        }
        else {

        if (options.html5) {
		html += '<iframe class="youtube-player" type="text/html" width="' + options.playerWidth + '" height="' + (options.playerHeight-3) + '" src="http://www.youtube.com/embed/' + id + '?' + options.theme + autoPlay + autoHide + showRelated + showInfo + wmode + controls + modestbranding + options.customtitle + '" frameborder="0">';
		html += '</iframe>';
		}
		else {

        html += '<object height="' + options.playerHeight + '" width="' + options.playerWidth + '">';
        html += '<param name="movie" value="http://www.youtube.com/v/' + id + options.playerversion + autoPlay + showRelated + showInfo + fullScreen + iv_load_policy + controls + hd + '&color1=0x' + options.playerColor + '&color2=0x' + options.playerColor + autoHide + modestbranding + options.theme + '" />';
        html += '<param name="wmode" value="transparent" />';
        html += '<param name="allowscriptaccess" value="always" />';
        html += '<param name="bgcolor" value="#000000" />';
        if (options.allowFullScreen) {
            html += '<param name="allowfullscreen" value="true" /> '
        }
        html += '<embed src="http://www.youtube.com/v/' + id + options.playerversion + autoPlay + showRelated + showInfo + fullScreen + iv_load_policy + + controls + hd + '&color1=0x' + options.playerColor + '&color2=0x' + options.playerColor + autoHide + modestbranding + options.theme + '"';
        if (options.allowFullScreen) {
            html += ' allowfullscreen="true" '
        }
        html += 'type="application/x-shockwave-flash" wmode="transparent" bgcolor="#000000" allowscriptaccess="always" height="' + options.playerHeight + '" width="' + options.playerWidth + '"></embed>';
        html += '</object>';
        }
        return html
        }
    };

    function youtubeid(url) {
        if (url == undefined) {
            return false
        } else {
            var ytid = url.match("[\\?&]v=([^&#]*)");
            ytid = ytid[1];
            return ytid
        }
    };
    var firstVid = $(selector).filter(function (index) {
        return index == options.playfirst
    }).addClass('currentvideo').attr('href');
    $('#' + options.holderId + '').html(play(youtubeid(firstVid)));
    selector.click(function () {
        $('#' + options.holderId + '').html(play(youtubeid($(this).attr('href'))));
        $(selector).filter('.currentvideo').removeClass('currentvideo');
        $(this).addClass('currentvideo');
        return false
    });
    if (options.addThumbs) {
        selector.each(function (i) {
            var replacedText = $(this).text();
            var replacedTextTitle = $(this).text().substring(0, 35);
            if (options.thumbSize == 'small') {
                var thumbUrl = 'http://img.youtube.com/vi/' + youtubeid($(this).attr('href')) + '/2.jpg'
            } else {
                var thumbUrl = 'http://img.youtube.com/vi/' + youtubeid($(this).attr('href')) + '/0.jpg'
            }
            $(this).empty().html('<img src="' + thumbUrl + '" alt="' + replacedTextTitle + '" />' + replacedText).attr('title', replacedTextTitle)
        })
    }
};
