/**
 * TopRock
 *
 * NOTICE OF LICENSE
 *
 *
 * Web+Aid AjaxCart Premium
 * 
 * End-User License Agreement 
 * This AjaxCart Basic End-User License Agreement ("EULA") is a legal AGREEMENT between you and TopRock LLC for the Webandaid AjaxCart Basic software product identified above, which product includes computer software and may include associated media, printed materials, and "online" or electronic documentation ("Software Product" or "Software " or "Product"). By downloading, installing, copying, or otherwise using the Software product, you agree to be bound by the terms of this EULA.
 * Do not use this Software Product until you have carefully read the following terms and conditions. By using the Software Product, you agree to the terms of this Agreement. If you do not wish to so agree, do not install or use this Software Product.
 * 
 * LICENSE GRANT
 * TopRock LLC grants to you, on these terms and conditions, the non-exclusive right and license to use this Software. The Software is offered under a SINGLE LICENSE and it can only be used under ONE DOMAIN NAME. 
 * 
 * DISCLAIMER OF WARRANTY
 * By downloading this software you agree that you wish to use the selected software and that you will not hold TopRock LLC responsible in any way. Using of this software means that you have understood and agreed to all the terms and conditions of this disclaimer. 
 * TopRock LLC does not take any responsibility and is not liable for any damage caused through use of this software, be it indirect, special, incidental or consequential damages (including damages for loss of business, loss of profits, interruption or the like). TopRock LLC does not take responsibility for the correct workings of software that has been modified in any way or by their use in ways they weren't designed for. 
 * We cannot guarantee full compatibility of the products acquired by you with Magento Commerce versions released after the purchase. We do not bear any responsibility for such compatibility problems. 
 * This software is sold on an AS IS basis. All features are listed on the product page. By downloading this software you agree that you have read and understood the product specifications and have assessed that they can be used by you in the way that you require. 

 * OWNERSHIP
 * All right, title and interest in the intellectual property embodied in the Software and accompanying materials, if any, are owned by TopRock LLC or its licensors. The Software is copyrighted and protected by international treaty provisions. Exclusive rights for commercial use of the Software worldwide are owned by TopRock LLC.
 * As defined by this Agreement, End-User receives limited rights of using the Software only. This Agreement does not grant you any intellectual property rights in the Software.

*/

if (!window.WAUpdater) var WAUpdater = new Object();

WAUpdater.Methods =
{
	options: {
		redirectOnError: true,
		baseUrl: "",
		updateColor:'#666666',
		mode:"add",
		removeRow:null
	},
	_options: new Object,
	setOptions: function(options) {
		Object.extend(this.options, options || {});
	},
	
	showAjaxModal: function()
	{
		Modalbox.show('<div id="MB_loading"><br/>Please wait...</div>'); 
	},
	
	updateMiniCart: function(transport, options)
	{
		this.setOptions(options);
		Modalbox.hide();
		window.scroll(0,0);
	  	var data = transport.responseText.evalJSON();
		//IF THERE'S ANY ERROR
		if(data.error)
		{
			if(this.options.redirectOnError)
			{
				if (data.url) {
					window.location = data.url;
					
				}
				else {
					history.go(-1);
				}
				return true;
			}
			else
			{
				var error_msg = 'Sorry. There was an error in your operation.';
				if(data.error_msg) error_msg = data.error_msg;
				Modalbox.show('<div id="MB_message">ERROR:'+error_msg+'</div>',{modal:true}); 
			}
		}
		else
		{
			var item_count= data.item_count;
			var cart_subtotal = data.cart_subtotal;
			var onepage_checkout = data.onepage_checkout;
			var link_text = data.link_text;
			var cart_subtotal_str = data.cart_subtotal_str;
			var cart_subtotal_tax = data.cart_subtotal_tax;
			var my_cart_str = data.my_cart_str;
			var qty_str = data.qty_str;
			var checkout_str = data.checkout_str;
			var no_cart_str = data.no_cart_str;
			
			//update item count on top menu
			if($$('a.top-link-cart').size()!=0)
			{
				$$('a.top-link-cart')[0].update(link_text);
			}
			
			
			if(this.options.removeRow)
			{
				if($$('.mini-cart h5').size()!=0 && item_count==0) $$('.mini-cart h5')[0].remove();
				new Effect.SlideUp(this.options.removeRow);
			}
			
			//update recently added items
			if(data.recent_items)
			{
				//if($$('.mini-cart h5').size()!=0) $$('.mini-cart h5')[0].remove();
				if($$('.mini-cart .content').size()!=0)
				{
					$$('.mini-cart .content')[0].update(data.recent_items);
					decorateList('cart-sidebar', 'non-recursive');
					new Effect.SlideDown('cart-sidebar');
				} 
			}
			
			//total price
			if($$('.mini-cart .head').size()!=0)
			{
				var minicart_itemcount = qty_str;
				
				if(item_count > 0)
				{
					var minicart_string = minicart_itemcount+'<p class="subtotal">'+cart_subtotal_str+'<strong>'+cart_subtotal+'</strong>'+cart_subtotal_tax +'</p>';
					
					if($$('.mini-cart .cart-count').size()==0)
					{
						$$('.mini-cart .head')[0].insert({
							after: '<div class="cart-count"></div>'
						});
					}
					$$('.mini-cart .cart-count')[0].update(minicart_string);
				}
				else
				{
					
					if($$('.mini-cart .actions').size() !=0) $$('.mini-cart .actions')[0].remove();
					$$('.mini-cart .cart-count')[0].remove();
					$$('.mini-cart .content')[0].update('<p>'+no_cart_str+'</p>');
					
				}
				
				
				new Effect.Highlight($$('.mini-cart .head')[0],{startcolor: this.options.updateColor});
				if($$('.mini-cart .content').size()!=0) new Effect.Highlight($$('.mini-cart .content')[0],{startcolor: this.options.updateColor});
				if($$('.mini-cart .actions').size()!=0) new Effect.Highlight($$('.mini-cart .actions')[0],{startcolor: this.options.updateColor});
			}
			
			if($$('#shopping-cart-totals-table tfoot').size()!=0)
			{
				$$('#shopping-cart-totals-table tfoot')[0].update(data.totals_footer);
				$$('#shopping-cart-totals-table tbody')[0].update(data.totals_top);
				new Effect.Highlight($$('#shopping-cart-totals-table tfoot')[0],{startcolor: this.options.updateColor});
				new Effect.Highlight($$('#shopping-cart-totals-table tbody')[0],{startcolor: this.options.updateColor});
			}
			
		}
	}
};

Object.extend(WAUpdater, WAUpdater.Methods);
function submit_ajax_cart(url)
{
	/*added fix for 1.1.1*/
	if( url.include('options=cart'))
	{
		window.location=url;
	}
	else
	{
		new Ajax.Request(url, {
		  method: 'get',
		  onLoad:WAUpdater.showAjaxModal(),
		  onComplete: function(transport)
		  {
		  	bu =  url.substring(0,url.lastIndexOf("/checkout") ) ;
			//added for 1.1.2
		  	var redirected= WAUpdater.updateMiniCart(transport, {baseUrl:bu, mode:"ADD"});
		  	if(location.href.include("checkout/cart") && !redirected)
			{
				location.reload(true);
			}
		  }
		});
	}
	
}


function submit_ajax_cart_p(url)
{
	window.opener.submit_ajax_cart(url); 
}

/* from this section on is premium */
function delete_ajax_cart(url,row)
{
	if (url.include('options=cart')) {
		window.location=url;
	}
	else {
		new Ajax.Request(url, {
			method: 'get',
			onLoad: WAUpdater.showAjaxModal(),
			onComplete: function(transport){
				bu = url.substring(0, url.lastIndexOf("/checkout"));
				WAUpdater.updateMiniCart(transport, {
					baseUrl: bu,
					mode: "DELETE",
					removeRow: row,
					redirectOnError: false
				});
			}
		});
	}
}