function createSilverlight()
{
	var scene = new Stw_slplayback.Page();
	Silverlight.createObjectEx({
		source: "assets/js/Page.xaml",
		parentElement: document.getElementById("silverlightControlHost"),
		id: "SilverlightControl",
		properties: {
			width: "100%",
			height: "100%",
			version: "1.0",
			inplaceInstallPrompt: true
		},
		events: {
			onLoad: Silverlight.createDelegate(scene, scene.handleLoad),
			onError: function(sender, args) {
				var errorDiv = document.getElementById("errorLocation");
				if (errorDiv != null) {
					var errorText = args.errorType + "- " + args.errorMessage;
							
					if (args.ErrorType == "ParserError") {
						errorText += "<br>File: " + args.xamlFile;
						errorText += ", line " + args.lineNumber;
						errorText += " character " + args.charPosition;
					}
					else if (args.ErrorType == "RuntimeError") {
						errorText += "<br>line " + args.lineNumber;
						errorText += " character " +  args.charPosition;
					}
					errorDiv.innerHTML = errorText;
				}	
			}
		},
		initParams: "prerollURL=" + prerollURL + ",linkURL=" + linkURL
	});
}

if (!window.Silverlight) 
	Silverlight = {};

Silverlight.createDelegate = function(instance, method) {
	return function() {
		return method.apply(instance, arguments);
	}
}

function thisMovie(movieName)
{
    if (navigator.appName.indexOf("Microsoft") != -1) {
        return window[movieName]
    }
    else {
        return document[movieName]
    }
}
function loadLogoSmall(LogoSmall, WebsiteURL, Name)
{
	document.getElementById('LogoSmallContainer').innerHTML = '<a href="' + WebsiteURL + '" target="_blank"><img alt="' + Name + '" title="' + Name + '" border="0" src="' + LogoSmall + '"></a>';
}

currentAdBannerSmallURL = "";
function loadAdBannerSmall(bannerURL)
{
	if (currentAdBannerSmallURL != bannerURL)
	{
		document.getElementById("IframeAdBannerSmallContainer").contentWindow.location.replace(bannerURL);
		currentAdBannerSmallURL = bannerURL;
	}
}
function loadLogoBigBoxContainer(StageArt, WebsiteURL, Name)
{
	document.getElementById('LogoBigBoxContainer').innerHTML = '<a href="' + WebsiteURL + '" target="_blank"><img alt="' + Name + '" title="' + Name + '" border="0" src="' + StageArt + '"></a>';
}
function onTSAdvertising()
{
	hideStatusMessage();
	targetSpotOnTop();
	thisMovie("EnterComPlayer").onTSAdvertising();
}
function onTSAdvertised()
{
	logoBigBoxOnTop();
	thisMovie("EnterComPlayer").onTSAdvertised();
}
function onSilverLightAdvertising()
{
	hideStatusMessage();
	document.getElementById('silverlightControlHost').style.visibility = "visible";
	thisMovie("EntercomStationList").onSilverLightAdvertising();
}
function onSilverLightAdvertised()
{
	document.getElementById('silverlightControlHost').style.visibility = "hidden";
	document.getElementById('silverlightControlHost').innerHTML = "";
	thisMovie("EntercomStationList").onSilverLightAdvertised();
}

function onVolumeTS(volume)
{
	 thisMovie("EnterComPlayer").onVolumeTS(volume);
}
function onVolume(volume)
{
	thisMovie("EntercomStationList").onVolume(volume);
	thisMovie("TargetSpotPlayer").onVolume(volume);
}
function onLiveStop()
{
	thisMovie("EntercomStationList").onLiveStop();
	thisMovie("TargetSpotPlayer").onLiveStop();
}
function onLivePlay()
{
	thisMovie("EntercomStationList").onLivePlay();
}
function onTargetSpotID(TargetSpotID)
{
	thisMovie("TargetSpotPlayer").onTargetSpotID(TargetSpotID);
}

var loadStationTimeout = null;
function loadStation(CallSign, sid, nonMusicProgramming, Name, WebcamCallsign)
{
	if(loadStationTimeout != null)
		clearTimeout(loadStationTimeout);

	try
	{
		thisMovie("EnterComPlayer").loadStation(CallSign, sid, nonMusicProgramming, WebcamCallsign);
	}
	catch (e)
	{
		loadStationTimeout = setTimeout(function() { loadStation(CallSign, sid, nonMusicProgramming, Name, WebcamCallsign) }, 1000 );
	}
	
	top.document.title = Name;
}
function onStationListLoaded()
{
	thisMovie("EnterComPlayer").onStationListLoaded();
}
function loadFirstStation()
{
	thisMovie("EntercomStationList").loadFirstStation();
}
function onNowPlaying(nowPlaying)
{
	thisMovie("EntercomStationList").onNowPlaying(nowPlaying);
}
function onAds(ads)
{
	thisMovie("TargetSpotPlayer").onAds(ads);
}
function onStationList()
{
	thisMovie("EntercomStationList").onStationList();
}
function onSongHistory()
{
	thisMovie("EntercomStationList").onSongHistory();
}
//playLive() is called by Silverlight on end of preroll or when preroll is not found / Or by Flash Video if preroll is flv
function playLive()
{
	thisMovie("EnterComPlayer").playLive();
}
function onPlayerLoaded()
{
	thisMovie("EntercomStationList").onPlayerLoaded();
}
function updateStatusMessage(message)
{
	document.getElementById('StreamStatusContainer').style.visibility = "visible";
	document.getElementById('StreamStatusContainer').innerHTML = '<div style="margin-left:5px; margin-top:1px;">' + message + '</div>';
}
function hideStatusMessage()
{
	document.getElementById('StreamStatusContainer').innerHTML = "";
	document.getElementById('StreamStatusContainer').style.visibility = "hidden";
}
function loadExternalAd(adURL)
{
	hideStatusMessage();
	document.getElementById('IframeBigBoxAdContainer').style.display = "block";
	document.getElementById("IframeBigBoxAdContainer").contentWindow.location.replace(adURL);
	bigboxAdOnTop();
}
function hideExternalAd()
{
	logoBigBoxOnTop();
}
function onAdBreakAdvertising()
{
	thisMovie("EntercomStationList").onAdBreakAdvertising();
}
function onAdBreakAdvertised()
{
	thisMovie("EntercomStationList").onAdBreakAdvertised();
}
function onCustomButton(customButtonDisplayed, buttonName, buttonLink, customButtonPopup)
{
	thisMovie("EnterComPlayer").onCustomButton(customButtonDisplayed, buttonName, buttonLink, customButtonPopup);
}
function setCustomSongHistoryButton(buttonName, buttonLink)
{
	thisMovie("EnterComPlayer").setCustomSongHistoryButton(buttonName, buttonLink);
}

//Flash Video
function onPlayAdVideo(videoURL, linkURL)
{
	adVideoOnTop();
	thisMovie("EnterComVideo").onPlayAdVideo(videoURL, linkURL);
}
function onFlashVideoAdvertising()
{
	hideStatusMessage();
	thisMovie("EntercomStationList").onFlashVideoAdvertising();
}
function onFlashVideoAdvertised()
{
	logoBigBoxOnTop();
	thisMovie("EntercomStationList").onFlashVideoAdvertised();
}
function hideEntercomVideo()
{
	logoBigBoxOnTop();
}

//Flash Video
function onPlayLiveVideo(callsign)
{
	document.getElementById('EntercomVideoContainer').style.visibility = "visible";
	thisMovie("EnterComVideo").onPlayLiveVideo(callsign);
}
function controlLiveVideo(command)
{
	thisMovie("EnterComVideo").controlLiveVideo(command);
}
function controlLiveControls(command)
{
	thisMovie("EnterComPlayer").controlLiveControls(command);
}

function onSetTimeOut(timeout)
{
	thisMovie("EnterComPlayer").onSetTimeOut(timeout);
}
function loadMainBackground(mainBackgroundImageURL)
{
	document.getElementById('TopBackground').style.backgroundImage = "url('"+ mainBackgroundImageURL +"')";
}
function loadDefaultBackground()
{
	document.getElementById('TopBackground').style.backgroundImage = "url('assets/img/background.jpg')";
}
function showSharing(sid, twitter_status)
{
	document.getElementById('IframeSharingContainer').style.display = "block";
	document.getElementById("IframeSharingContainer").contentWindow.location.replace("sharing.php?id=" + sid + "&twitter_status=" + twitter_status);
}
function hideSharing()
{
	document.getElementById('IframeSharingContainer').style.display = "none";
}
function mogoTracking(sid)
{
	if (sid.toLowerCase() == "kbwf")
	{
		document.getElementById("mogo_ad_iframe").contentWindow.location.replace("http://adserver.mogo-spotlight-targeting.com/track.fcgi?merchantid=2105552234&amp;eventid=0&amp;ifmode=1");
		document.getElementById("mogo_ad_image").src = "http://ads.revsci.net/adserver/ako?rsi_noads=1&amp;rsi_pixel=1&amp;rsi_account=1CF61DF0FEE3073A3170940821E988E5&amp;rsi_site=25DC4555F0A2504034E529F580A005DD&amp;rsi_event=8853E3AE8517FF6FAC178A1EB9D70898";
	}
}
function quantCastTracking(sid)
{
	if (sid.toLowerCase() == "weei")
	{
		_qoptions={
		qacct:"p-e1LfmtdyqxIM6"
		};
		
		$.ajax({
		   type: "GET",
		   url: "http://edge.quantserve.com/quant.js",
		   dataType: "script"
		 });
	}
}
function gaTracking(sid)
{
	try
	{
		var firstTracker = _gat._getTracker("UA-15118652-2"); //STW
		firstTracker._initData();
		firstTracker._trackPageview();
		if (sid.toLowerCase() == "weei")
		{
			var secondTracker = _gat._getTracker("UA-7418341-1"); //WEEI
			secondTracker._setDomainName("none");
			secondTracker._setAllowLinker(true);
			secondTracker._initData();
			secondTracker._trackPageview();
		}
	}
	catch (err)
	{}
}
function bigboxAdOnTop()
{
	document.getElementById('BigBoxAdContainer').style.visibility = "visible";
	document.getElementById('LogoBigBoxContainer').style.visibility = "hidden";
	document.getElementById('TargetSpotContainer').style.visibility = "hidden";
	document.getElementById('EntercomVideoContainer').style.visibility = "hidden";
}
function logoBigBoxOnTop()
{
	document.getElementById('LogoBigBoxContainer').style.visibility = "visible";
	document.getElementById('TargetSpotContainer').style.visibility = "hidden";
	document.getElementById('BigBoxAdContainer').style.visibility = "hidden";
	document.getElementById('EntercomVideoContainer').style.visibility = "hidden";
}
function adVideoOnTop()
{
	document.getElementById('EntercomVideoContainer').style.visibility = "visible";
	document.getElementById('LogoBigBoxContainer').style.visibility = "hidden";
	document.getElementById('TargetSpotContainer').style.visibility = "hidden";
	document.getElementById('BigBoxAdContainer').style.visibility = "hidden";
}
function targetSpotOnTop()
{
	document.getElementById('TargetSpotContainer').style.visibility = "visible";
	document.getElementById('EntercomVideoContainer').style.visibility = "hidden";
	document.getElementById('LogoBigBoxContainer').style.visibility = "hidden";
	document.getElementById('BigBoxAdContainer').style.visibility = "hidden";
}
