/**
 * 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 test = '.top-link-cart';

var test1 = '.cart_items';
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').$_('.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){
	showProgress();
	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.$_(test).innerHTML = resp.links;
	
	
	
	fireMyPopup();
}


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

	updateTopLinks(resp);

	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
		);
	}*/
	win.$_(test1).innerHTML = resp.cart;
	updateDeleteLinks();
	updateTopLinks(resp);

}

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

function showProgress(){
	var pW = 100;
	var pH = 45;
	var pPT = 5;
	var aw_ajaxcartpro_proganim = 'center';
	
	p = win.$_('.ajaxcartpro_progress');
	p.style.width = pW + 'px';		
	p.style.height = pH + 'px';
	p.style.paddingTop = pPT + 'px';
	
	var IE6 = false;
	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';
}

function setOpacity( value ) {
	document.getElementById("styled_popup").style.opacity = value / 10;
	document.getElementById("styled_popup").style.filter = 'alpha(opacity=' + value * 10 + ')';
}

function fadeInMyPopup() {
	for( var i = 0 ; i <= 100 ; i++ )
		setTimeout( 'setOpacity(' + (i / 10) + ')' , 8 * i );
}

function fadeOutMyPopup() {
	for( var i = 0 ; i <= 100 ; i++ ) {
		setTimeout( 'setOpacity(' + (10 - i / 10) + ')' , 8 * i );
	}

	setTimeout('closeMyPopup()', 800 );
}

function closeMyPopup()
{
	document.getElementById("styled_popup").style.display = "none"
}

function fireMyPopup() {
	setOpacity( 0.10 );
	document.getElementById("styled_popup").style.display = "block";
	fadeInMyPopup();
	setTimeout('closeMyPopup()', 3000);
	//setTimeout('closeMyPopup()', 800 );
}


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);
		}
	}
} 
