// Script: tjbFrontAnimation.js
// Description: Provides front-end animation to website
// Author: Tom Belknap
// Date: December 4th, 2009
// Version: 0.1

// Revision History:
// 0.1: initial release. Provides slide-in/out animation
//      to left side navigation, exclusively. 09-12-04

var cartUrl = 'http://192.168.0.75:3620/wholelattelove_beta/Includes/minicart_ajax.cfm';

/*
// tjbLoadNavButtons:Assigns events to each of the navigation buttons in the LHN.
*/
function tjbLoadNavButtons() {
	// All left-hand navigation H2s are assigned the same class:
	var buttons = document.getElementsByClassName('navigation');
	if(typeof(buttons) != 'undefined') {
		for(x = 0; x < buttons.length; x++) {
			addEvent(buttons[x], 'click', function() { tjbBlinds(this); });
		}
	}
}


/*
// tjbBlinds:Animates the LHN on click.
*/
function tjbBlinds(elm) {
	while (elm.className != 'navElem') {
		elm = elm.nextSibling;
	}
	if(elm.style.display == 'none') {
		Effect.BlindDown(elm.id, { duration: .3 });

	} else {
		Effect.BlindUp(elm.id, { duration: .3 });
	}
}


/*
// miniCart_reveal:Animates the mini-cart on click.
*/
function miniCart_reveal() {
	var cart = $('minicart');
	if(cart.style.display == 'none') {
		new Ajax.Updater(cart.id, cartUrl, { method: 'get',
											 parameters: { 
						 								 method: 'refresh',
														 cartid: '700318'
														 },
											 onSuccess: Effect.BlindDown(cart.id, { duration: .3, direction: 'top-right', transition: Effect.Transitions.sinoidal }),
											 onFailure: function() { alert('Whoops!') } });
		
	} else {
		Effect.BlindUp(cart.id, { duration: .3, direction: 'top-right', transition: Effect.Transitions.sinoidal });
	}
}


/*
// miniCart_reveal:Animates the mini-cart on click.
*/
function miniCart_qty(elm) {
	var cart = $('minicart');
	var q = elm.value;
	var itemid = elm.id.split('_')[1];
	new Ajax.Updater(cart.id, cartUrl, { method: 'get',
					 					 parameters: { 
					 								 method: 'qty',
													 cartitem: itemid,
													 qty: q
													 } });
}


/*
// miniCart_reveal:Animates the mini-cart on click.
*/
function miniCart_delete(elm) {
	var cart = $('minicart');
	var itemid = elm.id.split('_')[1];
	new Ajax.Updater(cart.id, cartUrl, { method: 'get',
					 					 parameters: { 
					 								 method: 'delete',
													 cartitem: itemid
													 } });
}


/*
// miniCart_reveal:Animates the mini-cart on click.
*/
function miniCart_addToCart(elm) {
	var cart	= $('minicart');
	var form	= document.forms['cpform'];
	var sku		= form.purchaseID.options[form.purchaseID.selectedIndex].value;
	new Ajax.Updater(cart.id, cartUrl, { method: 'get',
					 					 parameters: { 
					 								 method: 'addToCart',
													 sku: sku
													 },
										 onComplete: function() {
										 						//$('cartbutton').innerHTML = 'Items added!!';
																//Effect.Highlight('cartbutton', { startcolor: '#FFFF00', endcolor: '#FF0000'});
																alert("you are here.");
																}});
}



// Helper functions:
// addEvent: allows for unobtrusive JS.  Can be called to insert functions onto page where required.
if (typeof(addEvent) != 'function') {
	function addEvent(elm, evType, fn, useCapture) {
	    elm["on"+evType]=fn;return;
	}
}

// getElementsByClassName: extends and unifies this poorly-implemented function
document.getElementsByClassName = function(clsName){
	var retVal = new Array();
	var elements = document.getElementsByTagName("*");
	for(var i = 0;i < elements.length;i++){
		if(elements[i].className.indexOf(" ") >= 0){
			var classes = elements[i].className.split(" ");
			for(var j = 0;j < classes.length;j++){
				if(classes[j] == clsName)
					retVal.push(elements[i]);
			}
		}
		else if(elements[i].className == clsName)
			retVal.push(elements[i]);
	}
	return retVal;
}

addEvent(window, "load", tjbLoadNavButtons);
