//Project:E-trade
var dataset;
Start = function() {
    foutje.info.fstart = 0;
    w.plantconnect = new function() {
        foutje.info.plantconnectfunctie = 0;
        var 
			t = this,
			bepaalId = function(e) { return e.id.na('_') },
			pasMenuAan = function(MenuObj) {
			    var divmidden = MenuObj.childNodes[1],
					divboven = d.cE("div"),
					divonder = d.cE("div"),
					divmenu = d.cE("div");
			    zetClassAan(divmenu, "hovermenu");
			    zetClassAan(divmidden, "hovermenumidden");
			    zetClassAan(divboven, "hovermenuboven");
			    zetClassAan(divonder, "hovermenuonder");
			    divmenu.appendChild(divboven);
			    divmenu.appendChild(divmidden);
			    divmenu.appendChild(divonder);
			    MenuObj.appendChild(divmenu);

			};

        foutje.info.plantconnectfunctie = 1;

        t.veranderHoverMenu = function() {
            foutje.info.hoverfunctie = 0;
            var hoverhoofdmenu = d.E("hoverhoofdmenu");
            var hovermenu, menu;
            for (i = 0; i < hoverhoofdmenu.childNodes.length; i++) {
                menu = d.E("hoverhoofdmenu").childNodes[i].firstChild.firstChild;
                menu.style.zIndex = (20 - i);
                menu.childNodes.length > 1 ? pasMenuAan(menu) : "";
                foutje.info.hoverfunctie = 1;
            }
            foutje.info.hoverfunctie = 2;
        }

        foutje.info.plantconnectfunctie = 2;

        t.SetDatum = function(e) {
            foutje.info.setdatumfunctie = 0;
            var vrz = new HTTPVerzoek();
            vrz.URL.Args.Verzoek = 'Berichtdatum';
            vrz.URL.Args.pagina = Lokatie.Args.pagina;
            vrz.URL.Args.datum = e.value;
            vrz.Reactie = function() {
                if (this.Respons != 'True') {
                    alert('De datum is niet opgeslagen');
                }
            }
            vrz.Start();
            foutje.info.setdatumfunctie = 1;
        }

        foutje.info.plantconnectfunctie = 3;

        t.voorstelling = function() {

            foutje.info.voorstellingfunctie = 0;
            var dia = new diaVoorstelling();
            for (var i = 0; i < Math.min(dia.initDias, dataset.length); i++) {
                dia.voegproductToe(dataset[i]);
            }
            //849 ondervangen; gebruikers kunnen eerder wegnavigeren.
            setTimeout(function() { if (Lokatie.Args.pagina == 849) { dia.filterProducten(); dia.startDiavoorstelling(); } }, 2000);

            //new diaVoorstelling().startDiavoorstelling();
            foutje.info.voorstellingfunctie = 1;
        }
        foutje.info.plantconnectfunctie = 4;
    } //einde w.plantconnect
    foutje.info.fstart = 1;
    w.diaVoorstelling = function() {
        foutje.info.diavoorstellingfunctie = 0;
        var 
			t = this,
			SetOpacity = function(elem, opacityAsInt) {
			    var opacityAsDecimal = opacityAsInt;

			    if (opacityAsInt > 100)
			        opacityAsInt = opacityAsDecimal = 100;
			    else if (opacityAsInt < 0)
			        opacityAsInt = opacityAsDecimal = 0;

			    opacityAsDecimal /= 100;
			    if (opacityAsInt < 1)
			        opacityAsInt = 1; // IE7 bug, text smoothing cuts out if 0

			    elem.style.opacity = (opacityAsDecimal);
			    elem.style.filter = "alpha(opacity=" + opacityAsInt + ")";
			},
			FadeOpacity = function(elem, fromOpacity, toOpacity, time, fps) {
			    foutje.info.fade = 0;
			    var steps = Math.ceil(fps * (time / 1000));
			    var delta = (toOpacity - fromOpacity) / steps;
			    foutje.info.fade = 1;
			    t.FadeOpacityStep(elem, 0, steps, fromOpacity, delta, (time / steps));
			    foutje.info.fade = 2;
			},
			zetAfmeting = function(elIMG) {
			    if (elIMG.height > elIMG.width) {
			        elIMG.width = Math.round(elIMG.width * (155 / elIMG.height));
			        elIMG.style.marginLeft = (Math.round((155 - elIMG.width) / 2) + 'px');
			        elIMG.height = 155;
			    }
			    else {
			        elIMG.height = Math.round(elIMG.height * (155 / elIMG.width));
			        elIMG.style.marginTop = (Math.round((155 - elIMG.height) / 2)) + 'px';
			        elIMG.width = 155;
			    }
			},
			maakProductRegel = function(drager, titel, inhoud) {
			    var producttitel = d.cE("div"),
			        productinhoud = d.cE("div");

			    zetClassAan(producttitel, "ptitel");
			    zetClassAan(productinhoud, "pinhoud");
			    titel?producttitel.innerText = titel:'';
			    inhoud?productinhoud.innerText = inhoud:'';
			    drager.appendChild(producttitel);
			    drager.appendChild(productinhoud);
			},
			eersteDia = true,
            maakProduct = function(product) {
                var drager = d.E('producten_temp'),
			        nieuwproduct = d.cE("div"),
			        productimg = d.cE("div"),
			        productgegevens = d.cE("div"),
			        productomschrijving = d.cE("div"),
			        img = d.cE("img");

                zetClassAan(nieuwproduct, "product");
                zetClassAan(productomschrijving, "productomschrijving");
                productomschrijving.innerText = product.PRODUCTNAAM;
                img.src = "http://vmcab.florahollandetrade.com/VMCab/UploadedImages/" + product.IMAGE_REF;
               //Dat was: img.src = "http://212.178.98.110/VMCab/UploadedImages/" + product.IMAGE_REF;
                zetClassAan(productimg, "productIMG");
                zetClassAan(productgegevens, "productGegevens");
                productimg.appendChild(img);
                productgegevens.appendChild(productomschrijving);
                maakProductRegel(productgegevens, product.SORTK1_OMSCHR, product.SORTK1_WAARDE); 
                maakProductRegel(productgegevens, product.SORTK2_OMSCHR, product.SORTK2_WAARDE); 
                maakProductRegel(productgegevens, product.SORTK3_OMSCHR, product.SORTK3_WAARDE);
                maakProductRegel(productgegevens, product.LOGK1_OMSCH, product.LOGK1_WAARDE); 
                nieuwproduct.appendChild(productimg);
                nieuwproduct.appendChild(productgegevens);
                SetOpacity(nieuwproduct, 0);
                //eersteDia ? eersteDia = false : SetOpacity(nieuwproduct, 0);

                drager.appendChild(nieuwproduct);
            };

        foutje.info.diavoorstellingfunctie = 1;

        t.initDias = 6;
        t.productendoorlopen = t.initDias;

        t.voegproductToe = function(product) {
            maakProduct(product);
        }

        t.filterProducten = function() {
            if (Lokatie.Args.pagina == 849) {
                var producten_temp = d.E('producten_temp').childNodes,
                                quantprods = producten_temp.length,
						        el,
						        drager = d.E('producten'),
                                dragergeenfoto = d.E('producten_zonderFoto');
                //alert(producten_temp.length);
                for (var teller = 0; teller < quantprods; teller++) {
                    if (producten_temp[0]) {
                        el = producten_temp[0];
                        var img = el.firstChild.firstChild;
                        if (img.height < 31) {
                            //alert(img.height);
                            dragergeenfoto.appendChild(el);
                        }
                        else { zetAfmeting(img); drager.appendChild(el); }
                    }
                }
            }
        }

        t.zetAfbeeldingAfmetingen = function() {
            var quantElems = d.E('producten').childNodes.length,
						el;
            for (var teller = 0; teller < quantElems; teller++) {
                if (d.E('product' + (teller + 1))) {
                    el = d.E('product' + (teller + 1));
                    var img = el.firstChild.firstChild;
                    //img.onerror= function(){alert('jeAjoKje')};
                    if (img.height < 31) { el.firstChild.style.background = "url(GIF/defaultIMG.gif) no-repeat 0px 63px"; img.height = 0 + 'px'; img.width = 0 + 'px'; }
                    else { zetAfmeting(img); }
                }
            }
        }

        t.startDiavoorstelling = function() {
            if (Lokatie.Args.pagina == 849) {
                foutje.info.startdiafunctie = 0;
                var producten = d.E('producten')
                if (producten) {
                    foutje.info.startdiafunctieI = 0;
                    var max = producten.childNodes.length,
						    el;
                    //alert('max: ' + max);
                    for (var teller = 0; teller < max; teller++) {
                        if (producten.childNodes[teller]) {
                            el = producten.childNodes[teller];
                            //alert(max);
                            teller == 0 ? SetOpacity(el, 100) : SetOpacity(el, 0);
                        }
                    }
                    foutje.info.startdiafunctieI = 1;
                    max > 0 ? setTimeout(function() { t.wisselDia(0) }, 2000) : '';
                }
                foutje.info.startdiafunctie = 1;
            }
        }
        foutje.info.diavoorstellingfunctie = 2;

        t.FadeOpacityStep = function(elem, stepNum, steps, fromOpacity, delta, timePerStep) {
            foutje.info.fadestepfunctie = 0;
            if (elem) {
                SetOpacity(elem, Math.round(parseInt(fromOpacity) + (delta * stepNum)));
                if (stepNum < steps) {
                    foutje.info.fadestepfunctieI = 0;
                    //setTimeout("diaVoorstelling.FadeOpacityStep('" + elemId + "', " + (stepNum+1) + ", " + steps + ", " + fromOpacity + ", "+ delta + ", " + timePerStep + ")", timePerStep);
                    setTimeout(function() { t.FadeOpacityStep(elem, (stepNum + 1), steps, fromOpacity, delta, timePerStep) }, timePerStep);
                    foutje.info.fadestepfunctieI = 1;
                }
            }
            foutje.info.fadestepfunctie = 1;
        }

        foutje.info.diavoorstellingfunctie = 3;

        t.wisselDia = function(volgnummer) {
            if (Lokatie.Args.pagina == 849) {
                foutje.info.wisseldiafunctie = 0;
                foutje.info.timer = 0;

                if (t.productendoorlopen < dataset.length) {
                    t.filterProducten();
                    t.voegproductToe(dataset[t.productendoorlopen]);
                    t.productendoorlopen = t.productendoorlopen + 1;
                }

                if (t.productendoorlopen == dataset.length) { t.filterProducten(); t.productendoorlopen = t.productendoorlopen + 1; }

                var max = d.E('producten').childNodes.length;
                var el1, el2, volgnummer;
                var el1 = d.E('producten').childNodes[volgnummer];
                if ((volgnummer + 1) == max) {
                    volgnummer = 0;
                }
                else { volgnummer = volgnummer + 1; }
                el2 = d.E('producten').childNodes[volgnummer];
                foutje.info.timer = 1;
                FadeOpacity(el1, 100, 0, 1000, 12);
                FadeOpacity(el2, 0, 100, 1000, 12)

                foutje.info.timer = 1;

                //setTimeout('diaVoorstelling.wisselDia('+volgnummer+','+max+')',4000) kan ook. setTimeOut verwacht functie als string om uit te voeren.
                var timeout = setTimeout(function() { t.wisselDia(volgnummer) }, 4000)
                Lokatie.Args.pagina != 849 ? clearTimeout(timeout) : '';
                foutje.info.timer = 2
                foutje.info.wisseldiafunctie = 1;
            }
        }
        foutje.info.diavoorstellingfunctie = 4;
    } //einde w.diavoorstelling
    foutje.info.fstart = 2;
}//einde start



PaginaStart = function() {
    foutje.info.start = 0;
    if (d.E("hoverhoofdmenu")) plantconnect.veranderHoverMenu();
    var optioneel = d.E("p0");
    if (optioneel && optioneel.firstChild) (optioneel.firstChild.childNodes.length == 4) ? zetClassUit(d.E("rechts1"), 'verberg') : zetClassAan(d.E("rechts1"), 'verberg');
    optioneel = d.E("p1");
    if (optioneel && optioneel.firstChild) (optioneel.firstChild.childNodes.length == 4) ? zetClassUit(d.E("rechts2"), 'verberg') : zetClassAan(d.E("rechts2"), 'verberg');
    optioneel = d.E("homep0");
    if (optioneel && optioneel.firstChild) (optioneel.firstChild.childNodes.length == 4) ? zetClassUit(d.E("hometekstveld1"), 'verberg') : zetClassAan(d.E("hometekstveld1"), 'verberg');
    optioneel = d.E("homep1");
    if (optioneel && optioneel.firstChild) (optioneel.firstChild.childNodes.length == 4) ? zetClassUit(d.E("hometekstveld2"), 'verberg') : zetClassAan(d.E("hometekstveld2"), 'verberg');

    foutje.info.start = 1;

    if (Lokatie.Args.pagina == 849) {
        startscript('http://pipes.yahoo.com/pipes/pipe.run?_id=a56cc9932b0887dff295b6ea78c563bc&_render=json&_callback=verwerkItems');
    }
  
    foutje.info.start = 2;
} //einde paginastart

verwerkItems = function(o) {
    try {
        //alert(Deval(o.value.items[0].GRN_T_HIGHLIGHT));
        dataset = o.value.items[0].GRN_T_HIGHLIGHT;
        (dataset.length > 2) && (Lokatie.Args.pagina == 849) ? plantconnect.voorstelling() : '';
    }
    catch (e) { e instanceof Error ? mailerror(e) : ''; }
}

startscript = function(scripturl) {
    var scriptbestaat = d.E(scripturl);
    scriptbestaat ? scriptbestaat.parentNode.removeChild(scriptbestaat) : '';
    laadScript(scripturl);
}

mailerror = function(e) {
    var errormessage = 'error.name: ' + e.name + '<br> error.message:' + e.message + '<br>' + 'op pagina: ' + Lokatie.Args.pagina;
    var vrz = new HTTPVerzoek();
    vrz.URL.Args = { 'Verzoek': 'FOUT', 'pagina': Lokatie.Args.pagina, 'errormessage': errormessage };
    vrz.Reactie = function() { /*alert(this.Respons);*/ }
    vrz.Start();
}




