//Common funcs

function trim(str) {
	str = str.replace(/^\s+|\s+$/g, '') 
	return str;
}

function extract(source, middleMarker, endMarker){
	var result = new Object();
	var name, value;
	var beginning, middle, end;
		
	beginning = 0; 
	while (beginning < source.length){
		middle = source.indexOf(middleMarker, beginning);
		end = source.indexOf(endMarker, beginning);
		
		if (end == -1){ 
			end = source.length;
		}
		
		if ((middle > end) || (middle == -1)){
			name = source.substring(beginning, end);
			value ="";
		}
		else{
			name = source.substring(beginning, middle);
			value = source.substring(middle + 1, end);
		}
	
		result[name] = unescape(value);
		beginning = end + 2;
	}	
	
	return result;
}

function extractArray(source, middleMarker, endMarker) {
	value = source.split(endMarker);
	for (var i = 0; i < value.length-1; i++)
	{
	var tmp = value[i].split(middleMarker);
	value[i] = new Array();
	for (var j = 0; j < tmp.length; j++)
	value[i][j] = tmp[j];
	}
	return value;
}



//Journey cookie handlers
function updateJourneyCookie(addPage) {
	var currentPage;
	var cookieString;
	//if window.location.substring(15,16)
	var currentPage = window.location;
	if (document.title.substring(15,16) == '-') {
		var currentTitle = document.title.substr(17)
	} else {
		var currentTitle = document.title.substr(15)
	}
	var cookies = new Object();
	var JourneyCookie = new Object();
	cookies = extract(document.cookie, '=', ';');
	var newCookieString;
	if (((document.cookie) && !cookies["CBeebiesJourney"] && addPage)){
		cookieString = 'CBeebiesJourney=' + currentPage + '£' + currentSection + '£' + currentTitle + '|; path=/cbeebies;' ;
		document.cookie = cookieString;	
		JourneyCookie = extractArray(cookieString, '£', '|');
		return;
	}
	else {
		cookieString = cookies["CBeebiesJourney"];
		cbeebiesCookie = extractArray(cookieString, '£', '|');
		var counter = 0;
		while(cbeebiesCookie[counter]) {
			counter = counter + 1;
		}
		if (currentPage != cbeebiesCookie[0][0]) {
			newCookieString = 'CBeebiesJourney='
			if (addPage) {
			newCookieString = newCookieString + currentPage + '£' + currentSection + '£' + currentTitle + '|';
			}
			for (var i = 0; ((i < counter) & (i < 10)); i++) {
				newCookieString = newCookieString + cbeebiesCookie[i][0] + '£' + cbeebiesCookie[i][1] + '£' + cbeebiesCookie[i][2] + '|'			
			}	
			newCookieString = newCookieString + '; path=/cbeebies;';	
			document.cookie = newCookieString;
			return cbeebiesCookie;	
		}
		else {
			return cbeebiesCookie;		
		}
		
	}
}

function showJourneys(showJourney,addPage) {
	journeyList = updateJourneyCookie(addPage);
	if(journeyList && showJourney){
			document.write('<div id="journey" class="box2 box-home2 home3"><h2 class="box-header box-title-wherehaveibeen noicon" title="Where have I been?"><span></span>Where have I been?</h2><ol>');
			var counter = 0;
			while(journeyList[counter]) {
				document.write('<li><a href="' + journeyList[counter][0] + '" class="journey-' + journeyList[counter][1] +'">' + journeyList[counter][2] + '</li>');
				if (counter == 2) {
					document.write('</ol><ol id="morejourney">')
				}
				counter = counter + 1;
			}
			document.write('</ol>')
			noToSlide = counter - 3
			if (counter >= 4) {
				document.write ('<a id="morejourneylink" href="javascript:togglemorejourney()">More links</a>')
			}
			document.write ('<div id="bouncebug"></div></div>');
	}
	obj = document.getElementById('morejourney');
	if (obj) {
		obj.style.height = noToSlide*14 + 'em';
		obj.style.display = 'none';
	}
}

// Journey toggler
function togglemorejourney() {
	var slidedur
	slidedur = noToSlide * 0.3
	var morejourney = document.getElementById('morejourney')
	var morejourneylink  = document.getElementById('morejourneylink')
	//obj.style.height = '400px'
	if (morejourney.style.display != 'block') {
		Slide('morejourney',{duration:slidedur}).down()
		morejourneylink.innerHTML = "Fewer links" 
	} else {
		Slide('morejourney',{duration:slidedur}).up()
		morejourneylink.innerHTML = "More links" 
	}
}

// Slider
function Slide(objId, options) {
	this.obj = document.getElementById(objId);
	this.duration = 1;
	this.height = parseInt(this.obj.style.height);

	if(typeof options != 'undefined') { this.options = options; } else { this.options = {}; }
	if(this.options.duration) { this.duration = this.options.duration; }
		
	this.up = function() {
		this.curHeight = this.height;
		this.newHeight = '1';
		if(slideInUse[objId] != true) {
			var finishTime = this.slide();
			window.setTimeout("Slide('"+objId+"').finishup("+this.height+");",finishTime);
		}
	}
	
	this.down = function() {
		this.newHeight = this.height;
		this.curHeight = '1';
		if(slideInUse[objId] != true) {
			this.obj.style.height = '1px';
			this.obj.style.display = 'block';
			this.slide();
		}
	}
	
	this.slide = function() {
		slideInUse[objId] = true;
		var frames = 30 * duration; // Running at 30 fps

		var tIncrement = (duration*1000) / frames;
		tIncrement = Math.round(tIncrement);
		var sIncrement = (this.curHeight-this.newHeight) / frames;

		var frameSizes = new Array();
		for(var i=0; i < frames; i++) {
			if(i < frames/2) {
				frameSizes[i] = (sIncrement * (i/frames))*4;
			} else {
				frameSizes[i] = (sIncrement * (1-(i/frames)))*4;
			}
		}
		
		for(var i=0; i < frames; i++) {
			this.curHeight = this.curHeight - frameSizes[i];
			window.setTimeout("document.getElementById('"+objId+"').style.height='"+Math.round(this.curHeight)+"px';",tIncrement * i);
		}
		
		window.setTimeout("delete(slideInUse['"+objId+"']);",tIncrement * i);
		
		if(this.options.onComplete) {
			window.setTimeout(this.options.onComplete, tIncrement * (i-2));
		}
		
		return tIncrement * i;
	}
	
	this.finishup = function(height) {
		this.obj.style.display = 'none';
		this.obj.style.height = height + 'px';
	}
	
	return this;
}

// Flash nav toggler
function getCookieVal (offset) {  
	var endstr = document.cookie.indexOf (";", offset);  
	if (endstr == -1)    
	endstr = document.cookie.length;  
	return unescape(document.cookie.substring(offset, endstr));
}

function GetCookie (name) {  
	var arg = name + "=";  
	var alen = arg.length;  
	var clen = document.cookie.length;  
	var i = 0;  
	while (i < clen) {    
		var j = i + alen;    
		if (document.cookie.substring(i, j) == arg) return getCookieVal (j);    
		i = document.cookie.indexOf(" ", i) + 1;    
		if (i == 0) break;   
	}  
	return null;
}

function SetCookie (name, expiry, path) {  
	var exp = new Date(); 
	exp.setTime(exp.getTime() + (expiry*24*60*60*1000));
	document.cookie = name + "= yes; expires=" + exp.toGMTString() + "; path=" + path; 
}

function DeleteCookie (name, path) {  
	var exp = new Date();  
	exp.setTime (exp.getTime() - 1);
	var cval = GetCookie (name);  
	document.cookie = name + "=" + cval + "; expires=" + exp.toGMTString() + "; path=" + path; 
}
function tglaudio(tgl){
	if (tgl == 'on') {
		SetCookie('cbeebies_snd',5,'/cbeebies/');
	}else{
		DeleteCookie('cbeebies_snd', '/cbeebies/');
	}
}

// Generic Pop-Up Function
function newpopup(href,id,w,h){
	var params = 'width='+w+',height='+h+',toolbar=no,personalbar=no,location=no,directories=no,statusbar=no,menubar=no,status=no,resizable=yes,left=60,screenX=60,top=100,screenY=100';
	window.name="main";
	var newwindow = window.open(href,id,params);
	if(window.focus){ newwindow.focus(); }
}

// Radioplayer ... to be removed once fully redundant
function aodpopup(){
	window.name="main";
	window.open('/cbeebies/radioplayer/','aod','width=660,height=385,toolbar=no,personalbar=no,location=no,directories=no,statusbar=no,menubar=no,status=no,resizable=yes,left=60,screenX=60,top=100,screenY=100');
}
if(location.search.substring(1)=="focuswin"){
	window.focus();
}

function aodpopup2(){
	window.name="main";
	window.open('/cbeebies/radioplayer/books.shtml','aod','width=660,height=385,toolbar=no,personalbar=no,location=no,directories=no,statusbar=no,menubar=no,status=no,resizable=yes,left=60,screenX=60,top=100,screenY=100');
}
if(location.search.substring(1)=="focuswin"){
	window.focus();
}


// Setup vars
var slideInUse = new Array();
var noToSlide;

// Call stuff
preloadNavImgs()

// Print
function printMe(){
	if(window.print){
		document.writeln('<a href="#" onclick="window.print();return false;">Print Out</a>');
	} 
	else {
		//ensures that people using safari browser have the option to print
		document.write('<p>please select "file - print"</p>');
	}
}