(function(d){function f(a){this.id=a;this.context=d("#"+this.id);this.widget=this.context.find(".video-widget");this.defaultVideo=this.currentVideo=null;this.channels=[];this.playlists=[];this.videoServices={yt:"youtube",v:"vimeo"};this.YOU_TUBE_EVENTS={"-1":"unstarted","0":"ended",1:"playing",2:"paused",3:"buffering",5:"video cued"};this.ClickTrackingEvent="com.cobaltgroup.ws.action.open.widget"}function k(a){if(!a||"&nbsp;"==a)throw"Cannot initialize an empty Video.";if("string"==typeof a)if(-1!=
a.indexOf("youtu")){this.videoType="yt";this.id=a.split("v=")[1];if(!this.id)throw"A YouTube URL was specified without a valid video ID.";this.id=this.id.substring(0,11);this.url="https://gdata.youtube.com/feeds/api/videos/"+this.id+"?v=2&alt=json-in-script"}else{if(-1!=a.indexOf("vimeo")&&(this.videoType="v",this.id=parseInt(a.split(".com/")[1]),this.url="http://vimeo.com/api/v2/video/"+this.id+".json",!this.id))throw"A Vimeo URL was specified without a valid video ID.";}else{d.extend(this,a);if(!this.id)throw"Warning: an invalid video ID was specified";
this.videoType=isNaN(this.id)?"yt":"v"}var b;"yt"==this.videoType?b={autohide:1,rel:0,border:0,fs:1,hd:1,showinfo:0,theme:"dark",modestbranding:1,autoplay:0,enablejsapi:1,version:3}:"v"==this.videoType&&(b={server:"vimeo.com",show_title:0,show_byline:0,show_portrait:0,fullscreen:1,autoplay:0});this.params=d.extend(b,this.params)}function j(a,b){this.baseURL="http://gdata.youtube.com/feeds/api/playlists";this.playlistID=a;this.template=b;this.videos=[]}f.prototype.options={};f.prototype.populatePlaylists=
function(a){for(var b=this,c=a||[],e=d.Deferred(),m=[],a=0,f=d("#"+b.id+"-playlist-tmpl").template(),g=0,i=c.length;g<i;g++)c[g]&&c[g].playlistId&&(function(a){var e=new j(c[g].playlistId,f),d=e.fetch().done(function(){b.playlists[a]=e});m.push(d)}(a),a++);d.when.apply(d.when,m).done(function(){b.defaultVideo?b.playlists[0].videos.unshift(b.defaultVideo):(b.defaultVideo=b.playlists[0].videos[0],b.show(b.defaultVideo),b.initSharing());e.resolve()});return e.promise()};f.prototype.renderPlaylists=function(){var a=
this.playlists,b=a.length,c=this.context.find(".playlists");if(b){if(1<b){c.append('<option value="0">Other Video Playlists:</option>');for(var e=0;e<b;e++){if(e<b-1){var d=this.widget.find(".playlist-tab.original");d.clone(!0).removeClass("original").addClass("playlist-tab").insertAfter(d)}d=this.context.find(".playlist-tab").eq(e);this.setCurrentChannel(d,a[e]);c.append('<option value="'+e+'">'+a[e].title+"</option>")}c.addClass("active")}else 1==b&&(d=this.context.find(".playlist-tab").eq(0),this.setCurrentChannel(d,
a[b-1]));this.widget.find("h3 .video-playlist-name").text(a[0].title);this.widget.find("h3 .video-playlist-count").text(a[0].videos.length);this.initNav()}};f.prototype.setCurrentChannel=function(a,b){var c=d(a,this.context).find(".recent");b.render(c.find("ul"),b.videos);2<b.videos.length?c.find(".playlist-nav").css("visibility","visible"):c.find(".playlist-nav").css("visibility","hidden");c=d(a,this.context).find(".custom");b.render(c.find("ul"),b.videosByViews);2<b.videosByViews.length?c.find(".playlist-nav").css("visibility",
"visible"):c.find(".playlist-nav").css("visibility","hidden")};f.prototype.show=function(a,b){if(!a)return!1;this.currentVideo=a;var c=d(".video-container",this.context),e=d(".video-desc",this.context),f=[this.id,"player"].join("-"),h={allowScriptAccess:"always",allowfullscreen:"true",wmode:"opaque",bgcolor:"ffffff"};a.params.autoplay=b?1:0;d("#"+f).find("iframe").remove();if("yt"==a.videoType){var g=navigator.userAgent.toLowerCase(),i=-1!=g.indexOf("iphone"),g=-1!=g.indexOf("ipad");i||g?new YT.Player(f,
{height:c.height(),width:c.width(),videoId:a.id,playerVars:a.params,events:{onStateChange:this.onytplayerStateChange}}):(a.params.playerapiid=f,swfobject.embedSWF(a.createURL(),[this.id,"player"].join("-"),c.width(),c.height(),"8",null,null,h))}else if("v"==a.videoType)i||g?d("#"+f).append('<iframe src="http://player.vimeo.com/video/'+a.id+'" width="'+c.width()+'" height="'+c.height()+'" frameborder="0"></iframe>'):(d.extend(h,{flashVars:"api=1"}),a.params.player_id=f,swfobject.embedSWF(a.createURL(),
[this.id,"player"].join("-"),c.width(),c.height(),"8",null,null,h));e.find("h4").html(a.title);e.find(".desc-long").html(a.description);this.initToggleDescription()};f.prototype.initPlaylistTabNavigation=function(){var a=this;a.widget.delegate(".playlists","change",function(){var b=d(this),c=b.val(),e;b.find("option:selected").each(function(){e=d(this).text()});a.context.find(".playlist-tab").removeClass("active").addClass("playlist-tab");a.context.find(".playlist-tab").eq(c).addClass("active");a.widget.find("h3 .video-playlist-name").text(e);
return!1})};f.prototype.initToggleDescription=function(){var a=this;this.context.find(".video-desc").find("a").toggle(function(){a.widget.removeClass("withSummary").addClass("withFullSummary");d(this).html("less")},function(){a.widget.removeClass("withFullSummary").addClass("withSummary");d(this).html("more...")});return!1};f.prototype.onytplayerStateChange=function(a){a=this.YOU_TUBE_EVENTS[a];this.pixelTag({pageName:["video",a].join("_"),pageLayout:["video",a,"youtube"].join("_"),pageLabel:["video",
a,"youtube",ContextManager.getPageName()].join("_")})};f.prototype.reloadCurrentVideo=function(){this.show(this.currentVideo)};f.prototype.activateVideo=function(a){a=d(a);this.show(this.findVideo(a.data("video")),!0);d(".playlist-tab li").removeClass("selected").addClass("video-thumb");a.addClass("selected")};f.prototype.findVideo=function(a){for(var b=0;b<this.playlists.length;b++){var c=this.playlists[b].getVideo(a);if(c)return c}return null};f.prototype.clickTrack=function(a){var b={pageName:"VideoWidget",
pageLayout:"VideoWidget",pageLabel:a.label};if(a.parm)b.queryString=a.parm;try{EventManager.publish({eventName:TRACKEVENT,publisherData:b})}catch(c){}};f.prototype.pixelTag=function(a){var b={pageName:"VideoWidget",pageLayout:"VideoWidget",pageLabel:"VideoWidget_"+ContextManager.getPageName()};a&&d.extend(b,a);(new CobaltPixelTag(a)).writePixelTag();try{EventManager.publish({eventName:TRACKEVENT,publisherData:b})}catch(c){}};f.prototype.initSharing=function(){var a=this;this.widget.find(".video-share").find("a").each(function(b,
c){var e=d(c),f=e.attr("title"),h=e.attr("href");e.click(function(){var b=a.currentVideo,c=f.toLowerCase(),e="share"+c,j=ContextManager.getPageName(),k=ContextManager.getDealershipName(),n=a.videoServices[b.videoType],e={pageName:e,pageLayout:[e,n].join("_"),pageLabel:[e,n,j].join("_")};a.pixelTag(e);var l;"yt"==b.videoType?l="http://www.youtube.com/watch?v="+b.id:"v"==b.videoType&&(l="http://vimeo.com/"+b.id);a.shareThis(h+d.param({url:l,u:l,feature:"share",text:"Check out this video from "+k,t:"Check out this video from "+
k}),c);return!1})})};f.prototype.shareThis=function(a,b){if("email"==b){var c=EmailAFriend.getInstance();c.getUrl=function(){var a;delete c.getUrl;return a};c.open(null,null,this.ClickTrackingEvent)}else window.open(a,b+"_share","height=440,width=620,scrollbars=true")};f.prototype.initNav=function(){function a(a){a=d(a.currentTarget);if(!a.hasClass("disabled")){var c=a.siblings(".playlist-nav"),e=a.siblings(".video-playlist-container"),f=e.find("ul"),h=f.find("li").outerHeight(!0),g=e.scrollTop(),
i=Math.floor(e.height()/h);a.hasClass("down")?f.height()>g+h*i?e.animate({scrollTop:Math.min(f.height()-h*i,g+h*i)},function(){e.height()>=f.height()-e.scrollTop()&&a.addClass("disabled");0<e.scrollTop()&&c.removeClass("disabled")}):e.animate({scrollTop:g+20},200).animate({scrollTop:g}):a.hasClass("up")&&(0<g?e.animate({scrollTop:Math.max(0,g-h*i)},function(){0==e.scrollTop()&&a.addClass("disabled");f.height()>e.scrollTop()+h*i&&c.removeClass("disabled")}):f.animate({marginTop:20},200).animate({marginTop:0}))}}
this.widget.delegate(".playlist-nav.up","click",a);this.widget.delegate(".playlist-nav.down","click",a)};f.prototype.init=function(){var a=this,b={};a.defaultVideo&&(b=a.defaultVideo.fetchDetails());d.when(b).done(function(){a.show(a.defaultVideo);a.widget.removeClass("loading");a.initSharing()});if(a.channels.length){var c=a.populatePlaylists(a.channels);d.when(c,b).then(function(){a.renderPlaylists()}).done(function(){d(".playlist-tab:first",a.context).addClass("active").find("li:first").addClass("selected")})}a.initPlaylistTabNavigation();
this.widget.bind("widgetSizeChanged",function(){a.reloadCurrentVideo()});this.widget.find(".video-playlist li").live("click",function(){a.activateVideo(this)})};k.prototype.setDetails=function(a){if("yt"==this.videoType)a=a.entry,this.id=a.media$group.yt$videoid.$t,this.title=a.title.$t,this.description=a.media$group.media$description.$t,this.thumb=a.media$group.media$thumbnail[0].url;else if("v"==this.videoType)a=a[0],this.id=a.id,this.title=a.title,this.description=a.description,this.thumb=a.thumbnail_medium};
k.prototype.fetchDetails=function(){return d.ajax({url:this.url,context:this,dataType:"jsonp"}).done(this.setDetails)};k.prototype.createURL=function(){if("yt"==this.videoType)return this.url="http://www.youtube.com/v/"+this.id+"?"+d.param(this.params);if("v"==this.videoType)return this.url="http://vimeo.com/moogaloop.swf?clip_id="+this.id+"&"+d.param(this.params);throw"Attempted to get the URL of an uninitialized Video";};j.prototype.fetch=function(){var a=this,b=[this.baseURL,this.playlistID].join("/"),
c=d.param({v:"2",alt:"json-in-script"}),b=d.ajax(b+"?"+c,{dataType:"jsonp"});b.done(function(b){a.title=b.feed.title.$t;a.subtitle=b.feed.subtitle.$t;d.each(b.feed.entry,function(b,c){var d=c.media$group;a.addVideo(new k({id:d.yt$videoid.$t,title:d.media$title.$t,description:d.media$description.$t,thumb:d.media$thumbnail[0].url,views:c.yt$statistics.viewCount}))});a.videosByViews=a.sortVideosByViews(a.videos)});return b};j.prototype.sortVideosByViews=function(a){a=a.slice();a.sort(function(a,c){return a.views-
c.views});return a};j.prototype.addVideo=function(a){this.videos.push(a)};j.prototype.render=function(a,b){a.empty();for(var c=0;c<b.length;c++)a.append(d.tmpl(this.template,b[c]))};j.prototype.getVideo=function(a){for(var b=0;b<this.videos.length;b++)if(this.videos[b].id==a)return this.videos[b];return null};window.VideoWidgetController=f;window.VideoWidgetChannel=j;window.Video=k})(jQuery);jQuery(function(){jQuery(window).trigger("videoWidgetLoaded")});
