// launch the mini mode for the given asin, with the specified width and height
function launchMiniModeWindow(asin, width, height) {
	var miniModeURL = "/gp/video/streaming/mini-mode.html?asin=";
   var newWindow = window.open(miniModeURL + asin, "screening_window", "toolbar=0,scrollbars=0,location=0,statusbar=0,menubar=0,resizable=1,top=0,left=0,width=" + width + ",height=" + height);

   if (!newWindow) {
      var client = getSwfClient();
      if (client) {
         client.openNewWindow(miniModeURL + asin);
      }
   }
}

// launch the mini mode for SD content (resolution 400x300)
function launchMiniMode(asin) {
	launchMiniModeWindow(asin, 400, 300);
}

// launch the mini mode for HD content (resolution 1280x720)
function launchHDMiniMode(asin) {
	launchMiniModeWindow(asin, 1280, 720);
}

function sendMetric(eventType, customerID, sessionID, ubid, userAgent, token) {
   var base_url = "http://atv-sr.amazon.com/proxy/";
   var curTime = new Date().getTime();
   var parameter = "m={\"metrics\":[{\"event_type\":\"" + eventType + "\",\"event_timestamp\":" + curTime + ",\"encrypted_customer_id\":\"" + customerID + "\",\"device_id\":\"PREFLASH\",\"amazon_session_id\":\"" + sessionID + "\", \"unique_browser_id\":\"" + ubid + "\", \"browser\":\"" + userAgent + "\"}]}&f=uploadMetrics&token=" + token + "&c=" + customerID;
	
   var url = base_url + "proxy?" + parameter;
   var ajaxRequest = new AjaxRequest();
   ajaxRequest.IsAsync = false;
   ajaxRequest.forceIFrame = true;
   ajaxRequest.URL = url;
   ajaxRequest.StartRequest();
}

function getSwfClient() {
   var client;
	
   if (navigator.appName.indexOf("Microsoft") != -1) {
      client = window['streaming'];
   } else {
      client = document['streaming'];
   }

   return client;
}

function exitScreeningRoom() {
   var client = getSwfClient();
	
   //guard against externalInterface not being loaded
   if (client && client.exitVideoPlayer) {
      var closingCalls = client.exitVideoPlayer();
   }

   if (closingCalls && g_exitScreeningRoomCalled == false) {
      for (i = 0; i < closingCalls.length; i++) {	
		// if IE, use wacky image mechanism to send update
		if (navigator.appVersion.indexOf("MSIE") != -1) {
			var e = new Image();
			e.src = closingCalls[i];
		}
		// if not IE, send a jQuery get request
		else {
			jQuery.get(closingCalls[i]);
		}
      }
      g_exitScreeningRoomCalled = true;
   }
}

function getHeight() {
   return screen.height;
}

function getWidth() {
   return screen.width;
}

function hasSelectedEpisodes() {
   var client;
	
   if (navigator.appName.indexOf("Microsoft") != -1) {
      client = window['streaming'];
   } else {
      client = document['streaming'];
   }

   if (client) {
      return client.hasEpisodesSelected();
   } else {
      return false;
   }
}

function setEpisodeListHeight(containerID, height){
   document.getElementById(containerID).style.height = height;
}

