/*!
 * jQuery YouTube Popup Player Plugin v1.0
 * http://lab.abhinayrathore.com/jquery_youtube/
 * Date: May 3 2011
 * Modifications for Delphax by Webifi Inc.
 * Date: June 6 2011
 */
(function ($) {
    var YouTubeDialog = null;
    var methods = {
        //initialize plugin
        init: function (options) {
			setDefaults();
            options2 = $.extend({}, $.fn.YouTubePopup.defaults, options);

            // initialize YouTube Player Dialog
            //if (YouTubeDialog == null) {
                
            //}

            return this.each(function () {
                var obj = $(this);
                var data = obj.data('YouTube');
                //if (!data) { //check if event is already assigned
                    obj.data('YouTube', { target: obj, 'active': true });
                    $(obj).bind('click.YouTubePopup', function () {
						setDefaults();
            			options2 = $.extend({}, $.fn.YouTubePopup.defaults, options);
                        var youtubeId = options2.youtubeId;
                        if ($.trim(youtubeId) == '') youtubeId = obj.attr(options2.idAttribute);
                        var videoTitle = options2.title;
                        if ($.trim(videoTitle) == '') videoTitle = obj.attr('title');
						
						var YouTubeDialog = $('<div class="modal-video-container"></div>').css({ display: 'none', padding: 0 });
							$('body').append(YouTubeDialog);
							YouTubeDialog.dialog({ autoOpen: false, resizable: false, draggable: options2.draggable, modal: options2.modal,
								close: function () { YouTubeDialog.html(''); }
							});
                        //Format YouTube URL
						var YouTubeURL = youtubeId + "?hd=1&rel=0&showsearch=0&autohide=" + options2.autohide;
                        YouTubeURL += "&autoplay=" + options2.autoplay + "&color1=" + options2.color1 + "&color2=" + options2.color2;
                        YouTubeURL += "&controls=" + options2.controls + "&fs=" + options2.fullscreen + "&loop=" + options2.loop;
                        YouTubeURL += "&hd=" + options2.hd + "&showinfo=" + options2.showinfo;
						
						if( navigator.userAgent.match(/Android/i) ||
						 navigator.userAgent.match(/webOS/i) ||
						 navigator.userAgent.match(/iPhone/i) ||
						 navigator.userAgent.match(/iPod/i)
						 ){
						 	//Open youtube url directly for mobile plaforms
							YouTubeURL = "http://www.youtube.com/v/" + YouTubeURL;
							window.location = YouTubeURL;
							return false;
						}else{
							//Setup YouTube Dialog
							YouTubeURL = "http://www.youtube.com/embed/" + YouTubeURL;
							YouTubeDialog.html(getYouTubePlayer(YouTubeURL, options2.width, options2.height));
							YouTubeDialog.dialog({ 'minWidth': options2.width, 'minHeight': options2.height, title: videoTitle });
							YouTubeDialog.dialog('open');
							$('div.modal-video-container').siblings('div.ui-dialog-titlebar').addClass('ui-corner-top');
							$('div.modal-video-container').siblings('div.ui-dialog-titlebar').removeClass('ui-corner-all');
							return false;
						}

                        
                    });
               // }
            });
        },
        destroy: function () {
            return this.each(function () {
                $(this).unbind(".YouTubePopup");
                $(this).removeData('YouTube');
            });
        }
    };

    function getYouTubePlayer(URL, width, height) {
        var YouTubePlayer = '<iframe title="YouTube video player" style="margin:0; padding:0;" width="' + width + '" ';
        YouTubePlayer += 'height="' + height + '" src="' + URL + '" frameborder="0" allowfullscreen></iframe>';
        return YouTubePlayer;
    }

    $.fn.YouTubePopup = function (method) {
        if (methods[method]) {
            return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
        } else if (typeof method === 'object' || !method) {
            return methods.init.apply(this, arguments);
        } else {
            $.error('Method ' + method + ' does not exist on jQuery.YouTubePopup');
        }
    };

    //default configuration
	function setDefaults(){
		$.fn.YouTubePopup.defaults = {
			'youtubeId': '',
			'title': '',
			'idAttribute': 'rel',
			'draggable': false,
			'modal': true,
			'width': (($(window).height())-40),
			'height': Math.round(((($(window).height())-40)*.75)),
			'autohide': 2,
			'autoplay': 1,
			'color1': 'FFFFFF',
			'color2': 'FFFFFF',
			'controls': 1,
			'fullscreen': 1,
			'loop': 0,
			'hd': 1,
			'showinfo': 0
		};
	}
})(jQuery);
$(window).scroll(function() {
    $('.modal-video-container').dialog('option', 'position', 'center');
});
$(window).resize(function() {
    $('.modal-video-container').dialog('option', 'position', 'center');
});
