window.addEvent('domready', function(){
// Tool Tips
// var Tips2 = new Tips($$('.Tips2'), {
//	initialize:function(){
//		this.fx = new Fx.Style(this.toolTip, 'opacity', {duration: 500, wait: false}).set(0);
//	},
//	onShow: function(toolTip) {
//		this.fx.start(1);
//	},
//	onHide: function(toolTip) {
//		this.fx.start(0);
//	}
//});


// Navigation Scripting
var szNormal = 134, szSmall  = 134, szFull   = 219;
 
var kwicks = $$("#kwicks .kwick");
var fx = new Fx.Elements(kwicks, {wait: false, duration: 200, transition: Fx.Transitions.Back.easeOut});
kwicks.each(function(kwick, i) {
	kwick.addEvent("mouseenter", function(event) {
		var o = {};
		o[i] = {width: [kwick.getStyle("width").toInt(), szFull]}
		kwicks.each(function(other, j) {
			if(i != j) {
				var w = other.getStyle("width").toInt();
				if(w != szSmall) o[j] = {width: [w, szSmall]};
			}
		});
		fx.start(o);
	});
});
 
$("kwicks").addEvent("mouseleave", function(event) {
	var o = {};
	kwicks.each(function(kwick, i) {
		o[i] = {width: [kwick.getStyle("width").toInt(), szNormal]}
	});
	fx.start(o);
});


// Accordion
	var accordion = new Accordion('h3.atStart', 'div.atStart', {
	opacity: false,
	onActive: function(toggler, element){
		toggler.setStyle('color', '#222');
		element.setStyle('overflow', 'auto');
	},
 
	onBackground: function(toggler, element){
		toggler.setStyle('color', '#222');
		element.setStyle('overflow', 'auto');
	},

	
onComplete: function(){
		var el = $(this.elements[this.previous]);
		if (el.offsetHeight > 0) el.setStyle('height', 'auto');  
		},
	onActive: function(toggle,content){                          
		toggle.addClass('active'); content.addClass('active'); 
	},
	onBackground: function(toggle,content){ 
		content.setStyle('height', content['offsetHeight']);  /* turn off auto */
		toggle.removeClass('active'); content.removeClass('active');
	}
	

	
}, $('accordion'));


	var accordion2 = new Accordion('h3.atStart2', 'div.atStart2', {
	start:'all-closed',
	alwaysHide: true,
	opacity: false,
	onActive: function(toggler, element){
		toggler.setStyle('color', '#222');
		element.setStyle('overflow', 'auto');
	},
 
	onBackground: function(toggler, element){
		toggler.setStyle('color', '#222');
		element.setStyle('overflow', 'auto');
	},

	
onComplete: function(){
		var el = $(this.elements[this.previous]);
		if (el.offsetHeight > 0) el.setStyle('height', 'auto');  
		},
	onActive: function(toggle,content){                          
		toggle.addClass('active'); content.addClass('active'); 
	},
	onBackground: function(toggle,content){ 
		content.setStyle('height', content['offsetHeight']);  /* turn off auto */
		toggle.removeClass('active'); content.removeClass('active');
	}
	

	
}, $('accordion'));

	step1open = function() {
	accordion.display(0); // 0 will open the 1st panel,1 will open the 2nd and so on...
	};

	$('step2open').addEvent('click', function() {
	accordion.display(1); // 0 will open the 1st panel,1 will open the 2nd and so on...
	});

	$('step3open').addEvent('click', function() {
	accordion.display(2); // 0 will open the 1st panel,1 will open the 2nd and so on...
	});	
	
	$('step4open').addEvent('click', function() {
	accordion.display(3); // 0 will open the 1st panel,1 will open the 2nd and so on...
	});	
	 
// End Accordion

// Fruit Addition

runningtotal = function() {
	var runningurl = "/includes/runningtotal.php";
	var RunningAjax = new Ajax(runningurl, {method: 'get', update: $('ajaxDiv')}).request();	
};


// Running Total
runningtotal = function() {
	var totalurl = "/includes/runningtotal.php";
	var TotalAjax = new Ajax(totalurl, {method: 'get', update: $('ajaxDiv')}).request();
	};
	
	
 // Size Selector	
getsize = function() {
$('ajaxDiv3').setStyle('display', 'none');
var counterurl = "/includes/counter.php?dir=reset";
var priceurl = "/includes/runningtotal.php?dir=reset";
var ResetAjax = new Ajax(counterurl, {method: 'get', update: $('ajaxDiv3')}).request();	
var PriceReset = new Ajax(priceurl, {method: 'get', update: $('ajaxDiv')}).request();
document.getElementById('sizeStore').innerHTML = "0";
chosen = ""
len = document.sizes.size.length
	
for (i = 0; i <len; i++) {
	if (document.sizes.size[i].checked) {
		chosen = document.sizes.size[i].value
		}
	}
var sizeurl = "/includes/size.php?size="+chosen;
var SizeAjax = new Ajax(sizeurl, {method: 'get', update: $('sizeStore'), onComplete: function() {maxfruit = document.getElementById('sizeStore').innerHTML;}}).request();
	$$('.cartitems').each(function(el){
	el.remove();
	});
	};


// Create Size Dialogue

checksize = function() {
	var amount = $('sizeStore').innerHTML;
	if (amount == "") {
		$('checkSize').innerHTML = "<p>Please Select a size <a href='#1' onclick='step1open()'>up above.</a></p>";
		}
		else {
			if (amount == 4) {
				var size = "SMALL";
				}
				
			else if (amount == 6) {
				var size = "MEDIUM";
				}
			
			else if (amount == 10) {
				var size = "LARGE";
				}
			$('checkSize').innerHTML = "You have selected the <span style='font-size: 15px; font-weight:bold;'>" + size + "</span> smoothie size! Please select <span style='font-size: 15px; font-weight:bold;'>" + amount + "</span> fruits from the list!";
			}
			};


// Cart Drag 'n Drop 

var drop = $('cart');
var dropFx = drop.effect('background-color', {wait: false}); // wait is needed so that to toggle the effect,
var countnum = new Number();
var maxfruit = document.getElementById('sizeStore').innerHTML;

$$('.item').each(function(item){
 
	item.addEvent('mousedown', function(e) {
		e = new Event(e).stop();
		if (document.getElementById('ajaxDiv3').innerHTML == "") {
			countnum = 0;
		} else {
			countnum = parseInt(document.getElementById('ajaxDiv3').innerHTML);
		}
		if (countnum <= (maxfruit-1)) {
			var clone = this.clone()
				.setStyles(this.getCoordinates()) // this returns an object with left/top/bottom/right, so its perfect
				.setStyles({'opacity': 0.7, 'position': 'absolute'})
				.addEvent('emptydrop', function() {
					this.remove();
					drop.removeEvents();
				}).inject(document.body);
		}
		else {
			$('ajaxDiv3').setStyle('display', 'block');
			document.getElementById('ajaxDiv3').innerHTML = "Warning: Too much fruit!";
			clone.remove();
		}
		
		drop.addEvents({
			
			'drop': function() {
				drop.removeEvents();
				clone.remove();
				item.clone().inject(drop).removeClass('item').addClass('cartitems').addEvent('click', function(){
						/* var cloneremove = item.id;
						var clonefx = new Fx.Styles(cloneremove, {
						duration: 1500,
							wait: false,
							transition: Fx.Transitions.Quad.easeOut
						});
						clonefx.start({
							'opacity': [1, 0],
						}) */
				this.remove();
					var id = item.id;
					var url = "/includes/cart.php?dir=down&name="+id;
					var MyAjax = new Ajax(url, {method: 'get', update: $('ajaxDiv2'),
						onComplete: function() {
						$('ajaxDiv3').setStyle('display', 'none');
						runningtotal();
						var final = $('ajaxDiv');
						var fx = new Fx.Styles(final, {
						duration: 800,
							wait: false,
							transition: Fx.Transitions.Quad.easeOut
						});
						fx.start({
							'background-color': ['#A7C83F', '#fff'],
						})					
						}
					}).request();
					
										
					var dropurl = "/includes/counter.php?dir=down";
					var DropAjax = new Ajax(dropurl, {method: 'get', update: $('ajaxDiv3'),
						onComplete: function() {	
						}
					}).request();
				});				
				
				dropFx.start('FAAF4C').chain(dropFx.start.pass('A7C83F', dropFx));
					var id = item.id;
					var url = "/includes/cart.php?dir=up&name="+id;
					var MyAjax = new Ajax(url, {method: 'get', update: $('ajaxDiv2'),
						onComplete: function() {	
						runningtotal();
						}
					}).request();
					
					var dropurl = "/includes/counter.php?dir=up";
					var DropAjax = new Ajax(dropurl, {method: 'get', update: $('ajaxDiv3')}).request();
				},
				
			'over': function() {
				dropFx.start('D85A77');
			},
			
			'leave': function() {
				dropFx.start('A7C83F');

			}
			
		});
		var drag = clone.makeDraggable({
			droppables: [drop]
		}); // this returns the dragged element
		drag.start(e); // start the event manual
				});


// Trail Mix Function

gettrailmix = function() {
     var amount = document.trialMix.tmixamount.value;
     var mixurl = "/includes/trialmix.php?mixamount="+amount;
     var MixAjax = new Ajax(mixurl, {method: 'get', update: $('mixStore'), onComplete: function() {     
		runningtotal();		
     }}).request();

	};



// Dry Fruit
getdryfruit = function() {
     var amount = document.trialMix.dryfruitamount.value;
     var dryurl = "/includes/dryfruit.php?amount="+amount;
     var MixAjax = new Ajax(dryurl, {method: 'get', update: $('mixStore'), onComplete: function() {     
		runningtotal();		
     }}).request();

	};


// Get Juice
getjuice = function() {
	var juice = document.trialMix.juicechosen.value;
	var juiceurl = "/includes/juice.php?juice="+juice;
	var JuiceAjax = new Ajax(juiceurl, {method: 'get', update: $('juiceStore'), onComplete: function() {
	runningtotal();
	}
	}).request();

};


// Number Pad Limiter
isNumberKey = function(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57))
		return false;
	return true;
	};

finalize = function() {
	var finaltotal = $('ajaxDiv').innerHTML;
	$('content').innerHTML = "<p id='final'>Congratulations, you have successully concoted a SCIENTIFICALLY DELICIOUS fruit smoothie. Please prepare " + finaltotal + " dollars and visit your nearest FruitLabs(tm) location. Thank you for buying!<br />(Click <a href='/mix.php'>here</a> to start again!)</p>"
	};
	


});

});

