/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

/*	Ben's changeContent function */

/* -------------------------------------------------------------------------- */
/* -------------------------------------------------------------------------- */

function changeContent(ele,content)
{
	var $ele = $(ele);
	
	if(!Browser.Engine.trident4) // all but IE6 can do this
	{
		if(!$ele.hasClass( "opened" ) ){ // content not opened yet - needs a wrapper div
			
			// wrap in container
			var wrap = new Element ('div',{'margin': '0px', 'overflow': 'hidden', 'position': 'static'}).wraps($ele);
			wrap.setStyle('height',$ele.getSize().y);
			
			$ele.setProperty('class','opened');
			
			changeContent(ele,content);
		}
		else{
			var resizeFade = function()
			{		
				// set content
				$ele.set('html',content||"");
				//sifrReplace();
				
				// if height wrong, resize then fade
				if($ele.getParent().getSize().y != $ele.getSize().y) var resize = new Fx.Tween($ele.getParent())
					.start('height',$ele.getSize().y)
					.chain(function(){
						var myFade = new Fx.Tween(ele).start('opacity',1)
					});
				// else fade now
				else var myFade = new Fx.Tween(ele).start('opacity',1)
						
			};
			
			if(!$ele.getSize().y) // no content = no fadeout
			{
				$(ele).setStyle('opacity',0);
				resizeFade();
			}
			else var myFade = new Fx.Tween(ele).start('opacity',0).chain(resizeFade);
		}
	}
	else // IE 6
	{
		$ele.set('html',content||"");
		sifrReplace();
	}
}


/* -------------------------------------------------------------------------- */

function reveal(ele)
{
	var $ele = $(ele);
	$ele.style.display='block';
	
	if(!Browser.Engine.trident4) // all but IE6 can fade
	{
		$ele.fade('hide'); // set opacity to 0
		
		var mySlide = new Fx.Slide(ele)
			.hide() // mask ele
			.toggle() // bring in
			.chain(function(){ // oncomplete, fade
				$ele.fade('in');
			});
	}
	
}

/* -- other stuff ----------------------------------------------------------- */


function checkQuestionForm() {

	errors = '';
	
	if (document.questionForm.name.value=='')
		errors += "\nYour Name";
	
	if (checkEmailQuietly(document.questionForm.email.value)==false)
		errors += "\nYour Email Address";
	
	if (document.questionForm.question.value=='')
		errors += "\nYour Question";

	if (errors != '') {

		window.alert('Please complete the following:'+errors);
		return false;

	} else {

		return true;

	}

}

/* -------------------------------------------------------------------------- */


function checkEmail(emailAddress) {

	var str = emailAddress;
	if ((str.indexOf("@") > 0) == true) {
		return true;
	} else {
		window.alert('Please enter a valid email address');
		return false;
	}
}

/* -------------------------------------------------------------------------- */

function checkEmailQuietly(emailAddress) {

	var str = emailAddress;
	if ((str.indexOf("@") > 0) == true) {
		return true;
	} else {
		return false;
	}
}

/* -------------------------------------------------------------------------- */

function showFormat(f) {

	for (i=0; i<formats.length; i++) {
		
		document.getElementById('format'+i).style.display = 'none';
	}
	document.getElementById('format'+f).style.display = 'block';

}

/* -------------------------------------------------------------------------- */

function updateTotal() {
	total = 0;
	quantity = 0;
	
	//window.alert(instances.length+'/'+prices.length);

	for (i = 1; i < instances.length; i++) {

		q = 'q'+instances[i];
		thisQuan = eval('document.orderForm.'+q+'.value');
		total += (thisQuan*prices[i]);
		quantity += thisQuan;

	}

	loadContent('productDetailsTotalPrice', '/product_includes/itemTotal.php?total='+total);
//	document.getElementById('productDetailsTotalPrice').innerHTML = 'Total Price: &pound;'+total.toFixed(2);
}

/* -------------------------------------------------------------------------- */

function checkQuantities() {

	if (quantity == 0) {

		window.alert('Please enter a quantity of an item to proceed');
		return false;

	} else

		return true;

}

/* -------------------------------------------------------------------------- */

function showImage(image, id, count) {

	//	window.alert(image+', '+id+', '+count);

/*	for (i = 0; i < count; i++) {

		doDiv('caption'+i, 'none');
	}

	doDiv('caption'+id, 'block');*/
	document.mainImage.src = image;

}

/* -------------------------------------------------------------------------- */

function doDiv(thisElement, mode) { 

	if (document.getElementById) { // DOM3 = IE5, NS6 
		document.getElementById(thisElement).style.display = mode; 
	} else { 
		if (document.layers) { // Netscape 4 
			document[thisElement].display = mode; 
		} else { // IE 4 
			document.all[thisElement].style.display = mode; 
		} 
	} 
}

var noContent = '';
var liveReq = false;
var liveReqLast = '';
var liveReqDestination = '';

if (window.XMLHttpRequest) {
	liveReq = new XMLHttpRequest();
}

/* -------------------------------------------------------------------------- */

function loadContent(destination, uri) {
	
	liveReqDestination = destination;

	if (liveReqLast != liveReqDestination && liveReqDestination != '') {
	
		//document.getElementById(liveReqDestination).innerHTML = noContent;
	
		if (window.XMLHttpRequest) {
			liveReq = new XMLHttpRequest();
		} else if (window.ActiveXObject) {
			liveReq = new ActiveXObject("Microsoft.XMLHTTP");
		}
	
		liveReq.onreadystatechange = liveReqProcessReqChange;
		liveReq.open('GET', uri);
		liveReq.send(null);

	}

}

/* -------------------------------------------------------------------------- */

function liveReqProcessReqChange() {

	if (liveReq.readyState == 4) {
		document.getElementById(liveReqDestination).innerHTML = liveReq.responseText;
	}
}

/* -------------------------------------------------------------------------- */

function selectInstance(i, noInstances, instance_id, price, pds) {

	for (n=0; n<noInstances; n++) {

		this_instance = 'instance'+n;

		document.getElementById(this_instance).style.border='1px solid #ffffff';
		document.getElementById(this_instance).style.background='#ffffff';

	}

	this_instance = 'instance'+i;

	document.getElementById(this_instance).style.border='1px solid #4a4944';
	document.getElementById(this_instance).style.background='#f4f3e6';

	showBuyOptions(price, instance_id, pds);
	

}

/* -------------------------------------------------------------------------- */

function showBuyOptions(price, inst, pds) {

	buyHTML = '<input type="hidden" name="chosenFormat" value="'+inst+'" />';
	buyHTML = buyHTML + '<div class="dynPrice">Price <img src="/themes/original/arrow.gif" alt="&gt;" width="8" height="7" border="0" /> &pound;'+price+'</div>';
	buyHTML = buyHTML + 'Quantity <img src="/themes/original/arrow.gif" alt="&gt;" width="8" height="7" border="0" /> <input type="text" size="2" name="quantity" value="1" class="orderFormInput" /><br /><br />';
	buyHTML = buyHTML + pds;
	buyHTML = buyHTML + '<input type="image" title="Add this item to your order" src="/themes/original/buy.gif" alt="Buy" width="80" height="33" border="0" />';

	document.getElementById('price').innerHTML = buyHTML;


}

/* -------------------------------------------------------------------------- */

function launchSurvey() {

	var w = 620;
	var h = 600;
	var winl = (screen.width - w) / 2;
	var wint = (screen.height - h) / 2;

	winProps = 'width='+w+',height='+h+',top='+wint+',left='+winl+',scrollbars=1,status=1,resizable=1';

	survey = window.open('/surveySpring2008/', 'nestSurvey', winProps);
	survey.focus();

}

/* -------------------------------------------------------------------------- */

function checkMailingListEmail() {

	if (document.userForm.email.value != '' && document.userForm.email.value != 'Your email address') {
	
		return true;
		
	} else {
	
		window.alert('Please enter your email address.');
		return false;

	}

}

/* -------------------------------------------------------------------------- */

var css_browser_selector = function()
{
	var 
		ua=navigator.userAgent.toLowerCase(),
		is=function(t){ return ua.indexOf(t) != -1; },
		h=document.getElementsByTagName('html')[0],
		b=( !(/opera|webtv/i.test(ua)) && /msie (\d)/.test(ua) )? ('ie ie'+RegExp.$1):
			is('gecko/')? 'gecko':
				is('opera/9')? 'opera opera9':
					/opera (\d)/.test(ua)? 'opera opera'+RegExp.$1:
						is('konqueror')? 'konqueror':
							is('applewebkit/')? 'webkit safari':
								is('mozilla/')? 'gecko':'',
		os=( is('x11')||is('linux') )? ' linux':
			is('mac')? ' mac':
				is('win')? ' win':
					'';
	var c=b+os+' js';
	
	h.className += h.className?' '+c:c;
}();

/* -------------------------------------------------------------------------- */

function loadContentSlide(targetDiv, uri, callback)
	{	
		
		$(targetDiv).set('text','');
		var req = new Request({
			url: uri, method: 'get', 
			onComplete: function(response) {
				//$(targetDiv).set('html',response);
				
				/*if(!Browser.Engine.trident4){
					$(targetDiv).fade('hide');
					var mySlide = new Fx.Slide(targetDiv).hide().toggle()
						.chain(function(){ $(targetDiv).fade('in'); });
				}*/
				changeContent(targetDiv,response);
				if(callback)callback();
			}
		}).send();
	}



