if (window.NTorus === undefined)
    window.NTorus = new Object();

if (window.NTorus.Showcase === undefined)
    window.NTorus.Showcase = new Object();

if (window.NTorus.Showcase.Common === undefined)
    window.NTorus.Showcase.Common = new Object();

//--------------------------------------------------------------------
//Common Static Method
//--------------------------------------------------------------------
window.NTorus.Showcase.Common.createVideoArrayFromJsonString = function(jsonString) {

    var list = eval("("+jsonString+")");

    if (list === undefined || list.VideoList === undefined)
        throw new Error("Video List data malformed.");

    return list.VideoList;
};

//--------------------------------------------------------------------
//Common Static Method
//--------------------------------------------------------------------
window.NTorus.Showcase.Common.createVideoGroupArrayFromJsonString = function(jsonString) {

    var list = eval("("+jsonString+")");

    if (list === undefined || list.VideoGroupList === undefined)
        throw new Error("Video Group List data malformed.");

    return list.VideoGroupList;
};


//--------------------------------------------------------------------
//Common Static Method
//--------------------------------------------------------------------
window.NTorus.Showcase.Common.getSpotCreditsFromJsonString = function(jsonString) {

    var data = eval("("+jsonString+")");

    if (data === undefined || data.SpotCredits === undefined)
        throw new Error("Spot Credits data malformed.");

		//TODO: Handle errors returned

		return data.SpotCredits;
}

 //TODO: Rewrite this cut & paste code.
 //--------------------------------------------------------------------
 function getFlashMovie(movieName) {
  var isIE = navigator.appName.indexOf("Microsoft") != -1;
  return (isIE) ? window[movieName] : document[movieName];
 }

//--------------------------------------------------------------------
var isPurchasedVid = false;
var purchasedVideoInfo = null;
var PurchasedLabel = "Replay";
var videoElementLookup = {};
function buildVideoPanel(
    videoArray, panelElementName, includeShowName, useDirectLink) 
{

    // Fill in optional parameters
    if (!panelElementName) panelElementName = "VideoPanel";

	var vg;
   var text;
   var video, thumbnail, innerPanel, status, title, seasonInfo, points, v;
	var vp = document.getElementById(panelElementName);
	for(var i = 0; i < videoArray.length; i++) {

		v = videoArray[i];

        video       = document.createElement("A");
        thumbnailHolder = video.appendChild(document.createElement("DIV"));
        thumbnail   = thumbnailHolder.appendChild(document.createElement("IMG"));
        status      = video.appendChild(document.createElement("DIV"));
        points      = video.appendChild(document.createElement("DIV"));
        info        = video.appendChild(document.createElement("IMG"));

        if (videoElementLookup[v.id]) {
            videoElementLookup[v.id].push(video);
        } else {
            videoElementLookup[v.id] = [video];
        }
        
        if ( isPurchasedVid && purchasedVid == v.id ) {
            purchasedVideoInfo = v;
        }

        //video
        video.setAttribute("purchased", v.isPurchased ? "true" : "false");
        if (useDirectLink) 
        {
            video.onclick = new Function(
                "location.href = 'arena.php?vgid=" + v.vgid + "&vid=" + v.id + "';");
        }
        else {
            video.onclick = new Function(
                "clickVideo('" + v.id + "');return false;");
                
        }
        NTorus.Dom.SetStyleClass(
            video, v.isPurchased ? "Video_Purchased" : "Video");
            
        //thumbnailHolder
        NTorus.Dom.SetStyleClass(thumbnailHolder, "VideoThumbnailHolder");

    //thumbnail
    thumbnail.setAttribute("src", v.thumbnailUrl);
    thumbnail.setAttribute("width", "120");
    thumbnail.setAttribute("height", "90");
    NTorus.Dom.SetStyleClass(thumbnail, "VideoThumbnail");

    //playOverlay
    var playOverlay = video.appendChild(document.createElement("DIV"));
    //playOverlay.setAttribute("src", "images/Common/play_btn_overlay.gif");
    NTorus.Dom.SetStyleClass(playOverlay, "VideoPlayOverlay");
    
    //status
    NTorus.Dom.SetStyleClass(status, "VideoStatus");

    //showName
    if (includeShowName) {
        var showName = video.appendChild(document.createElement("DIV"));
        text = v.showName.length > 28 ? v.showName.substring(0, 23)+"..." : v.showName; 
        showName.appendChild(document.createTextNode(text));
        NTorus.Dom.SetStyleClass(showName, "ShowName");
    }
    
    //title
    title = video.appendChild(document.createElement("DIV"));
    text = v.title.length > 28 ? v.title.substring(0, 23)+"..." : v.title; 
    title.appendChild(document.createTextNode(text));
    NTorus.Dom.SetStyleClass(title, includeShowName ? "VideoName" : "ShowName");

    //seasonInfo
    if (v.seasonInfo) {
        seasonInfo = video.appendChild(document.createElement("DIV"));
        seasonInfo.appendChild(document.createTextNode(v.seasonInfo));
        NTorus.Dom.SetStyleClass(seasonInfo, "SeasonInfo");
    }

    //duration
    var durationDiv = video.appendChild(document.createElement("DIV"));
    NTorus.Dom.SetStyleClass(durationDiv, "DurationInfo");
    durationDiv.appendChild(
        document.createTextNode(v.isFullEpisode ? 'Full Episode' : 'Clip'));
    if (v.duration) {
      durationDiv.appendChild(document.createTextNode(secondsToSegmentedDurationString(v.duration)));
    }
        
    //air date
    if (v.airDate) {
        var airDiv = video.appendChild(document.createElement("DIV"));
        NTorus.Dom.SetStyleClass(airDiv, "AirInfo");
        airDiv.appendChild(
            document.createTextNode("Air Date: " + v.airDate));
    }
        
    //points
    points.appendChild(document.createTextNode(
      v.isPurchased ? PurchasedLabel :
		"Requires " + v.points + " Credit" + (v.points == 1 ? "" : "s") ));
    NTorus.Dom.SetStyleClass(points, "VideoPoints");

    //info
    info.setAttribute("src", "images/arena/video_spot_warning.png");
    NTorus.Dom.SetStyleClass(info, "VideoInfo");

		vp.appendChild(video);
	}
}

//--------------------------------------------------------------------
function secondsToSegmentedDurationString(duration) {
   var str = "";
   if (duration) {
      var h = Math.floor(duration/3600);
      var m = Math.floor(duration/60)-(h*60);
      var s = duration-(h*3600)-(m*60);
      str = 
         " (" +
         (h?(h<10?"0"+h:h)+":":"") +
         (m?(m<10?"0"+m:m)+":":(h?"00:":"")) +
         (s?(s<10?"0"+s:s):(m?"00":"")) +
         ")";
   }
   return str;
}

//--------------------------------------------------------------------
function displayShowsMenu() {
    var dropdown = document.getElementById('ShowsDropdown');
    if (dropdown) {
        var parentDiv = document.getElementById('Content');
        var x = 0, y = 0;
        while (parentDiv) {
            x += parentDiv.offsetLeft;
            y += parentDiv.offsetTop;
            parentDiv = parentDiv.offsetParent;
        }
        dropdown.style.left = x + 'px';
        dropdown.style.top = (y - 10) + 'px';
        dropdown.style.display = 'block';
    }
}

//--------------------------------------------------------------------
function hideShowsMenu() {
    var dropdown = document.getElementById('ShowsDropdown');
    if (dropdown) {
        dropdown.style.display = 'none';
    }
}

//--------------------------------------------------------------------
function displayLearnMore() {

   //scroll(0,0);
   
   var ghostElm = document.getElementById('GhostOverlay');
   if (!ghostElm) {
      ghostElm = document.createElement("div");
      ghostElm.setAttribute("id","GhostOverlay");
      document.body.appendChild(ghostElm);
   }
   NTorus.Dom.SetStyleClass(ghostElm, "ghostOverlay");
   
   var knockoutElm = document.getElementById('GhostOverlayKnockout');
   if (!knockoutElm) {
      knockoutElm = document.createElement("div");
      knockoutElm.setAttribute("id","GhostOverlayKnockout");      
      document.body.appendChild(knockoutElm);
   }
   
   var learnMoreElm  = document.getElementById('LearnMore');
   if (!learnMoreElm) {
      learnMoreElm = document.createElement("div");   
      learnMoreElm.setAttribute("id","LearnMore");   
      document.body.appendChild(learnMoreElm);   
   }

   window.onmousewheel = document.onmousewheel = 
      window.onscroll = document.body.onscroll = function(){return false;};
   
   var flashvars = {
      videoUrl: "rtmp://cp81408.edgefcs.net/ondemand/mp4:corp/LearnMoreVideo_v2.mp4"
   };
   
   var params = {
     menu: "false",
     allowscriptaccess: "always",
     allowfullscreen: "true",
     wmode: "transparent"
   };
   
   var attributes = {
     id:"LearnMorePlayer",
     name:"LearnMorePlayer"
   };

   swfobject.embedSWF(
     "flash/LearnMore.swf", 
     "LearnMore", 
     "664", 
     "396", 
     "10.0.0",
     "js/swfobject/expressInstall.swf", 
     flashvars, 
     params, 
     attributes);  

     
   if (document.getElementById('banner')) {
      try {
         getFlashMovie("banner").stopCycle();
      } catch(err) {}
   } else if (document.getElementById('player')) {
      try {
         getFlashMovie("player").pauseVideo();
      } catch(err) {}
   }
}

//--------------------------------------------------------------------
function hideLearnMore() {
   var learnMorePlayer = document.getElementById('LearnMorePlayer');
   if (learnMorePlayer) {
      setTimeout("document.body.removeChild(document.getElementById('LearnMorePlayer'))",1);
   }
   var ghostElm = document.getElementById('GhostOverlay');
   if (ghostElm) {
      document.body.removeChild(ghostElm);
   }   
   var knockoutElm = document.getElementById('GhostOverlayKnockout');
   if (knockoutElm) {
      document.body.removeChild(knockoutElm);
   }
   
   window.onmousewheel = document.onmousewheel = 
      window.onscroll = document.body.onscroll = null;
   
   if (document.getElementById('banner')) {
      try {
         getFlashMovie("banner").startCycle();
      } catch(err) {}
   }
}
