YUI({
	base: '/includes/js/yui_3.1.1/build/',
	combine: false,
	classNamePrefix: 'masi',

	modules: {

		'ma-validator': {
			fullpath: '/includes/js/ma/ma-validator/ma-validator.js',
			requires: ['widget', 'event-custom', 'dump', 'substitute', 'json-parse', 'anim']
		}

	}

}).use('passthrough', 'node', 'io', 'console', 'io-base', 'json', 'ma-validator', function(Y) {

	var init = function() {

		/*** Cart Listeners ****************************************************/

		// process checked listeners
		Y.on("click", selectAll, ".selectAll_btn");
		Y.on("click", deselectAll, ".deselectAll_btn");
		Y.on("click", downloadChecked, ".downloadChecked_btn");
		Y.on("click", emailChecked, ".emailChecked_btn");
		Y.on("click", removeChecked, ".removeChecked_btn");
		Y.on("click", orderChecked, ".orderChecked_btn");

		// process individual listeners
		Y.on("click", clickCheckbox, ".checkbox");
		Y.on("click", downloadIndividual, ".download_btn");
		Y.on("click", emailIndividual, ".email_btn");
		Y.on("click", removeIndividual, ".remove_btn");
		Y.on("click", orderIndividual, ".order_btn");

		//pop up continue / send buttons listeners
		Y.on("click", removeFormSubmit, "#continue_remove");
		Y.on("click", removeFormSubmitIndividual, "#continue_remove_individual");

		if (Y.one("#email_remove")) {
			Y.one("#email_remove").on("click", emailFormSubmit);
		}

		Y.on("click", emailFormSubmitIndividual, "#email_remove_individual");
		Y.on("click", orderFormSubmit, "#continue_order");
		Y.on("click", orderFormSubmitIndividual, "#continue_order_individual");

		Y.on("change", quantityChange, "#quantity_select");
		Y.on("click", quantityInputChange, '#update_price');

		//close popus listerners
		Y.on("click", removePopupClose, ".pop_over_remove_close");
		Y.on("click", emailPopupClose, ".pop_over_email_close");
		if (Y.one("#pop_over_email_cl")) {
			Y.one("#pop_over_email_cl").on("click", emailPopupClose);
		}

		/*** Shipping Listeners *************************************************/
		if (Y.one("#billing_shipping_form")) {

			Y.on("click", useBillingInfo, "#use_billing_info");
			Y.on("click", calculate, "#calculate");
			Y.on('change', billingCountryChange, '#billing_country');
			Y.on('change', shippingCountryChange, '#shipping_country');

			//input change listeners
			Y.on('change', calculateOnChange, '#billing_shipping_form select, #use_billing_info');
			Y.on('keydown', calculateOnChange, '#billing_shipping_form input');

			//popup
			Y.on('click', openInfoPopup, '.cart_summary_item');
			Y.on('click', closeInfoPopup, '#cart_display_more_info_close');
			Y.on('click', displayListUpload, '.list_icon');
			Y.on('click', closeListUpload, '#cart_display_list_upload_close');

			//form validation
			validate = new Y.maValidator({
				boundingBox: '#content_outer',
				contentBox: '#content_inner',
				formName: 'billing_shipping_form',
				emailName: 'billing_email',
				termsName: 'accept_terms',
				callback: validationCallback
			});
			validate.render();

			YAHOO.masi.calendar.setup_calendars();
		}

	}

	/*** Cart JS **************************************************************/

	///////////////////////
	// SELECT ALL BUTTON //
	var selectAll = function(e) {
		e.preventDefault();
		// list of selected ids
		var idList = Y.one("#checkedIdList");
		idList.set("value", "");
		Y.all("#cart_table .checkbox").each(function(node) {
			node.set("checked", "checked");
			curValue = idList.get("value");
			if (curValue == "") {
				newValue = node.getAttribute("baseId");
			} else {
				newValue = curValue + "," + node.getAttribute("baseId");
			}
			idList.set("value", newValue);
		});
	}

	/////////////////////////
	// DESELECT ALL BUTTON //
	var deselectAll = function(e) {
		e.preventDefault();
		// uncheck all boxes.
		Y.all("#cart_table .checkbox").each(function(node) {
			node.set("checked", false);
		});
		Y.one("#checkedIdList").set("value", "");
	}

	/////////////////////////////
	// DOWNLOAD CHECKED BUTTON //	
	var downloadChecked = function(e) {
		e.preventDefault();

		//setup vars
		var idList = Y.one("#checkedIdList").getAttribute('value'),
			requestURL = "/handler?func=process_export_cart";

		makeRequest(e, 'download', requestURL, "item_list=" + idList);

	}

	//////////////////////////
	// EMAIL CHECKED BUTTON //
	//TODO: if you click the form validation twice, it submits the form
	var emailChecked = function(e) {
		e.preventDefault();

		Y.one('.error').addClass('hidden');

		//switch the btns
		Y.one('#email_remove').removeClass('hidden');
		Y.one('#email_remove_individual').addClass('hidden');

		//show
		showEmailPopup();

		//cleanout old html
		Y.one('.pop_email_cart_summary').setContent('');

		//setup vars
		var idList = Y.one("#checkedIdList").getAttribute('value'),
			requestURL = "/handler?func=get_cart_image_info";

		//make ajax request
		makeRequest(e, 'populateEmail', requestURL, "item_list=" + idList);

	}

	var emailFormSubmit = function(e) {
		e.preventDefault();

		var emailInput = Y.one('.pop_column_1 input[type=text]'),
			idList = Y.one("#checkedIdList").getAttribute('value'),
			requestURL = "/handler?func=process_email_cart";
		emailFormWrap = Y.one('.pop_column_1 .pop_instruction');

		Y.one('.error').addClass('hidden');

		//validate emails input
		if (emailInput.get('value').indexOf("@") == -1) {
			Y.one('.error').removeClass('hidden');

		//no errors
		} else {

			//get form vals
			var recipients = emailInput.get('value');
			var comments = Y.one('.pop_column_1 textarea').get('value');

			var postStr = "item_list=" + idList + '&recipients=' + recipients + '&comments=' + comments;

			//make ajax request
			makeRequest(e, 'sendEmail', requestURL, postStr);
		}

	}

	///////////////////////////
	// REMOVE CHECKED BUTTON //
	var removeChecked = function(e) {
		e.preventDefault();

		//switch the continue btns
		Y.one('#continue_remove').removeClass('hidden');
		Y.one('#continue_remove_individual').addClass('hidden');
		Y.one('#continue_order').addClass('hidden');
		Y.one('#continue_order_individual').addClass('hidden');
		Y.all('.pop_remove').removeClass('hidden');
		Y.all('.pop_order').addClass('hidden');

		//show the popup
		showRemovePopup();

		//setup vars
		var contBtn = Y.all('#continue_remove'),
			removePopupUl = Y.one('#pop_over_confirm_remove ul'),
			html = '',
			showMess = false;

		Y.one('.pop_note').addClass('hidden');
		removePopupUl.setContent('');

		//loop over checkboxes, get names and append to popup
		Y.all('.checkbox').each(function(node) {

			if (node.get('checked')) {

				var name = node.get('parentNode').one('.cart_item_name').get("text");
				var checkId = node.getAttribute('baseId');

				if (checkId.indexOf("document") != -1) {
					html += '<li>' + name + '<span class="pop_star"> *</span></li>';
					showMess = true;
				} else {
					html += '<li>' + name + '</li>';
				}

			}

		});

		//show note about asterisk
		if (showMess) {
			Y.one('.pop_note').removeClass('hidden');
		}

		//append html
		removePopupUl.append(html)

	}

	//submits the form when continue is clicked
	var removeFormSubmit = function(e) {
		e.preventDefault();

		var requestURL = "/handler?func=remove_from_cart",
			idList = Y.one("#checkedIdList").getAttribute('value');

		makeRequest(e, 'remove', requestURL, "item_list=" + idList);
	}

	//////////////////////////
	// ORDER CHECKED BUTTON //
	var orderChecked = function(e) {
		e.preventDefault();

		//switch the continue btns
		Y.one('#continue_remove').addClass('hidden');
		Y.one('#continue_remove_individual').addClass('hidden');
		Y.one('#continue_order').removeClass('hidden');
		Y.one('#continue_order_individual').addClass('hidden');
		Y.all('.pop_remove').addClass('hidden');
		Y.all('.pop_order').removeClass('hidden');

		var idList = [],
			removePopupUl = Y.one('#pop_over_confirm_remove ul'),
			html = '',
			count = 0;

		//show the popup
		showRemovePopup();

		removePopupUl.setContent('');

		Y.one("#continue_order").removeClass('hidden');

		//loop over checkboxes, get names and append to popup   
		Y.all('.checkbox').each(function(node) {
			//needed for IE
			if (node.get('checked')) {
				var name = node.get('parentNode').one('.cart_item_name').get("text");
				//var price = node.get('parentNode').one('.price').get("text");
				//var price = node.siblings('.price').get("text");
				if (node.get('parentNode').one('.price')) {
					var price = parseFloat(node.get('parentNode').one('.price').get("text"));

					var checkId = node.getAttribute('baseId');

					if (parseInt(price) > 0) {
						idList.push(checkId);
						html += '<li>' + name + '</li>';
						count++;
					}
				}

			}

		});

		if (count == 0) {
			html += '<li>No items</li>';
			Y.one("#continue_order").addClass('hidden');
		}

		//add the id's to the hidden form elem
		Y.one('#checkedIdList').setAttribute('value', idList.toString());

		removePopupUl.append(html);

	}

	var orderFormSubmit = function(e) {
		e.preventDefault();

		var cart_items = Y.one('#cart_items'),
			func = Y.one('#func'),
			requestFunc = "display_billing_shipping";

		func.setAttribute('value', requestFunc);

		cart_items.submit();
	}

	///////////////////////////
	// INDIVIDUAL CHECKBOXES //
	var clickCheckbox = function(e) {

		var idList = Y.one("#checkedIdList");
		var curList = idList.get("value");
		var baseId = e.target.getAttribute("baseId");
		var newList = "";
		// selecting
		if (e.target.get("checked")) {
			if (curList == "") {
				newList = baseId;
			} else {
				newList = curList + "," + baseId;
			}
		} else {
			// deselecting
			var curListArray = curList.split(",");
			var baseIdPos = curListArray.indexOf(baseId);
			if (baseIdPos > -1) {
				curListArray.splice(baseIdPos, 1)
			}
			for (var i = 0; i < curListArray.length; i++) {
				if (i == 0) {
					newList = curListArray[i];
				} else {
					newList = newList + "," + curListArray[i];
				}
			}
		}
		// set hidden input value.
		idList.set("value", newList);
	}

	////////////////////////////////
	// DOWNLOAD INDIVIDUAL BUTTON //
	var downloadIndividual = function(e) {
		e.preventDefault();

		//setup vars
		var idList = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId'),
			requestURL = "/handler?func=process_export_cart";

		makeRequest(e, 'download', requestURL, "item_list=" + idList);
	}

	/////////////////////////////
	// EMAIL INDIVIDUAL BUTTON //
	var emailIndividual = function(e) {
		e.preventDefault();

		//switch btns
		Y.one('#email_remove').addClass('hidden');
		Y.one('#email_remove_individual').removeClass('hidden');

		//cleanout old html
		Y.one('.pop_email_cart_summary').setContent('');

		//show    
		showEmailPopup();

		//setup vars
		var idList = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId'),
			requestURL = "/handler?func=get_cart_image_info";

		//add badeId to div, so we can get it later
		Y.one('.pop_email_cart_summary').setAttribute('id', idList);

		//make ajax request
		makeRequest(e, 'populateEmail', requestURL, "item_list=" + idList);

	}

	var emailFormSubmitIndividual = function(e) {
		e.preventDefault();

		var emailInput = Y.one('.pop_column_1 input[type=text]'),
			idList = Y.one('.pop_email_cart_summary').getAttribute('id'),
			requestURL = "/handler?func=process_email_cart";
		emailFormWrap = Y.one('.pop_column_1 .pop_instruction');

		if (Y.one('.error')) {
			Y.one('.error').remove();
		}

		//validate emails input
		if (emailInput.get('value').indexOf("@") == -1) {
			var emailFormContent = emailFormWrap.get("innerHTML");
			emailFormWrap.setContent(emailFormContent + '<p class="error">Please enter and Email Address</p>');

			//no errors
		} else {

			//get form vals
			var recipients = emailInput.get('value');
			var comments = Y.one('.pop_column_1 textarea').get('value');

			var postStr = "item_list=" + idList + '&recipients=' + recipients + '&comments=' + comments;

			//make ajax request
			makeRequest(e, 'sendEmail', requestURL, postStr);
		}

	}

	//////////////////////////////
	// REMOVE INDIVIDUAL BUTTON //
	var removeIndividual = function(e) {
		e.preventDefault();

		//switch the continue btns
		Y.one('#continue_remove').addClass('hidden');
		Y.one('#continue_remove_individual').removeClass('hidden');
		Y.one('#continue_order').addClass('hidden');
		Y.one('#continue_order_individual').addClass('hidden');
		Y.all('.pop_remove').removeClass('hidden');
		Y.all('.pop_order').addClass('hidden');

		//show the popup
		showRemovePopup();

		//setup vars
		var contBtn = Y.all('#continue_remove'),
			removePopupUl = Y.one('#pop_over_confirm_remove ul'),
			html = '',
			showMess = false;

		Y.one('.pop_note').addClass('hidden');
		removePopupUl.setContent('');

		var baseId = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId');
		var name = this.get('parentNode').get('parentNode').one('.cart_item_name').get('text');

		if (baseId.indexOf("document") != -1) {
			html += '<li id="' + baseId + '">' + name + '<span class="pop_star"> *</span></li>';
			showMess = true;
		} else {
			html += '<li id="' + baseId + '">' + name + '</li>';
		}

		//show note about asterisk
		if (showMess) {
			Y.one('.pop_note').removeClass('hidden');
		}

		removePopupUl.append(html);

	}

	//submits the form when continue is clicked
	var removeFormSubmitIndividual = function(e) {
		e.preventDefault();

		var requestURL = "/handler?func=remove_from_cart",
			idList = Y.one('#pop_over_confirm_remove li').getAttribute('id');

		makeRequest(e, 'remove', requestURL, "item_list=" + idList);
	}

	/////////////////////////////
	// ORDER INDIVIDUAL BUTTON //
	var orderIndividual = function(e) {
		e.preventDefault();

		//switch the continue btns
		Y.one('#continue_remove').addClass('hidden');
		Y.one('#continue_remove_individual').addClass('hidden');
		Y.one('#continue_order').addClass('hidden');
		Y.one('#continue_order_individual').removeClass('hidden');
		Y.all('.pop_remove').addClass('hidden');
		Y.all('.pop_order').removeClass('hidden');

		var removePopupUl = Y.one('#pop_over_confirm_remove ul'),
			html = '';

		//show the popup
		showRemovePopup();

		removePopupUl.setContent('');

		var baseId = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId');
		var name = this.get('parentNode').get('parentNode').one('.cart_item_name').get('text');
		var price = this.get('parentNode').get('parentNode').one('.price').get('text');

		//strip all non ints or dots
		price = price.replace(/ /g, '').replace(/[^\d\.]/g, '');

		if (parseFloat(price) > 0) {
			Y.one("#checkedIdList").setAttribute('value', baseId);
			html += '<li>' + name + '</li>';
		}
		removePopupUl.append(html);
	}

	var orderFormSubmitIndividual = function(e) {
		e.preventDefault();

		var cart_items = Y.one('#cart_items'),
			func = Y.one('#func'),
			requestFunc = "display_billing_shipping";

		func.setAttribute('value', requestFunc);

		cart_items.submit();
	}

	////////////////////////////////////////////////////
	//  Close popups
	////////////////////////////////////////////////////
	var removePopupClose = function(e) {
		e.preventDefault();
		Y.one('#pop_over_confirm_remove').addClass('hidden');
		Y.one('#dimmed').addClass('hidden');
	}

	var emailPopupClose = function(e) {
		e.preventDefault();
		Y.one('#pop_over_email_checked').addClass('hidden');
		Y.one('#dimmed').addClass('hidden');
	}

	////////////////////////////////////////////////////
	// Change quantity and update price (select)
	////////////////////////////////////////////////////
	var quantityChange = function(e) {

		var baseId = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId'),
			quantity = 0,
			requestURL = '/handler?func=update_cost',
			postStr = '',
			price = this.siblings('.price');

		price.setContent('loading');

		this.get('options').each(function(node) {
			selected = node.get('selected');
			if (selected) {
				quantity = node.get('text');
			}
		});

		postStr = 'quantity=' + quantity + '&base_id=' + baseId;

		makeRequest(e, 'quantityChange', requestURL, postStr);
	}

	////////////////////////////////////////////////////
	// Change quantity and update price (input)
	////////////////////////////////////////////////////
	var quantityInputChange = function(e) {
		e.preventDefault();

		var baseId = this.get('parentNode').get('parentNode').one('.checkbox').getAttribute('baseId'),
			quantity = 0,
			requestURL = '/handler?func=update_cost',
			postStr = '',
			price = this.siblings('.price'),
			oldPrice = price.get('text');

		price.setContent('loading');

		//get quantity
		var q = this.siblings('#quantity_input').get('value').join();
		quantity = ((q - 0) == q && q.length > 0) ? q : false;

		if (!quantity) {
			alert('please enter a number');
			price.setContent(oldPrice);
			return false;
		}

		postStr = 'quantity=' + quantity + '&base_id=' + baseId;

		makeRequest(e, 'quantityChange', requestURL, postStr);
	}

	/*** Shipping JS **************************************************************/

	////////////////////////////////////////////////////
	//  Use Billing address for shipping
	////////////////////////////////////////////////////
	var useBillingInfo = function(e) {

		var shipCol = Y.one('#cart_shipping_info_column ul'),
			formElem = '#cart_shipping_info_column ul input, #cart_shipping_info_column ul select',
			id = null;

		var billCompanyName = Y.one('#billing_company_name'),
			billFirstName = Y.one('#billing_first_name'),
			billLastName = Y.one('#billing_last_name'),
			billAddress = Y.one('#billing_address'),
			billCity = Y.one('#billing_city'),
			billState = Y.one('.billing_state'),
			billZip = Y.one('#billing_zip'),
			billCountry = Y.one('#billing_country');

		var shipCompanyName = Y.one('#shipping_company_name'),
			shipFirstName = Y.one('#shipping_first_name'),
			shipLastName = Y.one('#shipping_last_name'),
			shipAddress = Y.one('#shipping_address'),
			shipCity = Y.one('#shipping_city'),
			shipState = Y.one('.shipping_state'),
			shipZip = Y.one('#shipping_zip'),
			shipCountry = Y.one('#shipping_country');

		this.toggleClass('not_checked');

		if (this.hasClass('not_checked')) {

			//disable
			Y.all(formElem).each(function(node) {
				node.set('disabled', '');
				if (node.get('tagName') != 'SELECT') {
					node.addClass('required');
				}
			});

			//add billing values to shipping
			shipCompanyName.set('value', billCompanyName.get('value'));
			shipFirstName.set('value', billFirstName.get('value'));
			shipLastName.set('value', billLastName.get('value'));
			shipAddress.set('value', billAddress.get('value'));
			shipCity.set('value', billCity.get('value'));
			shipZip.set('value', billZip.get('value'));
			shipCountry.set('value', billCountry.get('value'));

			//get the billing country selected id
			billCountry.get('options').each(function(node) {
				selected = node.get('selected');
				if (selected) {
					id = node.get('id');
				}
			});

			Y.all('.shipping_state').each(function(node) {
				node.addClass('hidden');
			});
			Y.one('.shipping_state_' + id).removeClass('hidden');

			//add new shipping state
			Y.one('.shipping_state_' + id).set('value', Y.one('.billing_state_' + id).get('value'));

			//checked
		} else {
			Y.all(formElem).each(function(node) {
				node.set('disabled', 'disabled');
				node.removeClass('required');
				node.siblings('label').setStyle('color', '');
			});

			//clear shipping inputs
			shipCompanyName.set('value', '');
			shipFirstName.set('value', '');
			shipLastName.set('value', '');
			shipAddress.set('value', '');
			shipCity.set('value', '');
			shipZip.set('value', '');
			shipCountry.set('value', '');
		}

	}

	////////////////////////////////////////////////////
	//  Calculate shipping
	////////////////////////////////////////////////////
	var calculate = function(e) {
		e.preventDefault();

		var requestURL = '/handler?func=get_cost';

		var billCompanyName = Y.one('#billing_company_name').get('value'),
			billFirstName = Y.one('#billing_first_name').get('value'),
			billLastName = Y.one('#billing_last_name').get('value'),
			billAddress = Y.one('#billing_address').get('value'),
			billCity = Y.one('#billing_city').get('value'),
			billZip = Y.one('#billing_zip').get('value'),
			billCountry = Y.one('#billing_country').get('value');

		var shipCheckbox = Y.one('#use_billing_info').get('checked'),
			shipCompanyName = Y.one('#shipping_company_name').get('value'),
			shipFirstName = Y.one('#shipping_first_name').get('value'),
			shipLastName = Y.one('#shipping_last_name').get('value'),
			shipAddress = Y.one('#shipping_address').get('value'),
			shipCity = Y.one('#shipping_city').get('value'),
			shipZip = Y.one('#shipping_zip').get('value'),
			shipCountry = Y.one('#shipping_country').get('value');

		var billState = Y.one('.billing_state_' + billCountry).get('value');
		shipState = Y.one('.shipping_state_' + shipCountry).get('value');

		var postStr = "billCompanyName=" + billCompanyName + '&customer_firstname=' + billFirstName + '&customer_lastname=' + billLastName + '&customer_address=' + billAddress + '&customer_city=' + billCity + '&customer_state=' + billState + '&customer_postal_code=' + billZip + '&customer_country=' + billCountry + '&use_billing_info=' + shipCheckbox + '&shipCompanyName=' + shipCompanyName + '&delivery_firstname=' + shipFirstName + '&delivery_lastname=' + shipLastName + '&delivery_address=' + shipAddress + '&delivery_city=' + shipCity + '&delivery_state=' + shipState + '&delivery_postal_code=' + shipZip + '&delivery_country=' + shipCountry;

		Y.one('#btnSubmit').addClass('has_changed');

		makeRequest(e, 'calcShip', requestURL, postStr);
	}

	////////////////////////////////////////////////////
	//  Billing Country Change
	////////////////////////////////////////////////////
	var billingCountryChange = function(e) {

		var selected = null,
			id = null;

		this.get('options').each(function(node) {
			selected = node.get('selected');
			if (selected) {
				id = node.get('id');
			}
		});

		if (id) {
			Y.all('.billing_state').each(function(node) {
				node.addClass('hidden');
			});
			Y.one('.billing_state_' + id).removeClass('hidden');
		}

	}

	////////////////////////////////////////////////////
	//  Shipping Country Change
	////////////////////////////////////////////////////
	var shippingCountryChange = function(e) {

		var selected = null,
			id = null;

		this.get('options').each(function(node) {
			selected = node.get('selected');
			if (selected) {
				id = node.get('id');
			}
		});

		if (id) {
			Y.all('.shipping_state').each(function(node) {
				node.addClass('hidden');
			});
			Y.one('.shipping_state_' + id).removeClass('hidden');
		}

	}

	////////////////////////////////////////////////////
	//  Calculate Button Change
	////////////////////////////////////////////////////
	var calculateOnChange = function(e) {
		Y.log('calculateOnChange');

		//checks to see if the user has changed a field since clicking calculate
		if (Y.one('#btnSubmit').hasClass('has_changed')) {

			Y.one('#shipping_recalculate').setStyle('color', '#ff0000').removeClass('hidden');
			Y.one('#cart_totals').setStyle('borderColor', '#ff0000');
			Y.one('#btnSubmit_disabled').removeClass('hidden');
			Y.one('#btnSubmit').addClass('hidden');
		}

	}

	////////////////////////////////////////////////////
	//  Open List Upload Popup
	////////////////////////////////////////////////////
	var displayListUpload = function(e) {
		e.preventDefault();

		alert('clicked');
		Y.log('nate');

		var popup = Y.one('#cart_display_list_upload');
		var width = popup.getComputedStyle("width");
		var height = 500;
		var winHeight = popup.get('winHeight');

		//align box
		popup.setStyle('top', winHeight / 2);
		popup.setStyle('marginLeft', -parseInt(width) / 2);
		popup.setStyle('marginTop', -height / 2);

		//show box
		Y.one('#dimmed').removeClass('hidden');
		popup.removeClass('hidden');
	}

	////////////////////////////////////////////////////
	//  Close List Upload Popup
	////////////////////////////////////////////////////
	var closeListUpload = function(e) {
		e.preventDefault();
		Y.one('#cart_display_list_upload').addClass('hidden');
		Y.one('#dimmed').addClass('hidden');
	}

	////////////////////////////////////////////////////
	//  Open Information Popup
	////////////////////////////////////////////////////
	var openInfoPopup = function(e) {
		e.preventDefault();

		//setup vars
		var infoPopup = Y.one('#cart_display_more_info');
		var width = infoPopup.getComputedStyle("width");
		var height = 500;
		var winHeight = infoPopup.get('winHeight');

		//align box
		infoPopup.setStyle('top', winHeight / 2);
		infoPopup.setStyle('marginLeft', -parseInt(width) / 2);
		infoPopup.setStyle('marginTop', -height / 2);

		//populate box
		//setup vars
		var id = this.get('parentNode').get('id'),
			requestURL = "/handler?func=get_cart_image_info";

		//make ajax request
		makeRequest(e, 'populateInfo', requestURL, "item_list=" + id);

		//show box
		Y.one('#dimmed').removeClass('hidden');
		infoPopup.removeClass('hidden');

		window.location = "#header_stripe";
	}

	////////////////////////////////////////////////////
	//  Close Information Popup
	////////////////////////////////////////////////////
	var closeInfoPopup = function(e) {
		e.preventDefault();
		Y.one('#cart_display_more_info').addClass('hidden');
		Y.one('#dimmed').addClass('hidden');
	}

	////////////////////////////////////////////////////
	//  Validation Widget Callback
	////////////////////////////////////////////////////
	var validationCallback = function(e) {
		if (parseInt(Y.one('#cart_total').get('text')) == 0) {
			Y.one('#shipping_recalculate').setStyle('color', '#ff0000').removeClass('hidden');
			Y.one('#cart_totals').setStyle('borderColor', '#ff0000');
		} else {
			if (Y.one('#btnSubmit_disabled').hasClass('hidden')) {
				var func = '';
				//
				Y.one('#func').setAttribute('value', func);
				Y.one('#billing_shipping_form').submit();
			}
		}
	}

	////////////////////////////////////////////////////
	//  START - Server Request
	////////////////////////////////////////////////////
	//Get a reference to the Node that we are using
	//to report results:
	//var div = Y.Node.get('#processingRequest ul');
	//A function handler to use for successful requests:
	// arguments: contains 'event': the eventObj and 'panel': the panel the event was called from.
	var handleSuccess = function(ioId, o, arguments) {
		Y.log("The success handler was called.  Id: " + ioId + ".", "info", "example");

		if (o.responseText != undefined) {

			////////////////////////////////
			// Cart
			if (arguments.type == 'remove') {
				window.location.reload();
			}
			if (arguments.type == 'download') {
				window.location = o.responseText;
			}
			if (arguments.type == 'populateEmail') {
				var data = Y.JSON.parse(o.responseText);
				var html = '';

				if (data.length > 0) {

					//append html image previews
					for (var i = 0; i < data.length; i++) {
						html += '<div class="pop_email_cart_summary_item">' + '<div class="pop_email_cart_summary_item_thumb">' + '<img class="thumb" src="' + data[i]['prev'] + '" height="" width="" />' + '</div> <!-- end .pop_email_cart_summary_item_thumb -->' + '<p><b>' + data[i]['name'] + '</b><br />' + data[i]['type'] + '</p>' + '</div> <!-- end .pop_email_cart_summary_item -->';
					}

					//no items were checked
				} else {
					html += '<h2>No items were checked.</h2>';
				}

				Y.one('.pop_email_cart_summary').append(html);
			}
			if (arguments.type == 'sendEmail') {
				Y.one('#dimmed').addClass('hidden');
				Y.one('#pop_over_email_checked').addClass('hidden');
			}

			if (arguments.type == 'quantityChange') {
				Y.all('.price').each(function(node) {
					if (node.get('text') == 'loading') {
						node.setContent(o.responseText);
					}
				},
				this);
			}

			////////////////////////////////////
			// Shipping
			if (arguments.type == 'calcShip') {
				var data = Y.JSON.parse(o.responseText);

				var cartSubtotal = Y.one('#cart_subtotal'),
					cartShipping = Y.one('#cart_shipping'),
					cartTax = Y.one('#cart_tax'),
					cartTotal = Y.one('#cart_total');

				Y.one('#shipping_recalculate').setStyle('color', '').addClass('hidden');
				Y.one('#cart_totals').setStyle('borderColor', '');
				Y.one('#btnSubmit_disabled').addClass('hidden');
				Y.one('#btnSubmit').removeClass('hidden');

				//add new shipping, tax and total
				cartSubtotal.setContent(data.sub_total);
				cartShipping.setContent(data.shipping);
				cartTax.setContent(data.tax);
				cartTotal.set('text', data.total);

			}

			if (arguments.type == 'populateInfo') {
				var data = Y.JSON.parse(o.responseText),
					box = Y.one('#cart_display_more_info_info'),
					img = Y.one('#cart_display_more_info_thumb');
				html = '';

				data = data[0];

				//box info
				html += '<ul>';
				if (data.name) {
					html += '<li><b>Name:</b>' + data.name + '</li>';
				}
				if (data.paper) {
					html += '<li><b>Paper Size:</b> ' + data.paper + '</li>';
				}
				if (data.quantity) {
					html += '<li><b>Quantity:</b> ' + data.quantity + '</li>';
				}
				if (data.type) {
					html += '<li><b>Type:</b> ' + data.type + '</li>';
				}
				if (data.price) {
					html += '<li><b>Price:</b> ' + data.price + '</li>';
				}
				if (data.rate) {
					html += '<li><a href=""><b>Rate Card</b></a></li>';
				}
				if (data.comments) {
					html += '<li><b>Comments:</b> <textarea name="more_info_comments" readonly="readonly">' + data.comments + '</textarea></li>';
				}
				html += '</ul>';
				box.setContent(html);

				//box image
				if (data.thumb) {
					img.setContent('<img src="' + data.thumb + '" alt="' + data.name + '" width="" height="" />');
				}
			}

			// panel object
			/*
  			var panel = arguments.panel;
  			Y.one('#template-preview img').set('src', previewPath);
  	    panel.addClass("hide");
  	    hideStepProcessingAnimation(panel);
  	    */

		}
	}

	///////////////////////////////////////////////////
	//A function handler to use for failed requests: //
	var handleFailure = function(ioId, o) {
		Y.log("The failure handler was called.  Id: " + ioId + ".", "info", "example");

		if (o.responseText !== undefined) {
			var s = "<li>Transaction id: " + ioId + "</li>";
			s += "<li>HTTP status: " + o.status + "</li>";
			s += "<li>Status code message: " + o.statusText + "</li>";
			Y.log(s);
		}
	}

	/////////////////////////////////////////////////////////
	//Subscribe our handlers to IO's global custom events: //
	Y.on('io:success', handleSuccess);
	Y.on('io:failure', handleFailure);

	////////////////////////////////////////////////////////////////
	//Handler to make our XHR request when the button is clicked: //
	function makeRequest(event, type, requestURL, funcStr) {
		// pass event and panel so response functions can control.
		// Configuration object for POST transaction 
		var cfg = {
			method: "POST",
			data: funcStr,
			headers: {
				'X-Transaction': 'POST Example'
			},
			arguments: {
				event: event,
				type: type //str that identifies the caller function
			}
		};

		//The URL of the resource to which we're POSTing data:
		var sUrl = requestURL;

		//div.set("innerHTML", "Loading data from new request...");
		var request = Y.io(sUrl, cfg);
		Y.log("Initiating request; Id: " + request.id + ".", "info", "example");

	}

	// Step Processing Animation - during Surver Request 
	function showStepProcessingAnimation(panel) {
		if (panel.one('.processingDisplayText')) {
			panel.one('.processingDisplayText').setStyle('display', 'block');
		}
	}


	function hideStepProcessingAnimation(panel) {
		if (panel.one('.processingDisplayText')) {
			panel.one('.processingDisplayText').setStyle('display', 'none');
		}
	}

	////////////////////////////////////////////////////
	//  END - Server Request
	////////////////////////////////////////////////////

	////////////////////////////////////////////////////
	//  Helper: Show Email Popup
	////////////////////////////////////////////////////
	function showEmailPopup() {

		var emailPopup = Y.one('#pop_over_email_checked');
		var width = emailPopup.getComputedStyle("width");
		var height = 500; //max height of the popup      
		var winHeight = emailPopup.get('winHeight');

		emailPopup.setStyle('top', winHeight / 2);
		emailPopup.setStyle('marginLeft', -parseInt(width) / 2);
		emailPopup.setStyle('marginTop', -height / 2);

		Y.one('#dimmed').removeClass('hidden');
		emailPopup.removeClass('hidden');

		window.location = "#header_stripe";

	}



	function showRemovePopup() {

		var removePopup = Y.one('#pop_over_confirm_remove');
		var width = removePopup.getComputedStyle("width");
		var height = 500;
		var winHeight = removePopup.get('winHeight');

		removePopup.setStyle('top', winHeight / 2);
		removePopup.setStyle('marginLeft', -parseInt(width) / 2);
		removePopup.setStyle('marginTop', -height / 2);

		Y.one('#dimmed').removeClass('hidden');
		removePopup.removeClass('hidden');

		window.location = "#header_stripe";

	}

	/*
	///////////////////////////
	// EMAIL SELECTED FILES  //
	var show_email_section = function(e) {
    var myClass = e.target.get("class");
    if (myClass == "emailChecked_btn") {
      var idList = Y.one("#checkedIdList");
      Y.one("emailIdList").set("value", idList);
      
    } 
	}
	
*/
	//Fires when all elements are loaded
	Y.on("domready", init);

});

