/**
 * 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 = '.box base-mini mini-cart';
var aw_cartDivClass = '.block block-cart';


var aw_topLinkCartClass = '.top-link-cart';

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

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

__intId = setInterval(
	function(){
		if(typeof productAddToCartForm != 'undefined'){
			try {
				$_('#product_addtocart_form').$_('.button').setAttribute('type', 'button');
			}
			catch(err){
				
			}
			clearInterval(__intId);
			productAddToCartForm.submit = function(url){
				if(this.validator && this.validator.validate()){					
					ajaxcartsend('?awacp=1', 'form', this, '');
				}
				return false;
			}
		}
	},
	500
);

__intId2 = setInterval(
	function(){	
		if(!$_(aw_cartDivClass).empty()){
			clearInterval(__intId2);
			updateDeleteLinks();
		}
	},
	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();
	if (type == 'form'){		
		$_('#product_addtocart_form').action += url;		
		$_('#product_addtocart_form').POST(			
			function(resp){
				hideProgressAnimation();				
				if (resp.r != 'success'){
					obj.form.submit();
				}
				else{					
					updateCartView(resp);
				}
			}
		);
	}
	if (type == 'url'){
		$_(url).GET(
			function(resp){
				hideProgressAnimation();
				if (resp.r != 'success'){
					win.location.href=url;
				}
				else{					
					updateCartView(resp);
				}
			}
		);
	}
}

function updateDeleteLinks(){
	var tmpLinks = 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);
			tmpLinks[i].href = 'javascript:ajaxcartprodelete("' + aw_base_url + 'ajaxcartpro/cart/remove/?id=' + id +'")';
		}
	}
}

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

function updateCartView(resp){
	var oldHeight = win.$_(aw_cartDivClass).offsetHeight;

	var tmpDiv = win.document.createElement('div');
	tmpDiv.innerHTML = resp.cart;

	var tmpParent = win.$_(aw_cartDivClass).parentNode;
	tmpParent.replaceChild(tmpDiv.firstChild, win.$_(aw_cartDivClass));
	
	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();
	updateTopLinks(resp);
}

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

function showProgressAnimation(){
	var pW = 260;
	var pH = 50;
	p = win.$_('.ajaxcartpro_progress');
	p.style.width = pW + 'px';		
	p.style.height = pH + 'px';
	var IE6 = false /*@cc_on || @_jscript_version < 5.7 @*/;
	if (IE6){
		p.style.position = 'absolute';
	}
	if (aw_ajaxcartpro_proganim == 'center'){
		p.style.left = (screen.width/2) - (pW/2) + 'px';
		p.style.top = (screen.height/2) - (pH) + 'px';
	}
	if (aw_ajaxcartpro_proganim == 'top'){
		p.style.left = (screen.width/2) - (pW/2) + 'px';
		p.style.top = '0px';
	}
	if (aw_ajaxcartpro_proganim == 'bottom'){		
		p.style.left = (screen.width/2) - (pW/2) + 'px';		
		p.style.bottom = '0px';		
	}
	if (aw_ajaxcartpro_proganim != 'none'){
		p.style.display = 'block';	
	}
}

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

window.onload = function(){
	ats = $_('~a');
	for (i=0; i<ats.length; 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)";
			console.log(ats[i].href);
		}
	}
} 

