(function () {
    $.fn.infiniteCarousel = function () {
        function repeat(str, n) {
            return new Array( n + 1 ).join(str);
        }
        
        return this.each(function () {
            // magic!
            var $wrapper = $('> div', this).css('overflow', 'hidden'),
                $slider = $wrapper.find('> ul').width(9999),
                $items = $slider.find('> li'),
                $single = $items.filter(':first'),
                
                singleWidth = $single.outerWidth(),
                visible = Math.ceil($wrapper.innerWidth() / singleWidth),
                currentPage = 1,
                pages = Math.ceil($items.length / visible);
                
            /* TASKS */
            
            // 1. pad the pages with empty element if required
            if ($items.length % visible != 0) {
                // pad
                $slider.append(repeat('<li class="empty" />', visible - ($items.length % visible)));
                $items = $slider.find('> li');
            }
            
            // 2. create the carousel padding on left and right (cloned)
            $items.filter(':first').before($items.slice(-visible).clone().addClass('cloned'));
            $items.filter(':last').after($items.slice(0, visible).clone().addClass('cloned'));
            $items = $slider.find('> li');
            
            // 3. reset scroll
            $wrapper.scrollLeft(singleWidth * visible);
            
            // 4. paging function
            function gotoPage(page) {
                var dir = page < currentPage ? -1 : 1,
                    n = Math.abs(currentPage - page),
                    left = singleWidth * dir * visible * n;
                
                $wrapper.filter(':not(:animated)').animate({
                    scrollLeft : '+=' + left
                }, 500, function () {
                    // if page == last page - then reset position
                    if (page > pages) {
                        $wrapper.scrollLeft(singleWidth * visible);
                        page = 1;
                    } else if (page == 0) {
                        page = pages;
                        $wrapper.scrollLeft(singleWidth * visible * pages);
                    }
                    
                    currentPage = page;
                });
            }
            
            // 5. insert the back and forward link
            $wrapper.after('<a href="#" class="arrow back">&lt;</a><a href="#" class="arrow forward">&gt;</a>');
            
            // 6. bind the back and forward links
            $('a.back', this).click(function () {
                gotoPage(currentPage - 1);
                return false;
            });
            
            $('a.forward', this).click(function () {
                gotoPage(currentPage + 1);
                return false;
            });
            
            $(this).bind('goto', function (event, page) {
                gotoPage(page);
            });
            
            // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
            $(this).bind('next', function () {
                gotoPage(currentPage + 1);
            });
        });
    };
})(jQuery);

$(document).ready(function () {
    // THIS IS NEW CODE FOR THE AUTOMATIC INFINITE CAROUSEL
    var autoscrolling = false;
    
    $('.infiniteCarousel').infiniteCarousel().mouseover(function () {
        autoscrolling = false;
    }).mouseout(function () {
        autoscrolling = false;
    });
    
    setInterval(function () {
        if (autoscrolling) {
            $('.infiniteCarousel').trigger('next');
        }
    }, 2000);
	
	 // Set up a listener so that when anything with a class of 'tab' 
	 // is clicked, this function is run.
	 $('.tab').click(function () {

		  // Remove the 'active' class from the active tab.
		  $('#infotabs_container > .tabs > li.active')
			  .removeClass('active');
			  
		  // Add the 'active' class to the clicked tab.
		  $(this).parent().addClass('active');
	
		  // Remove the 'tab_contents_active' class from the visible tab contents.
		  $('#infotabs_container > .infotab_contents_container > div.infotab_contents_active')
			  .removeClass('infotab_contents_active');
	
		  // Add the 'tab_contents_active' class to the associated tab contents.
		  $(this.rel).addClass('infotab_contents_active');

	 });
});
function vaseaddremove(pos, upsaleid,bolChecked){
	var upsaleElem;
	upsaleElem = document.getElementById('upsale'+pos);

	if(bolChecked == true)
	{
		upsaleElem.value = upsaleid;
		document.getElementById('upgradeDiv'+upsaleid).className = "upsellselected";
	}
	else
	{
		upsaleElem.value = 0;
		document.getElementById('upgradeDiv'+upsaleid).className = "upsell_carousel";
	}
}										
		
	function UpgradeProd(upsaleid,bolChecked){
		
	if(bolChecked == true)
		document.frmBuyFlower.upgrade.value = upsaleid;
			
	else
		document.frmBuyFlower.upgrade.value = 0;
			
	}	
	
	<!------------ NEW BIT FOR PRODUCT SIZE OPTIONS ------------->
	function UpgradeProduct(upsaleid)
	{
		document.frmBuyFlower.upgrade.value = upsaleid;
	}	
	<!------------ END OF NEW BIT FOR PRODUCT SIZE OPTIONS ------------->
	

function seldateNew()
{
	// get the current selection of delivery option
	if (document.frmBuyFlower.tdelivery)
	{
		for (i=0;i<document.frmBuyFlower.tdelivery.length;i++) {
			if (document.frmBuyFlower.tdelivery[i].checked) {
				var TDelivery = document.frmBuyFlower.tdelivery[i].value;
			}
		} 

	var Deliverdate = document.getElementById('delDate').value;
	var deliveryOverlay = document.getElementById('deliveryOverlay');
	
	if (deliveryOverlay)
	{
		var dateArray = Deliverdate.split('/')
		var newDelDt = dateArray[1] + "/" + dateArray[0] + "/" + dateArray[2];
		var delDt = new Date(newDelDt);
		var wkDay = delDt.getDay();
		if (wkDay == 6) // Saturday
		{
			deliveryOverlay.style.display = 'none';
		}
		else
		{
			deliveryOverlay.style.display = ''; 
		}
	}
	
	var PostalMessageOne = document.getElementById('PostalMessageOne');
	var PostalMessageTwo = document.getElementById('PostalMessageTwo');
	
	if (Deliverdate == '13/03/2010')// || (Deliverdate == '21/03/2009'))
	{
	PostalMessageTwo.style.display = 'none';
	PostalMessageOne.style.display = ''; 
	}
	else
	{
	PostalMessageTwo.style.display = '';
	PostalMessageOne.style.display = 'none'; 
	}	

	var numDays = document.getElementById('numDays').value;
	var currencySymbol = document.getElementById('currencySymbol').value;
	var partnerid = document.getElementById('partnerid').value;
	var productID = document.getElementById('productID').value;
	var deliveryinclusive = document.getElementById('deliveryinclusive').value;
	var serverName = document.getElementById('serverName').value;
	
	url = "http://" + serverName + "/delOption.asp?partnerid=" + partnerid  + "&deliverydate="+ Deliverdate + "&currencySymbol=" + currencySymbol + "&deliveryinclusive=" + deliveryinclusive + "&productID=" + productID + '&num1=' + Math.random() + '&num2=' + Math.random();
	
	var httpXml = GetXmlHttpObject(null);
	httpXml.open('GET', url, false);
	httpXml.send(null);
	
	var response = httpXml.responseText;
	
	element = document.getElementById('delOptionSection');
	element.innerHTML = response;
	}
}	
function GetXmlHttpObject(handler) 
{ 
	var isIE = (navigator.userAgent.indexOf('MSIE') >= 0) ? 1 : 0; 
	var isIE5 = (navigator.appVersion.indexOf("MSIE 5.5")!=-1) ? 1 : 0; 
	var isOpera = ((navigator.userAgent.indexOf("Opera6")!=-1)||(navigator.userAgent.indexOf("Opera/6")!=-1)) ? 1 : 0; 
	//netscape, safari, mozilla behave the same??? 
	var isNetscape = (navigator.userAgent.indexOf('Netscape') >= 0) ? 1 : 0; 
	var localXmlHttp = null;

	// Depending on the browser, try to create the xmlHttp object 
	if (isIE)
	{ 
		// The object to create depends on version of IE 
		// If it isn't ie5, then default to the Msxml2.XMLHTTP object 
		var strObjName = (isIE5) ? 'Microsoft.XMLHTTP' : 'Msxml2.XMLHTTP'; 
		 
		//Attempt to create the object 
		try
		{ 
			localXmlHttp = new ActiveXObject(strObjName); 

			if (handler != null)
			{
				localXmlHttp.onreadystatechange = handler; 
			}
		} 
		catch(e)
		{ 
			// Object creation error 
			alert('IE detected, but object could not be created. Verify that active scripting and activeX controls are enabled'); 
			return; 
		} 
	} 
	else if (isOpera)
	{ 
		// Opera has some issues with xmlHttp object functionality 
		//alert('Opera detected. The page may not behave as expected.'); 
		//return; 
		return Sarissa.getXmlHttpRequest();
	} 
	else
	{ 
		// Mozilla | Netscape | Safari 
		localXmlHttp = new XMLHttpRequest(); 

		if (handler != null)
		{
			localXmlHttp.onload = handler; 
			localXmlHttp.onerror = handler; 
		}
	} 
	 
	// Return the instantiated object 
	return localXmlHttp; 
}
