$(function()
{
	$.fn.itemDialog = function(){}

	$.fn.itemDialog._style = function(t)
	{
		switch( t )
		{
			case 'Add to Cart'		:	$('#dialog').css(
													{
														background: '#fbc587',	color: '#574d56'
													});
			case 'Add to Lightbox'	:	$('#dialog').css(
													{

													});
			case 'Confirm'			:	$('#dialog').css(
													{

													});
		}
	}

	$.fn.itemDialog.defaults =
	{
		pricingToolDataSrc	:		'ajax/pricing_tool.php'
	};

	$.fn.itemDialog.show = function(t, c, opt)
	{
		if( !$('#dialog').length)
		{
			$('body')
				.append('<div id="dialog"></div>');

			$('#dialog')
				.css({display: 'none'});
		}

		// add style to the dialog
		$.fn.itemDialog._style(t);

		if( t == 'View Article Image')
		{
			$("#dialog")
			.html('<img src="' + $.fn.itemActions.options.loadingImage.src + '"/> loading please wait...')
			.load('ajax/item-article.php?'+ Math.random(), 'item_id='+ c[0] +'&item_url='+ escape(c[1]) +'&snt=true')
			.dialog({
				bgiframe: false,
				title: t,
				width: 540,
				height: 615,
				minHeight: 615,
				minWidth: 540,
				maxHeight: 615,
				maxWidth: 540,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						//$.fn.itemActions.action.enable();
					}
				},
				close: function()
				{
					//$.fn.itemActions.action.enable();
					$('.ui-dialog').remove();
				}
			});
		}
		else if( t == 'Preview Animation')
		{
			$("#dialog")
			.html('<img src="' + $.fn.itemActions.options.loadingImage.src + '"/> loading please wait...')
			.load('ajax/animation_preview.php?snt=true&id='+ c)
			.dialog({
				bgiframe: false,
				title: t,
				width: 540,
				height: 615,
				minHeight: 615,
				minWidth: 540,
				maxHeight: 615,
				maxWidth: 540,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						//$.fn.itemActions.action.enable();
					}
				},
				close: function()
				{
					//$.fn.itemActions.action.enable();
					$('.ui-dialog').remove();
				}
			});
		}
		else if( t == 'Add to Cart' )
		{
			var item_type = $('.item-type').html().split('-');
			$("#dialog")
			.html('<img src="' + $.fn.itemActions.options.loadingImage.src + '"/> loading please wait...')
			.load('ajax/cart.php', 'item_type=' + item_type[(item_type.length-1)])
			.dialog({
				bgiframe: false,
				title: t,
				width: 570,
				height: 350,
				minHeight: 350,
				minWidth: 570,
				maxHeight: 450,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					'Add to Cart': function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					},
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						$.fn.itemActions.action.enable();
					}
				},
				close: function()
				{
					$.fn.itemActions.action.enable();
					$('.ui-dialog').remove();
				}
			});
		}
		else if( t == 'Get Price' )
		{
			var itemtype = $('#get-price').html();
			var itemtitle = $('#item-title').html();
			$("#dialog")
			.html('<img src="' + $.fn.itemActions.defaults.loadingImage.src + '"/> loading please wait...');

			function handleData(data, status)
			{
				var ar = data.split('||')
				$.fn.itemActions.options.priceObject = eval(ar[0]);

				$("#dialog")
				.html(ar[1]);

				$('.get-price-section-list')
				.find('select[rel=formats]')
				.bind('change', function()
				{
					$.fn.itemActionFunctions.getPrice();
				});

				$('.get-price')
				.bind('click', function()
				{
					$.fn.itemActionFunctions.getPrice();
				});

			}

			function handleError(XHR, error, except)
			{
				if( error == 'Timeout' )
				{
					alert("Your request has timed out. Please try again.");
				}
				else
				{
					alert(error + ' : ' + except);
				}
			}

			function handleComplete(XHR, status)
			{
				//alert('Completed: ' + status);
			}

			function handleFilter(data, type)
			{
				return data;
			}

			$.ajax(
			{
				type		:	'GET',
				url			:	$.fn.itemActions.options.getpriceDataSrc,
				dataType	: 	'html',
				data		:	({item_type: itemtype, item_title: itemtitle}),
				success		:	handleData,
				cache		:	true,
				async		:	true,
				error		:	handleError,
				complete	:	handleComplete,
				dataFilter	:	handleFilter,
				timeout		:	20000
			});

			//.load('ajax/get_price.php', 'item_type=' + item_type + '&item_title=' + item_title)

			var form = $('<form></form>');
			var hidden = $('<input name=sent" type="hidden" value="true"></input>');

			form
			.html(hidden)
			.css
			({
				disdplay: 'none'
			})
			.attr({
				method: 'post',
				target: '_self'
			});

			$('body').append(form);

			var obj = $('a[rel=cart]').find('span.action-text');
			if( /Remove from cart/.test($(obj).html()) )
			{
				$("#dialog")
				.dialog({
					bgiframe: false,
					title: t,
					width: 670,
					height: 490,
					minHeight: 490,
					minWidth: 670,
					maxHeight: 600,
					maxWidth: 475,
					zIndex: 10001,
					modal: true,
					resizable: false,
					draggable: false,
					closeOnEscape: true,
					buttons:
					{
						'Remove from Cart': function()
						{
							form.attr('action', remove_from_cart_url).submit();
							$('a[rel=cart]').trigger('click');
							$('.ui-dialog').remove();
						},

						Cancel: function()
						{
							$(this).dialog('close');
							$('.ui-dialog').remove();
							$.fn.itemActions.action.enable();
						}

					},
					close: function()
					{
						$.fn.itemActions.action.enable();
						$('.ui-dialog').remove();
					}
				});
			}
			else
			{
				$("#dialog")
				.dialog({
					bgiframe: false,
					title: t,
					width: 670,
					height: 490,
					minHeight: 490,
					minWidth: 670,
					maxHeight: 600,
					maxWidth: 475,
					zIndex: 10001,
					modal: true,
					resizable: false,
					draggable: false,
					closeOnEscape: true,
					buttons:
					{
						'Add to Cart': function()
						{
							$(this).dialog('close');
							$('.ui-dialog').remove();
							$.fn.itemActions.action.enable();
							$('#pricing_btn').trigger('click');
						},

						Cancel: function()
						{
							$(this).dialog('close');
							$('.ui-dialog').remove();
							$.fn.itemActions.action.enable();
						}

					},
					close: function()
					{
						$.fn.itemActions.action.enable();
						$('.ui-dialog').remove();
					}
				});
			}
		}
		else if( t == 'Add to Lightbox' )
		{
			$("#dialog")
			.html(c)
			.dialog({
				bgiframe: true,
				title: t,
				width: 502,
				height: 250,
				minHeight: 250,
				minWidth: 502,
				maxHeight: 550,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					'Add to Lightbox': function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					},
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					}
				},
				close: function()
				{
					//action.enable(o);
					$('.ui-dialog').remove();
				}
			});
		}
		else if( t == 'Pricing Tool' )
		{
			var object = null;
			var form = $('<form id="pricing-tool-form"></form>');
			var hidden = $('<input name=sent" type="hidden" value="true"></input>');
			var step = 0;
			var num_steps = 4;
			var completed = new Array();
			completed[0] = 0;
			var usage = 'none';

			if( window['added_as_group'] != undefined )
				if( added_as_group.length )
					item_to_edit = added_as_group;

			var item_id = parseInt(queryString('ItemID', '?')) || item_to_edit;
			var price = 0;
			var extend = function(o,n)
			{
				for( x in n )
					o[x] = n[x];
				return o;
			}

			//console.log(params['multi_items']);

			if( typeof item_id == 'object' && item_id.length > 0 && (params['multi_items'] != undefined || window['added_as_group'] != undefined) )
					params = extend(params, {item_id:item_id,current_page:current_page, multi_items: item_id.join(',')});
			else
				params = extend(params, {item_id:item_id,current_page:current_page});

			t = $('#item-title').html();


			// Edit Stuff
			function editTabs()
			{
				$('.review-edit-button')
				.each(function()
				{
					$(this)
					.bind('click', function()
					{
						var num = parseInt($(this).attr('rel'));

						$('.pricing-tool-steps-link')
						.each(function()
						{
							if( $(this).attr('id') == 'pricing-tool-step-link-'+ num )
								$(this).trigger('click');
						});
					});
				});
			}

			// Tabs Stuff
			function tabs()
			{
				$('.pricing-tool-steps-link')
				.each(function()
				{
					console.log('Step links');
					$(this)
					.unbind()
					.bind('click', function()
					{
						if( !$(this).hasClass('active-step') )
						{
							var previous_step = step;
							step = parseInt($(this).attr('id').split('-').pop());
							completed[step] = step;
							// Prevent skipping steps
							if( completed[step - 1] != undefined || step == 0 )
							{
								//cons.log('Tab clicked and usage reset');
								params = extend(params,
								{
									snt:true,
									step:step,
									completed:completed.join(','),
									usage:usage
								});

								getPricingData(params);
							}
							else
							{
								// Reset step
								step = previous_step;
								completed[step] = '';
							}
						}
					})
					.bind('mouseover', function()
					{
						if( !$(this).hasClass('active-step') )
							$(this).addClass('pricing-tool-steps-link-over');
					})
					.bind('mouseout', function()
					{
						$(this).removeClass('pricing-tool-steps-link-over');
					});
				});
			}

			// Step 1
			function stepOne()
			{
				console.log('Step One');
				tabs();
				navigation();

				console.log('Step One after tabs and navigation');

				// Remove any handlers to prevent mutiplying of events
				$('.pricing-tool-button-usage').unbind();

				$('.pricing-tool-button-usage')
				.bind('click', function()
				{

					// Reset the application
					if( usage != parseInt($(this).attr('rel')) && completed[1] != undefined )
					{
						var start_over = confirm('If you change the type all data will\nbe erased and you must start over.\n\nClick "OK" to continue or "Cancel" to stop');
						if( start_over == true )
						{
							if( typeof item_id == 'object' && item_id.length > 0 )
									params = {item_id:item_id,current_page:current_page, multi_items: item_id.join(',')};
							else
								params = {item_id:item_id,current_page:current_page};

							completed = new Array();
							completed[0] = 0;
							step = 0;
							usage = 'none';

							params = extend(params,
							{
								snt:true,
								step:step,
								completed:completed,
								usage:usage
							});

						$('.pricing-tool-button-usage').removeClass('pricing-tool-button-usage-checked');
						$(this).addClass('pricing-tool-button-usage-checked');
						usage = parseInt($(this).attr('rel'));
						}
					}
					else
					{
						$('.pricing-tool-button-usage').removeClass('pricing-tool-button-usage-checked');
						$(this).addClass('pricing-tool-button-usage-checked');
						usage = parseInt($(this).attr('rel'));
					}
					params = extend(params, {usage:usage});

					// Highlight action
					$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
					$(this).removeClass('pricing-tool-button-usage-over');
				})
				.bind('mouseenter', function()
				{
					if( !$(this).hasClass('pricing-tool-button-usage-checked') )
						$(this).addClass('pricing-tool-button-usage-over');
				})
				.bind('mouseleave', function()
				{
					$(this).removeClass('pricing-tool-button-usage-over');
				});
			}

			// Step 2
			function stepTwo()
			{
				tabs();
				navigation();

				var size = 'none';
				var prime_usage = 'none';

				// Remove any handlers to prevent mutiplying of events
				$('.pricing-tool-option-size').unbind();
				$('.pricing-tool-option-usage-toggler').unbind();
				$('.pricing-tool-option-usage').find('a').unbind();

				$('.pricing-tool-option-size')
				.each(function()
				{
					if( $(this).hasClass('pricing-tool-option-size-always-checked') )
					{
						size = parseInt($(this).attr('rel'));
						params = extend(params, {size:size});
					}
					else
					{
						$(this)
						.bind('click', function()
						{
							$('.pricing-tool-option-size').removeClass('pricing-tool-option-size-checked');
							$(this).addClass('pricing-tool-option-size-checked');
							size = parseInt($(this).attr('rel'));
							params = extend(params, {size:size});

							// Highlight action
							if( prime_usage != 'none' && prime_usage >= 0 )
								$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
						});
					}

					// Trigger a click if this option is already selected
					if( $(this).hasClass('pricing-tool-option-size-checked') || $(this).hasClass('pricing-tool-option-size-always-checked') )
						$(this).trigger('click');
				});

				$('.pricing-tool-option-usage-toggler, .pricing-tool-option-usage-text span, .pricing-tool-option-usage-text input')
				.bind('click', function()
				{
						$('.pricing-tool-option-usage').slideToggle();
				});

				$('#pricing-tool-option-usage-select')
				.bind('change', function()
				{
					var option = $(this).find('option:selected');
					var val = option.val();
					var label = option.html();
					prime_usage = parseInt(val);
					prime_usage_label = label;
					params = extend(params, {prime_usage:prime_usage, prime_usage_label:prime_usage_label});

					// Highlight action
					if( size != 'none' && size >= 0 )
						$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
				});

				/*$('.pricing-tool-option-usage').find('a')
				.each(function()
				{
					$(this)
					.click(function()
					{
						$('.pricing-tool-option-usage').find('a').parent().removeClass('pricing-tool-option-usage-checked');
						$(this).parent().addClass('pricing-tool-option-usage-checked');
						$('.pricing-tool-option-usage-text')
						.find('input:first')
						.val($(this).html())
						.attr('rel', $(this).attr('rel'));
						$('.pricing-tool-option-usage').slideToggle();
						prime_usage = parseInt($(this).attr('rel'));
						prime_usage_label = $(this).html();
						params = extend(params, {prime_usage:prime_usage, prime_usage_label:prime_usage_label});

						// Highlight action
						if( size != 'none' && size >= 0 )
							$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
					})
					.bind('mouseover', function()
					{
						$(this).addClass('pricing-tool-option-usage-over');
					})
					.bind('mouseout', function()
					{
						$(this).removeClass('pricing-tool-option-usage-over');
					});

					// Trigger a click if this option is already selected
					if( $(this).hasClass('pricing-tool-option-usage-checked') )
						$(this).trigger('click');
				});*/
			}

			// Step 3
			function stepThree()
			{
				tabs();
				navigation();

				var quantity = 'none';
				var quantity_manual = 'none';
				var longevity = 'none';

				// Remove any handlers to prevent mutiplying of events
				$('.pricing-tool-option-size').unbind();
				$('.pricing-tool-option-usage-toggler').unbind();
				$('.pricing-tool-option-usage').find('a').unbind();
				$('.pricing-tool-option-quantity-toggler, .pricing-tool-option-quantity-text span, .pricing-tool-option-quantity-text input').unbind();
				$('.pricing-tool-option-longevity-toggler, .pricing-tool-option-longevity-text span, .pricing-tool-option-longevity-text input').unbind();

				$('.pricing-tool-option-quantity-toggler, .pricing-tool-option-quantity-text span, .pricing-tool-option-quantity-text input')
				.bind('click', function()
				{
						$('.pricing-tool-option-quantity').slideToggle();
				});

				$('.pricing-tool-option-longevity-toggler, .pricing-tool-option-longevity-text span, .pricing-tool-option-longevity-text input')
				.bind('click', function()
				{
						$('.pricing-tool-option-longevity').slideToggle();
				});

				// Quantity
				if( $('select#pricing-tool-option-quantity-select').length )
					console.log('Found It');

				$('select#pricing-tool-option-quantity-select')
				.bind('change', function()
				{
					var option = $(this).find('option:selected');
					var val = option.val();

					quantity = val;
					if( isNaN(quantity) )
						quantity = 'none';
					params = extend(params, {quantity:quantity});
				});

				/*$('.pricing-tool-option-quantity').find('a')
				.each(function()
				{
					$(this)
					.click(function()
					{
						$('.pricing-tool-option-quantity').find('a').parent().removeClass('pricing-tool-option-quantity-checked');
						$(this).parent().addClass('pricing-tool-option-quantity-checked');
						$('.pricing-tool-option-quantity-text')
						.find('input:first')
						.val($(this).html())
						.attr('rel', $(this).attr('rel'));
						$('.pricing-tool-option-quantity').slideUp();

						if( $('#pricing-tool-quantity-manual-input').val() == undefined || $('#pricing-tool-quantity-manual-input').val() == '' )
						{
							quantity = parseInt($(this).attr('rel'));
							if( isNaN(quantity) )
								quantity = 'none';
							params = extend(params, {quantity:quantity});
							$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
						}

					})
					.bind('mouseover', function()
					{
						$(this).addClass('pricing-tool-option-quantity-over');
					})
					.bind('mouseout', function()
					{
						$(this).removeClass('pricing-tool-option-quantity-over');
					});

					// Trigger a click if this option is already selected
					if( $(this).parent().hasClass('pricing-tool-option-quantity-checked') )
						$(this).trigger('click');
				});*/

				// Longevity
				$('#pricing-tool-option-longevity-select')
				.bind('change', function()
				{
					var val = $(this).find('option:selected').val();

					longevity = toDays(val);

					function toDays(duration)
					{
						if( duration.match(/day/) )
							return parseInt(duration);
						if( duration.match(/week/) )
							return (parseInt(duration) * 7);
						if( duration.match(/month|months/) )
							return (parseInt(duration) * 30);
						if( duration.match(/year|years/) )
							return (parseInt(duration) * 365);
					}
					params = extend(params, {longevity:longevity});
				});

				$('.pricing-tool-option-longevity').find('a')
				.each(function()
				{
					$(this)
					.click(function()
					{
						$('.pricing-tool-option-longevity').find('a').parent().removeClass('pricing-tool-option-longevity-checked');
						$(this).parent().addClass('pricing-tool-option-longevity-checked');
						$('.pricing-tool-option-longevity-text')
						.find('input:first')
						.val($(this).html())
						.attr('rel', $(this).attr('rel'));
						$('.pricing-tool-option-longevity').slideUp();

						longevity = toDays($(this).attr('rel'));

						function toDays(duration)
						{
							if( duration.match(/day/) )
								return parseInt(duration);
							if( duration.match(/week/) )
								return (parseInt(duration) * 7);
							if( duration.match(/month|months/) )
								return (parseInt(duration) * 30);
							if( duration.match(/year|years/) )
								return (parseInt(duration) * 365);
						}
						params = extend(params, {longevity:longevity});
						$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-over');
					})
					.bind('mouseover', function()
					{
						$(this).addClass('pricing-tool-option-quantity-over');
					})
					.bind('mouseout', function()
					{
						$(this).removeClass('pricing-tool-option-quantity-over');
					});

					// Trigger a click if this option is already selected
					if( $(this).parent().hasClass('pricing-tool-option-quantity-checked') )
						$(this).trigger('click');
				});

				$('#pricing-tool-quantity-manual-input')
				.bind('blur', function()
				{
					params = extend(params, {quantity:parseInt($(this).val())});
				});

				$('.pricing-tool-option-quantity-manual-toggler')
				.bind('click', function()
				{
						$('#pricing-tool-quantity-manual-input').val('');
						var val = $('#pricing-tool-quantity-input').val();
						params = extend(params, {quantity:val.replace(/[^\d]+/, '')});
				});
			}

			// Step 4
			function stepFour()
			{
				tabs();
				navigation();
				editTabs();

				$('.pricing-tool-nav-button-add-to-cart, .pricing-tool-nav-button-update-cart')
				.click(function()
				{
					//window.location.href = add_to_cart_url;
					$.fn.itemActions.ajaxMessage.show();
					var hidden = '';
					for( x in params )
						if( x != 'action' )
							hidden += '<input type="hidden" name="'+ x +'" value="'+ params[x] +'">';

					$('body').append(form);

					form
					.html(hidden)
					.attr({action:add_to_cart_url, method:'post'})
					.submit();
				});
			}

			// Navigation Stuff
			function navigation()
			{
				$('.pricing-tool-nav-button')
				.each(function()
				{
					$(this)
					.unbind()
					.bind('click', function()
					{
						var way = $(this).attr('id').split('-').pop();
						if( step >= 1 )
						{
							$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-final');
							$('#pricing-tool-nav-button-back').removeClass('pricing-tool-nav-button-inactive');

						}

						if( step < (num_steps - 2) )
						{
							$('#pricing-tool-nav-button-back').addClass('pricing-tool-nav-button-inactive');
							$('#pricing-tool-nav-button-next').removeClass('pricing-tool-nav-button-final');
						}

						if( step == (num_steps - 2) )
							{
								$('#pricing-tool-nav-button-next').html('Add to Cart');
							}

						if( way == 'next' )
						{
							if( step < (num_steps - 1) )
							{
								step++;
								completed[step] = step;
								params = extend(params,
								{
									snt:true,
									step:step,
									completed:completed.join(','),
									usage:usage
								});
								getPricingData(params);
								//cons.log(step);

							}
						}
						else if( way == 'back' )
						{
							if( step > 0 )
							{
								step--;
								completed[step] = step;
								params = extend(params,
								{
									snt:true,
									step:step,
									completed:completed.join(','),
									usage:usage
								});
								getPricingData(params);

							}
						}
					})
					.bind('mouseover', function()
					{
						if( !$(this).hasClass('pricing-tool-nav-button-inactive') )
							$(this).addClass('pricing-tool-nav-button-over');
					})
					.bind('mouseout', function()
					{
						$(this).removeClass('pricing-tool-nav-button-over');
					});
				});
			}

			var buttons;
			if( opt )
			{
				c = '<strong>This item is already in your cart</strong>.<br>Would you like to <a href="javascript:void(0);" id="edit-item-usage">edit its usage</a>?';

				buttons =
				{
					'Cancel' :
					function()
					{
						$('.ui-dialog').remove();
						$.fn.itemActions.action.enable();
						params = {};
					}
				};
			}
			else
			{
				var button = '<button class="pricing-tool-button"/>';
				var ubtn = new Array();
				ubtn[0] = $(button).html('Print');
				ubtn[1] = $(button).html('Electronic');
				ubtn[2] = $(button).html('Broadcast');

				buttons =
				{
					'Cancel' :
					function()
					{
						$('.ui-dialog').remove();
						$.fn.itemActions.action.enable();
						params = {};
					}
				};
			}

			var section =
			$("#dialog").html('<div class="pricing-tool-section">Please select a usage.</div>');
			for( x in ubtn )
			{
				$("#dialog").append(ubtn[x]);
			}

			// Attempt to display either multi-license or the single item title when itgem is added from lightbox
			var missing_single_item_id = $('#cart-items').find('a.missing-license').parent().attr('rel');
			var missing_single_item_title = $('a#item-thumb-anchor-'+ missing_single_item_id).attr('title');

			if( (window['unlicensed_items'] != undefined || window['added_as_group'] != undefined) && (window['unlicensed_items'].length > 1 || window['added_as_group'].length > 1) )
				t = '&raquo; Multiple Item License' ;
			else if( t == ''  || t == undefined )
				t = missing_single_item_title;

			$("#dialog")
			.dialog({
				bgiframe: true,
				title: t,
				width: 670,
				height: 550,
				minHeight: 550,
				minWidth: 670,
				maxHeight: 800,
				maxWidth: 800,
				zIndex: 10001,
				modal: true,
				resizable: true,
				draggable: true,
				closeOnEscape: true,
				buttons: buttons,
				close: function(){$('.ui-dialog').remove(); $.fn.itemActions.action.enable(); params = {};}
			});

			function handlePricingData(data, status)
			{
				var resp = data.split('|');
				$('#dialog').html(resp[4]);
				step = parseInt(resp[1]);
				price = resp[3];
				params = extend(params,{price:price});

				if( step > 0 )
					object = null;

				switch( step )
				{
					case 0 	:	object = new stepOne(); 		break;
					case 1	: 	object = new stepTwo(); 		break;
					case 2	:	object = new stepThree(); 		break;
					case 3	:	object = new stepFour();		break;
				}

				completed_obj = eval("("+ resp[0] +")");

				if( typeof completed_obj != 'object')
					completed[0] = parseInt(completed_obj);
				else
					completed = completed_obj;

				//cons.log(completed);

				if( step == 0 )
					$('#pricing-tool-nav-button-back').addClass('pricing-tool-nav-button-inactive');
				if( step > (num_steps - 2) )
					$('#pricing-tool-nav-button-next').addClass('pricing-tool-nav-button-final');
			}

			function handlePricingError(XHR, error, except)
			{
				if( error == 'Timeout' )
				{
					alert("Your request has timed out. Please try again.");
				}
				else
				{
					alert(error + ' : ' + except);
				}
			}

			function handlePricingComplete(XHR, status)
			{
				//alert('Completed: ' + status);
			}

			function handlePricingFilter(data, type)
			{
				return data;
			}

			function getPricingData(data)
			{
				//cons.log(data);

				$.ajax(
				{
					type		:	'POST',
					url			:	$.fn.itemDialog.defaults.pricingToolDataSrc,
					dataType	: 	'html',
					data		:	(data),
					success		:	handlePricingData,
					cache		:	false,
					async		:	true,
					error		:	handlePricingError,
					complete	:	handlePricingComplete,
					dataFilter	:	handlePricingFilter,
					timeout		:	20000
				});
			}

			if( params['completed'] == undefined )
			{
				//cons.log('completed first time');
				params = extend(params,
				{
					snt:true,
					step:0,
					completed:completed.join(','),
					usage:usage
				});
			}
			else
			{
				usage = params['usage'];
				price = params['price'];

			}


			// cons.log(params);
			getPricingData(params);
			//cons.log('Begin Pricing Tool');
		}
		else if( t == 'Confirm' )
		{
			$("#dialog")
			.html(c)
			.dialog({
				bgiframe: true,
				title: t,
				width: 502,
				height: 250,
				minHeight: 250,
				minWidth: 502,
				maxHeight: 550,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					'Ok': function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					},
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					}
				},
				close: function()
				{
					//action.enable(o);
					$('.ui-dialog').remove();
				}
			});
		}
		else if( t == 'Custom Request Tool' )
		{
			var message_loader = $('#global-load-message-wrapper');
			message_loader
			.css({
				left: ($(window).width() - message_loader.width())/2 + 'px',
				top: (($(window).height() - message_loader.height())/2 + $(window).scrollTop()) +'px',
				display: 'none'
			});

			$("#dialog")
			.html(c)
			.dialog({
				bgiframe: true,
				title: t,
				width: 502,
				height: 250,
				minHeight: 375,
				minWidth: 502,
				maxHeight: 550,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					'Ok': function()
					{
						$('.ui-dialog').remove();
						custom_request_form.submit();
						$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
					},
					'Cancel': function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						custom_request_form.remove();
						if( window.location.search == '' || window.location.search == undefined )
							window.location.href = window.location.href +'?custom_request=canceled';
						else
							window.location.href = window.location.href +'?'+ window.location.search +'&custom_request=canceled';

						$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
					}
				},
				close: function()
				{
					$('.ui-dialog').remove();
					custom_request_form.remove();
					if( window.location.search == '' || window.location.search == undefined )
						window.location.href = window.location.href +'?custom_request=canceled';
					else
						window.location.href = window.location.href +'?'+ window.location.search +'&custom_request=canceled';

					$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
				}
			});
		}
		else if( t == 'Add to Cart Verification Tool' )
		{
			/*var message_loader = $('#global-load-message-wrapper');
			message_loader
			.css({
				left: ($(window).width() - message_loader.width())/2 + 'px',
				top: (($(window).height() - message_loader.height())/2 + $(window).scrollTop()) +'px',
				display: 'none'
			});
			*/


			$("#dialog")
			.html(c)
			.dialog({
				bgiframe: true,
				title: t,
				width: 502,
				height: 250,
				minHeight: 502,
				minWidth: 502,
				maxHeight: 550,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					Ok: function()
					{
						$('.ui-dialog').remove();
						$(collection_form).append('<input type="hidden" name="action" value="Add To Cart"/>');
						$(collection_form).submit();
						$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
					},
					/*Close: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						//window.location.href = window.location.href;
					},*/
					Cancel: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
						var form = $('<form action="'+ window.location.href +'" method="post"/>');
						form.html('<input type="hidden" name="canceled" value="canceled"/>');

						$('body').append(form);
						form.submit();
						$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
					}
				},
				close: function()
				{
					$('.ui-dialog').remove();
					var form = $('<form action="'+ window.location.href +'" method="post"/>');
					form.html('<input type="hidden" name="canceled" value="canceled"/>');

					$('body').append(form);
					form.submit();
					$(document).LoadingMessage({show:true, borderWidth: 2, opacity: 0.8});
				}
			});
		}
		else
		{
			$("#dialog")
			.html(c)
			.dialog({
				bgiframe: true,
				title: t,
				width: 502,
				height: 250,
				minHeight: 250,
				minWidth: 502,
				maxHeight: 550,
				maxWidth: 802,
				zIndex: 10001,
				modal: true,
				resizable: false,
				draggable: false,
				closeOnEscape: true,
				buttons:
				{
					Close: function()
					{
						$(this).dialog('close');
						$('.ui-dialog').remove();
					}
				},
				close: function()
				{
					//action.enable(o);
					$('.ui-dialog').remove();
				}
			});
		}
	}
});
