﻿gallery = function (oConfig) {
    for (var c in oConfig)
        this[c] = oConfig[c];

    this.gCurrent = 0;
}

gallery.prototype =
{
    init: function () {
        //this.initControls();
        this.refreshThumbs();
        this.preloadThumbs();
    },

    initControls: function () {
        var self = this;
        this.$(this.next).onclick = function () {
            self.itemNext();
        }

        this.$(this.previous).onclick = function () {
            self.itemPrevious();
        }

        this.$(this.next).style.display = "block";
        this.$(this.previous).style.display = "block";
    },

    itemNext: function () {
        this.gCurrent -= 1;

        if (this.gCurrent < 0)
            this.gCurrent = 0;

        this.gItems.push(this.gItems.shift())

        this.refreshThumbs();
        this.refreshContentProd(0);

    },

    itemPrevious: function () {
        this.gCurrent += 1;

        var len = this.gItems.length;
        if (len > 3) len = 3;

        if (this.gCurrent > len)
            this.gCurrent = len;

        this.gItems.unshift(this.gItems.pop())

        this.refreshThumbs();
        this.refreshContentProd(0);
    },

    preloadThumbs: function () {
        /*var qty = this.querySt('qty');
        var id = this.querySt('productId');


        if (id != null && qty != null) {

        while (this.gItems[this.gCurrent].product_id != id && this.gItems[this.gCurrent] != null) {
        this.gCurrent = 1;
        this.gItems.push(this.gItems.shift());
        }
        this.refreshThumbs();
        this.displayQty(this.gCurrent);
        this.SetQty('qtyChoose' + this.gCurrent, qty);
        }*/
    },

    refreshThumbs: function () {
        for (var i = 0; i < this.displayLimit; i++) {
            if (this.$("gItem" + i)) {
                this.$("gItem" + i).self = null;
                this.$("gItem" + i).onmouseover = null;
            }
        }
        this.$(this.chooser).innerHTML = "";
        for (var i = 0; i < this.displayLimit; i++) {
            if (this.gItems[i]) {


                var cdiv = document.createElement("div");
                cdiv.width = this.thumbWidth;
                cdiv.self = this;
                cdiv.id = "galleryChooserContent" + i;

                cdiv.style.textAlign = "left";
                cdiv.className = "galleryChooserContent"
                cdiv.style.cssFloat = "left";
                cdiv.onclick = function () {
                    //this.self.displayQty(this.id.substr(21, 1));
                    this.self.refreshContentProd(this.id.substr(21, 1));
                }
                /*
                if(this.gItems[i].lot!="")
                {
                var cdivlot = document.createElement("div");
                cdivlot.width = this.thumbWidth;
                cdivlot.self = this;
                cdivlot.id  = "galleryChooserLot"+i;
                cdivlot.style.width = "32px";
                cdivlot.style.height = "64px";
                cdivlot.style.height = "64px";
                cdivlot.style.position="absolute";
                cdivlot.style.marginTop = "32px"; 
                //cdivlot.style="position:asbolute;width:64px;height:64px;";
                //"position:absolute;z-index:99;";
                var lotPicture = document.createElement("img");			
                lotPicture.src = 'images/produit/'+this.gItems[i].lang+'_lot'+ this.gItems[i].lot+'.gif'; 
                lotPicture.id = "imgLot" + i;
                lotPicture.self = this;
                lotPicture.width = 32;
                lotPicture.height = 32;
                cdivlot.appendChild(lotPicture);
                cdiv.appendChild(cdivlot);		
                }
                */
                var cdivcontent = document.createElement("div");
                cdivcontent.width = this.thumbWidth;
                cdivcontent.self = this;
                cdivcontent.id = "galleryChooserContainer" + i;

                cdivcontent.style.textAlign = "center";
                cdivcontent.style.cssFloat = "left";

                //productPicture.width = this.thumbWidth;
                //productPicture.height = 80;
                switch (i.toString()) {
                    case "0":
                        var productPicture = document.createElement("img");
                        productPicture.src = "/" + this.imagePath + this.gItems[i].lang + "_" + this.gItems[i].src;
                        productPicture.id = "gItem" + i;
                        productPicture.self = this;
                        var chemin = this.imagePath;
                        var image = this.gItems[i].src;
                        var choix = this.gItems[i].product_id;
                        var lang = this.gItems[i].lang;
                        productPicture.onmouseover = function () {
                            productPicture.src = "/" + chemin + lang + "_" + "over_" + image;
                        }
                        productPicture.onmouseout = function () {
                            productPicture.src = "/" + chemin + lang + "_" + image;
                        }
                        productPicture.onclick = function () {
                            popupConfigurator(choix);
                            //this.self.refreshContent(this.id);
                            //this.onclick.refreshContentProd(this.id);

                        }
                        cdiv.appendChild(productPicture);
                        break;
                    case "1":
                        var productPicture1 = document.createElement("img");
                        productPicture1.src = "/" + this.imagePath + this.gItems[i].lang + "_" + this.gItems[i].src;
                        productPicture1.id = "gItem" + i;
                        productPicture1.self = this;
                        var chemin1 = this.imagePath;
                        var image1 = this.gItems[i].src;
                        var choix1 = this.gItems[i].product_id;
                        var lang1 = this.gItems[i].lang;
                        productPicture1.onmouseover = function () {
                            productPicture1.src = "/" + chemin1 + lang1 + "_" + "over_" + image1;
                        }
                        productPicture1.onmouseout = function () {
                            productPicture1.src = "/" + chemin1 + lang1 + "_" + image1;
                        }
                        productPicture1.onclick = function () {
                            popupConfigurator(choix1);
                            //this.self.refreshContent(this.id);
                            //this.onclick.refreshContentProd(this.id);

                        }
                        cdiv.appendChild(productPicture1);
                        break;
                    case "2":
                        var productPicture2 = document.createElement("img");
                        productPicture2.src = "/" + this.imagePath + this.gItems[i].lang + "_" + this.gItems[i].src;
                        productPicture2.id = "gItem" + i;
                        productPicture2.self = this;
                        var chemin2 = this.imagePath;
                        var image2 = this.gItems[i].src;
                        var choix2 = this.gItems[i].product_id;
                        var lang2 = this.gItems[i].lang;
                        productPicture2.onmouseover = function () {
                            productPicture2.src = "/" + chemin2 + lang2 + "_" + "over_" + image2;
                        }
                        productPicture2.onmouseout = function () {
                            productPicture2.src = "/" + chemin2 + lang2 + "_" + image2;
                        }
                        productPicture2.onclick = function () {
                            popupConfigurator(choix2);
                            //this.self.refreshContent(this.id);
                            //this.onclick.refreshContentProd(this.id);

                        }
                        cdiv.appendChild(productPicture2);
                        break;
                    default:

                }



                /* var space =document.createElement("br");
                cdivcontent.appendChild(space);
                */
                var productDescription = document.createElement("span");
                //productDescription.innerHTML = "<BR/>"+this.gItems[i].id +"<BR/>minimum: "+this.gItems[i].min+"<BR/>"+ this.gItems[i].price + "€ "; 

                if (this.gItems[i].lot == "") {
                    productDescription.innerHTML = "<span class='galleryChooserTitle'>" + this.gItems[i].id + "<a href='javascript:popupHelp(\"" + this.gItems[i].product_id + "\");'><img src='/images/galerie/pastille-interrogation.jpg' class='galleryHelp'/></a></span>";
                }
                else {

                    productDescription.innerHTML = "<span class='galleryChooserTitle'>" + this.gItems[i].lotX + this.gItems[i].lot + "<a href='javascript:popupHelp(\"" + this.gItems[i].product_id + "\");'><img src='/images/galerie/pastille-interrogation.jpg' class='galleryHelp'/></a></span>";
                }
                "<br>";
                productDescription.style.width = this.thumbWidth;
                productDescription.style.textAlign = "center";
                cdivcontent.appendChild(productDescription);
                //<input type='radio' name='natureContrat' id='radio"+i+"' value='"+this.gItems[i].product_id+"' />
                var qtySelector = document.createElement("div");
                qtySelector.id = "radioContainer" + i;
                qtySelector.className = "radioContainer";



                var input = document.createElement("input");
                input.id = "radio" + i;
                input.type = "radio";
                input.name = "productNumber";
                input.className = "qtyInputRadio";
                input.self = this;
                input.value = this.gItems[i].product_id;
                input.onclick = function () {
                    this.self.refreshContentProd(this.id.substr(5, 1));
                }

                var qtyText = document.createElement("div");
                qtyText.id = "radioText" + i;
                qtyText.className = "galleryChooserMin";
                qtyText.innerHTML = "min: " + this.gItems[i].min;


                var qty = document.createElement("div");
                qty.id = "radioText" + i;



                var qtySwitch = document.createElement("div");
                qtySwitch.id = "qtySwitch" + i;
                qtySwitch.className = "qtySwitch";



                var qtyChoose = document.createElement("div");
                qtyChoose.id = "qtyChooseDiv" + i;
                qtyChoose.className = "qtyChoose";
                //qtyChoose.innerHTML = "<img src='images/galerie/qte-btn-moins.jpg'/><input id='qtyValue"+i+"'type='text' class='galleryChooserQty'/> <img src='images/galerie/qte-btn-plus.jpg'/>";

                var moins = document.createElement("img");
                moins.id = "minus" + i;
                moins.src = "/images/galerie/qte-btn-moins.jpg";
                moins.className = "qtyChooseMin";
                moins.self = this;
                moins.width = 20;
                moins.height = 19;
                moins.onclick = function () {
                    this.self.RemoveQty("qtyChoose" + this.id.substr(5, 1));

                }

                var plus = document.createElement("img");
                plus.src = "/images/galerie/qte-btn-plus.jpg";
                plus.className = "qtyChoosePlus";
                plus.id = "plus" + i;
                plus.self = this;
                plus.width = 20;
                plus.height = 19;

                plus.onclick = function () {
                    this.self.AddQty("qtyChoose" + this.id.substr(4, 1));

                }
                var qtyinput = document.createElement("input");
                qtyinput.id = "qtyChoose" + i;
                qtyinput.src = "/images/galerie/qte-middle.jpg";
                qtyinput.self = this;
                qtyinput.width = 31;
                qtyinput.height = 19;
                qtyinput.className = "galleryChooserQty"
                qtyinput.value = this.gItems[i].min;
                qtyinput.onblur = function () {
                    if (this.value > 0) {
                        var min = this.self.GetMin("qtyChoose" + this.id.substr(9, 1));

                        if (parseInt(this.value) >= parseInt(min)) {
                            this.self.refreshContentProd(this.id.substr(9, 1));
                        }
                        else {
                            this.self.SetToMin("qtyChoose" + this.id.substr(9, 1));
                        }

                    }
                    else {
                        this.self.SetToMin("qtyChoose" + this.id.substr(9, 1));
                    }


                }

                qtyChoose.appendChild(moins);
                qtyChoose.appendChild(qtyinput);
                qtyChoose.appendChild(plus);

                qtyChoose.style.display = "none";


                qtySelector.appendChild(input);
                //qtySwitch.appendChild(qtySelector);
                //qtySwitch.appendChild(qtyText);
                qtySwitch.appendChild(qtyChoose);

                cdivcontent.appendChild(qtySwitch);
                var price = document.createElement("div");
                price.id = "price" + i;
                price.className = "galleryChooserPrice";
                price.innerHTML = this.gItems[i].price + " € " + this.gItems[i].ttc;
                cdivcontent.appendChild(price);

                //cdiv.appendChild(cdivcontent);
                this.$(this.chooser).appendChild(cdiv);
            }
        }



    },

    querySt: function (ji) {
        hu = window.location.search.substring(1);
        gy = hu.split("&");
        for (i = 0; i < gy.length; i++) {
            ft = gy[i].split("=");
            if (ft[0] == ji) {
                return ft[1];
            }
        }
    },/*
    displayQty: function (id) {
        for (var counter = 0; counter <= 2; counter++) {
            if (counter == id) {
                this.$("qtyChooseDiv" + counter).style.display = "block";
                this.$("radioText" + counter).style.display = "none";
            }
            else {
                this.$("qtyChooseDiv" + counter).style.display = "none";
                this.$("radioText" + counter).style.display = "block";
            }
        }
    },*/

    SetToMin: function (id) {

        var activeID = id.substr(9, 1);
        this.$(id).value = this.gItems[activeID].min;
        this.refreshContentProd(activeID);
    },
    GetMin: function (id) {

        var activeID = id.substr(9, 1);
        return this.gItems[activeID].min
    },
    RemoveQty: function (id) {
        var activeID = id.substr(9, 1);

        min = this.gItems[activeID].min;
        if ((parseInt(this.$(id).value) - 1) >= min)
            this.$(id).value = parseInt(this.$(id).value) - 1;
        this.refreshContentProd(activeID);
    },
    AddQty: function (id, min) {

        this.$(id).value = parseInt(this.$(id).value) + 1;
        var activeID = id.substr(9, 1);
        this.refreshContentProd(activeID);
    },
    SetQty: function (id, value) {
        this.$(id).value = parseInt(value);
        var activeID = id.substr(9, 1);
        this.refreshContentProd(activeID);
    },
    refreshContent: function (id) {

        var activeID = (id) ? id : this.gCurrent;

        //	this.refreshContentProd: function(id) ;
        this.gCurrent = activeID;
    },

    refreshContentProd: function (id) {

        this.clearRadio();
        //this.$("ButtonAddToChart").style.display = "block";
        //this.$("ProductInfo").style.display = "block";
        //this.$("radio" + id).checked = true;
        //var qty = this.$("qtyChoose" + id).value;
        /*this.$("priceProduct").innerHTML = this.round_decimals(this.gItems[id].price.replace(",", "."), 2) + " &euro;";
        this.$("titleProduct").innerHTML = qty + " " + this.gItems[id].title;
        this.$("totalProduct").innerHTML = this.round_decimals((this.gItems[id].price.replace(",", ".") * qty), 2) + " &euro;";
        this.gCurrent = id;*/
        setPack(id);
        askColorsAndMessages();
    },
    round_decimals: function (original_number, decimals) {
        var result1 = original_number * Math.pow(10, decimals)
        var result2 = Math.round(result1)
        var result3 = result2 / Math.pow(10, decimals)
        return this.pad_with_zeros(result3, decimals)
    },
    pad_with_zeros: function (rounded_value, decimal_places) {
        var value_string = rounded_value.toString()
        var decimal_location = value_string.indexOf(".")
        if (decimal_location == -1) {
            decimal_part_length = 0
            value_string += decimal_places > 0 ? "." : ""
        }
        else {
            decimal_part_length = value_string.length - decimal_location - 1
        }
        var pad_total = decimal_places - decimal_part_length
        if (pad_total > 0) {
            for (var counter = 1; counter <= pad_total; counter++)
                value_string += "0"
        }
        return value_string
    }
    ,
    clearContent: function () {
        for (var i = 0; i < this.gItems.length; i++) {
            if (this.$(this.gItems[i].id) != null)
                this.$(this.gItems[i].id).style.display = "none";

        }
    },
    clearRadio: function () {
        for (var i = 0; i < this.gItems.length; i++) {
            if (this.$("radio" + i) != null)
                this.$("radio" + i).checked = false;

        }
    },


    $: function (n) {
        return document.getElementById(n);
    }
}


