﻿
var _cartVisible = false;



function ToggleCartOpen(open) {
    _cartVisible = open;
}

function UpdateCartInfo(transport) {
    if (location.href.indexOf("CheckOut") > 0)
        location.reload();
    else
        LoadCartInfo();
}

function UpdateCartAmounts() {
    var productString = "";
    var inputs = document.getElementById("cart-table").getElementsByTagName("input");
    for (var i = 0; i < inputs.length; i++) {
        if (inputs[i].getAttribute("type") == "hidden") {
            if (inputs[i + 1].value != "0")
                productString += inputs[i].value + ",";
        }        
        else if (inputs[i].getAttribute("type") == "text") {
            if (!isNaN(inputs[i].value)) {
                if (parseInt(inputs[i].value) > 0)                // FIXA
                    productString += inputs[i].value + "!";
            }
        }
    }
    
//    alert(productString);
    
    // Schparen kundvagnus
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "UpdateCart",
            productString: productString,
            rnd: Math.round()
        },
        onSuccess: UpdateCartInfo,
        onFailure: function(){ alert('Fel uppstod') }
    });    
}

function RemoveProduct(productID) {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "RemoveProduct",
            productID: productID
        },
        onSuccess: UpdateCartInfo,
        onFailure: function(){ alert('Fel uppstod') }
    });  
}

function ToggleSumVisibility(show) {
    var sum = document.getElementById("cart-flyout-sum");
    sum.style.visibility = (show ? "visible" : "hidden");
}

function SetCartInfo(transport) {
    var flyout = document.getElementById("cart-flyout");
    flyout.innerHTML = transport.responseText;
    ToggleCartOpen(true);
    
    var sum = document.getElementById("sum-value").innerHTML;
    var amount = document.getElementById("total-amount-value").value;
    SetTopCart(amount, sum);
    
    if (amount == "0")
    {
        document.getElementById("Template_Cart_cart_icon_disabled").style.display = "block";
        document.getElementById("Template_Cart_cart_icon").style.display = "none";
    }
    else
    {
        document.getElementById("Template_Cart_cart_icon_disabled").style.display = "none";
        document.getElementById("Template_Cart_cart_icon").style.display = "block";
    }
    
    // Blinka summa
//    setTimeout("ToggleSumVisibility(false)", 500);
//    setTimeout("ToggleSumVisibility(true)", 700);
//    setTimeout("ToggleSumVisibility(false)", 1200);
//    setTimeout("ToggleSumVisibility(true)", 1400);    
}

function LoadCartInfo() {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "GetCartInfo"
        },
        onSuccess: SetCartInfo,
        onFailure: function(){ alert('Fel uppstod') }
    });
}

function ShowCartMessage(show, productID) {
    document.getElementById("cart-update-" + productID).style.visibility = (show ? "visible" : "hidden");
    if (show)
        setTimeout("ShowCartMessage(false, '" + productID + "')", 5000);
}

function SetTopCart(amount, sum) {
    document.getElementById("cart-amount").innerHTML = amount;
    document.getElementById("cart-sum-text").innerHTML = sum;
}

function UpdateCart(transport) {
    if (transport.responseText.length > 0) {
        var arr = GetCallbackArray(transport.responseText);
        SetTopCart(arr[0], arr[1]);
        ShowCartMessage(true, arr[2]);
        UpdateCartInfo(transport);
    }
}

function UpdateWishList(transport) {
    document.getElementById("Template_Cart_wishlist_icon").style.display = "block";
    alert("Produkten lades till i din önskelista.\n\nDu kan redigera, skriva ut och e-posta listan från länken Önskelista ovan.");
}    

function AddToCart(productID, amount, reload) {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "AddToCart",
            productID: productID,
            amount: amount,
            rnd: Math.round()
        },
        onSuccess: UpdateCart,
        onFailure: function(){ alert('Fel uppstod') }
    });
    return false;
}

function AddToWishList(productID, amount) {
    new Ajax.Request("/shop/Events.aspx", {
        method: "get",
        parameters: {
            func: "AddToWishList",
            productID: productID,
            amount: amount,
            rnd: Math.random()
        },
        onSuccess: UpdateWishList,
        onFailure: function(){ alert('Fel uppstod: AddToWishList') }
    });
    return false;
}

function PrintWishList()
{
	var x = screen.width, y = screen.height;
	var width = 660, height = 830;
	window.open("WishListPrint.aspx", "WishListEmail", "top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",height=" + height + ",scrollbars=1, status=1, resizable=0");
}

function EmailWishList()
{
	var x = screen.width, y = screen.height;
	var width = 400, height = 360;
	window.open("WishListEmail.aspx", "WishList", "top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",height=" + height + ",scrollbars=1, status=1, resizable=0");
}

function OpenProductLink(list) {
    var url = list.options[list.selectedIndex].value;
    if (url.length > 0) {
        if (url.indexOf("http://") < 0)
            url = "http://" + url;

        if (url.indexOf(".wmv") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "wmp"
            });
        } else if (url.indexOf(".mov") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "qt"
            });
        } else if (url.indexOf(".flv") > 0) {
            Shadowbox.open({
                content: url,
                width: 400,
                height: 300,
                player: "flv"
            });
        } else if (url.indexOf(".jpg") > 0 || url.indexOf(".gif") > 0 || url.indexOf(".png") > 0) {
            Shadowbox.open({
                content: url
            });
        }
        else
            window.open(url);
    }
}

function ShowInPanel(link) {
    var img = link.firstChild;
    if (img) {
        document.getElementById("mainImage").src = img.src.replace("thumb", "standard");
        document.getElementById("mainImage").onclick = function(e) { popImage(img.src.replace("thumb", "big"), "Produktbild"); };
    }
}

function ToggleListView(showImages) {
    var url = location.href.replace("?display=1", "").replace("&display=1", "").replace("?display=2", "").replace("&display=2", "");
    if (!showImages) {
        if (location.href.indexOf("?") > -1)
            location.href = url + "?display=2";
        else
            location.href = url + "&display=2";
    } else {
        if (location.href.indexOf("?") > -1)
            location.href = url + "?display=1";
        else
            location.href = url + "&display=1";
    }
}

function findPosX(obj)
{
var curleft = 0;
if(obj.offsetParent)
    while(1) 
    {
      curleft += obj.offsetLeft;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.x)
    curleft += obj.x;
return curleft;
}

function findPosY(obj)
{
var curtop = 0;
if(obj.offsetParent)
    while(1)
    {
      curtop += obj.offsetTop;
      if(!obj.offsetParent)
        break;
      obj = obj.offsetParent;
    }
else if(obj.y)
    curtop += obj.y;
return curtop;
}

function ToggleListImage(icon, prodID, show) {
    var img = document.getElementById("listProductImage");
    if (show) {
        img.src = "/img/products/" + prodID + "/product-" + prodID + "-thumb.jpg";
        var posX = (findPosX(icon) - 230 + (120 - img.width));
        
        if (navigator.userAgent.indexOf("MSIE 6") > -1)
            posX -= 110;
        else if (navigator.userAgent.indexOf("MSIE 7") > -1)
            posX += 20;
        else if (navigator.userAgent.indexOf("Safari") > -1)
            posX += 30;
        
        img.style.left = posX + "px";
//        document.getElementById("debug1").value = navigator.userAgent;
        img.style.top = (findPosY(icon) - 70) + "px";
        img.style.display = "block";
    } else {
       img.style.display = "none";
       img.src = "/gfx/px.gif";
    }
}

function CompareProducts(containerID) {
    var c = document.getElementById(containerID);
    var boxes = c.getElementsByTagName("input");
    var url = "/shop/CompareProducts.aspx?pid=";
    var a = 0;
    
    for (var i = 0; i < boxes.length; i++) {
        if (boxes[i].getAttribute("type") == "checkbox") {
            if (boxes[i].checked) {
                url += boxes[i].id.replace("compare_", "") + ";";
                a++;
            }
        }
    }
    
    if (a > 0) {
	    var x = screen.width, y = screen.height;
	    var width = 750, height = 600;
	    window.open(url, "CompareProducts", "top=" + parseInt(y/2-height/2-16) + ",left=" + parseInt(x/2-width/2-5) + ",width=" + width + ",height=" + height + ",scrollbars=1, status=1, resizable=1");
    } else
        alert("Markera de produkter du vill jämföra.");
}