var cCart = new Class({
					initialize: function()
					{
						if($('cartswitch'))
						{
							var aCartSwitcher	= $('cartswitch');
							
							this._disableLink(aCartSwitcher);
						
							aCartSwitcher.addEvent('click', function()
															{
																var divSwitcher = $('cartswitcher');
																if(divSwitcher && divSwitcher.hasClass('active'))
																	divSwitcher.removeClass('active');
																else if(divSwitcher)
																	divSwitcher.addClass('active');
															});
							
						}
						
						if(!$('shoppingcart_full'))
						{
							var divCartContents	= $('cartcontents');
							
							if(divCartContents)
							{
								// get the links in the cart
								var arrCartContents	 = $$('#cartcontents .remove_title');
								
								// go through each item
								// remove the anchor behaviour
								for(var i = 0; i < arrCartContents.length; i++)
								{
									this.setDeleteBehaviour(arrCartContents[i]);
								}
							}
							
							// if we have the add title link
							if($('add_title'))
							{
								var aAddTitle = $('add_title');
								
								this._disableLink(aAddTitle);

								aAddTitle.addEvent('click', function()
															{
																if(cart && this.getAttribute && this.getAttribute('href'))
																{
																	cart.alterCartContents(this.getAttribute('href').replace(/^.*\?/,"?"));
																}
															});
							}
							
							if($('add_topic'))
							{
								var aAddTopic = $('add_topic');
								
								this._disableLink(aAddTopic);

								aAddTopic.addEvent('click', function()
															{
																if(cart && this.getAttribute && this.getAttribute('href'))
																{
																	cart.alterCartContents(this.getAttribute('href').replace(/^.*\?/,"?"));
																}
															});
							}
							
							if($('lnk_addtopic'))
							{
								var aAddTopic = $('lnk_addtopic');
								
								this._disableLink(aAddTopic);
							
								aAddTopic.addEvent('click', function()
															{
																if(cart && this.getAttribute && this.getAttribute('href'))
																{
																	cart.alterCartContents(this.getAttribute('href').replace(/^.*\?/,"?"));
																}
															});
							}
							
							if($('addtopictobasketlink'))
							{
								var aAddTopicAccordion = $('addtopictobasketlink');
								
								this._disableLink(aAddTopicAccordion);
							
								aAddTopicAccordion.addEvent('click',function()
																	{
																		if(cart && this.getAttribute && this.getAttribute('href'))
																		{
																			cart.alterCartContents(this.getAttribute('href').replace(/^.*\?/,"?"));
																		}
																	});
							}
							
							//this.getCurrentCart();
						}
					},
					
					setDeleteBehaviour: function(aDeleteLink)
					{
						this._disableLink(aDeleteLink);
								
						// now create a handler
						aDeleteLink.addEvent('click', function()
													  {
														  if(cart && this.getAttribute && this.getAttribute('href'))
														  {
															  cart.alterCartContents(this.getAttribute('href'));
														  }
													  });
					},
					
					getCurrentCart: function()
					{
						this.alterCartContents("?get-cart=1");
					},
					
					alterCartContents: function(strRequest)
					{
						// we need to work out what the request was
						var rqCart 	= new Request({url:		"/z_ajax/ajax_handler.php"+strRequest,
												   method: 	"post"});

						// add a callback function
						rqCart.onSuccess = function(strResponseText, strResponseXML)
						{
							// create a new array for the cart contents
							var arrCartContents 		= new Array();
							arrCartContents["ITEMS"]	= new Array();
																 
							// go through each item in the xml
							for(var i = 0; i < strResponseXML.childNodes.length; i++)
							{
								// if we find a response node, parse it
								if(strResponseXML.childNodes[i].nodeName == "response")
								{
									var xmlResponse = strResponseXML.childNodes[i].childNodes;
									for(var j = 0; j < xmlResponse.length; j++)
									{
										// grab the item in the array
										var xmlNode = xmlResponse[j];
										
										switch(xmlNode.nodeName)
										{
											// --- OTHER PEOPLE IN THE CART --- //
											case "proxy":		 if(!arrCartContents["PROXIES"])
																 	arrCartContents["PROXIES"]		= new Array();
																	
																 var arrProxyDetails				= xmlNode.childNodes;
																 var objProxy						= new Object();
																	 
																 // go through the title details
																 // and add them into an object
																 for(var p = 0; p < arrProxyDetails.length; p++)
																 {
																	 // make sure they match the profile
																	 // of the expected information
																	 switch(arrProxyDetails[p].nodeName)
																	 {
																		 case "name":
																		 case "count":	objProxy[arrProxyDetails[p].nodeName] = arrProxyDetails[p].firstChild.nodeValue;
																						break;
																		 
																	 }
																 }
																 
																 // add the object into the array
																 arrCartContents["PROXIES"].push(objProxy);
																 break;
																 
											// --- YOU --- //
											case "you":			 arrCartContents["YOUR_COUNT"]			= xmlNode.firstChild.nodeValue;
																 break;
											
											// --- YOU --- //
											case "total":		 arrCartContents["TOTAL"]				= xmlNode.firstChild.nodeValue;
																 break;
											
											// --- FULL TOPICS --- //
											case "fulltopics":	 arrCartContents["ITEMS"]["FULL_TOPICS"]	= new Array();
																 
																 // go through the child nodes
																 var arrTitles 	= xmlNode.childNodes;
																 for(var d = 0; d < arrTitles.length; d++)
																 {
																	 // create an object
																	 var objTitle			= new Object();
																	 var xmlTitle 			= arrTitles[d];
																	 var arrTitleDetails	= xmlTitle.childNodes;
																	 
																	 // go through the title details
																	 // and add them into an object
																	 for(var e = 0; e < arrTitleDetails.length; e++)
																	 {
																		 // make sure they match the profile
																		 // of the expected information
																		 switch(arrTitleDetails[e].nodeName)
																		 {
																			 case "id":
																			 case "name":
																			 case "subject":
																			 case "rlink":
																			 case "price":
																			 case "count":
																			 case "rclass": objTitle[arrTitleDetails[e].nodeName] = arrTitleDetails[e].firstChild.nodeValue;
																						    break;
																			 
																		 }
																	 }
																	 
																	 // add the object into the array
																	 arrCartContents["ITEMS"]["FULL_TOPICS"].push(objTitle);
																	 
																 }
																 break;
																 
											// --- SUBJECTS --- //
											case "subjects":	 arrCartContents["ITEMS"]["SUBJECTS"]	= new Array();
																 
																 // go through the child nodes
																 var arrTitles 	= xmlNode.childNodes;
																 for(var d = 0; d < arrTitles.length; d++)
																 {
																	 // create an object
																	 var objTitle			= new Object();
																	 var xmlTitle 			= arrTitles[d];
																	 var arrTitleDetails	= xmlTitle.childNodes;
																	 
																	 // go through the title details
																	 // and add them into an object
																	 for(var e = 0; e < arrTitleDetails.length; e++)
																	 {
																		 // make sure they match the profile
																		 // of the expected information
																		 switch(arrTitleDetails[e].nodeName)
																		 {
																			 case "name":
																			 case "rlink":
																			 case "slink":
																			 case "price":
																			 case "count":
																			 case "rclass": objTitle[arrTitleDetails[e].nodeName] = arrTitleDetails[e].firstChild.nodeValue;
																						    break;
																			 
																		 }
																	 }
																	 
																	 // add the object into the array
																	 arrCartContents["ITEMS"]["SUBJECTS"].push(objTitle);
																	 
																 }
																 break;
																 
											// --- SINGLE TITLES --- //
											case "singletitles": arrCartContents["ITEMS"]["SINGLE_TITLES"]	= new Array();
																 
																 // go through the child nodes
																 var arrTitles 	= xmlNode.childNodes;
																 for(var d = 0; d < arrTitles.length; d++)
																 {
																	 // create an object
																	 var objTitle			= new Object();
																	 var xmlTitle 			= arrTitles[d];
																	 var arrTitleDetails	= xmlTitle.childNodes;
																	 
																	 // go through the title details
																	 // and add them into an object
																	 for(var e = 0; e < arrTitleDetails.length; e++)
																	 {
																		 // make sure they match the profile
																		 // of the expected information
																		 switch(arrTitleDetails[e].nodeName)
																		 {
																			 case "id":
																			 case "name":
																			 case "rlink":
																			 case "price":
																			 case "rclass": objTitle[arrTitleDetails[e].nodeName] = arrTitleDetails[e].firstChild.nodeValue;
																						    break;
																			 
																		 }
																	 }
																	 
																	 // add the object into the array
																	 arrCartContents["ITEMS"]["SINGLE_TITLES"].push(objTitle);
																	 
																 }
																 break;
										}
									}
								}
							}
							
							// if we have instantiated the cart
							// write the cart details out
							if($defined(cart))
								cart.update(arrCartContents);
							
							if($defined(MessageStore))
								MessageStore.read(strResponseXML);
						}
						
						// now send the request
						rqCart.send("updatecart=true");
					},
					
					update: function(arrCartContents)
					{
						var divCartContents	= $('cartcontents');
						
						if($('cartfulltopics'))
							$('cartfulltopics').destroy();
						
						if($('cartsingleitems'))
							$('cartsingleitems').destroy();
									
						if($('cartsubjects'))
							$('cartsubjects').destroy();
						
						if(divCartContents)
						{
							divCartContents.empty();
							
							var bItems			= false;
							
							if(arrCartContents["ITEMS"]["FULL_TOPICS"])
							{
								
								// create a new element
								var divFullTopics 	= new Element('div');
								var h4FullTopics	= new Element('h4');
								
								// set its ID
								divFullTopics.setAttribute('id', 'cartfulltopics');
								h4FullTopics.set('text', 'Full Topics');
								
								// add it into the cart contents
								divFullTopics.adopt(h4FullTopics);
								divCartContents.adopt(divFullTopics);
							
								bItems = true;
								
								for(var i = 0; i < arrCartContents["ITEMS"]["FULL_TOPICS"].length; i++)
								{
									var objItem			= arrCartContents["ITEMS"]["FULL_TOPICS"][i];
									
									var divCartItem 	= new Element('div');
									var aTopicLink		= new Element('a');
									var spanBtnDelete	= new Element('span');
									var aBtnRemove	 	= new Element('a');
									
									bClear					= (i%2==1);
									bLastRow				= (i >= arrCartContents["ITEMS"]["FULL_TOPICS"].length - (bOdd ? 1 : 2));
								
									divCartItem.addClass('cartitem');
									divCartItem.set('html', '&nbsp;- &pound;'+objItem.price);
									
									if(bClear)
										divCartItem.addClass('clearafter');
										
									if(bLastRow)
										divCartItem.addClass('lastrow');
										
									aTopicLink.addClass(objItem.rclass);
									aTopicLink.set('title', 'Navigation: View the topic '+objItem.name);
									aTopicLink.set('text', objItem.name+" ("+objItem.count+" title"+(objItem.count==1?"":"s")+")");
									aTopicLink.setAttribute('href', objItem.rlink);
									aTopicLink.inject(divCartItem, 'top');
									
									spanBtnDelete.addClass('btn_delete');
									
									aBtnRemove.addClass('remove-title');
									aBtnRemove.setAttribute('href', '?remove-topic-from-cart='+objItem.id);
									aBtnRemove.setAttribute('title', 'Function: Remove '+objItem.name+' from your cart');
									aBtnRemove.set('text', 'Delete');
									
									this.setDeleteBehaviour(aBtnRemove);
									
									spanBtnDelete.adopt(aBtnRemove);
									
									divCartItem.adopt(spanBtnDelete);
									divFullTopics.adopt(divCartItem);
									
									if(bClear)
									{
										var divClear = new Element('div');
										divClear.addClass('clear');
										divFullTopics.adopt(divClear);
									}
								}
								
								var divClear = new Element('div');
								divClear.addClass('clear');
								divFullTopics.adopt(divClear);
							}
							
							// if we found the div
							if(arrCartContents["ITEMS"]["SINGLE_TITLES"])
							{
								// create a new element
								var divSingleItems 	= new Element('div');
								var h4SingleItems	= new Element('h4');
								
								// set its ID
								divSingleItems.setAttribute('id', 'cartsingleitems');
								h4SingleItems.set('text', 'Single Items');
								
								// add it into the cart contents
								divSingleItems.adopt(h4SingleItems);
								divCartContents.adopt(divSingleItems);
							
								bItems = true;
								
								// go through the cart and 
								// write out the divs
								var iSingleItemsCount	= arrCartContents["ITEMS"]["SINGLE_TITLES"].length;
								var bOdd				= (iSingleItemsCount%2 == 1);
								
								for(var i = 0; i < iSingleItemsCount; i++)
								{
									var objItem				= arrCartContents["ITEMS"]["SINGLE_TITLES"][i];
									var divCartItem			= new Element('div');
									
									bClear					= (i%2==1);
									bLastRow				= (i >= iSingleItemsCount - (bOdd ? 1 : 2));
								
									divCartItem.addClass('cartitem');
									divCartItem.set('html', '&nbsp;- &pound;'+objItem.price);
									
									if(bClear)
										divCartItem.addClass('clearafter');
										
									if(bLastRow)
										divCartItem.addClass('lastrow');
						
									var aMasterTitleLink	= new Element('a');
									aMasterTitleLink.setAttribute('title', 'Navigation: View the title '+objItem.name);
									aMasterTitleLink.setAttribute('href', objItem.rlink);
									aMasterTitleLink.addClass(objItem.rclass);
									aMasterTitleLink.set('text', objItem.name);
									
									var spanBtnDelete		= new Element('span');
									spanBtnDelete.addClass('btn_delete');
									
									var aBtnRemove			= new Element('a');
									aBtnRemove.addClass('remove-title');
									aBtnRemove.setAttribute('href', '?remove-title-from-cart='+objItem.id);
									aBtnRemove.setAttribute('title', 'Function: Remove '+objItem.name+' from your cart');
									aBtnRemove.set('text', 'Delete');
									
									this.setDeleteBehaviour(aBtnRemove);
									
									spanBtnDelete.adopt(aBtnRemove);
									
									aMasterTitleLink.inject(divCartItem, 'top');
									
									divCartItem.adopt(spanBtnDelete);
									divSingleItems.adopt(divCartItem);
									
									if(bClear)
									{
										var divClear = new Element('div');
										divClear.addClass('clear');
										divSingleItems.adopt(divClear);
									}
								}
								
								var divClear = new Element('div');
								divClear.addClass('clear');
								divSingleItems.adopt(divClear);
							}
							
							if(arrCartContents["ITEMS"]["SUBJECTS"])
							{
								// create a new element
								var divSubjects = new Element('div');
								var h4Subjects	= new Element('h4');
								
								// set its ID
								divSubjects.setAttribute('id', 'cartsubjects');
								h4Subjects.set('text', 'Subjects');
								
								// add it into the cart contents
								divSubjects.adopt(h4Subjects);
								divCartContents.adopt(divSubjects);
							
								bItems = true;
								
								for(var i = 0; i < arrCartContents["ITEMS"]["SUBJECTS"].length; i++)
								{
									var objItem			= arrCartContents["ITEMS"]["SUBJECTS"][i];
									
									var divCartItem 	= new Element('div');
									var aTopicLink		= new Element('a');
									var spanBtnDelete	= new Element('span');
									var aBtnRemove	 	= new Element('a');
									
									bClear					= (i%2==1);
									bLastRow				= (i >= arrCartContents["ITEMS"]["SUBJECTS"].length - (bOdd ? 1 : 2));
								
									divCartItem.addClass('cartitem');
									divCartItem.set('html', '&nbsp;- &pound;'+objItem.price);
									
									if(bClear)
										divCartItem.addClass('clearafter');
										
									if(bLastRow)
										divCartItem.addClass('lastrow');
										
									aTopicLink.addClass(objItem.rclass);
									aTopicLink.set('title', 'Navigation: View the subject '+objItem.name);
									aTopicLink.set('text', objItem.name+" ("+objItem.count+" title"+(objItem.count==1?"":"s")+")");
									aTopicLink.setAttribute('href', objItem.rlink);
									aTopicLink.inject(divCartItem, 'top');
									
									spanBtnDelete.addClass('btn_delete');
									
									aBtnRemove.addClass('remove-title');
									aBtnRemove.setAttribute('href', '?remove-subject-from-cart='+objItem.slink);
									aBtnRemove.setAttribute('title', 'Function: Remove '+objItem.name+' from your cart');
									aBtnRemove.set('text', 'Delete');
									
									this.setDeleteBehaviour(aBtnRemove);
									
									spanBtnDelete.adopt(aBtnRemove);
									
									divCartItem.adopt(spanBtnDelete);
									divSubjects.adopt(divCartItem);
									
									if(bClear)
									{
										var divClear = new Element('div');
										divClear.addClass('clear');
										divSubjects.adopt(divClear);
									}
								}
								
								var divClear = new Element('div');
								divClear.addClass('clear');
								divSubjects.adopt(divClear);
							}
							
							if(!bItems)
							{
								divCartContents.set("html", "<p>When you see a title you think is suitable, just add it to your basket and continue browsing the site. You can always review your basket's contents at any time.</p>");
							}
							
							// update the total
							if($('shopping_cart_grand_total'))
								$('shopping_cart_grand_total').set('html', "&pound;"+arrCartContents["TOTAL"]);
								
							// update the number of items count
							if($('shopping_cart_status'))
							{
								// add 'you' into the status
								var strStatus 	= "You've got <strong>"+arrCartContents["YOUR_COUNT"]+"</strong> title"+(arrCartContents["YOUR_COUNT"] != 1 ? "s" : "")+" in your shopping basket";
								
								// if there are proxies
								if(arrCartContents["PROXIES"])
								{
									// write them in
									strStatus 	+= ", plus ";
									for(var p = 0; p < arrCartContents["PROXIES"].length; p++)
									{
										var objProxy = arrCartContents["PROXIES"][p];
										
										// set the conjunction to be empty
										var strConjunction = "";
										if(p > 0)
										{
											// if we are past the 0th entry
											// bob in a comma
											strConjunction = ", ";
											
											// if this is the last conjunction (but not
											// the only entry) then use 'and'
											if(p == arrCartContents["PROXIES"].length - 1)
												strConjunction = " and ";
										}
				
										strStatus 	+= strConjunction + "<strong>"+objProxy.count+" title"+(objProxy.count != 1 ? "s":"")+"</strong> for "+objProxy.name;
									}
									
									strStatus 	+= ".";
								}
								
								$('shopping_cart_status').set('html', strStatus);
							}
							
							divCartContents.removeClass('waiting');
						}
					},
					
										 
					_disableLink: function(mxLabel)
					{
						// assume failure
						var bDisabled 	= false;
						
						// try and locate the item
						var elTarget	= $(mxLabel);
						
						// if we located it
						if(elTarget)
						{
							// disable the link
							if(Browser.Engine.trident) // IE
								elTarget.onclick = function(){ return false; };
							else // FF, Safari, Opera
								elTarget.setAttribute('onclick', 'return false');
							
							// declare success
							bDisabled = true;
						}
						
						// return the result
						return bDisabled;
					}
});

var cart = null;
window.addEvent('domready', function()
							{
								cart = new cCart();
							});