glow.ready(function() {
	/*
	All pages are numbered in groups
	p-grp-theme-000 	= home
	prg-grp-theme-111	= episodes (never used because this is APS (/programmes)
	prg-grp-theme-222	= characters
	prg-grp-theme-333	= music
	prg-grp-theme-444	= gallery
	prg-grp-theme-555	= mobile
	prg-grp-theme-666	= downloads
	*/
	if(glow.dom.get('.p-grp-theme-000').length>0)
		PageAction.Home(true);
})

var PageAction = function() {
	return {
		Home: function( hackOnly ) {
			if(hackOnly) /* ie6 hck */
				glow.dom.get('#prg-wrapper-featured .prg-featured-lead .p-grp-theme-000 h2').html(glow.dom.get('#prg-wrapper-featured .prg-featured-lead .p-grp-theme-000 h2').html());
				
			if(!hackOnly) {
				if(window.location.hash!=null&&window.location.hash.length>0)
				{
					var href = window.location.hash.replace('#','');
					var title = '';
					var aList = glow.dom.get('#blq-content #prg-wrapper-extra .p-grp .p-mod .promo a');
					for(var i=0; i<aList.length; i++) {
						if(glow.dom.get(aList[i]).attr("href").replace('#','')==href)
						{
							try { title = glow.dom.get(aList[i]).attr("title"); }
							catch(error) { title = glow.dom.get(aList[i]+' img').attr("title"); }
							PageAction.LoadHomeVideo(title,href);
						}
					}
				}
				
				if(glow.dom.get('body#prg-type-index')!=null&&glow.dom.get('body#prg-type-index').length>0)
				{
					var aList = glow.dom.get('#blq-content #prg-wrapper-extra .p-grp .p-mod .promo a');
					
					for(var i=0; i<aList.length; i++) {
						glow.events.addListener(
							aList[i],
							'click',
							function(e) {
								var href = '';
								var title = '';
								
								try { href = glow.dom.get(e.source).attr("href"); } 
								catch(error) {}
								
								try { title = glow.dom.get(e.source).attr("title"); } 
								catch(error) {}
								
								if(href==null||href=='') {
									href = glow.dom.get(e.source.parentNode).attr("href");
									title = glow.dom.get(e.source.parentNode).attr("title");
								}
								href = href.replace('#','');
								PageAction.LoadHomeVideo(title, href, "http://www.bbc.co.uk/ashestoashes","");
							}
						);
					}
				}
			}
		},
		LoadHomeVideo: function(title, href) {
			glow.dom.get('#prg-wrapper-featured .prg-featured-lead .p-grp-theme-000 h2').html(title);
			empDivReady("512", "emp1", "black", href, "http://www.bbc.co.uk/ashestoashes","");
		}
	}
}();

var CookieFunc = function() {
	/*
	Slightly restructured from http://www.quirksmode.org/js/cookies.html
	createCookie('ppkcookie','testcookie') - name,value
	*/
	return {
		createCookie: function(name, value) {
			// sets the expiry to the next monday at 5pm
			var date = new Date();
			var currentDay = parseInt(date.getDay()); // 0 = Sunday
			var currentTime = date.getTime(); // not date.getTime()
			var dayOffset = (7-currentDay)+1; // +1 to make it monday
			date.setTime(currentTime+(dayOffset*24*60*60*1000));
			var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
			var currentMonth = monthname[parseInt(date.getMonth())];
			var date2 = new Date(currentMonth+' '+date.getDate()+', '+date.getFullYear()+' 17:00:00');
			//alert(date2.toGMTString());
			
			var expires = "; expires="+date2.toGMTString();
			document.cookie = name+"="+value+expires+"; path=/";
		},
		readCookie: function(name) {
			var nameEQ = name + "=";
			var ca = document.cookie.split(';');
			for(var i=0;i < ca.length;i++) {
				var c = ca[i];
				while (c.charAt(0)==' ') c = c.substring(1,c.length);
				if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
			}
			return null;
		},
		// assumes that the cookie is built up like a url - key1=value1&key2=value2 etc.
		// getKeyValueofCookie('dvl','key1') would return string 'value1';
		// returns '' if key doesn't exist
		getKeyValueofCookie: function(cookiename, key) {
			var result = '';
			
			var cookiedata = CookieFunc.readCookie(cookiename);
			if(cookiedata!=null&&cookiedata.length>0)
			{	
				if(cookiedata.indexOf('&')>-1)
				{
					var cookieArray = new Array();
					cookieArray = cookiedata.split('&');
					for(var i=0; i<cookieArray.length; i++) {
						var cookieEntry = cookieArray[i];
						var cookieEntryArray = new Array();
						cookieEntryArray = cookieEntry.split('=');
						
						if(cookieEntryArray.length>0)
						{
							if(cookieEntryArray[0]==key)
							{
								if(cookieEntryArray[1]!=null&&cookieEntryArray[1].length>0)
									result = cookieEntryArray[1];
							}
						}
					}
				}
				else if(cookiedata.indexOf('=')>-1)
				{
					var cookieEntryArray = new Array();
					cookieEntryArray = cookiedata.split('=');
					
					if(cookieEntryArray.length>0)
					{
						if(cookieEntryArray[0]==key)
						{
							if(cookieEntryArray[1]!=null&&cookieEntryArray[1].length>0)
								result = cookieEntryArray[1];
						}
					}
				}
			}
			
			return result;
		},
		addKeyValuetoCookie: function(cookiename, key, value) {
			CookieFunc.addKeyValuetoCookie(cookiename, key, value, false);
		},
		// first of all checks to see if the key already exists, if it does then ammends the value of that key (if forceOverwrite is true), if it doesn't then creates the key value pair inside the cookie
		// syntax addKeyValuetoCookie('dvl', 'key1', 'value1')
		addKeyValuetoCookie: function(cookiename, key, value, forceOverwrite) {
			var previousChoice = CookieFunc.getKeyValueofCookie(cookiename, key);
			if(forceOverwrite||(previousChoice==null||previousChoice.length<1)) // only add key value combo if the key hasn't already been added
			{
				var originalcookie = CookieFunc.readCookie(cookiename); // read cookie details
				var cookiecookie = '';
				
				var valyou = key+'='+value+'&';
				// valyou might typically look like - dilemmas_dilemma1=4&dilemmas_dilemma2=1
				
				if(originalcookie!=null&&originalcookie.length>0)
				{
					if(originalcookie.indexOf('&')>-1)
					{
						var cookieArray = new Array();
						var currentIdExistInLoop = false;
						cookieArray = originalcookie.split('&');
						for(var i=0; i<cookieArray.length; i++) {
							var cookieEntry = cookieArray[i];
							if(cookieEntry!=null&&cookieEntry.length>0) {
								var cookieEntryArray = new Array();
								cookieEntryArray = cookieEntry.split('=');
								
								if(cookieEntryArray[0]==key) {
									if(forceOverwrite)
										cookiecookie+=cookieEntryArray[0]+'='+value+'&';
									else
										cookiecookie+=cookieEntryArray[0]+'='+cookieEntryArray[1]+'&';
										
									currentIdExistInLoop = true;
								}
								else
									cookiecookie+=cookieEntryArray[0]+'='+cookieEntryArray[1]+'&';
							}
						}
						if(!currentIdExistInLoop)
							cookiecookie+=key+'='+value+'&';
					}
					else // else compare to see if this id = value of cookie
					{
						if(originalcookie.indexOf('=')>-1) {
							var cookieEntryArray = new Array();
							cookieEntryArray = cookieEntry.split('=');
							
							if(cookieArray[0]!=key)
								cookiecookie+=key+'='+value+'&';
						}
					}
				
					if(originalcookie!=cookiecookie) // Finally, if there are any changes then update with the new data
					{
						CookieFunc.eraseCookie(cookiename);
						CookieFunc.createCookie(cookiename,cookiecookie,28); // read back updated cookie details
					}
				}
				else
					CookieFunc.createCookie(cookiename,valyou,28);
			}
		},
		eraseCookie: function(name) {
			var date = new Date();
			date.setTime(date.getTime()+(-1*24*60*60*1000));
			var expires = "; expires="+date.toGMTString();
			document.cookie = name+"="+expires+"; path=/";
		}
	}
}();