// This file contains functions for the Dashboard area and include methods to setup the
// pagination control and for season selection

// This value is the width of each item (figure out how to determine with JS instead?)
var dash_item_width = 320;

var oldId;
var row_count = 1;
// Toggle display of the hidden description for each item overlayed on top
function DashboardEpisodeInfo(id)
{
	//kills previously opened items
	if(oldId && oldId != id) {
		$("clipInfo"+oldId).hide();
	}
	var info = $("clipInfo"+id);
	if (info)
	{
		info.style.display = (info.style.display=="block")?"none":"block";
		//CONTENT LIMITER HAS BEEN MOVED TO A FUNCTION
		/* BEGIN SLIDER */
		var slider = new Control.Slider('handle'+id, 'track'+id, {
  			axis: 'vertical',
  			onSlide: function(v) { scrollVertical(v, $('scrollable'+id), slider);  },
  			onChange: function(v) { scrollVertical(v, $('scrollable'+id), slider); }
  		});
		// disable vertical scrolling if text doesn't overflow the div
		if ($('scrollable'+id).scrollHeight <= $('scrollable'+id).offsetHeight && $('clipInfo'+id).getStyle('display')=="block") {
			slider.setDisabled();
			$('track'+id).hide();
		}
		/* END SLIDER */
	}
	//remember what is opened
	oldId = id;
}

//This function trims any div's innerHTML to a set content level
function ContentLimiter(id) {
	//SET CHAR LIMIT
	var contentLimiter = 250;
	var clipContentStr = $("clipInfoContent"+id).innerHTML;
	//only limit content if its beyond the desired limiter
	if(clipContentStr.length >= contentLimiter) {
		$("clipInfoContent"+id).innerHTML = clipContentStr.substr(0,contentLimiter)  + "...";
	}
}

//this is the function where the little bubble buttons are created
// Initialize a MN.PageNav object for each season, passing it info about the items it will control
function ShowDashboardPageNav()
{          
	
	//MN.Log.ShowPane();
	var seas = dash_full_ep_seas;
	
	for (var n=0; n < seas.length; n++)
	{
		var seas_num = seas[n].seasonNum;
		var ep_count = seas[n].showCount;
		$("fullEpSeas" + seas_num).style.width = "%spx".format(ep_count * dash_item_width);
		if (!isNaN(ep_count) && ep_count > 3)
		{
			var config = {"item_width":dash_item_width,"item_count":ep_count,"button_prefix":"dashBut","button_class":"dashPageNavButton",
						  "button_active_class":"active","holder_id":"dashPageNav" + seas_num,"season":seas_num,"page_holder":"fullEpSeas" + seas_num};
			var pageNav = new MN.PageNav(config);
		}
	}
}

function ShowDashboardAgeGatePageNav()
{          
	
	//MN.Log.ShowPane();
	var seas = dash_full_ep_seas;
	
	for (var n=0; n < seas.length; n++)
	{
		var seas_num = seas[n].seasonNum;
		var ep_count = shortFormCount;
		$("ageGateSeas" + seas_num).style.width = "%spx".format(ep_count * dash_item_width);
		if (!isNaN(ep_count) && ep_count > 3)
		{
			var config = {"item_width":dash_item_width,"item_count":ep_count,"button_prefix":"dashAgeGateBut","button_class":"dashPageNavButton",
						  "button_active_class":"active","holder_id":"dashAgeGatePageNav" + seas_num,"season":seas_num,"page_holder":"ageGateSeas" + seas_num};
			var pageNav = new MN.PageNav(config);
		}
	}
}



function ShowDashboardExtrasPageNav()
{
	
	var config =[];
	
	for (var i = 0; i < seasons.length; i++){	
		var seas = seasons[i];
		for (var n=0; n < seas.length; n++)
		{
			var seas_num = seas[n].seasonNum;
			var ep_count = seas[n].showCount;        
			$("extras"+row_count+"Seas" + seas_num).style.width = "%spx".format(ep_count * dash_item_width);
			if (!isNaN(ep_count) && ep_count > 3)
			{
				config[row_count] = {"item_width":dash_item_width,"item_count":ep_count,"button_prefix":"dashExtra"+row_count+"But","button_class":"dashPageNavButton",
							  "button_active_class":"active","holder_id":"dashPageExtra"+row_count+"Nav" + seas_num,"season":seas_num,"page_holder":"extras"+row_count+"Seas" + seas_num};
				var pageNav = new MN.PageNav(config[row_count]);
				row_count++;
			}else{
				row_count++;
			}
		}
	}
	
}

function findPosX(obj)
  {
    var curleft = 0;
    if(obj.offsetParent)
        while(1) 
        {
          curleft += obj.offsetLeft;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.x)
        curleft += obj.x;
    return curleft;
  }

  function findPosY(obj)
  {
    var curtop = 0;
    if(obj.offsetParent)
        while(1)
        {
          curtop += obj.offsetTop;
          if(!obj.offsetParent)
            break;
          obj = obj.offsetParent;
        }
    else if(obj.y)
        curtop += obj.y;
    return curtop;
  }
  
function DashboardSeasonSelect(selName,arrowName, seas)
{
    
	var selDiv = $(selName);
	for (var n=0; n < selDiv.childNodes.length; n++)
	{
		var child = selDiv.childNodes[n];
		if (child.nodeType == 1 && child.className.indexOf("seasonOption")!=-1)
		{
			var vis = child.style.display;
			if (vis != "block") vis = "none";
			child.style.display = (vis == "block")?"none":"block";
			if(IEPresent == true){
			child.style.top =((n*15) - 0)+"px";
			child.style.zIndex = "1000";
			}else{
			child.style.top =((n*9) - 10)+"px";
			child.style.zIndex = "1000";
			}
			
			$(arrowName).src = "../images/yellow_arrow_" + ((vis == "block")?"down":"up") + ".gif";
		}
		
		
	}
	seasArr = $(seasonSelection);
	//selDiv.style.margin = "-20px 0 0 0";
	if(IEPresent == true){
	selDiv.style.left = (findPosX(seasArr)-35) + $(preDetails).getWidth() + "px";
	//alert((findPosX(seasArr)-45) + $(preDetails).getWidth());
	selDiv.style.zIndex = "1000";
	}else{
	//alert((findPosX(seasArr)-45));
	selDiv.style.left = (findPosX(seasArr)-45) + "px";
	selDiv.style.zIndex = "1000";
	
	}	
}

function DashboardSeason(seas_num, selName, arrowName, numName, itemPre, navPre, obj, seas)
{


getEl = "seas"+seas_num;
$(getEl).addClassName("seasonSelected");

if(seas_num==0){
printNum = 1;
}else{
printNum = seas_num;
}
$(seasonSelectionNum).innerHTML =printNum;
	

	DashboardSeasonSelect(selName,arrowName, seas);
	for (var n=0; n < seas.length+1; n++)
	{
	
	var trySeas = "seas"+n;
	
	if(trySeas!=getEl){
	if($(trySeas)){
$(trySeas).removeClassName("seasonSelected");
}
}
	if(seas[n].seasonNum){
		var seas_val = seas[n].seasonNum;
		}
		
		if (seas_val == seas_num)
		{
			MN.CSS.RemoveClass($(itemPre + seas_val), "hidden");
			MN.CSS.RemoveClass($(navPre + seas_val), "hidden");
		}
		else
		{
			MN.CSS.AddClass($(itemPre + seas_val), "hidden");
			MN.CSS.AddClass($(navPre + seas_val), "hidden");
		}
	}
	MN.SetInnerText($(numName), seas_num);
	
	var selDiv = $(selName);
	for (var n=0; n < selDiv.childNodes.length; n++)
	{
		var child = selDiv.childNodes[n];
		if (child.nodeType == 1 && child.className.indexOf("seasonOption")!=-1)
		{
		
	MN.CSS.RemoveClass(child, "seasonSelected");
		}
	}
	//child.AddClass("seasonSelected");
	MN.CSS.AddClass(seasonLink, "seasonSelected");
	
$(seasonSelection).innerHTML = "<a class=\"seasonLink\" href=\"javascript:DashboardSeasonSelect('seasonOptions','seasonLinkArrow', dash_full_ep_seas);\">Season"+seas_num+"</a>";
}

// scroll the element vertically based on its width and the slider maximum value
function scrollVertical(value, element, slider) {
	element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
}