var $j = jQuery.noConflict();
var t = null;
var cartopen = false;
var shoppingToteOpen = false;
var navigatingShoppingTote = false;
var clickEventSet = false;

function ajaxFunction() {
    var xmlHttp;

    try {
        xmlHttp = new XMLHttpRequest();
    }
    catch (e) { 
        try {
            xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
        }
        catch (e) {
            try {
                xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
            }
            catch (e) {
                alert("Your browser does not support AJAX!");
                return false;
            }
        }
    }
    return xmlHttp;
}

function showMiniCart() { 
    var minicartcontainer = document.getElementById('mini-cart-container'); 
      
    var xmlHttp = ajaxFunction();
    var params = '';

    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4) {

            if (document.all) {
                var result=xmlHttp.responseText;
                 result=result.replace(/<[//]{0,1}(form|Form)[^><]*>/g, ""); 
                 minicartcontainer.innerHTML = result;
            }
            else
            {
                minicartcontainer.innerHTML = xmlHttp.responseText;
            } 
            
            down = false;
        }
    }
  
    xmlHttp.open("POST", "/minicart.aspx", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);

    return;
}

function processPromoCode(type) {
    var minicartcontainer = document.getElementById('mini-cart-container'); 
    var promo = document.getElementById('tbPromoCode');
    var xmlHttp = ajaxFunction();
    var params;
    if(type == 'apply')
    {
        params = 'applypromo=1&pcode=' + promo.value;
    }
    else if(type == 'remove'){
        params = 'applypromo=2';
    }
    xmlHttp.onreadystatechange = function() {

        if (xmlHttp.readyState == 4) {
                if (document.all) {
                var result=xmlHttp.responseText;
                    result=result.replace(/<[//]{0,1}(form|Form)[^><]*>/g, ""); 
                    minicartcontainer.innerHTML = result;
            }
            else
            {
                minicartcontainer.innerHTML = xmlHttp.responseText;
            }
        }
    }
  
    xmlHttp.open("POST", "/minicart.aspx", true);
    xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
    xmlHttp.setRequestHeader("Content-length", params.length);
    xmlHttp.setRequestHeader("Connection", "close");
    xmlHttp.send(params);

    if(window.location.href.toString().toUpperCase().indexOf("SHOPPINGCART.ASPX") > 0){
        window.location.href = "/store/os/shoppingcart.aspx";
    }
}

function trigger_cartajax() {
    showMiniCart();
    setClickEvent();
    navigatingShoppingTote = true;
    t = setTimeout(function() { closeCart(); }, 5000);
}

function closeCart() {
    J("#slidingTopWrap").fadeTo(0, 1, function(){
        navigatingShoppingTote = false;
        setClickEvent();
    });
}

function openTote() {
    shoppingToteOpen = true;
    $j("#slidingTopContent").show("blind", { direction: "vertical" }, 600);
    
    if (typeof document.body.style.maxHeight == "undefined") {
        hideSelects('hidden');
    }
}

function closeTote() {
    if (navigatingShoppingTote == false) {
        $j("#slidingTopContent").hide("blind", { direction: "vertical" }, 10);
        shoppingToteOpen = false;
    }
    navigatingShoppingTote = false;

    if (typeof document.body.style.maxHeight == "undefined") {
        hideSelects('visible');
    }
}

function hideSelects(action) {
    if (action!='visible'){action='hidden';}
        if (navigator.appName.indexOf("MSIE")) {
        for (var S = 0; S < document.forms.length; S++){
            for (var R = 0; R < document.forms[S].length; R++) {
            if (document.forms[S].elements[R].options) {
                document.forms[S].elements[R].style.visibility = action;
                }
            }
        }
    }
}

function setClickEvent() {
  
    if (!navigatingShoppingTote) {
        if (shoppingToteOpen == true)
            closeTote();
        else
            openTote();
    }
    else {
        clearTimeout(t);
    }
}

function setMouseLeave() {
    $j("#shopping-cart-content").mouseleave(function() {
        if (shoppingToteOpen == true)
            setTimeout(closeTote, 100);
    });
}

$j(document).ready(function() {
    setMouseLeave();
    $j("#slidingTopContent").hide();
    showMiniCart();
    $j('#shopping-cart-content').unbind("click").click(function() {
        window.location = "/store/os/shoppingcart.aspx";
    });

    $j("div.enterleave").mouseenter(function() {
        navigatingShoppingTote = true;       
    }).mouseleave(function() {
        navigatingShoppingTote = false;        
    });   
     
});

$j(document).click(function() {
    $j("#slidingTopContent").slideUp("slow", function() {
        $j("#slidingTopContent").hide();
    });
}); 

