/* 
BBC Scotland - Rivercity - Abrar Khan 19/08/2009
*/

var DownloadsServer = 'http://downloads.bbc.co.uk';
var PlaylistServer = 'http://www.bbc.co.uk';
var PlaylistDevServer = 'http://www.bbc.co.uk';
var SiteURL = "/rivercity/";
// load an instance of glow
var glow;
var dom;
var $;
var events;
var anim;
var pages_array = new Array();
/*pages_array["rivercity"]="home";*/
pages_array["features_index"]="features_index";
pages_array["gallery_index"]="gallery_index";
pages_array["videos_index"]="videos_index";
pages_array["characters_profile"]="characters_profile";
pages_array["characters_new_profile"]="characters_new_profile";
pages_array["faq_index"]="faq_index";

gloader.load(["glow", "1", "glow.dom", "glow.anim", "glow.net", "glow.data", "glow.events", "glow.widgets.Carousel"], 
{
	async: true,
	onLoad: function(glowLocal)
	{
		glowLocal.ready(function()
		{
			glow = glowLocal;
			dom = glowLocal.dom;
			$ = glowLocal.dom.get;
			events = glowLocal.events;
			anim = glowLocal.anim;

			/* ------------------------------------------------------------------------ */
			/* -------------------->>>   G L O B A L  O B J E C T   <<<---------------- */
			/* ------------------------------------------------------------------------ */
			Site = {
			/* ------------------------------------------------------------------------ */
			/* ---------------------->>> P A G E    O P T I O N S  <<<----------------- */
			/* ------------------------------------------------------------------------ */
				pageOptions: function()
				{
					// set the var "pageID" to the pages body class
					var pageID = Site.GetPageID();

				// run a conditional statement on the var "pageID" 
					switch(pageID)
					{
						case 'rivercity_index':
							Site.Homepage.InitialiseHomepage();
							break;
						case 'videos_index':
							VideoSet.Initialise();
							break;
						case 'characters_index':
							Site.CharactersIndex.InitialiseCharactersIndex();
							break;
						case 'nav-feature':
							break;
						default:
							break;
					}
				},
				Homepage:
				{
					/* ------------------------------------------------------------------------ */
					/* ------------->>> I N I T I A L I S E  H O M E P A G E   <<<------------- */
					/* ------------------------------------------------------------------------ */
					InitialiseHomepage: function()
					{	
						var myCarousel = new glow.widgets.Carousel("#carouselContainer", {
							loop: false,
							step: 1,
							theme: "dark",
							className: "carouselContainer"
						});
						
						carouselComponents(glow, "#carouselContainer");
					}
				},
				CharactersIndex:
				{
					/* ------------------------------------------------------------------------ */
					/* ------>>> I N I T I A L I S E   C H A R A C T E R S   I N D E X <<<----- */
					/* ------------------------------------------------------------------------ */
					InitialiseCharactersIndex: function()
					{
						events.addListener($(".links li img"),"click",function(e)
						{
							e.preventDefault();
							anchor=$(this.parentNode).get("a");
							window.location.href=anchor[0].href;
						});

						glow.dom.get(".links li img").css( "cursor", "pointer");

						//Init videosets
						VideoSet.Initialise();
					}
				},
				NavFeatures:
				{
					/* ------------------------------------------------------------------------ */
					/* ------------->>> I N I T I A L I S E  N A V F E A T U R E S  <<<------------- */
					/* ------------------------------------------------------------------------ */
					InitialiseNavFeatures: function()
					{
						//Init videosets
						VideoSet.Initialise();
					}
				},
				GetPageID: function()
				{
					var pageID = "";
					var this_url = window.location.href.toString();
					var last_slash_pos = this_url.lastIndexOf("/");
					var file_name = this_url.slice(last_slash_pos+1);
					var current_directory_path = this_url.slice(0, last_slash_pos);
					last_slash_pos = current_directory_path.lastIndexOf("/");
					var current_directory = current_directory_path.slice(last_slash_pos+1);

					var current_path = "/"+current_directory+"/";
					if(current_path == SiteURL)
					{
						set_id(current_directory);
					}
					else
					{
						var parent_last_slash_pos = current_directory_path.lastIndexOf("/");
						var parent_directory_path = this_url.slice(0, parent_last_slash_pos);
						parent_last_slash_pos = parent_directory_path.lastIndexOf("/");
						var parent_directory = parent_directory_path.slice(parent_last_slash_pos+1);

						//Ok its not the homepage file so we need to append the parent directory and current directory unless its characters
						switch(current_directory)
						{
							case 'characters':
								if((file_name == "index.shtml") || (file_name == ""))
								{
									//we are in the characters index page
									set_id(current_directory+"_index");
								}
								else
								{
									//we are not in the index page so must be a profile page
									set_id(current_directory+"_new_profile");
								}
								break;
							case 'features':
							case 'faq':
							case 'gallery':
							case 'videos':
								//we are in the characters index page
								set_id(current_directory+"_index");
								break;
							default:
								//we are not in an index page so we are in a subdirectory
								switch(parent_directory)
								{
									case 'characters':
										//we are in the characters profile page
										set_id(parent_directory+"_profile");
										break;

									case 'features':
										break;
									default:
										break;
								}
							break;
						}
					}

					function set_id(page_type)
					{
						//Overriding pageID with value specified in pages_array for pages within specified directories in the pages_array
						if(typeof(pages_array[page_type]) != "undefined")
						{
							//Now lets set the body id to be this
							var body_element_container = glow.dom.get("body");
							body_element_container[0].id = pages_array[page_type];
							pageID = pages_array[page_type];
						}
						else
						{
							pageID = glow.dom.get('body').attr('id');
						}	
					}

					return pageID;
				}

			/* close Site object */
			}
			Site.pageOptions();
		/* close glowLocal.ready */
		});
	/* close onLoad*/
	}
/* close gloader.load */
});