
var coad = new CoAdBrowser("coadTarget");

MTVM.player = new function(){
	
	this.configParams;
	this.playerMediaHost;
	this.videoUri;
	this.playlistUri;
	this.continuousPlay = true;
	this.fullEpisode = false;
	this.player;
	this.instanceOfMTVMPlayer = null;
	
	var coAdInterval;
	var onNoAdObject;
	
	this.init = function(){}	
	
	this.createVideoPlayer = function(swfPlayerId, flashVars, movieSrc, height)
	{
		var playerHeight =height?height:640;
		
		document.write('<object height="390" width="'+playerHeight+'" name="'+swfPlayerId+'" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" id="'+swfPlayerId+'">'+
				'<param value="always" name="allowScriptAccess"/>'+
				'<param value="true" name="allowFullScreen"/>'+
				'<param value="transparent" name="wmode"/>'+
				'<param value="true" name="autoPlay"/>'+
				'<param name="movie" value="'+movieSrc+'"/>'+									
				'<param name="flashvars" value="'+flashVars+'">'+				
				'<embed height="390" width="'+playerHeight+'" allowscriptaccess="always" allowfullscreen="true" wmode="transparent" type="application/x-shockwave-flash" name="'+swfPlayerId+'" id="'+swfPlayerId+'" autoPlay="true"'+
				'flashvars="'+flashVars+'" src="'+movieSrc+'"'+
				'/>'+
			'</object>');
	}
	
	/* not using this
	this.addPlayer = function(host,uri,config,fullepisode) {
		var flashVersion = "9.0.115.0";		
		var configParams = config;
		this.configParams = config;
	  this.playerMediaHost = host;
	  this.videoUri = uri;
	  this.fullEpisode = fullepisode;
	
		var so = new SWFObject(MTVM.player.playerMediaHost + "/" + MTVM.player.videoUri, "embeddedPlayer", "100%", "100%", flashVersion, "#cccccc");
		so.addVariable("configParams", encodeURIComponent(configParams));
		
		so.addParam("allowFullScreen",true);
		so.addParam("allowScriptAccess","always");
		so.addParam("wmode", "transparent");
		so.addParam("swliveconnect",true);
		so.addParam("bgcolor", "#000000");
		so.write("videoPlayer");
		controller = new MTVNPlayerController("embeddedPlayer","MTVM.player.onPlayerLoaded");

	}
	*/
	
	this.onPlayerLoaded = function(controller){
		controller.player.addEventListener('NO_AD','MTVM.player.onNoAd');
	}
	
	this.onNoAd = function(info) {
		/*
		var adObject = {};
		adObject.url = info.metadata.adUrl.replace(/pfadx/, "adi").replace(/640x480/,"300x250").replace(/text\/xml/,"text/html");
		adObject.width = 300;
		adObject.height = 250;
		*/
		
		var adSquare = new MTVM.services.Ads("300x250", 2);
		adSquare.codaParams.ads.size = adSquare.adSize;		
		com.mtvi.ads.AdManager.setDefaultContentType(adSquare.codaParams.ads.defaultContentType);
		com.mtvi.ads.AdManager.setDartSite(adSquare.codaParams.ads.dartSite);
		com.mtvi.ads.AdManager.setKeyValues(adSquare.codaParams.ads.keyValues+adSquare.extraKeyValues);
		com.mtvi.ads.AdManager.setPositionThreshold(adSquare.adPositionThreshold);
		com.mtvi.ads.AdManager.setDefaultSections(adSquare.codaParams.ads.defaultSections);
		
		var adObject = com.mtvi.ads.AdManager.getAd( { size : adSquare.adSize } );
		
		onNoAdObject = adObject;
		MTVM.player.noAdSetCoad();
		//coAdInterval = setInterval('MTVM.player.noAdSetCoad()', 60000);
	}
	
	this.noAdSetCoad = function() {
		MTVM.player.setAdSquare(onNoAdObject);
		//MTVM.player.setCoad(onNoAdObject);
	}
	
	this.setCoad = function(adObject) {
		var src = adObject.url;
		var w = adObject.width;
		var h = adObject.height;
		coad.updateWindow(src, w, h);
	}

	this.setAdSquare = function(adObject) {
		coad.updateWindow(adObject.getUrl(), 300, 250);
	}
	
	this.isContinuousPlay = function() {
		return this.continuousPlay;
	}
	
	/* 	these are for editorial playlist player and should 
		probably be moved to their own instance*/
	this.onEdPlayerLoaded = function(controller) {
		this.player = controller.player;
		player.addEventListener('NO_AD','MTVM.player.onNoAd');
		player.addEventListener('READY','MTVM.player.onReady');		
		//player.addEventListener('PLAYLIST_COMPLETE','MTVM.player.onPlaylistComplete');
		//player.addEventListener('STATE_CHANGE','onStateChange');
	}
	
	this.onReady = function () {		
		//this is called as each _media item_ is ready
		var pl = player.getPlaylist();
		var currentIndex = pl.index + 1; //player is zero-based; xsl is not
		this.setNowPlaying(currentIndex);
		var currentItem = player.getMetadata();

		if (!currentItem.isAd) {
			this.updateBreadCrumb(
					currentItem.artistName,
					currentItem.description,
					currentItem.title
			);
			this.reloadWidgetsIframe( currentItem.guid );
		}
	}
	
	this.onStateChange = function ( state ) {
		if ( state == 'playing') {
			var md = controller.player.playlistMetadata;
			//alert(reflect(controller.player.playlistMetadata));
		}
	}
	
	this.onPlaylistComplete = function () {
		//reserved for future use	
	}
	
	this.setNowPlaying = function(index) {
		$J("div.MTVN_PL-items div").removeClass("MTVN_PL-playing");
		document.getElementById("MTVN_PL-item"+index).className += " MTVN_PL-playing";
	}
	
	this.playVideo = function(index) {
		player.playIndex(index-1);		//player is zero-based; xsl is not
		this.setNowPlaying(index);
	}
	
	this.updateBreadCrumb = function ( artistName, artistId, title ) {
		var breadCrumbString = this.getBreadCrumb( artistName, artistId, title );
		document.getElementById("breadcrumbDetails").innerHTML = breadCrumbString;
	}

	this.getBreadCrumb = function ( artistName, artistId, title ) {
		//var tmpString = "<h1 class=\"grey title\">"+playlistTitle+"&nbsp;&raquo;&nbsp;";
		
		var tmpString = "&nbsp;&raquo;&nbsp;";
		if (artistName != "MTV") {
			tmpString += "<a href=\"/"+artistId+"\" class=\"blue title bold\">";
			tmpString += artistName;
			tmpString += "</a>&nbsp;&raquo;&nbsp;";
		} else {
			tmpString += artistName;
			tmpString += "&nbsp;&raquo;&nbsp;";
		}

		tmpString += title+"&nbsp;";
		//tmpString += "</h1>";
		return tmpString;
	}

	this.reloadWidgetsIframe = function ( guid ) {
		var iframe = document.getElementById("widgetIframe");
		iframe.src = "http://"+ document.location.hostname + "/sitewide/dataservices/videoDetails/index.jhtml?mgid="+guid+"&referrerPlaylistMgid="+controller.playerId		
	}
	
	this.reflect = function (obj){
		var temp = "";
		for (var a in obj){
			temp += a + " = " + obj[a] + "\n";
		}
		return temp;
	}
	
}


var mtvnSetCoad = MTVM.player.setCoad;