/*





























































*/
Prototype.Browser.IE6 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 6;
Prototype.Browser.IE7 = Prototype.Browser.IE && parseInt(navigator.userAgent.substring(navigator.userAgent.indexOf("MSIE")+5)) == 7;
Prototype.Browser.IE8 = Prototype.Browser.IE && !Prototype.Browser.IE6 && !Prototype.Browser.IE7;

window.QCTop = 200;
 
if(!Prototype.Browser.IE6){

    setLocation = function(url){
        if(window.location.href.match('https://') && !url.match('https://')){
            url = url.replace('http://','https://')
        }
         if(MSPL_QC.isCartPage && ((url.search('/add') != -1 ) || (url.search('/remove') != -1 )) ){
            ajaxquickcartsend(url+'msplqc/1/is_checkout/1', 'url', '', '');
        }else if (url.search('checkout/cart/add') != -1){
            ajaxquickcartsend(url+'msplqc/1', 'url', '', '');
        }else if (url.search('wishlist/index/cart') != -1){
            ajaxquickcartsendwishlist(url+'msplwishl/1/msplqc/1', 'url', '', '');
        }else{
            window.location.href = url;
        }
    }
}


if(!Prototype.Browser.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 '+mspl_addToCartButtonClass).each(function(el){
                        el.setAttribute('type', 'button')
                    })
				}catch(err){
					
				}
				productAddToCartForm.submit = function(url){
					if(this.validator && this.validator.validate()){					
						ajaxquickcartsend('?msplqc=1', 'form', this, '');
					}
					return false;
				}

                productAddToCartForm.form.onsubmit = function() {
                    productAddToCartForm.submit();
                    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 mspl_cartDivClass!= 'undefined' && $$(mspl_cartDivClass).length || ((typeof MSPL_QC !== 'undefined') && MSPL_QC.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();

        if (url[url.length-1] == '/') delim = '';
        else delim = '/';

        if (window.opener.location.pathname.search('checkout/cart') == -1)
            window.opener.ajaxquickcartsend(url+delim+'msplqc/1', 'url', '');
        else
            window.opener.ajaxquickcartsend(url+delim+'msplqc/1/is_checkout/1', 'url', '');
	}
	else{
		if(setFocus) {
			window.opener.focus();
		}
        window.opener.location.href = url;
	}
}

function ajaxquickcartsendwishlist(url, type, obj){
    url = getCommonUrl(url);
    showProgressAnimation();
    new Ajax.Request(url, {
          onSuccess: function(resp){
                try{
                    if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
				}catch(e){
					win.location.href=url;
					hideProgressAnimation();
					return;
				}
                hideProgressAnimation();
				if (resp.r != 'success'){
                    win.location.href=url;
                }
				else{
					if(MSPL_QC.useConfirmation){	
						showConfirmDialog();	
					}
					__onQCRender();
					updateCartView(resp);
                    updateTopLinks(resp);
                    updateWishlist(resp);
                    updateWishlistTopLinks(resp)
				}
			}
        });
}

function ajaxquickcartsend(url, type, obj){
    url = getCommonUrl(url)
	
	showProgressAnimation();
	if (type == 'form'){		
		$('product_addtocart_form').action += url;		

        $('product_addtocart_form').request({
            onComplete:  function(resp){

                if (typeof(resp.responseText) == 'string'){
					try{
						eval('resp = ' + resp.responseText);
					}catch(e){
						return obj.form.submit();
					}
				}
				hideProgressAnimation();
				if (resp.r != 'success'){
					obj.form.submit();
				}
				else{
					__onQCRender();
                    if(MSPL_QC.useConfirmation && (url.search('is_checkout/1') != 1)){
						showConfirmDialog();
					}
					updateCartView(resp);
				}
			}
        })

	}
	if (type == 'url'){
		new Ajax.Request(url, {
          onSuccess: function(resp){
				try{
					if (typeof(resp.responseText) == 'string') eval('resp = ' + resp.responseText);
				}catch(e){
					win.location.href=url;
					hideProgressAnimation();
					return;
				}
				hideProgressAnimation();
                if (resp.r != 'success'){
					win.location.href=url;
				}
				else{		
                    if(MSPL_QC.useConfirmation && (url.search('is_checkout/1') == -1)){
						showConfirmDialog();	
					}
					__onQCRender();
					updateCartView(resp);
				}
			}
        });

	}
}

function __onQCRender(){
    if(MSPL_QC.onRender && MSPL_QC.onRender.length){
	$A(MSPL_QC.onRender).each(function(h){h(MSPL_QC)})
    }
}

function addEffectQC(obj, effect)
{
    if (effect == 'opacity'){
        $(obj).hide();
        new Effect.Appear(obj);

	}
	if (effect == 'grow'){
        $(obj).hide();
        new Effect.BlindDown(obj);
	}
	if (effect == 'blink'){
        new Effect.Pulsate(obj);
	}
}




function updateTopLinks(resp){
    if($$(mspl_topLinkCartClass).length){
        $$(mspl_topLinkCartClass)[0].title = $$(mspl_topLinkCartClass)[0].innerHTML = resp.links;
    }
}

function updateWishlistTopLinks(resp){
    if($$(mspl_topWishlistLinkCartClass).length){
        $$(mspl_topWishlistLinkCartClass)[0].innerHTML = resp.wishlist_links;
    }
}

window.updateBigCartView = function (resp){
	
    $$(mspl_bigCartClass)[0].innerHTML = resp.cart
	if($('shopping-cart-table')){
		decorateTable('shopping-cart-table')
	}

    updateDeleteLinks();
	updateTopLinks(resp);
	updateAddLinks();
	
	
	var scripts = resp.cart.match(/<script[^>]*>([^<]+)<\/script>/gim);
    if (scripts)
    {
        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 showProgressAnimation(){
	var pW = 260;
	var pH = 50;
	var p = $$('.quickcart_progress')[0];
	
	p.style.width = pW + 'px';		
	p.style.height = pH + 'px';
	if (Prototype.Browser.IE && !navigator.appVersion.match("8")){
		p.style.position = 'absolute';
		window.QCTop = 200;
	}
	if (mspl_quickcart_proganim == 'center'){		
		if (!(Prototype.Browser.IE && !navigator.appVersion.match("8"))){
			p.style.top = (screen.height/2) - (pH) + 'px';
		}else{
		    window.QCTop = 200;
		}
	}
	if (mspl_quickcart_proganim == 'top'){		
		if (!(Prototype.Browser.IE && !navigator.appVersion.match("8"))){
		    p.style.top = '0px';
		}else{
		     // IE7-
		    window.QCTop = 0;
		}
	}
	if (mspl_quickcart_proganim == 'bottom'){
		
		p.style.bottom = '0px';
	}
	if (mspl_quickcart_proganim != 'none'){
		p.style.display = 'block';	
	}
	
}

var beginCounter;
Event.observe(window, 'load',
      function() 
      {
        if(typeof $$('#QCcountdown')[0] != 'undefined')
            beginCounter = parseInt($$('#QCcountdown')[0].innerHTML);
      }
    );


function showConfirmDialog(){
    var pW = 260;
	var pH = 104;
	var p = $$('.quickcart_confirm')[0];
	p.style.width = pW + 'px';		
	p.style.height = pH + 'px';
	
    if (Prototype.Browser.IE && !navigator.appVersion.match("8")){
		p.style.position = 'absolute';
	}else{
		p.style.position = 'fixed';
		if (mspl_quickcart_proganim == 'center'){		
			p.style.top = (screen.height/2) - (pH) + 'px';
		}
		if (mspl_quickcart_proganim == 'top'){		
			p.style.top = '0px';
		}
		if (mspl_quickcart_proganim == 'bottom'){
			p.style.bottom = '0px';		
		}
        if (mspl_quickcart_proganim == 'none'){
			p.style.top = (screen.height/2) - (pH) + 'px';
		}
	}
	p.style.display = 'block';

    var QCcountdown = $$('#QCcountdown')[0];
    if(typeof QCcountdown != 'undefined')
    {
        QCcountdown.innerHTML = beginCounter;
        if (typeof __intId3 != 'undefined') clearInterval(__intId3);
        __intId3 = setInterval(
            function(){
                if ( parseInt(QCcountdown.innerHTML) ){
                    QCcountdown.innerHTML = parseInt(QCcountdown.innerHTML)-1;
                }
                else
                { 
                    clearInterval(__intId3);
                    p.style.display = "none";
                    QCcountdown.innerHTML = beginCounter;
                }

            },
            1000
        );
    }
}

function hideProgressAnimation(){

	$$('.quickcart_progress')[0].style.display = 'none';
}

if(!Prototype.Browser.IE6){
	window.onload = function(){
		updateAddLinks()
		
		$('mspl_qc_continue').onclick = function(e){
			e = e||event;
			if(e.preventDefault)
				e.preventDefault()
			$$('.quickcart_confirm')[0].style.display='none';return false;
		}
		
		$('mspl_qc_checkout').onclick = function(e){
			$$('.quickcart_confirm')[0].style.display='none';return true;
		}	
		if((typeof mspl_cartDivClass != 'undefined') && ($$(mspl_cartDivClass).length || ((typeof MSPL_QC !== 'undefined') && MSPL_QC.isCartPage))){
			updateDeleteLinks();
		}
		
	} 
}

function updateAddLinks(){
	var qws = document.links;
	for (i=qws.length-1; i>=0; i--){
		if (qws[i].href.search('checkout/cart/add') != -1){
			qws[i].onclick = function(link){
				return function(){
					setLocation(link)
				}
			}(qws[i].href);
			qws[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;
}

