
		/***********************************************************************
		*
		* Shopping Cart Javascript		-	contains code to convert the Actinic 
		*									cart to AJAX call and add popups to 
		*									display information.
		*
		* Tod Designs (c) 2011
	        * 
	        * Issue V1.01.0042
	        * Revised 12/1/2011 to add a popup to the form that is used to 
	        * add product to cart. This popup displays error messages, and the
	        * DisplayMyCart popup only appears if products are added to cart.
	        * Improved error handling when extracting messages from returned page
		*
		* Revised 18/1/2011 to increase amount of button that is active, added
		* thememargins to cart button pop up
		************************************************************************/
	
		
		function display_cart(displayPopUp, $form) {
			var seconds_to_wait = 15;
            $('.form_button_add_to_cart').HideAllBubblePopups();                
            if (displayPopUp == 1)	{
                $form.ShowAllBubblePopups();   
                $form.FreezeBubblePopup();
            } else {
                $('#DisplayMyCart').ShowBubblePopup();
                $('#DisplayMyCart').FreezeBubblePopup(); 
            }
			function doCountdown(){
				var timer = setTimeout(function(){
					seconds_to_wait--;
					if(seconds_to_wait > 0){
						doCountdown();
					}else{
						$('#DisplayMyCart').HideBubblePopup();
                        $form.RemoveBubblePopup();    
					};
				},1000);
			};
			doCountdown();
		}	
		
		function showresponse(responseText, statusText, xhr, $form) {
            // the function append_basket() uses actinic variables so is defined in an actinic layout.
			myText = append_basket();
            displayIsNormal = 0;
            // an eorror occurred lets see if we can find it.
			if (myText =='FAIL') {
                // set default message
                //myError = "<span class=\"actrequiredcolor\"> THE ITEM WAS NOT ADDED TO THE CART<br /> </span>"
                myError = "<span class=\"actrequiredcolor\">This product is currently out of stock.</span>"
                displayIsNormal = 1;  
			    // first find the start of body
				startpoint= responseText.search("<body") + 18;
                // now find the first error string
                startpoint = responseText.indexOf("actrequiredcolor",startpoint) + 18;
                // did we find it
                if (startpoint > 18) {
                    // see if we can find a second string
                    startMessage = responseText.indexOf("actrequiredcolor",startpoint) + 18;
                    // sometimes there is no second message
                    if (startMessage < 18) {
                        startMessage =  startpoint;
                    }
                    // looking for end of message
                    LengthMessage   = responseText.indexOf("</span>",startMessage) - startMessage;
                    //get the error message otherwise we will use the default set above 
                    if (LengthMessage > 0) {
                        myError = "<span class=\"actrequiredcolor\"> THE ITEM WAS NOT ADDED TO THE CART<br /> " + responseText.substr(startMessage, LengthMessage) + "</span>"
                    }
                }
                //create a new popup to display the error attaching it to the product that generated the error
                $form.CreateBubblePopup({ 
                    position: 'bottom',
                    align: 'center',
                    mouseover: 'hide',
                    manageMouseEvents: false,
                    innerHtml: myError, 
                    innerHtmlStyle: {
                                        color:'#0000FF', 
                                        'text-align':'center'
                                    },
                    tail: { align: 'center', hidden: true },                      
                    themeName:     'orange',
                    themePath:     'jquerybubblepopup-theme'
                });
			} else {
                // update cart display when product is added to cart
                $('#DisplayMyCart').SetBubblePopupInnerHtml(myText ,true);
            }	
            // timed display of cart or error message
			display_cart(displayIsNormal, $form);
		}
		
		$(document).ready(function() { 
			// over write default basket
			append_basket();
			
			// create the cart popup
			$('#DisplayMyCart').CreateBubblePopup({
				align: 'bottom',
				innerHtml: '<p> This displays the summary of the cart contents!<br /></p>',						
				innerHtmlStyle: {
							color:'#0000FF', 
							'text-align':'center'
						},
				mouseover: 'hide',
				manageMouseEvents: false,
				openingSpeed: 1000,
				closingDelay: 12000,
				closingSpeed: 3000,
				tail: { align: 'center', hidden: true },
				themeName: 	'blue',
				themePath: 	'jquerybubblepopup-theme'			
			});
            
            // add to cart button popup	
			$('.form_button_add_to_cart').CreateBubblePopup({
				align: 'right',
				innerHtml: 'click to add <br />the product!', 
				innerHtmlStyle: {
									color:'#FFFFFF', 
									'text-align':'center'
								},					
                		themeMargins: {
                                    					total: '8px',
                                    					difference: '5px'
                                				},
     				themeName: 	'all-blue',
				themePath: 	'jquerybubblepopup-theme'
			});
			            
			// convert product form to use AJAX
			if($('.ProductForm').length > 0) {
				$('.ProductForm').ajaxForm({
					success: showresponse
				});
			}

			// display when add to cart is pressed
			$('.form_button_add_to_cart').click(function(){			
				var shoppingcart_button = $(this);
				var bubble_popup_id = shoppingcart_button.GetBubblePopupID(); 
				shoppingcart_button.ShowBubblePopup({		
					align: 'center',
					innerHtml: 	'<p>Product is being added to the cart</p><br />' + 
									'<p><a href="#null">close</a></p>',							
					innerHtmlStyle:{ 
									color:'#666666', 
									'text-align':'left'
					},					
					themeName: 	'blue',
					themePath: 	'jquerybubblepopup-theme'
				}, false); 
				shoppingcart_button.FreezeBubblePopup(); 		
				$('#'+bubble_popup_id+' a:last').click(function(){
					$(shoppingcart_button).HideBubblePopup();
				});	
			}); 
		});
