var playerDiv = "#cwpPlayerDiv";
var akamaiRoot = "http://slice.ca/global/";
var ScriptRoot = akamaiRoot + "ScriptBuilder.ashx";

function cwpPlaySingleVideo(pid){
	var rel = cwpManager.getRelease("PID", pid);
	if (rel){rel.requestVideoPlay();}
	// If a particular function is defined (in this case, on the media page) call it to register the playing release
	if(typeof(singlePlayerInitCall) == "function"){singlePlayerInitCall(rel);}
}

function initializePlayerTemplate(data){
	initData = {
		PID:data.PID,
		pidList:data.pidList,
		site:data.site,
		siteZone:data.siteZone,
		playAllInCat:data.playAllInCat,
		disableOmniture:data.disableOmniture,
		preloadFirstRelease:true
	};
	if (adSync){
		initData.adParameters = adSync.GetAdParameters(data.adParameters);
	}
	// initialize Canwest Player object
	cwpInitializeManager(initData);

	cwpManager.relOnVideoStart(function(rel,pdkEvent){
		// If the ad synching library is available, we will let it handle companion ads
		if (typeof adSync == "object"){
			adSync.TriggerVideoAdSynch(pdkEvent);
		}
	});

	cwpManager.OnPlayClick = function(){
		var rel = cwpManager.rootCats[0].getFirstRelease();
		if (rel){rel.requestVideoPlay();}
	};

	var constructedZone = location.href.replace(/^(?:http\:\/{2})?[^\/]+\//,"").replace(/\?.*$/,"").replace(/\#.*$/,"").replace(/[^\/]*$/,"").replace(/[^\/]+\/\d+\//,"") + "story";
	
	cwpManager.getReleasesFromPidList(constructedZone);
	cwpAjaxLinks();
	if (data.autoPlay){
		cwpPlaySingleVideo(data.pidList[0]);
	}
}

function cwpTogglePause(doPause){
	if (cwpManager.currentPlay){
		doPause = doPause || true;
		cwpManager.currentPlay.pauseVideo(doPause);
	}
}

/**********
* The first function called, which launches everything
**********/
function cwpInsertPlayer(data){
	// set defaults
	if(!ScriptRoot){alert("ScriptRoot must be defined somewhere in the page or the template.");}
	if(!data.PID){alert("You must specify the PID.");return;}
	if(!data.site){alert("You must specify the site.");return;}
	data.videoWidth = data.videoWidth || 608;
	data.videoHeight = Math.floor(data.videoWidth / 16 * 9 )+20;
	data.playAllInCat = (data.playAllInCat===true); // Assume false unless explicitly set true
	data.autoPlay = (data.autoPlay===true);
	data.disableOmniture = (data.disableOmniture===true);

	if (!data.skipFlash){
		cwpLoadFlashObjects({
			//skipPlayerOutput:true, // Because of a FF bug, we will not output the player flash until we have expanded that section
			width:data.videoWidth,
			height:data.videoHeight,
			akamaiRoot:akamaiRoot,
			rssURL:"", // no rss for single clip player
			targetContainer:playerDiv.substring(1),
			omnitureAccount:data.omnitureAccount || "canslice",
			variables:{
				backgroundColor:"0x131313",
				controlBackgroundColor:"0x212025",
				controlColor:"0xffffff",
				controlFrameColor:"0x545759",
				controlHoverColor:"0xFFFFFF",
				controlSelectedColor:"0xFFFFFF",
				frameColor:"0x000000",
				pageBackgroundColor:"0x000000",
				playProgressColor:"0xB42816",
				scrubberColor:"0x79797B",
				scrubberFrameColor:"0x333333",
				scrubTrackColor:"0xFF0000",
				loadProgressColor:"0xFFFFFF",
				textBackgroundColor:"0x79797B",
				textColor:"0xFFFFFF",
				previewImageURL:data.overlayImage || ""
			},
			bgcolor:"0x131313"
		});
	}
	// Notify the player tracker that this player is in use
	var parsedLocation = window.location.href.replace(/(&|)(release|category)(ID|id|PID|pid)=[^&]*/,"").replace(/[?]$/,"");
	var sbStr = ScriptRoot + "?type=Video&u=" + escape(parsedLocation) + "&PID=" + data.PID + "&playerTag=" +data.playerTag;
	logDebug("Scriptbuilder: " + sbStr);
	//$.get(sbStr);
	$(document).ready(function(){initializePlayerTemplate(data);});
}
