﻿var ObjectPath = new Array();
for (tggi=0;tggi<20;tggi++) ObjectPath[tggi] = '';

var CurrentDepth = 0;
var ElemFullName;
var ElemPrice = 0;
var CurrentNews = 0;
var CurrentShowProduct = 0;
var AddProduct_ProductModel = '';

ObjectPath[1] = 'root';

function isNumeric(value) {
    if (value == null || !value.toString().match(/^[-]?\d*\.?\d*$/)) return false;
    return true;
}

function roundVal(val) {
    var dec = 2;
    var result = Math.round(val * Math.pow(10, dec)) / Math.pow(10, dec);

    var text = result.toString();
    var sect = text.indexOf(".")+1;
    var subtext = text.substring(sect, text.length);

    if (subtext.length == 1) text = text = text + "0";
    
    return text;
}
function CountTotalPrice() {
    var Quantity = parseFloat(document.getElementById("ShoppingCartQuantity").value);
    var Price = parseFloat(document.getElementById("ShoppingCartPrice").innerHTML.replace(" €", ""));

    if ((Price == null) || (Quantity == null) || (Quantity < 1) || (document.getElementById("ShoppingCartQuantity").value == '') || (isNumeric(document.getElementById("ShoppingCartQuantity").value) == false)) {
        document.getElementById("ShoppingCartArrow").style.display = 'none';
        document.getElementById("ShoppingCartTotalPrice").innerHTML = "&nbsp;";
    } else {
        document.getElementById("ShoppingCartArrow").style.display = 'block';
        document.getElementById("ShoppingCartTotalPrice").innerHTML = roundVal((Quantity * Price)) + " €";
    }
}


function ShowMessage(text) {
    document.getElementById("Message").innerHTML = text;
    $('#Message').showTopbarMessage({ close: 5000 });
}

function ToggleGroup(ParentGroupID, Depth) {

    var obj = document.getElementById('acc' + ParentGroupID);
    if (obj != null) {
        if (obj.style.display == 'none') {
            var objCurrentMenu = document.getElementById("accselected" + ParentGroupID);
            objCurrentMenu.innerHTML = "";
            //eval('acci' + ParentGroupID).pr(-1);
            for (tggi = 0;tggi < Depth;tggi++) {
                var imgmp = document.getElementById('mp' + ObjectPath[tggi])
                if (imgmp != null) imgmp.src = '/images/plus.png';
            }
            for (tggi = Depth + 1; tggi < 20; tggi++) {
                if (ObjectPath[tggi] != '') {
                    fadeoff(ObjectPath[tggi]);
                    ObjectPath[tggi] = '';
                }
            }
            document.getElementById('mp' + ParentGroupID).src = '/images/minus.png';
        }
        else {
            document.getElementById('mp' + ParentGroupID).src = '/images/plus.png';
        }

        fadeoff('ShoppingCart');
        $("#SelectMore").hide();
        document.getElementById("ShoppingCartTotalPrice").innerHTML = ""
        document.getElementById("ShoppingCartQuantity").value = '';
        
        
        // Vaihda näkyvyyttä 
        fade2('acc' + ParentGroupID);
    }
}


function UpdateShoppingCart() {
    window.setTimeout("__doPostBack(UpdatePanel1ClientId, 'customPostback')", 1500);
    window.setTimeout('$("#MenuLeft").height($("#MenuRight").height())', 4000);
}

function AddProduct() {
   var QuantityElem = document.getElementById('ShoppingCartQuantity');
   if (QuantityElem != null) {
        var Quantity = QuantityElem.value;
        if (Quantity > 0) {
            QuantityElem.value = '';
            ShowMessage('<br>Ostoskoriin lisättiin ' + Quantity + ' kpl tuotetta: ' + ElemFullName);
            PageMethods.AddProduct(AddProduct_ProductModel, ElemFullName, Quantity, ElemPrice, CallSuccess, CallFailed);
            ToggleGroup('Root', 1);
        }
    }
    document.getElementById("ShoppingCartArrow").style.display = 'none';
    document.getElementById("ShoppingCartTotalPrice").innerHTML = '';

    $("#SelectMore").fadeIn('slow');
}

function CallSuccess(res) {
    __doPostBack(UpdatePanel1ClientId, 'customPostback');
    window.setTimeout('$("#MenuLeft").height($("#MenuRight").height())', 2000);
}

function CallFailed(res) {
   alert(res.get_message());
}


function Toggle(MenuId, Depth, EFullName, EPrice, ParentGroupID, ElemGroupID, EName, ProductPicture, ProductModel) {
   var hidemenu = false;
   var newdepth = Depth;
   var mainprefix = "";
   var prefix = "";

   for (tggi = (newdepth + 1); tggi < 20; tggi++) {
       var obj = document.getElementById(ObjectPath[tggi]);
       if (obj != null) fade(ObjectPath[tggi]);
       if (ObjectPath[tggi] != '') {
            fadeoff(ObjectPath[tggi]);
            ObjectPath[tggi] = '';
            fadeoff('ShoppingCart');
            $("#SelectMore").hide();
       }
       ObjectPath[tggi] = '';
   }
   
   
   
   for (tggi = 0; tggi < (newdepth+1);tggi++) {
        if (Depth == 1) fadeoff2('accRoot');
        if (ObjectPath[tggi] != '') {
            fadeoff2('acc' + ObjectPath[tggi]);
            var imgmp = document.getElementById('mp' + ObjectPath[tggi])
            if (imgmp != null) imgmp.src = '/images/plus.png';
            //window.alert(ObjectPath[tggi]);
        }
   }
   
   
   var objNextMenu = document.getElementById("accselected" + MenuId);
   if (objNextMenu != null) objNextMenu.innerHTML = "";

   var objCurrentMenu = document.getElementById("accselected" + ParentGroupID);
   objCurrentMenu.innerHTML = ": " + EName;

   
   

   var ShoppingCartEnabled = (document.getElementById('ShoppingCart').style.display == 'block');
   var obj = document.getElementById(MenuId);

  
   if (obj != null) {

       // Nollataan Accordio-valinta
       //eval('acci' + ElemGroupID).pr(-1);

       // Nollataan MenGroupin Fade (tuodaan menu näkyviin)
       resetfade2('acc' + ElemGroupID);

       // Nollataan plus/minus etumerkki
       var imgmp = document.getElementById('mp' + ElemGroupID)
       if (imgmp != null) imgmp.src = '/images/minus.png';

       resetfade(MenuId);
       window.setTimeout("fadeon('" + MenuId + "')", CurrentDepth >= newdepth ? 900 : 900);
       ObjectPath[Depth+1] = MenuId;
       if (ShoppingCartEnabled) {
           fade('ShoppingCart');
           document.getElementById("ShoppingCartTotalPrice").innerHTML = ""
           document.getElementById("ShoppingCartQuantity").value = '';
       }
   } else {
        if (!ShoppingCartEnabled) window.setTimeout("fade('ShoppingCart')", 800); 
        ElemFullName = EFullName;
        ElemPrice = EPrice;
        AddProduct_ProductModel = ProductModel;
        document.getElementById('ShoppingCartPrice').innerHTML = roundVal(EPrice) + " €";
        if (ProductPicture != "") {
            var imgObj = document.getElementById('ShoppingCartProductPicture');

            imgObj.style.display = 'block';
            imgObj.src = '/Images/' + ProductPicture;
        } else {
            imgObj.style.display = 'none';
        }
   }
   CurrentDepth = newdepth;


}






function GetNewsObjStr(number) {
    var st;
    st = "ctl00_dlNewsText_ctl0" + number + "_NewsTicker";
    return st;
}


function ShowNews(show) {
   fade(GetNewsObjStr(CurrentNews));
   if (show == false) {
       CurrentNews++;
       if (CurrentNews == MaxNews) CurrentNews=0;
   }
   if (show) window.setTimeout("ShowNews(false)",5000); else window.setTimeout("ShowNews(true)",1600);
}


function GetShowProductObjStr(number) {
    var st;
    st = "ctl00_MasterContentBody_dlProductTicker_ctl0" + number + "_ProductTicker";
    return st;
}

function ShowProduct(show) {
    fade(GetShowProductObjStr(CurrentShowProduct));
    if (show == false) {
        CurrentShowProduct++;
        if (CurrentShowProduct == MaxShowProducts) CurrentShowProduct = 0;
    }
    if (show) window.setTimeout("ShowProduct(false)", 10000); else window.setTimeout("ShowProduct(true)", 1600);
}



