/*jslint bitwise: true, eqeqeq: true, immed: true, newcap: true, nomen: true, 
 onevar: true, plusplus: true, regexp: true, undef: true, white: true, 
 indent: 4 */
/*global YAHOO:true, document:true, SWFObject:true */
/*
*/
YAHOO.EU.VideoPlayer = function () {
 var nPlayerId = YAHOO.util.Dom.generateId(),
 // oLinksCont,
 oPlayer;
 return {
 init: function (oConfig) {
 this.oConfig = oConfig;
 
 if (oConfig.videolinkCont && ("undefined" === typeof this.oConfig.samePlayer || 1 === this.oConfig.samePlayer)) {
 YAHOO.util.Event.on(oConfig.videolinkCont, "click",
 function (e) {
 var oTarget = YAHOO.util.Event.getTarget(e),
 videoId;
 
 if ((/^(a|button)$/).test(oTarget.tagName.toLowerCase())) {
 YAHOO.util.Event.stopEvent(e);
 videoId = this.videoIdFromControl(oTarget);
 this.playID(videoId);
 }
 
 },
 this, true);
 }
 // Stop player on escape
 this.escapeListener = new YAHOO.util.KeyListener(
 document,
 {
 "keys": YAHOO.util.KeyListener.KEY.ESCAPE
 },
 {
 "fn": this.pause, 
 "scope": this, 
 "correctScope": true
 });
 this.escapeListener.enable();
 this.initPlayer();
 
 },
 getAllIDs: function () {
 
 if (this.oConfig.videolinkCont && YAHOO.util.Dom.get(this.oConfig.videolinkCont)) {
 var list = YAHOO.util.Dom.getElementsBy(
 function isControl(el) {
 return (/^(a|button)$/).test(el.tagName.toLowerCase());
 },
 '*',
 YAHOO.util.Dom.get(this.oConfig.videolinkCont)),
 idList = [],
 i,
 len,
 temp;
 
 
 for (i = 0, len = list.length; i < len; i = i + 1) {
 
 temp = this.videoIdFromControl(list[i]);
 
 if (temp) {
 idList.push(temp);
 }
 
 }
 
 return idList.join(",");
 }
 
 },
 addParam: function (oFlash, param, value) {
 //console.log("Param: %s, Value: %s", param, value);
 oFlash.addParam(param, value);
 },
 addVariable: function (oFlash, variable, value) {
 //console.log("Variable: %s, Value: %s", variable, value);
 oFlash.addVariable(variable, value);
 },
 initPlayer: function () {
 var oFlash = new SWFObject(this.oConfig.swfUri, nPlayerId, 
 this.oConfig.width, this.oConfig.height, "9", "#ffffff"),
 cosmosIDs = '';
 
 if (typeof this.oConfig.cosmosid !== "undefined") {
 cosmosIDs = this.oConfig.cosmosid;
 }
 if (typeof this.oConfig.videolinkCont) {
 
 if (cosmosIDs) {
 cosmosIDs += "," + this.getAllIDs();
 }
 else {
 cosmosIDs = this.getAllIDs();
 }
 
 }
 this.addParam(oFlash, "allowScriptAccess", "always");
 this.addParam(oFlash, "allowFullScreen", "true");
 this.addVariable(oFlash, "enableFullScreen", "1");
 this.addVariable(oFlash, "lang", this.oConfig.lang);
 this.addParam(oFlash, "wmode", "opaque");
 if (this.oConfig.vycPlaylistID) {
 // Video.yahoo.com
 this.addVariable(oFlash, "playlistId", this.oConfig.vycPlaylistID);
 this.addVariable(oFlash, "isCarouselEnabled", 
 (typeof this.oConfig.carouselEnabled !== "undefined") ?
 this.oConfig.carouselEnabled:
 0);
 this.addVariable(oFlash, "intl", (typeof this.oConfig.intl !== "undefined") ? this.oConfig.intl: 'fr');
 }
 else {
 // YEP
 this.addVariable(oFlash, "autoPlay",
 (typeof this.oConfig.autoStart !== "undefined") ?
 this.oConfig.autoStart :
 0);
 this.addVariable(oFlash, "infoScreenUI",
 (this.oConfig.infoEnable) ? 'show': 'hide');
 
 if ((cosmosIDs + "").split(',').length > 1) {
 this.addVariable(oFlash, "epGalleryType",
 (typeof this.oConfig.epGalleryType !== "undefined") ?
 this.oConfig.epGalleryType: "current");
 this.addVariable(oFlash, "epCountdownDuration", "10");
 }
 
 this.addVariable(oFlash, "shareScreenUI",
 (this.oConfig.shareEnable) ? 'show': 'hide');
 this.addVariable(oFlash, "browseCarouselUI",
 (this.oConfig.shareEnable) ? 'show': 'hide');
 this.addVariable(oFlash, "vid", cosmosIDs);
 this.addVariable(oFlash, 'eventHandler', 'YAHOO.EU.VideoPlayer.events');
 }
 oFlash.write(YAHOO.util.Dom.get(this.oConfig.videoCont).id);
 oPlayer = YAHOO.util.Dom.get(nPlayerId);
 // YAHOONEWSGLOBALS.embeddedVideoObj = oPlayer;
 },
 // videoIdFromLink: function (oLink) {
 // var id = parseInt(oLink.href.split("cl=") [1], 10);
 // 
 // if (!id) {
 // id = parseInt(oLink.id.split("-") [1], 10);
 // }
 // 
 // return id;
 // },
 videoIdFromControl: function (oControl) {
 // Bug 3368155:
 var matches = oControl.className.match(/video-id-(\w+\d+)/);
 
 if (matches) { 
 return matches[1];
 }
 
 }, 
 events: function (sType, data) {
 // switch(sType){
 // case "onPlayerReady":
 // }
 },
 playID: function (nID) {
 oPlayer.addToPlaylist({
 mediaId: nID
 },
 "0", "1");
 oPlayer.setPlaylistPosition(0);
 oPlayer.playMedia();
 },
 pause: function () {
 try {
 oPlayer.pauseMedia();
 } 
 catch (e) {
 /* Not able to pause at the moment. */
 }
 
 }
 };
};
YAHOO.util.Event.onAvailable("sp-videocont",
function () {
 YAHOO.EU.RelatedVideoPlayer = YAHOO.EU.VideoPlayer();
 YAHOO.EU.RelatedVideoPlayer.init({
 swfUri: "http://d.yimg.com/m/up/ypp/sports-eu/player.swf",
 videolinkCont: YAHOO.util.Dom.getElementBy(
 function isRelatedVideoPlaylist(el) { 
 return YAHOO.util.Dom.hasClass(el, "sp-video-list");
 },
 "ul",
 "sp-related-video"),
 videoCont: "sp-videocont",
 width: 292,
 height: 250,
 lang: YAHOO.EU.property.replace("ukie", "en"),
 shareEnable: 0,
 showSkip: 0
 });
});
YAHOO.util.Event.onAvailable("sp-videocont-full",
function () {
 YAHOO.EU.MainVideoPlayer = YAHOO.EU.VideoPlayer();
 YAHOO.EU.MainVideoPlayer.init({
 swfUri: "http://d.yimg.com/m/up/ypp/sports-eu/player.swf",
 videolinkCont: YAHOO.util.Dom.getElementBy(
 function isRelatedVideoPlaylist(el) { 
 return YAHOO.util.Dom.hasClass(el, "sp-video-list");
 },
 "div",
 "sp-article"),
 videoCont: "sp-videocont-full",
 width: 648,
 height: 360,
 autoStart: 1,
 lang: YAHOO.EU.property.replace("ukie", "en"),
 // Prevent share carousel.
 shareEnable: 0
 });
});