/*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);
 
 if ("a" === oTarget.nodeName.toLowerCase()) {
 YAHOO.util.Event.stopEvent(e);
 this.playID(this.videoIdFromLink(oTarget));
 }
 
 },
 this, true);
 }
 
 this.initPlayer();
 },
 getAllIDs: function () {
 
 if (this.oConfig.videolinkCont && YAHOO.util.Dom.get(this.oConfig.videolinkCont)) {
 var list = YAHOO.util.Dom.get(this.oConfig.videolinkCont).getElementsByTagName("a"),
 idList = [],
 i,
 len,
 temp;
 
 for (i = 0, len = list.length; i < len; i = i + 1) {
 temp = this.videoIdFromLink(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;
 },
 events: function (sType, data) {
 // switch(sType){
 // case "onPlayerReady":
 // }
 },
 playID: function (nID) {
 oPlayer.addToPlaylist({
 mediaId: nID
 },
 "0", "1");
 oPlayer.setPlaylistPosition(0);
 oPlayer.playMedia();
 }
 };
}());
YAHOO.util.Event.onAvailable("sp-videocont",
function () {
 YAHOO.EU.VideoPlayer.init({
 swfUri: "http://d.yimg.com/m/up/ypp/sports-eu/player.swf",
 videolinkCont: "sp-video-list",
 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.VideoPlayer.init({
 swfUri: "http://d.yimg.com/m/up/ypp/sports-eu/player.swf",
 videolinkCont: "sp-video-list",
 videoCont: "sp-videocont-full",
 width: 648,
 height: 360,
 autoStart: 1,
 lang: YAHOO.EU.property.replace("ukie", "en"),
 // Prevent share carousel.
 shareEnable: 0
 });
});