var playingIft=0;
var playingT=0;

function playSound(ift,t,url) {

	var thePlayImage=document.getElementById("ift"+ift+"t"+t+"play");
	var theStopImage=document.getElementById("ift"+ift+"t"+t+"stop");
	
	//both images found
	if ((thePlayImage!=null)&&(theStopImage!=null)) {
	
		//about to play
		if ((thePlayImage.style.display=="")&&(theStopImage.style.display=="none")) {

			//already playing something
			if ((playingIft>0)||(playingT>0)) {
			
				//stop the current one
				stopSound(playingIft,playingT);
			
			}

			var theWrapper=document.getElementById("playerWrapper");

			//wrapper found
			if (theWrapper!=null) {
			
				//start the sound
				theWrapper.innerHTML='<embed src="'+url+'" autostart="true" style="height:0px;width:0px" id="banquetSoundIft'+ift+'T'+t+'" enablejavascript="true" type="'+getMimeType()+'" />';
				
				//hide the play button
				thePlayImage.style.display="none";
				
				//show the stop image
				theStopImage.style.display="";
				
				//remember what we're playing
				playingIft=ift;
				playingT=t;
			
			}
			
		}
		
	}
	
}

//from http://www.scriptwell.net/howtoplaysound.htm
function getMimeType() {

	var mimeType="application/x-mplayer2";
	var agt=navigator.userAgent.toLowerCase();
	
	//not ie, not windows
	if ((navigator.mimeTypes)&&(agt.indexOf("windows")==-1)) {

  		var plugin=navigator.mimeTypes["audio/mpeg"].enabledPlugin;
  		
  		//mac/safari and linux/firefox
  		if (plugin)
  			mimeType="audio/mpeg";

	}
	
	return mimeType;
	
}

function stopSound(ift,t) {

	var thePlayImage=document.getElementById("ift"+ift+"t"+t+"play");
	var theStopImage=document.getElementById("ift"+ift+"t"+t+"stop");
	
	//both images found
	if ((thePlayImage!=null)&&(theStopImage!=null)) {
				
		//about to stop
		if ((thePlayImage.style.display=="none")&&(theStopImage.style.display=="")) {
		
			var theEmbed=document.getElementById("banquetSoundIft"+ift+"T"+t);

			//embed found
			if (theEmbed!=null) {
			
				try {
				
					//stop the sound
					theEmbed.Stop();
					
					//hide the stop button
					theStopImage.style.display="none";

					//show the play image
					thePlayImage.style.display="";

					//remember that we're not playing
					playingIft=0;
					playingT=0;

				} catch(e) {
				
					//refresh the page
					location.reload(true);
				
				}
				
			}

		}

	}

}

function addToCart(catNo,formatName) {

	var url=catNo+"?add="+encodeURIComponent(formatName)+"&time="+new Date().getTime();

	//proper browser
	if (window.XMLHttpRequest)
		req=new XMLHttpRequest();

	//internet explorer 6 or earlier
	else if (window.ActiveXObject)
		req=new ActiveXObject("Microsoft.XMLHTTP");

	req.onreadystatechange=processAddToCartRequest;
	req.open("GET",url,true);
	req.send(null);
	
	return false;
	
}

function processAddToCartRequest() {

	//loaded
	if (req.readyState==4) {

		//ok
		if (req.status==200) {

			var response=unescape(req.responseText);

			var width=600;
			var height=225;

			//get the cart span
			var cart=document.getElementById("cart");
			
			//update the text
			cart.innerHTML=response;
			
			//get the overlay
			var overlay=document.getElementById("overlay");

			//size the overlay
			overlay.style.height=getDocHeight()+"px";
			overlay.style.width=getDocWidth()+"px";

			//style the overlay
			overlay.style.filter="alpha(opacity=70)";
			overlay.style.opacity="0.7";

			//display the overlay
			overlay.style.display="";

			//get the bought
			var bought=document.getElementById("bought");

			//position the bought
			bought.style.left=((getClientWidth()-width)/2)+"px";
			bought.style.top=((getClientHeight()-height)/2)+"px";

			//display the bought
			bought.style.display="";
			
		}
		
	}

}

function continueShopping() {

	//hide the bought
	document.getElementById("bought").style.display="none";
	
	//hide the overlay
	document.getElementById("overlay").style.display="none";
	
	return false;
	
}

//from http://www.softcomplex.com/docs/get_window_size_and_scrollbar_position.html
//and http://james.padolsey.com/javascript/get-document-height-cross-browser/
function getClientWidth() {
	return filterResults (
		window.innerWidth ? window.innerWidth : 0,
		document.documentElement ? document.documentElement.clientWidth : 0,
		document.body ? document.body.clientWidth : 0
	);
}

function getClientHeight() {
	return filterResults (
		window.innerHeight ? window.innerHeight : 0,
		document.documentElement ? document.documentElement.clientHeight : 0,
		document.body ? document.body.clientHeight : 0
	);
}

function getDocHeight() {
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}

function getDocWidth() {
    return Math.max(
        Math.max(document.body.scrollWidth, document.documentElement.scrollWidth),
        Math.max(document.body.offsetWidth, document.documentElement.offsetWidth),
        Math.max(document.body.clientWidth, document.documentElement.clientWidth)
    );
}

function filterResults(n_win, n_docel, n_body) {
	var n_result = n_win ? n_win : 0;
	if (n_docel && (!n_result || (n_result > n_docel)))
		n_result = n_docel;
	return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
}