// ===============================
//	String replacement method
// ===============================
if(!String.prototype.format) {
	String.prototype.format=function() {
		var _11=arguments;
		var i=0;
		var x=function() {
			return _11[i++];
		};
		return this.replace(/(%s)/g,x);
	};
}

// ===============================
//	LegacyFOD: 12.07.2009
//	Holds legacy methods from FOD migration, formerly under the MN namespace
// ===============================
LegacyFOD = {};

LegacyFOD.nonIE = (document.all&&document.attachEvent)?false:true;

LegacyFOD.Class = function(_20) {
	func = function() {
		LegacyFOD.BindMethods(this);
		this.initialize.apply(this,arguments);
	};
	return func;
}

LegacyFOD.BindMethods = function(_21) {
	for(var _22 in _21) {
		var _23=_21[_22];
		if(typeof (_23)=="function"&&_22!="initialize") {
			_21[_22]=LegacyFOD.MakeBound(_21,_23);
		}
	}
}

LegacyFOD.MakeBound = function(_24,_25) {
	var _26=_25;
	if(_26.__originalMethod) {
		_26=_26.__originalMethod;
	}
	var _27 = function() {
		return _26.apply(_24,arguments);
	};
	_27.__originalMethod=_26;
	return _27;
}

LegacyFOD.CSS = {
	AddClass:function(_d7,_d8) {
		if(!_d8) {
			return;
		}
		var el=$(_d7);
		if(!el) {
			return;
		}
		var _da=LegacyFOD.CSS.GetClasses(el);
		for(var i=0,cl=_da.length;i<cl;i++) {
			if(_da[i]==_d8) {
				return;
			}
		}
		el.className=el.className+" "+_d8;
	},
	RemoveClass:function(_dc,_dd) {
		if(!_dd) {
			return;
		}
		var el=$(_dc);
		if(!el) {
			return;
		}
		var _df=LegacyFOD.CSS.GetClasses(el);
		var _e0=[];
		for(var i=0,cl=_df.length;i<cl;i++) {
			var _e2=_df[i];
			if(_e2!=_dd) {
				_e0.push(_e2);
			}
		}
		el.className=_e0.join(" ");
	},
	GetClasses:function(_e3) {
		var el=$(_e3);
		if(!el||!el.className) {
			return [];
		}
		return el.className.split(" ");
	},
	GetElementsByClassName:function(_e5,_e6,_e7) {
		var _e8=[];
		if(_e5.hasChildNodes()) {
			var _e9;
			var _ea=_e5.childNodes;
			for(var i=0,cl=_ea.length;i<cl;i++) {
				_e9=_ea[i];
				if(_e9.nodeType==1) {
					if(_e9.className==_e6) {
						_e8.push(_e9);
					}
					if(_e7) {
						_e8=_e8.concat(this.GetElementsByClassName(_e9,_e6,_e7));
					}
				}
			}
		}
		return _e8;
	},
	SetInnerText:function(el,_1f) {
		if(LegacyFOD.nonIE) {
			el.innerHTML=_1f;
		}
		else {
			el.innerText=_1f;
		}
	}
};

// ===============================
//	PageNav: 12.07.2009
//	Formerly in pagenav.js
// ===============================
PageNav = LegacyFOD.Class();

PageNav.prototype.initialize = function(config)
{
	this._item_width = config.item_width;
	this._item_count = config.item_count;
	this._but_prefix = config.button_prefix;
	this._but_class = config.button_class;
	this._but_act_class = config.button_active_class;
	this._holder = config.holder_id;
	this._season = config.season;
	this._page_holder = config.page_holder;

	this._Setup();
}

PageNav.prototype._Setup = function()
{
	var num_nav = Math.floor(this._item_count / 3);
	if (this._item_count % 3 != 0) num_nav++;
	var nav = $(this._holder);

	var but = document.createElement("span");
	but.className = "dashPageNavLeft";
	but.onclick = this.PageLeft;
	if (this._season)
	{
		but.setAttribute("season",this._season);
	}
	nav.appendChild(but);

	for (var n=0; n < num_nav; n++)
	{
		var but = document.createElement("span");
		but.id = "%s%s-%s".format(this._but_prefix, n, this._season);
		but.className = (n!=0)?this._but_class:this._but_class + " " + this._but_act_class;
		but.setAttribute("page",n);
		if (this._season)
		{
			but.setAttribute("season",this._season);
		}
		nav.appendChild(but);
		but.onclick = this.SetPage;
	}

	var but = document.createElement("span");
	but.className = "dashPageNavRight";
	but.onclick = this.PageRight;
	if (this._season)
	{
		but.setAttribute("season",this._season);
	}
	nav.appendChild(but);

	this._active_page = 0;
}

PageNav.prototype.SetPage = function()
{
	var obj = (LegacyFOD.nonIE)?arguments[0].target:window.event.srcElement;
	var page = parseInt(obj.getAttribute("page"),10);
	var seas_num = (this._season)?parseInt(obj.getAttribute("season"),10):0;

	this.GotoPage(page, seas_num);
}

PageNav.prototype.PageLeft = function()
{
	var obj = (LegacyFOD.nonIE)?arguments[0].target:window.event.srcElement;
	var seas_num = (this._season)?parseInt(obj.getAttribute("season"),10):0;

	if (this._active_page > 0) this.GotoPage(this._active_page - 1, seas_num);
}

PageNav.prototype.PageRight = function()
{
	var obj = (LegacyFOD.nonIE)?arguments[0].target:window.event.srcElement;
	var seas_num = (this._season)?parseInt(obj.getAttribute("season"),10):0;

	var max_page = Math.ceil(this._item_count / 3) - 1;
	if (this._active_page < max_page) this.GotoPage(this._active_page + 1, seas_num);
}

PageNav.prototype.GotoPage = function(page, seas_num)
{
	var holder = $(this._page_holder);
	var left = parseInt(holder.style.left,10);
	left = (!isNaN(left))?left:0;
	var newleft = (page * (this._item_width * 3)) * -1;
	var tw = new Tween(holder.style,'left',Tween.backEaseOut,left,newleft,1,'px');
	tw.start();
	LegacyFOD.CSS.RemoveClass($("%s%s-%s".format(this._but_prefix, this._active_page, seas_num)),this._but_act_class);
	LegacyFOD.CSS.AddClass($("%s%s-%s".format(this._but_prefix, page, seas_num)),this._but_act_class);
	this._active_page = page;
}

// ===============================
//	Dashboard: 12.07.2009
//	Formerly in dashboard.js
// ===============================

// 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;

// 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;
}

// Initialize a 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 PageNav(config);
		}
	}
}

function ShowDashboardExtrasPageNav(row_count,dash_extras_seas2)
{

	console.log(dash_extras_seas2)
	for (var n=0; n < dash_extras_seas2.length; n++)
	{
		var row_num = row_count;
		var seas = dash_extras_seas2[n];

		var seasCount = 0;
		if (seas.length == undefined)
		{
			seasCount = 1;
		}

		//for (var n=0; n < seasCount; n++) //MOD BLITZ
		//{
			var seas_num = seas.seasonNum;
			var ep_count = seas.showCount;
			//console.log(ep_count);
			$("extras"+row_num+"Seas" + 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":"dashExtra" + row_num + "But","button_class":"dashPageNavButton", "button_active_class":"active","holder_id":"dashPageExtra" + row_num + "Nav" + seas_num,"season":seas_num,"page_holder":"extras" + row_num + "Seas" + seas_num};
				var pageNav = new PageNav(config);
			}
		//}
	}
}

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){

			/* MOD BLITZ replace with MOD copy from dashboard.js
			var vis = child.style.display;
			if (vis != "block") vis = "none";
			child.style.display = (vis == "block")?"none":"block";
			*/

			var vis = child.style.display;  //MOD copy from dashboard.js
			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))+"px";
				child.style.zIndex = "1000";
			}

			$(arrowName).src = "/fod/images/yellow_arrow_" + ((vis == "block")?"down":"up") + ".gif";
		}
	}

	//selDiv.style.left = ((8 + (40 * (seas.length-1))) * -1) + "px"; //MOD BLITZ replace with MOD copy from dashboard.js

	seasArr = $(seasonSelection);
	if(IEPresent == true){
		//alert(findPosX(seasArr))
		//selDiv.style.left = (findPosX(seasArr)-35) + $(preDetails).getWidth() + "px";
		selDiv.style.left = (findPosX(seasArr)+350) + "px";

		selDiv.style.zIndex = "1000";

	}else{
		//alert(findPosX(seasArr))
		selDiv.style.left = (findPosX(seasArr)-45) + "px";
		selDiv.style.zIndex = "1000";
	}
}

function DashboardSeason(seas_num, selName, arrowName, numName, itemPre, navPre, obj, seas){

	/*
	DashboardSeasonSelect(selName,arrowName, seas);
	for (var n=0; n < seas.length; n++)
	{
		var seas_val = seas[n].seasonNum;
		if (seas_val == seas_num)
		{
			LegacyFOD.CSS.RemoveClass($(itemPre + seas_val), "hidden");
			LegacyFOD.CSS.RemoveClass($(navPre + seas_val), "hidden");
		}
		else
		{
			LegacyFOD.CSS.AddClass($(itemPre + seas_val), "hidden");
			LegacyFOD.CSS.AddClass($(navPre + seas_val), "hidden");
		}
	}
	LegacyFOD.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)
		{
			LegacyFOD.CSS.RemoveClass(child, "seasonSelected");
		}
	}
	LegacyFOD.CSS.AddClass(obj, "seasonSelected");
	*/
	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; n++){

		var trySeas = "seas"+seas[n].seasonNum;

		if(trySeas!=getEl){
			if($(trySeas)){
				$(trySeas).removeClassName("seasonSelected");
			}
		}
		if(seas[n].seasonNum){
			var seas_val = seas[n].seasonNum;
		}

		if (seas_val == seas_num){

			LegacyFOD.CSS.RemoveClass($(itemPre + seas_val), "hidden");
			LegacyFOD.CSS.RemoveClass($(navPre + seas_val), "hidden");

		}else{

			LegacyFOD.CSS.AddClass($(itemPre + seas_val), "hidden");
			LegacyFOD.CSS.AddClass($(navPre + seas_val), "hidden");
		}
	}
	LegacyFOD.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){

			LegacyFOD.CSS.RemoveClass(child, "seasonSelected");
		}
	}
	//child.AddClass("seasonSelected");
	LegacyFOD.CSS.AddClass(seasonLink, "seasonSelected");

	$(seasonSelection).innerHTML = "<a class=\"seasonLink\" href=\"javascript:DashboardSeasonSelect('seasonOptions','seasonLinkArrow', dash_full_ep_seas);\">Season"+seas_num+"</a>";


}

//MOD BLITZ added from dashboard.js
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;
}

// 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));
}
