var moveTimer = 0, speedTimer = 0;
var position = 0;
var speed = 1;
var box, product, products, obj, current, finder, state, i, doc, owner, xSize, ySize, y, container;
var xPos, yPos;
var productWidth = [];
var cover = [];
var totalWidth = 0;
var loadCount = 0;

function getObj(id)
{
	return document.getElementById(id);
}

function getHeight()
{
	obj = getObj('contentBox');
	height = obj.offsetHeight;
	return height;
}

function move()
{
	clearTimeout(moveTimer);
	box = getObj('scrollBoxInner');
	products = box.getElementsByTagName("a");
	for (var count = 0; count < products.length; count++)
	{
		obj = products[count];
		current = obj.style.left;
		position = current.slice(0,-2);
		if (position < -119)
		{
			position = (products.length - 1) * 120;
		}
		else
		{
			position -= speed;
		}
		obj.style.left = position + "px";
	}
	moveTimer = setTimeout("move()",1);
}

function pause(val)
{
	if (val == 1)
	{
		speed = 0;
	}
	else
	{
		speed = 1; 
	}
}

function setSpeed()
{
	clearTimeout(speedTimer);
	speed = 1;
}

function init()
{
	if (getObj('scrollBoxInner'))
	{
		move();
	}
	change();
}

function getInnerWidth(win)
{
	var winWidth;
	if (win.innerWidth)
	{
		winWidth = win.innerWidth;
	}
	else if (win.document.documentElement && win.document.documentElement.clientWidth)
	{
		winWidth = win.document.documentElement.clientWidth;
	}
	else if (document.body)
	{
		winWidth = win.document.body.clientWidth;
	}
	return winWidth;
}

function getInnerHeight(win)
{
	var winHeight;
	if (win.innerHeight)
	{
		winHeight = win.innerHeight;
	}
	else if (win.document.documentElement && win.document.documentElement.clientHeight)
	{
		winHeight = win.document.documentElement.clientHeight;
	}
	else if (win.document.body)
	{
		winHeight = win.document.body.clientHeight;
	}
	return winHeight;
}

var changecount = 0;

var quotes = [];
quotes[0] = "Activate because it helps me do my work better. I like 'Move and Shake' best. It's really funny when you have to shake your bum.";
quotes[1] = "I like Activate because it makes my brain work better. I love 'Bounce and Punch' because it makes me do exercise.";
quotes[2] = "Activate is great because it's good fun and it warms you up to work. I love 'Noughts and Crosses'. I like jumping up like a star.";
quotes[3] = "All the teachers join in Activate even Miss Lees (Head). Sometimes my teacher gets it wrong but it makes us laugh and we make up new activities.";
quotes[4] = "I like Activate because it helps me concentrate.";
quotes[5] = "Activate is good because it's fun. It gets me moving a lot. I like the beans game especially when Mr Bourton says French Beans and we all say, Ooh La La.";

var names = [];
names[0] = 'Owen (Year 2)';
names[1] = 'Morgan (Y2)';
names[2] = 'Jamie (Y2)';
names[3] = 'Lauren (Y2)';
names[4] = 'Rhianna (Y2)';
names[5] = 'Megan (Y2)';


function opacity(id, opacStart, opacEnd, millisec) {
	//speed for each frame
	var speed = Math.round(millisec / 100);
	var timer = 0;

	//determine the direction for the blending, if start and end are the same nothing happens
	if(opacStart > opacEnd) {
		for(i = opacStart; i >= opacEnd; i--) {
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	} else if(opacStart < opacEnd) {
		for(i = opacStart; i <= opacEnd; i++)
			{
			setTimeout("changeOpac(" + i + ",'" + id + "')",(timer * speed));
			timer++;
		}
	}
}

//change the opacity for different browsers
function changeOpac(opacity, id)
{
	opacity = (opacity == 100)?99.999:opacity;
	var object = document.getElementById(id).style; 
	object.opacity = (opacity / 100);
	object.MozOpacity = (opacity / 100);
	object.KhtmlOpacity = (opacity / 100);
	object.filter = "alpha(opacity=" + opacity + ")";
}

function shiftOpacity(id, millisec) {
	//if an element is invisible, make it visible, else make it invisible
	if(document.getElementById(id).style.opacity == 0) {
		opacity(id, 0, 100, millisec);
	} else {
		opacity(id, 100, 0, millisec);
	}
}

function changer()
{
	box = document.getElementById("quote");
	box.innerHTML = '<p>\"' + quotes[changecount] + '\"<span>' + names[changecount] + '</span></p>';
	//box.innerHTML += '<p class="shadow">\"' + quotes[changecount] + '\"<span>' + names[changecount] + '</span></p>';
	if (changecount < quotes.length-1)
	{
		changecount++;	
	}
	else
	{
		changecount = 0;
	}
}

function change()
{
	opacity("quote", 100, 0, 1000);
	setTimeout ("changer()",1000);
	setTimeout ("opacity(\"quote\", 0, 100, 1000)",2000)
	setTimeout ("change()",11000);
}

function openMp3(win,owner)
{
	if (self.pageYOffset){y = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;}
	else if (document.body) {y = document.body.scrollTop;}
	
	xSize = 150;
	ySize = 30;
	
	doc = getObj('mp3Player');
	doc.width = 300;
	doc.height = 60;

	doc.src = owner.href;

	xPos = (getInnerWidth(win)/2) - (xSize+20);
	yPos = ((getInnerHeight(win)/2)+y) - (ySize+20);

	obj = getObj('mp3Viewer');
	obj.style.width = (xSize*2 + 20) + "px";
	obj.style.height = (ySize*2 + 50) + "px";
	obj.style.display = 'block';
	obj.style.top = yPos + 'px';
	obj.style.left = xPos + 'px';
}

function closeMp3()
{
	obj = getObj('mp3Viewer');
	obj.style.display = 'none';
	doc = getObj('mp3Player'); //the actual mp3 iframe!!
	doc.src = "http://www.valsabinpublications.com/audio/audio.php";
}

function openRecommendWindow(win,owner)
{
	if (self.pageYOffset){y = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;}
	else if (document.body) {y = document.body.scrollTop;}
	
	xSize = 150;	// Half of actual box width
	ySize = 160;	// Half of actual box height
	
	doc = getObj('recommendContent');
	doc.width = 300;
	doc.height = 320;

	xPos = (getInnerWidth(win)/2) - (xSize+20);
	yPos = ((getInnerHeight(win)/2)+y) - (ySize+20);

	obj = getObj('recommendWindow');
	obj.style.width = (xSize*2 + 20) + "px";
	obj.style.height = (ySize*2 + 50) + "px";
	obj.style.display = 'block';
	obj.style.top = yPos + 'px';
	obj.style.left = xPos + 'px';
}

function closeRecommendWindow()
{
	obj = getObj('recommendWindow');
	obj.style.display = 'none';
	doc = getObj('recommendContent'); //the actual mp3 iframe!!
	doc.src = "http://www.valsabinpublications.com/recommend.php";
}

function openOrderingWindow(win,owner)
{
	if (self.pageYOffset){y = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;}
	else if (document.body) {y = document.body.scrollTop;}
	
	xSize = 150;	// Half of actual box width
	ySize = 160;	// Half of actual box height
	
	doc = getObj('orderingContent');
	doc.width = 300;
	doc.height = 320;

	xPos = (getInnerWidth(win)/2) - (xSize+20);
	yPos = ((getInnerHeight(win)/2)+y) - (ySize+20);

	obj = getObj('orderingWindow');
	obj.style.width = (xSize*2 + 20) + "px";
	obj.style.height = (ySize*2 + 50) + "px";
	obj.style.display = 'block';
	obj.style.top = yPos + 'px';
	obj.style.left = xPos + 'px';
}

function closeOrderingWindow()
{
	obj = getObj('orderingWindow');
	obj.style.display = 'none';
	doc = getObj('orderingContent'); //the actual mp3 iframe!!
	doc.src = "http://www.valsabinpublications.com/ordering.php";
}

function openVideo(win,file,desc)
{
	if (self.pageYOffset){y = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;}
	else if (document.body) {y = document.body.scrollTop;}

	doc = getObj('player'); //the actual video iframe!!
	doc.src = "http://www.valsabinpublications.com/flash/player.php?file=" + file + "&desc=" + desc;

	xSize = 165;
	ySize = 170;

	xPos = (getInnerWidth(win)/2) - (xSize);
	yPos = ((getInnerHeight(win)/2)+y) - (ySize);

	obj = getObj('videoViewer');
	obj.style.width = (xSize*2 + 20) + "px";
	obj.style.height = (ySize*2 + 20) + "px";
	obj.style.display = 'block';
	obj.style.top = yPos + 'px';
	obj.style.left = xPos + 'px';
}

function closeVideo()
{
	obj = getObj('videoViewer');
	obj.style.display = 'none';
	doc = getObj('player'); //the actual video iframe!!
	doc.src = "http://www.valsabinpublications.com/flash/player.php";
}

function openPdf(win,owner,orientation)
{
	if (self.pageYOffset){y = self.pageYOffset;}
	else if (document.documentElement && document.documentElement.scrollTop) {y = document.documentElement.scrollTop;}
	else if (document.body) {y = document.body.scrollTop;}
	
	if (orientation == 0)
	{
		ySize = (getInnerHeight(win) - 100)/2;
		xSize = Math.floor((ySize*1.4143)/2)
	}
	else
	{
		xSize = 420;
		ySize = 297;
	}

	//	Set the url of the pdf to view
	doc = getObj('pdfDoc');
	doc.width = xSize*2;
	doc.height = ySize*2;

	doc.src = owner.href + "#toolbar=0&navpanes=0&scrollbar=0";
	//doc.href = owner.href + "#toolbar=0&navpanes=0&scrollbar=0";

	// Calculate and set the position of the pdf view window
	xPos = (getInnerWidth(win)/2) - (xSize+20);
	yPos = ((getInnerHeight(win)/2)+y) - (ySize+20);


	// Set the background colour of the pdf view window
	obj = getObj('pdfViewer');

	if (owner.parentNode.parentNode.className != "pdf")
	{
		obj.className = owner.parentNode.parentNode.className;
	}
	else
	{
		obj.className = "contents";
	}
	obj.style.width = (xSize*2 + 20) + "px";
	obj.style.height = (ySize*2 + 50) + "px";
	obj.style.display = 'block';
	obj.style.top = yPos + 'px';
	obj.style.left = xPos + 'px';
}

function closePdf()
{
	obj = getObj('pdfViewer');
	obj.style.display = 'none';
}



function createCookie(name, value, days)
{
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
    }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

/*	CHANGE FONT SIZE AND SET A COOKIE TO STORE THE SETTING	*/
function fontSizer(size)
{
	createCookie("valSabinFontSize", size, 365);
	window.location.reload();
}

/*	CHANGE FONT FAMILY AND SET A COOKIE TO STORE THE SETTING	*/
function fontStyler(family)
{
	createCookie("valSabinFontFamily", family, 365);
	window.location.reload();
}




/*	GOOGLE MAPS FUNCTIONS	*/
function load()
{
  if (GBrowserIsCompatible())
  {
	var map = new GMap2(document.getElementById("map"));
	map.addControl(new GMapTypeControl());
	map.addControl(new GLargeMapControl());
	map.addControl(new GScaleControl());

	var baseIcon = new GIcon();
	baseIcon.image = "http://www.valsabinpublications.com/images/layout/marker.png";
	baseIcon.iconSize = new GSize(60, 60);
	baseIcon.iconAnchor = new GPoint(30, 60);
	baseIcon.infoWindowAnchor = new GPoint(30, 60);
	baseIcon.infoShadowAnchor = new GPoint(30, 60);
	baseIcon.transparent = "http://www.valsabinpublications.com/images/layout/marker.png";

	map.setCenter(new GLatLng(52.267852, -0.925137), 11);

	var myIcon = new GIcon(baseIcon);

	markerOptions = { icon:myIcon };
	var point = new GLatLng(52.247852, -0.925137);
	var marker = new GMarker(point, markerOptions);

	GEvent.addListener(marker, "click", function() {
		marker.openInfoWindow("<b>Val Sabin Publications and Training</b><br />125 Harlestone Road,<br />Northampton,<br />Northamptonshire,<br />U.K.<br />NN5 6AA<br /><br /><b>TEL:</b> +44 (1604) 580974<br /><b>FAX:</b> +44 (1604) 584643<br /><br /><center><a href=\"http://maps.google.com/maps?saddr=&daddr=125, Harlestone Road, Northampton. UK\" target =\"_blank\">&raquo; Get directions to us! &laquo;</a></center>");
	});
	map.addOverlay(marker);
  }
}