(function() {

	var navJSON = {
		"video": [
			{"title": "Archer", 								"url": "http://vod.fxnetworks.com/watch/archer"},
			{"title": "It's Always Sunny in Philadelphia", 		"url": "http://vod.fxnetworks.com/watch/sunny"},
			{"title": "Justified", 								"url": "http://vod.fxnetworks.com/watch/justified"},
			{"title": "The League",								"url": "http://vod.fxnetworks.com/watch/theleague"},
			{"title": "Louie",									"url": "http://vod.fxnetworks.com/watch/louie"},
			{"title": "Rescue Me",								"url": "http://vod.fxnetworks.com/watch/rescueme"},
			{"title": "Sons of Anarchy",						"url": "http://vod.fxnetworks.com/watch/soa"},
			{"title": "Terriers",								"url": "http://vod.fxnetworks.com/watch/terriers"}
		],
		"show": [ 
			{"title": "Archer",									"url": "http://www.fxnetworks.com/shows/originals/archer"},
			{"title": "It's Always Sunny in Philadelphia",		"url": "http://www.fxnetworks.com/shows/originals/sunny"},
			{"title": "Justified",								"url": "http://www.fxnetworks.com/shows/originals/justified"},
			{"title": "The League",								"url": "http://www.fxnetworks.com/shows/originals/theleague"},
			{"title": "Lights Out",								"url": "http://www.fxnetworks.com/shows/originals/lightsout"},
			{"title": "Louie",									"url": "http://www.fxnetworks.com/shows/originals/louie"},
			{"title": "Rescue Me",								"url": "http://www.fxnetworks.com/shows/originals/rescueme"},
			{"title": "Sons of Anarchy",						"url": "http://www.fxnetworks.com/shows/originals/soa"},
			{"title": "Terriers",								"url": "http://www.fxnetworks.com/shows/originals/terriers"},
			{"title": "30 Days",								"url": "http://www.fxnetworks.com/shows/originals/30days"},
			{"title": "Spin City",								"url": "http://www.fxnetworks.com/shows/spincity"},
			{"title": "The Practice",							"url": "http://www.fxnetworks.com/shows/thepractice"},
			{"title": "That 70's Show",							"url": "http://www.fxnetworks.com/shows/that70sshow"},
			{"title": "Two and a Half Men",						"url": "http://www.fxnetworks.com/shows/twoandahalfmen"}
		]
	};
	
	var init = function() 
	{	
		var htmlAry = [];	// html code
		var selector = "";  // where to drop in htmlAry
		switch(scriptParams.page)
		{
			case "home":
				var perCols = [3, 4, 1];
				var i, ii;
				var iii = 0; //actual array count that doesn't reset.
				
				// Begin Episode
				htmlAry.push('<div class="eps menu-flyout-eps">');
				for(i = 0; i < perCols.length; i++)
				{
					htmlAry.push('<ul>');				
					for(ii = 0; ii < perCols[i] && iii < navJSON.video.length; ii++)
					{
						htmlAry.push('<li><a href="' + navJSON.video[iii].url + '">' + navJSON.video[iii].title + '</a></li>');
						iii++;
					}
					htmlAry.push('</ul>');
				}
				htmlAry.push('<ul class="last">');
				htmlAry.push('<div class="action"><a href="episodes.php" class="see-all-episodes">see all episodes</a></div>');
				htmlAry.push('</ul>');
				htmlAry.push('</div>');
				htmlAry.push('<div class="bg-container"><div class="bg"></div></div>');
				htmlAry.push('<div class="close"><a href="#">close</a></div>');
				document.getElementById('flyout-shows-inner').innerHTML = htmlAry.join("");
				// End Episode
				
				// Clear arrays
				htmlAry.clear();
				perCols.clear();
				
				// Begin Show
				perCols = [5, 6, 2];
				iii = 0;
				htmlAry.push('<div class="shows menu-flyout-shows">');
				for(i = 0; i < perCols.length; i++)
				{
					htmlAry.push('<ul>');				
					for(ii = 0; ii < perCols[i] && iii < navJSON.show.length; ii++)
					{
						htmlAry.push('<li><a href="' + navJSON.show[iii].url + '">' + navJSON.show[iii].title + '</a></li>');
						iii++;
					}
					htmlAry.push('</ul>');
				}
				htmlAry.push('<ul class="last">');
				htmlAry.push('<div class="action"><a href="shows.php" class="more2">see all shows</a></div>');
				htmlAry.push('</ul>');
				htmlAry.push('</div>');
				htmlAry.push('<div class="bg-container"><div class="bg"></div></div>');
				htmlAry.push('<div class="close"><a href="#">close</a></div>');
				document.getElementById('flyout-inner').innerHTML = htmlAry.join("");
				// End Show
				break;			
			case "microsite":	
				// video
				var perCols = [4, 4, 2, 0];
				var i, ii;
				var iii = 0;
				
				htmlAry.push('<div id="navPadding">');
				htmlAry.push('<table border="0" width="540" cellpadding="0" cellspacing="0"><tr>');
				
				for(i = 0; i < perCols.length; i++)
				{		
					// start table data
					if(i == perCols.length - 1) // last col
					{						
						htmlAry.push('<td align="right" valign="bottom">');
					}
					else
					{
						htmlAry.push('<td valign="top">');
					}
					
					for(ii = 0; ii < perCols[i] && iii < navJSON.video.length; ii++, iii++)
					{
						htmlAry.push('<a href="' + navJSON.video[iii].url + '">' + navJSON.video[iii].title + '</a>');
					}
					
					// last col, add in arrow and all episodes link
					if(i == perCols.length - 1)
					{
						htmlAry.push('<a href="http://www.fxnetworks.com/episodes.php"><img src="http://www.fxnetworks.com/global_nav/images/arrow.gif" alt="" /><strong>see all episodes</strong></a></td>');						
					}
					else
					{
						htmlAry.push('</td>');
					}
				}
				htmlAry.push('</tr></table>');
				document.getElementById("drop_down_w").innerHTML = htmlAry.join("");
				
				htmlAry = [];
				
				perCols = [4, 4, 4, 0];
				
				// shows
				htmlAry.push('<div id="navPadding">');
				htmlAry.push('<table border="0" width="540" cellpadding="0" cellspacing="0"><tr>');
				
				for(i = 0, iii = 0; i < perCols.length; i++)
				{		
					// start table data
					if(i == perCols.length - 1) // last col
					{						
						htmlAry.push('<td align="right" valign="bottom">');
					}
					else
					{
						htmlAry.push('<td valign="top">');
					}
					
					for(ii = 0; ii < perCols[i] && iii < navJSON.show.length; ii++, iii++)
					{
						htmlAry.push('<a href="' + navJSON.show[iii].url + '">' + navJSON.show[iii].title + '</a>');
					}
					
					// last col, add in arrow and all shows link
					if(i == perCols.length - 1)
					{
						htmlAry.push('<a href="http://www.fxnetworks.com/shows.php"><img src="http://www.fxnetworks.com/global_nav/images/arrow.gif"><strong>see all shows</strong></a></td>');						
					}
					else
					{
						htmlAry.push('</td>');
					}
				}
				htmlAry.push('</tr></table>');
				document.getElementById("drop_down").innerHTML = htmlAry.join("");
				break;
			case "interior":
			default: // interior
				var perCols = [4,4,2];
				var i, ii;
				var iii = 0; //actual array count that doesn't reset.
				
				// Begin Episode
				htmlAry.push('<div class="inner">');
				for(i = 0; i < perCols.length; i++)
				{
					if (i == 0)
					{
						htmlAry.push('<ul style="width:230px; overflow: hidden;">');		
					}
					else 
					{
						htmlAry.push('<ul style="width:120px; overflow: hidden;">');
					}
					for(ii = 0; ii < perCols[i] && iii < navJSON.video.length; ii++)
					{
						htmlAry.push('<li><a href="' + navJSON.video[iii].url + '">' + navJSON.video[iii].title + '</a></li>');
						iii++;
					}
					htmlAry.push('</ul>');
					
				}
				htmlAry.push('<div class="close" id="episodes-close"><a href="#" class="close"><!-- the shows subnav works --></a></div>');
				htmlAry.push('<div class="action"><a href="http://www.fxnetworks.com/episodes.php" class="more">see all episodes</a></div>');
				htmlAry.push('</div>');
				htmlAry.push('<div class="bg"><!-- --></div>');
				
				document.getElementById('episodes').innerHTML = htmlAry.join("");
				// End Episode

				// Clear arrays
				htmlAry.clear();
				perCols.clear();
				
				// Begin Episode
				perCols = [4, 4, 4];
				iii = 0;
				htmlAry.push('<div class="inner">');
				for(i = 0; i < perCols.length; i++)
				{
					if (i == 0)
					{
						htmlAry.push('<ul>');		
					}
					else if (i == 1)
					{
						htmlAry.push('<ul style="margin-left:-65px; width:120px">');
					}
					else if (i == 2)
					{
						htmlAry.push('<ul style="margin-left:15px; width:120px;">');
					}
					else if (i == 3)
					{
						htmlAry.push('<ul style="margin-left:5px; width:120px;">');
					}
					for(ii = 0; ii < perCols[i] && iii < navJSON.show.length; ii++)
					{
						htmlAry.push('<li><a href="' + navJSON.show[iii].url + '">' + navJSON.show[iii].title + '</a></li>');
						iii++;
					}
					htmlAry.push('</ul>');
					
				}
				htmlAry.push('<div class="close" id="gn-shows-close"><a href="#" class="close"><!-- the shows subnav works --></a></div>');
				htmlAry.push('<div class="action"><a href="http://www.fxnetworks.com/shows.php" class="more">see all shows</a></div>');
				htmlAry.push('</div>');
				htmlAry.push('<div class="bg"><!-- --></div>');
				
				document.getElementById('shows-titles').innerHTML = htmlAry.join("");
				// End Episode
				
				break;
		}
	};
	
	
	var getScriptParams = function() 
	{
		
		var thisFile = document.getElementById("fx-global-nav-list");
		if(!thisFile)
		{
			var scripts = document.getElementsByTagName("script");
			// get this file
			thisFile = scripts[scripts.length - 1];
		}
		
		return getParams(thisFile.src);
	};
	
	var getParams = function(url)
	{
		var result = {};
		var queryString;
		
		if(url)
		{
			result.hash = url.substr(url.indexOf("#") + 1);
			result.pageURL = url.substring(url.lastIndexOf('/') + 1);
			queryString = url.substr(url.indexOf("?") + 1);		
		}
		else
		{
			result.hash = document.location.hash.substr(1);
			result.pageURL = document.location.pathname.substring(document.location.pathname.lastIndexOf('/') + 1);
			queryString = document.location.search.substr(1);
		}
	
		queryString.replace(/([^=&]+)=([^&]*)/g, function(match, key, value) {
			result[unescape(key)] = unescape(value);
		});
		
		return result;
	};	
	
	var scriptParams = getScriptParams();
	init();	
	
})();