/**
 * 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) 2009-2010 aheadWorks Co. (http://www.aheadworks.com)
 * @license http://ecommerce.aheadworks.com/LICENSE-M1.txt
 */
 
 
var ie = 0/*@cc_on+@_jscript_version*10@*/;
$_.ie6 = $_.IE6 = ie == 56;
window.ACPTop = 200;
 
window.intPrevious = setInterval(function(){
 if(typeof AW_ACP != 'undefined' && document.body){
 if(typeof aw_cartDivClass == 'undefined'){
 aw_cartDivClass = 
 AW_ACP.theme == 'blank' ?
 '.block block-cart' :
 '.box base-mini mini-cart';

 if(!$_(aw_cartDivClass).tagName){
 aw_cartDivClass = '.block block-cart'
 }
 }
 if(typeof aw_topLinkCartClass == 'undefined'){
 aw_topLinkCartClass = '.top-link-cart';
 }
 if(typeof aw_addToCartButtonClass == 'undefined'){
 aw_addToCartButtonClass = '.form-button';
 }
 if(typeof aw_bigCartClass == 'undefined'){
 aw_bigCartClass = 
 AW_ACP.theme == 'modern' ?
 '.layout-1column':
 '.col-main';
 }
 

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


function setLocation(url){
 if(window.location.href.match('https://') && !url.match('https://')){
 url = url.replace('http://','https://')
 }
 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;
 }
}


if(!$_.ie6){

 var cnt1 = 20;
 __intId = setInterval(
 /* Hangs event listener for @ADD TO CART@ links*/
 function(){
 cnt1--;
 if(typeof productAddToCartForm != 'undefined'){
 try {
 // This fix is applied to magento <1.3.1
 $_('#product_addtocart_form').$_(aw_addToCartButtonClass).setAttribute('type', 'button');
 }catch(err){
 
 }
 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(typeof aw_cartDivClass!= 'undefined' && !$_(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){
 url = getCommonUrl(url)
 
 showProgressAnimation();
 if (type == 'form'){ 
 $_('#product_addtocart_form').action += url; 
 $_('#product_addtocart_form').POST ( 
 function(resp){
 if (typeof(resp) == 'string'){
 try{
 eval('resp = ' + resp);
 }catch(e){
 return obj.form.submit();
 }
 }
 hideProgressAnimation(); 
 if (resp.r != 'success'){
 obj.form.submit();
 }
 else{
 if(AW_ACP.useConfirmation){ 
 showConfirmDialog(); 
 } 
 updateCartView(resp);
 }
 }
 );
 }
 if (type == 'url'){
 $_(url).GET(
 function(resp){
 try{
 if (typeof(resp) == 'string') eval('resp = ' + resp);
 }catch(e){
 win.location.href=url;
 hideProgressAnimation();
 return;
 }
 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).title = win.$_(aw_topLinkCartClass).innerHTML = resp.links;
}

function updateCartView(resp){
 if (AW_ACP.isCartPage) return updateBigCartView(resp);
 
 var __cartObj = win.$_(aw_cartDivClass);
 
 if(!__cartObj) return false;
 
 
 if (typeof(__cartObj.length) == 'number') __cartObj = __cartObj[0];
 var oldHeight = __cartObj.offsetHeight;
 
 var tmpDiv = win.document.createElement('div');
 tmpDiv.innerHTML = resp.cart;
 
 var tmpParent = __cartObj.parentNode;
 tmpParent.replaceChild(tmpDiv.firstChild, __cartObj);
 
 /* Details popup support */
 
 var __cartObj = win.$_(aw_cartDivClass);
 if (typeof(__cartObj.length) == 'number') __cartObj = __cartObj[0];
 var newHeight = __cartObj.offsetHeight;
 
 if (aw_ajaxcartpro_cartanim == 'opacity'){
 $_(__cartObj).mutate(
 {opacity:0}, 
 {opacity:1},
 30
 );
 }
 if (aw_ajaxcartpro_cartanim == 'grow'){
 $_(__cartObj).style.overflow = 'hidden';
 $_(__cartObj).mutate( 
 {opacity:0, height: oldHeight + 'px'},
 {opacity:1, height: newHeight + 'px'},
 30
 );
 }
 if (aw_ajaxcartpro_cartanim == 'blink'){
 $_(__cartObj).mutate(
 {opacity:0}, 
 {opacity:1},
 10
 );
 }
 updateDeleteLinks();
 updateTopLinks(resp);
}

window.updateBigCartView = function (resp){
 
 $_(aw_bigCartClass).innerHTML = resp.cart
 if($_('#shopping-cart-table')){
 decorateTable('shopping-cart-table')
 }

 
 updateDeleteLinks();
 updateTopLinks(resp);
 updateAddLinks();
 
 
 var scripts = resp.cart.match(/<script[^>]*>([^<]+)<\/script>/gim);
 for(var i=0; i<scripts.length; i++){
 var code = scripts[i].match(/<script[^>]*>([^<]+)<\/script>/im)[1].replace(/var\s+/g, '');
 
 try{ 
 eval(code)
 }catch(e){
 }
 }
 
}


function ajaxcartprodelete(url){ 
 showProgressAnimation();
 url = getCommonUrl(url)
 $_(url).GET(
 function(resp) {
 if (typeof(resp) == 'string') eval('resp = ' + resp);
 hideProgressAnimation();
 updateCartView(resp, '');
 }
 );
}

function showProgressAnimation(){
 var pW = 400;
 var pH = 200;
 p = win.$_('.ajaxcartpro_progress');
 if(typeof p.length == 'number'){
 p = p[0]
 }
 p.style.width = pW + 'px'; 
 p.style.height = pH + 'px';
 if ($_.ie && !navigator.appVersion.match("8")){
 p.style.position = 'absolute';
 window.ACPTop = 200;
 }
 if (aw_ajaxcartpro_proganim == 'center'){ 
 if (!($_.ie && !navigator.appVersion.match("8"))){
 p.style.top = (screen.height/2) - (pH) + 'px';
 }else{
 window.ACPTop = 200;
 }
 }
 if (aw_ajaxcartpro_proganim == 'top'){ 
 if (!($_.ie && !navigator.appVersion.match("8"))){
 p.style.top = '0px';
 }else{
 // IE7-
 window.ACPTop = 0;
 }
 }
 if (aw_ajaxcartpro_proganim == 'bottom'){
 
 p.style.bottom = '0px';
 }
 if (aw_ajaxcartpro_proganim != 'none'){
 p.style.display = 'block'; 
 }
 
}


function showConfirmDialog(){
 var pW = 400;
 var pH = 200;
 p = $_('.ajaxcartpro_confirm');
 p.style.width = pW + 'px'; 
 p.style.height = pH + 'px';
 
 if ($_.ie && !navigator.appVersion.match("8")){
 p.style.position = 'absolute';
 }else{
 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 hideProgressAnimation(){

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

if(!$_.ie6){
 window.onload = function(){
 updateAddLinks()
 
 // 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;
 } 
 
 // Test for minicart
 
 if((typeof aw_cartDivClass != 'undefined') && (!$_(aw_cartDivClass).empty() || ((typeof AW_ACP !== 'undefined') && AW_ACP.isCartPage))){
 updateDeleteLinks();
 }
 
 } 
}

function updateAddLinks(){
 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)";
 }
 }
}

function getCommonUrl(url){
 if(window.location.href.match('www.') && url.match('http://') && !url.match('www.')){
 url = url.replace('http://', 'http://www.');
 }else if(!window.location.href.match('www.') && url.match('http://') && url.match('www.')){
 url = url.replace('www.', '');
 }
 return url;
}

