/*

main.js

copyright (c) 2006-2009 Eric Hossinger <eric.hossinger@jamieoliver.com>

*/

function helpM(id) {

	var hide = 'general';

	if(id == 'general') {
		var hide = 'shopping';
	}

	document.getElementById(id).style.visibility='visible';
	document.getElementById(id).style.position='relative';
	document.getElementById(id + 'H').style.background='#FFFFFF';

	document.getElementById(hide).style.visibility='hidden';
	document.getElementById(hide).style.position='absolute';
	document.getElementById(hide + 'H').style.background='#EEEEEE';

	for(i = 0; i < 99; i++) {
		if(document.getElementById('hq' + i)) {
			document.getElementById('hq' + i).style.visibility='hidden';
			document.getElementById('hq' + i).style.position='absolute';
		}
	}
}

//check an object's visibility and swap
function helpQ(id) {

	for(i = 0; i < 99; i++) {
		if(document.getElementById('hq' + i) && 'hq' + i != id) {
			document.getElementById('hq' + i).style.visibility='hidden';
			document.getElementById('hq' + i).style.position='absolute';
		}
	}

	if(document.getElementById(id).style.visibility == 'hidden' || document.getElementById(id).style.visibility == '') {

		document.getElementById(id).style.visibility='visible';
		document.getElementById(id).style.position='relative';
	
	} else {
	
		document.getElementById(id).style.visibility='hidden';
		document.getElementById(id).style.position='absolute';
	
	}

}

//swap an image's source
function swapImg(id, img) {

	document.getElementById(id).src=img;

}

//swap an object's css class
function swapClass(id, newClass) {

	document.getElementById(id).className=newClass;

}

//swap an object's visibility
function swapVis(id, vis) {

	document.getElementById(id).style.visibility=vis;

}

function showRecipes(show) {

	var noCats = document.getElementById('noCats').value;

	for(i = 0; i < noCats; i++) {
		document.getElementById('recipes' +  i).style.visibility='hidden';
	}

	if(show != 'none') {
		document.getElementById('recipes' +  show).style.visibility='visible';
	}

}

//e is event object passed from function invocation
function checkEnter(e, link) {

	var characterCode //literal character code will be stored in this variable

	if(e && e.which){ //if which property of event object is supported (NN4)
		e = e
		characterCode = e.which //character code is contained in NN4's which property
	}
	else{
		e = event
		characterCode = e.keyCode //character code is contained in IE's keyCode property
	}

	if(characterCode == 13){ //if generated character code is equal to ascii 13 (if enter key)
		window.location=link;
	}

}

function val_enq(form) {

	if(form.area.options[form.area.selectedIndex].value == '') {
		alert('Please select an area of enquiry!');
		return false;
	}

	if(empty_val(form.name.value)) {
		alert('Missing name!');
		return false;
	}

	if(email_val(form.email.value)) {
		alert('Invalid email!');
		return false;
	}

	if(empty_val(form.message.value)) {
		alert('Missing message!');
		return false;
	}

	return true;

}

function email_val(email) {
   var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
   if(reg.test(email) == false) {
      return true;
   }

	return false;
}

function empty_val(val) {
	if(val == '') {
		return true;
	}

	return false;
}

/* video functions */

function misc_video(id, video, width, height, img, autostart) {
	var s1 = new SWFObject('http://www.jamieoliver.com/core/js/flvplayer.swf', 'showplayer1', width, height, '1', '#000000');
	s1.addVariable('width',width);
	s1.addVariable('height',height);
	s1.addVariable('file','http://c0569222.cdn.cloudfiles.rackspacecloud.com/' + video + '.flv');
	s1.addVariable('image','http://c0577462.cdn.cloudfiles.rackspacecloud.com/' + img + '.jpg');
	s1.addVariable('backcolor','0x597B8E');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xEEE605');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('shuffle','false');
	s1.addVariable('callback','analytics');
	s1.addVariable('autostart',autostart);
	s1.write(id);
}

function video(id, name) {
	var s1 = new SWFObject('http://www.jamieoliver.com/core/js/flvplayer.swf', 'showplayer1', 320, 260, '1', '#000000');
	s1.addVariable('width',320);
	s1.addVariable('height',260);
	s1.addVariable('file','http://c0569222.cdn.cloudfiles.rackspacecloud.com/' + name + '.flv');
	s1.addVariable('image','http://c0577462.cdn.cloudfiles.rackspacecloud.com/' + name + '.jpg');
	s1.addVariable('backcolor','0x597B8E');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xEEE605');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('shuffle','false');
	s1.addVariable('callback','analytics');
	s1.addVariable('autostart',false);
	s1.write(id);
}

function wide_video(id, name) {
	var s1 = new SWFObject('http://www.jamieoliver.com/core/js/flvplayer.swf', 'showplayer1', 452, 276, '1', '#000000');
	s1.addVariable('width',452);
	s1.addVariable('height',276);
	s1.addVariable('file','http://c0569222.cdn.cloudfiles.rackspacecloud.com/' + name + '.flv');
	s1.addVariable('image','http://c0577462.cdn.cloudfiles.rackspacecloud.com/' + name + '.jpg');
	s1.addVariable('backcolor','0x597B8E');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xEEE605');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('shuffle','false');
	s1.addVariable('callback','analytics');
	s1.addVariable('autostart',false);
	s1.write(id);
}

function wide_video_play(id, name) {
	var s1 = new SWFObject('http://www.jamieoliver.com/core/js/flvplayer.swf', 'showplayer1', 452, 276, '1', '#000000');
	s1.addVariable('width',452);
	s1.addVariable('height',276);
	s1.addVariable('file','http://c0569222.cdn.cloudfiles.rackspacecloud.com/' + name + '.flv');
	s1.addVariable('image','http://c0577462.cdn.cloudfiles.rackspacecloud.com/' + name + '.jpg');
	s1.addVariable('backcolor','0x597B8E');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0xEEE605');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('shuffle','false');
	s1.addVariable('callback','analytics');
	s1.addVariable('autostart',true);
	s1.write(id);
}

/* create XMLHttp object */

function getXMLHttp()
{
	var xmlHttp

	try {
		//Firefox, Opera 8.0+, Safari
		xmlHttp = new XMLHttpRequest();
	} catch(e) {
		//Internet Explorer
		try {
			xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		} catch(e) {
			try	{
				xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			} catch(e) {
				alert("Your browser does not support AJAX!")
				return false;
			}
		}
	}

	return xmlHttp;
}

/* temporary: roadblock functions (can remove)  */

function startRbEffects() {
	var xmlHttp = getXMLHttp();

	xmlHttp.onreadystatechange = function() {
		if(xmlHttp.readyState == 4)	{
			if(xmlHttp.responseText == 'y') {
				echoRoadblock();
				//scroll to top, allow side scrolling and change element properties for IE6 or lower browsers
				var ver = getInternetExplorerVersion();
				if(ver > -1 && ver < 7) {
					document.getElementById('roadblockAd').style.position='absolute';
					document.getElementById('roadblockBg').style.height='1024px';
					document.getElementById('roadblockBg').style.width=screen.width + 'px';
					document.getElementById('roadblockBg').style.position='absolute';
					window.scrollBy(0,-2000);
					setTimeout("fadeInBg(10)", 100);
				//scroll to top and allow side scrolling for smaller resolutions
				} else if(screen.height < 865) {
					document.getElementById('roadblockAd').style.position='absolute';
					window.scrollBy(0,-2000);
					setTimeout("fadeInBg(10)", 100);
				} else {
					setTimeout("fadeInBg(10)", 500);
				}
			}
		}
	}

	xmlHttp.open("GET", "http://www.jamieoliver.com/roadblockShow.php?r=" + Math.random(), true);
	xmlHttp.send(null);
}

function fadeInBg(opac) {

	document.getElementById('roadblockBg').style.opacity='0.' + opac;
	document.getElementById('roadblockBg').style.filter='alpha(opacity=' + opac +')';
	if(parseInt(opac) < 75) {
		var newOpac = parseInt(opac) + 5;
		setTimeout("fadeInBg(" + newOpac + ")", 2);
	} else {
		setTimeout("slideDownAd(600)", 200);
	}

}

function slideDownAd(move) {
	var moveDown = parseInt(move) - 30; //must be divisor of top: <number>; for roadblockAd CSS
	document.getElementById('roadblockAd').style.top='-' + moveDown + 'px';
	if(moveDown > 0) {
		setTimeout("slideDownAd(" + moveDown + ")", 10);
	} else {
		//scroll to top for IE6 or lower browsers
		var ver = getInternetExplorerVersion();
		if(ver > -1 && ver < 7) {
			window.scrollBy(0,-2000);
		}
		//scroll to top for smaller resolutions
		else if(screen.height < 865) {
			window.scrollBy(0,-2000);
		}
		setTimeout("playVideo()", 250);
	}
}

function playVideo() {
	var s1 = new SWFObject('http://www.jamieoliver.com/core/js/flvplayer.swf', 'showplayer1', 452, 276, '1', '#000000');
	s1.addVariable('width',452);
	s1.addVariable('height',276);
	s1.addVariable('file','http://c0569222.cdn.cloudfiles.rackspacecloud.com/jme-garden.flv');
	s1.addVariable('backcolor','0x00b5e5');
	s1.addVariable('frontcolor','0xFFFFFF');
	s1.addVariable('lightcolor','0x00b5e5');
	s1.addParam('allowfullscreen','true');
	s1.addVariable('shuffle','false');
	s1.addVariable('autostart',true);
	s1.write('rbVideo');
}

function closeRoadblock() {
	document.getElementById('roadblockWrap').innerHTML='';
	document.getElementById('roadblockWrap').style.position='absolute';

	var xmlHttp = getXMLHttp();

	xmlHttp.open("GET", "http://www.jamieoliver.com/roadblockSkip.php?r=" + Math.random(), true);
	xmlHttp.send(null);
}

function echoRoadblock() {
	var html = '';
	html += '<div id="roadblockAd">';
	html += '<div id="rbSummer"> <div class="close"><a href="#" onclick="closeRoadblock()">ADVERTISMENT (CLOSE WINDOW)</a></div>  <div id="rbSummerInner"> 	<a href="http://www.jamieathome.com?isource=int_rb_jo" target="_blank"><img src="http://www.jamieoliver.com/roadblock/jah.jpg" width="500" height="262" border="0" alt="" /></a> </div> </div>';
	html += '</div>';
	html += '<div id="roadblockBg">&nbsp;</div>';

	document.getElementById('roadblockWrap').innerHTML=html;

}

/* from msdn - http://msdn.microsoft.com/en-us/library/ms537509.aspx */

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

function rmHide(id) {
	swapClass(id + 'H', 'h')
	swapClass(id + 'S', 'v')
	swapClass(id, 'hidden menuBody');
}

function rmShow(id) {
	swapClass(id + 'H', 'v')
	swapClass(id + 'S', 'h')
	swapClass(id, 'menuBody');

	if (navigator.appName == 'Microsoft Internet Explorer')
	{
		document.getElementById(id).style.margin='-2px 0px 0px 0px';
	}
}

function popitup(url) {
	newwindow=window.open(url,'name','height=450,width=550');
	if (window.focus) {newwindow.focus()}
	return false;
}

function clearSearch() {
	if(document.getElementById('searchBox').value == "type search term here") {
		document.getElementById('searchBox').value = '';
	}
}