/**
 * aheadWorks Co.
 *
 * NOTICE OF LICENSE
 *
 * This source file is subject to the EULA
 * that is bundled with this package in the file LICENSE.txt.
 * It is also available through the world-wide-web at this URL:
 * http://ecommerce.aheadworks.com/LICENSE-M1.txt
 *
 * @category   AW
 * @package    AW_Ajaxcartpro
 * @copyright  Copyright (c) 2003-2009 aheadWorks Co. (http://www.aheadworks.com)
 * @license    http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */
 
var aw_cartDivClass = '.base-mini mini-cart';
var aw_topLinkCartClass = '.top-link-cart';
var aw_addToCartButtonClass = '.form-button';
var aw_bigCartClass = '.middle_section.one_column';

if (window.location.toString().search('/product_compare/') != -1){
	var win = window.opener;
}
else{
	var win = window;
}

function setLocation(url){
	 if(AW_ACP.isCartPage && ((url.search('/add') != -1 ) || (url.search('/remove') != -1 )) ){
		ajaxcartsend(url+'awacp/1/is_checkout/1', 'url', '', '');
	}else if (url.search('checkout/cart/add') != -1 ){
		ajaxcartsend(url+'awacp/1', 'url', '', '');
	}else{
		window.location.href = url;
	}
}


/*			
			try {
				// This fix is applied to magento <1.3.1
				$_('#product_addtocart_form').$_(aw_addToCartButtonClass).setAttribute('type', 'button');
			}catch(err){
				
			}
*/

var cnt1 = 20;
__intId = setInterval(
	/* Hangs event listener for @ADD TO CART@ links*/
	function(){
		cnt1--;
		if(typeof productAddToCartForm != 'undefined'){
			
			if (typeof manyProductAddToCartForm != 'undefined'){
				
				for (var i in productAddToCartForm){
					
					productAddToCartForm[i].submit = function(url){
						if(this.validator && this.validator.validate()){					
							ajaxcartsend('?awacp=1', 'form', this, '');
						}
						return false;
					}
					
				}
				
				
			} else {

				productAddToCartForm.submit = function(url){
					if(this.validator && this.validator.validate()){					
						ajaxcartsend('?awacp=1', 'form', this, '');
					}
					return false;
				}

			}

			clearInterval(__intId);
		}
		
		
		if(!cnt1) clearInterval(__intId);
	},
	500
);

var cnt2 = 20;
__intId2 = setInterval(
	/* This hangs event listener on @DELETE@ items from cart*/
	function(){	
		cnt2--;
		if(!$_(aw_cartDivClass).empty() || ((typeof AW_ACP !== 'undefined') && AW_ACP.isCartPage)){
			updateDeleteLinks();
			clearInterval(__intId2);
		}
		if(!cnt2) clearInterval(__intId);
	},
	500
);






function setPLocation(url, setFocus){
	if (url.search('checkout/cart/add') != -1){ //CART ADD
		window.opener.focus();
		ajaxcartsend(url+'/awacp/1', 'url', '');
	}
	else{
		if(setFocus) {
			window.opener.focus();
		}
		window.opener.location.href = url;
	}
}

function ajaxcartsend(url, type, obj){
	showProgressAnimation();

	// var match = /product\/(\d+)/i.exec(url)
	// if (typeof(match) == null){
	// 	match = /product\/(\d+)/i.exec(url)
	// }
	// 

	var product_id = parseInt(jQuery('input[type=hidden][name=product]').val());;


	if (document.getElementById('product_addtocart_form' + product_id) != null &&
		typeof manyProductAddToCartForm != 'undefined'
	){			
		type = 'form';
	}
	
	if (document.getElementById('product_addtocart_form') != null){
		type = 'form';
		product_id = '';
	}

	if (type == 'form'){	
		
		var bvg_form = $_('#product_addtocart_form' + product_id);
		
		bvg_form.action += url;		
		bvg_form.POST(			
			function(resp){
				hideProgressAnimation();				
				if (resp.r != 'success'){
					jQuery('#product_addtocart_form').submit();
					obj.form.submit();
				}
				else{
					if(AW_ACP.useConfirmation){	
						showConfirmDialog();	
					}					
					updateCartView(resp);
				}
			}
		);
	}
	if (type == 'url'){
		$_(url).GET(
			function(resp){
				hideProgressAnimation();
				if (resp.r != 'success'){
					win.location.href=url;
				}
				else{		
					if(AW_ACP.useConfirmation){	
						showConfirmDialog();	
					}	
					updateCartView(resp);
				}
			}
		);
	}
}

function updateDeleteLinks(){
	var tmpLinks = document.links;//win.$_(aw_cartDivClass).getElementsByTagName('a',win.$_(aw_cartDivClass));
	for (i=0; i<tmpLinks.length; i++){
		if (tmpLinks[i].href.search('checkout/cart/delete') != -1){
			url = tmpLinks[i].href.replace(/\/uenc\/.+,/g, "");
			var del = url.match(/delete\/id\/\d+\//g);
			var id = del[0].match(/\d+/g);
			if (win.location.protocol == 'https:'){
				aw_base_url = aw_base_url.replace("http:", "https:");
			}	
			if(!AW_ACP.isCartPage){
				tmpLinks[i].href = 'javascript:ajaxcartprodelete("' + aw_base_url + 'ajaxcartpro/cart/remove/id/' + id +'")';
			}else{
				tmpLinks[i].href = 'javascript:ajaxcartprodelete("' + aw_base_url + 'ajaxcartpro/cart/remove/id/' + id +'/is_checkout/1")';
			}
		}
	}
}

function updateTopLinks(resp){
	win.$_(aw_topLinkCartClass).innerHTML = resp.links;
}

function updateCartView(resp){
	updateTopLinks(resp);
	
	jQuery('#cart-top').fadeOut('slow', function () {
		jQuery('#cart-top').html(resp.top_cart);
		jQuery('#cart-top').fadeIn('slow');
	});

	if(AW_ACP.isCartPage)
		return updateBigCartView(resp);

	
	var oldHeight = win.$_(aw_cartDivClass).offsetHeight;

	var tmpDiv = win.document.createElement('div');
	tmpDiv.innerHTML = resp.cart;
	
	var tmpParent = win.$_(aw_cartDivClass).parentNode;
	if (typeof tmpParent != 'undefined')
		tmpParent.replaceChild(tmpDiv.firstChild, win.$_(aw_cartDivClass));
	
	//Details popup support
	truncateOptions();
	
	var newHeight = win.$_(aw_cartDivClass).offsetHeight;
	
	if (aw_ajaxcartpro_cartanim == 'opacity'){
		$_(win.$_(aw_cartDivClass)).mutate(
			{opacity:0}, 
			{opacity:1},
			30
		);
	}
	if (aw_ajaxcartpro_cartanim == 'grow'){
		$_(win.$_(aw_cartDivClass)).style.overflow = 'hidden';
		$_(win.$_(aw_cartDivClass)).mutate(					
			{opacity:0, height: oldHeight + 'px'},
			{opacity:1, height: newHeight + 'px'},
			30
		);
	}
	if (aw_ajaxcartpro_cartanim == 'blink'){
		$_(win.$_(aw_cartDivClass)).mutate(
			{opacity:0}, 
			{opacity:1},
			10
		);
	}
	updateDeleteLinks();
}

function updateBigCartView(resp){
	$$(aw_bigCartClass).first().innerHTML = resp.cart	
//	$_(aw_bigCartClass).innerHTML = resp.cart
	if($_('#shopping-cart-table')){
		decorateTable('shopping-cart-table')
	}
	updateDeleteLinks();
	updateTopLinks(resp);
}


function ajaxcartprodelete(url){	
	showProgressAnimation();
	$_(url).GET(
		function(resp){			
			hideProgressAnimation();
			updateCartView(resp, '');
		}
	);
}

function showProgressAnimation(){
	p = win.$_('.ajaxcartpro_progress');
	if ($_.ie6){
		p.style.position = 'fixed';
	}
	if (aw_ajaxcartpro_proganim == 'center'){		
		p.style.top = (screen.height/2) - (pH) + 'px';
	}
	if (aw_ajaxcartpro_proganim == 'top'){		
		p.style.top = '0px';
	}
	if (aw_ajaxcartpro_proganim == 'bottom'){
		p.style.bottom = '0px';		
	}
	if (aw_ajaxcartpro_proganim != 'none'){
		p.style.display = 'block';	
	}
	
}


function showConfirmDialog(){
	p = win.$_('.ajaxcartpro_confirm');
	
	if ($_.ie6){
		p.style.position = 'fixed';
	}
	if (aw_ajaxcartpro_proganim == 'center'){		
		p.style.top = (screen.height/2) - (pH) + 'px';
	}
	if (aw_ajaxcartpro_proganim == 'top'){		
		p.style.top = '0px';
	}
	if (aw_ajaxcartpro_proganim == 'bottom'){
		p.style.bottom = '0px';		
	}
	if (aw_ajaxcartpro_proganim != 'none'){
		p.style.display = 'block';	
	}
	setTimeout("jQuery('.ajaxcartpro_confirm').fadeOut();",2100);
}

function hideProgressAnimation(){
	$_('.ajaxcartpro_progress').style.display = 'none';
}

window.onload = function(){
	var ats = document.links;
	for (i=ats.length-1; i>=0; i--){
		if (ats[i].href.search('checkout/cart/add') != -1){
			ats[i].onclick = function(link){
				return function(){
					setLocation(link)
				}
			}(ats[i].href);
			ats[i].href="javascript:void(0)";
		}
	}
	
	// Some other onclicks
	//$_('#aw_acp_continue').onclick = function(e){
	//	e = e||event;
	//	if(e.preventDefault)
	//		e.preventDefault()
	//	$_('.ajaxcartpro_confirm').style.display='none'; return false;
	//}
	
	//$_('#aw_acp_checkout').onclick = function(e){
	//	$_('.ajaxcartpro_confirm').style.display='none'; return true;
	//}	
	
	if(!$_(aw_cartDivClass).empty() || ((typeof AW_ACP !== 'undefined') && AW_ACP.isCartPage)){
		updateDeleteLinks();
	}
	
} 
