﻿var shownindex = 0;
var refindex = 0;

function startStopScroll(stop) {
    if (stop == true) {
        document.body.scroll = "no";
        document.body.style.overflow = 'hidden';
        scroll(0, 0);
    }
    else {
        document.body.scroll = "yes";
        document.body.style.overflow = 'scroll';
    }
}

function makeMail(deel1, deel2, naam) {
    var name = deel1;
    var domain = deel2;
    document.write('<a href=\"mailto:' + name + '@' + domain + '\">');
    document.write(naam + '</a>');
} 

function scrollToPhoto(index) {
    clearInterval(intBanner);
    var distance = 0;

    if (shownindex < index) {
        distance = (index - shownindex) * 250;
        Scroll('scrollContainer', -distance);
    }
    else {
        distance = (shownindex - index) * 250;
        Scroll('scrollContainer', distance);
    }

    for (var i = 0; i < 10; ++i) {
        var temp = document.getElementById("number-" + i);
        if (temp != null) {
            temp.className = "Number";
        }
    }
    
    var nmb = document.getElementById("number-" + (index + 1));
    if (nmb != null) {
        nmb.className = "NumberActive";
    }
    
    shownindex = index;
}

function scrollToReference(index, id) {
    clearInterval(intBanner);
    var distance = 0;

    if (refindex < index) {
        distance = (index - refindex) * 935;
        ScrollHorizontal("scrollContainerRef" + id, -distance);
    }
    else {
        distance = (refindex - index) * 935;
        ScrollHorizontal("scrollContainerRef" + id, distance);
    }

    refindex = index;
}

function scrollToNextReference(id) {
    clearInterval(intBanner);

    var max = document.getElementById("hfMaxRef" + id);
    if (max != null) {
        if (refindex < max.value - 1)
            scrollToReference(refindex+1, id);
        else
            scrollToReference(0, id);
            
    }
}


var m = 0;
function Scroll(controlId, distance) {
    $("#" + controlId).stop(true,true);
    calculateMargin(controlId);
    $("#" + controlId).animate({ marginTop: (m + distance) + "px" }, 1250);
}

function calculateMargin(controlId) {
    m = Number($("#" + controlId).css("margin-top").replace('px', ''));
    if (isNaN(m)) {
        m = 0;
    }
    return true
}

function ScrollHorizontal(controlId, distance) {
    // $("#" + controlId).marginLeft = $("#" + controlId).marginLeft + distance;
    var m = Number($("#" + controlId).css("margin-left").replace('px', ''));
    if (isNaN(m)) {
        $("#" + controlId).stop(true, true).animate({ marginLeft: (distance) + "px" }, 1250);
    }
    else {
        $("#" + controlId).stop(true, true).animate({ marginLeft: (m + distance) + "px" }, 1250);
    }
}

var intBanner = setInterval("showNextBanner()", 7500);

function showNextBanner() {
    var max = document.getElementById("hfMax");
    if (max != null) {
        if (shownindex < max.value)
            scrollToPhoto(shownindex + 1);
        else
            scrollToPhoto(0);
        intBanner = setInterval("showNextBanner()", 7500);
    }
}

function gotoOrderPage(from) {
    window.location = "http://www.webfabriek.be/OrderForm.aspx?from=" + from;
}

function animateHeight(id, newheight){
    $('#' + id).animate({ height: newheight + "px" }, 1500);
    var acntrl = document.getElementById(id + "Anchor");
    if (acntrl != null) {
        acntrl.style.visibility = "hidden";
    }

    var hcntrl = document.getElementById(id + "H1");
    if (hcntrl != null) {
        hcntrl.style.height = "20px";
    }

    var heightcntrl = document.getElementById(id + "Height");
    if (newheight > 0) {
        if (hcntrl != null) {
            hcntrl.onclick = function() { animateHeight(id, '0'); }
        }
    }
    else {
        if (hcntrl != null && heightcntrl != null) {
            if (acntrl != null) {
                acntrl.style.visibility = "visible";
            }
            hcntrl.onclick = function() { animateHeight(id, heightcntrl.value); }
        }
    }
        
}

var grayUrl = "/Gray.ashx?file=";
var menuFxDuration = 250;
var pnlCallMeStatus = 0;


$(document).ready(function() {
    
    $("img").each(function() {
        //var className = &(this).attr("class");
        //if(className != null && className != "png" && ){
        var className = $(this).attr("class");
        var src = $(this).attr("src");
        if (className != "png" && className != "color" && src.indexOf(".png") < 0) {
            //alert(className);
            var src = $(this).attr("src");
            if (src.substring(0, 1) == "/") {
                src = src.substring(1, src.length);
            }
            $(this).attr("src", grayUrl + src);
            $(this).mouseover(function() {
                $(this).attr("src", $(this).attr("src") + "&c=1");
            }).mouseout(function() {
                $(this).attr("src", $(this).attr("src").replace("&c=1", ""));
            });
        }
        else {
            //alert(src);
        }
    });

    $("a").each(function() {
        //var className = &(this).attr("class");
        //if(className != null && className != "png" && ){
        var className = $(this).attr("class");
        var href = $(this).attr("href");
        if (className == "" && href != null) {
            if (href.substring(0, 4) == "http" && href.substring(0, 21) != "http://www.webfabriek" && href.substring(0, 17) != "http://webfabriek") {
                //alert(href.substring(0, 21));
                $(this).attr("class", "external");
                //$(this).attr("target", "_blank");
            }
        }
        else {
            //alert(src);
        }
    });

    $('.Advies').mouseover(function() {
        $(this).animate({ left: "36px", width: "189px", height: "39px" }, menuFxDuration);
    }).mouseout(function() {
        $(this).animate({ left: "40px", width: "184px", height: "31px" }, menuFxDuration);
    });

    $('.Websites').mouseover(function() {
        $(this).animate({ left: "220px", width: "189px", height: "39px" }, menuFxDuration);
    }).mouseout(function() {
        $(this).animate({ left: "224px", width: "184px", height: "31px" }, menuFxDuration);
    });

    $('.Applicaties').mouseover(function() {
        $(this).animate({ left: "404px", width: "189px", height: "39px" }, menuFxDuration);
    }).mouseout(function() {
        $(this).animate({ left: "408px", width: "184px", height: "31px" }, menuFxDuration);
    });

    $('.Video').mouseover(function() {
        $(this).animate({ left: "588px", width: "189px", height: "39px" }, menuFxDuration);
    }).mouseout(function() {
        $(this).animate({ left: "592px", width: "184px", height: "31px" }, menuFxDuration);
    });

    $('.Samenwerken').mouseover(function() {
        $(this).animate({ left: "772px", width: "205px", height: "39px" }, menuFxDuration);
    }).mouseout(function() {
        $(this).animate({ left: "776px", width: "200px", height: "31px" }, menuFxDuration);
    });

    $('#aCallMeTEST').click(function() {
        if (event.preventDefault)
            event.preventDefault();
        event.returnValue = false;
        if (pnlCallMeStatus == 0) {
            $("#pnlCallMe").animate({ padding: "7px", top: "-124px", height: "110px", right: "-10px" }, 1000);
            pnlCallMeStatus = 1;
        }
        else {
            $("#pnlCallMe").animate({ padding: "0px", top: "-20px", height: "0px", right: "-10px" }, 1000);
            pnlCallMeStatus = 0;
        }
        return false;
    });

    var popup = document.getElementById("popup")

    $("a.iframe").fancybox({
        'transitionIn': 'elastic',
        'transitionOut': 'elastic',
        'speedIn': 1000,
        'speedOut': 700,
        'overlayShow': true,
        'width': 760,
        'height': 484,
        'scrolling': 'no',
        'showCloseButton': true,
        'hideOnContentClick': false,
        'onClosed': function() {
            $('#popup').css('visibility', 'visible');
            startStopScroll(false);
        },
        'onStart': function() {
            $('#popup').css('visibility', 'visible');
            startStopScroll(true);
        }
    });
    if (popup != null) {
        $("a.iframe").trigger('click');
        startStopScroll(true);

        //        $(window).scroll(function() {
        //            //            $('#popup').animate({ bottom: "-25px"}, 100, function() {
        //            //                $('#popup').animate({ bottom: "0px"}, 100);
        //            //            });

        //            var ScrollTop = document.body.scrollTop;

        //            if (ScrollTop == 0) {
        //                if (window.pageYOffset)
        //                    ScrollTop = window.pageYOffset;
        //                else
        //                    ScrollTop = (document.body.parentElement) ? document.body.parentElement.scrollTop : 0;
        //            }

        //            $('#popup').css('bottom', '-' + ScrollTop + 'px');
        //            //alert(document.body.scrollTop);
        //        });
    }
});

function animateMenuItem(id) {
    $("." + id).switchClass(id, id + 'Active', 250);
}

function animateMenuItemOut(id) {
    $("." + id + 'Active').switchClass(id + 'Active', id, 250);
}

function showCallMe() {
    if (pnlCallMeStatus == 0) {
        $("#pnlCallMe").animate({top: "-124px", height: "120px", right: "-10px" }, 1000);
        pnlCallMeStatus = 1;
    }
    else {
        $("#pnlCallMe").animate({top: "-7px", height: "0px", right: "-10px" }, 1000);
        pnlCallMeStatus = 0;
    }
}


function number_format(a, b, c, d) {
    a = Math.round(a * Math.pow(10, b)) / Math.pow(10, b);
    e = a + '';
    f = e.split('.');
    if (!f[0]) {
        f[0] = '0';
    }
    if (!f[1]) {
        f[1] = '';
    }
    if (f[1].length < b) {
        g = f[1];
        for (i = f[1].length + 1; i <= b; i++) {
            g += '0';
        }
        f[1] = g;
    }
    if (d != '' && f[0].length > 3) {
        h = f[0];
        f[0] = '';
        for (j = 3; j < h.length; j += 3) {
            i = h.slice(h.length - j, h.length - j + 3);
            f[0] = d + i + f[0] + '';
        }
        j = h.substr(0, (h.length % 3 == 0) ? 3 : (h.length % 3));
        f[0] = j + f[0];
    }
    c = (b <= 0) ? '' : c;
    return f[0] + c + f[1];
}

/*Google maps*/

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var co2bike = 0;
var co2train = 40.28;
var co2bus = 66.63;
var co2car = 168.60;
var addr = "";
var street;
var place;
var postal;

function initialize() {
    directionsDisplay = new google.maps.DirectionsRenderer();
    
    var geocoder = new google.maps.Geocoder();
    var myOptions = {
        zoom: 14,
        mapTypeId: google.maps.MapTypeId.ROADMAP
    }
    map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
    directionsDisplay.setMap(map);
    
    if (geocoder) {
        geocoder.geocode({ 'address': "De Webfabriek, kloosterstraat 85, 2180 ekeren, Belgie" }, function(results, status) {
            if (status == google.maps.GeocoderStatus.OK) {
                map.setCenter(results[0].geometry.location);
                var marker = new google.maps.Marker({
                    map: map,
                    position: results[0].geometry.location,
                    title: "De Webfabriek"
                });
            } else {

            }
        });
    }
}

function onKeyChangeCalculate(e) {
    //event.preventDefault();
    var ev = (window.event) ? window.event : e;
    var getEvent = ev.keyCode;
    if (getEvent == "13") {
        //alert("Enter pressed in route form");
        checkSubmit = false;
        calculateRoute();
        ev.cancelBubble = true;
        ev.returnValue = false;
        //ev.preventDefault();
        return false;
     }
     else {
        //alert("Firefox error");
        return true;
    }
    
    //return !(window.event && window.event.keyCode == 13);
 }

 function ie8SafePreventEvent(e) {
     if (e.preventDefault) { e.preventDefault() }
     else { e.stop() };

     e.returnValue = false;
     e.stopPropagation();
 }

 var canSubmit = true;
 function checkSubmit() {
     if (canSubmit) {
         //alert("Submitting...");
         return true;
     }
     else {
         //alert("Trying to cancel submit...");
         canSubmit = true;
         return false;
     }
 }
 
 function disableForm(theform) {
    if (document.all || document.getElementById) {
        for (i = 0; i < theform.length; i++) {
            var tempobj = theform.elements[i];
            if (tempobj.type.toLowerCase() == "submit" || tempobj.type.toLowerCase() == "reset")
            tempobj.disabled = true;
        }
        setTimeout('alert("Your form has been submitted.  Notice how the submit and reset buttons were disabled upon submission.")', 2000);
        return true;
        }
        else {
            alert("The form has been submitted.  But, since you're not using IE 4+ or NS 6, the submit button was not disabled on form submission.");
            return false;
    }
}

function calculateRoute() {
    street = document.getElementById("txtStreet");
    place = document.getElementById("txtPlace");
    postal = document.getElementById("txtPostcode");
    var lblError = document.getElementById("lblError");
    
    if (street != null && place != null & postal != null && lblError != null) {
        addr = street.value.replace(" ", "+");
        if (postal.value.length > 1)
            addr = addr + "+" + postal.value;
        if (place.value.length > 1)
            addr = addr + "+" + place.value;
            
        var request = {
            origin: street.value + ", " + postal.value + " " + place.value,
            destination: "De Webfabriek, kloosterstraat 85, 2180 ekeren, Belgie",
            travelMode: google.maps.DirectionsTravelMode.DRIVING
        };
        directionsService.route(request, function(result, status) {
            if (status == google.maps.DirectionsStatus.OK) {
                lblError.style.visibility = "hidden"
                directionsDisplay.setDirections(result);
                setRouteInfo(result);
            }
            else {
                lblError.style.visibility = "visible"
                $("#barTrain").animate({ width: "2px" }, 500);
                $("#barBus").animate({ width: "2px" }, 500);
                $("#barCar").animate({ width: "2px" }, 500, function() {
                    $("#pnlCalculateRoute").animate({ height: "150px" }, 500);
                });
            }
        });
    }
}

function setRouteInfo(result) {
    var lblTrain = document.getElementById("lblTrain");
    var lblBus = document.getElementById("lblBus");
    var lblCar = document.getElementById("lblCar");
    var distanceInMeters = result.routes[0].legs[0].distance.value;
    if (lblTrain != null && lblBus != null & lblCar != null & distanceInMeters > 0) {
        var distanceInKM = distanceInMeters / 1000;
        var train = Math.round(distanceInKM * co2train);
        var bus = Math.round(distanceInKM * co2bus);
        var car = Math.round(distanceInKM * co2car);
        lblTrain.innerHTML = number_format(train,0,","," ") + " gram";
        lblBus.innerHTML = number_format(bus, 0, ",", " ") + " gram";
        lblCar.innerHTML = number_format(car, 0, ",", " ") + " gram";
        var trainpx = Math.round((train / car) * 100);
        var buspx = Math.round((bus / car) * 100);
        var now = new Date();
        var googlemapsPrintPage = "http://maps.google.be/maps?f=d&source=s_d&saddr=" + addr + "&daddr=De+Webfabriek,+kloosterstraat+85,+2180+ekeren,+Belgie&geocode=FVzjDgMduIZDACk3i1LEAAnERzFVZ13MbP7MgA;FayADgMdz2dDACHfb7xOgSB6hymtOkIAyQnERzGO40Jb2YmWvA&hl=nl&mra=ls&sll=51.29472,4.418392&sspn=0.036228,0.071068&ie=UTF8&z=14&pw=2";
        var googlemapsWalkPrintPage = "http://maps.google.be/maps?f=d&source=s_d&saddr=" + addr + "&daddr=De+Webfabriek,+kloosterstraat+85,+2180+ekeren,+Belgie&geocode=FVzjDgMduIZDACk3i1LEAAnERzFVZ13MbP7MgA;FayADgMdz2dDACHfb7xOgSB6hymtOkIAyQnERzGO40Jb2YmWvA&hl=nl&mra=ls&dirflg=w&sll=51.29472,4.41925&sspn=0.036228,0.071068&ie=UTF8&z=14&pw=2";
        var nmbsToPlace = "";
        if (place.value != null && place.value.length > 1)
            nmbsToPlace = place.value;
        var nmbsLink = "http://hari.b-holding.be/Hafas/bin/query.exe/nn?&REQ0JourneyStopsS0A=1&REQ0JourneyStopsS0G=" + nmbsToPlace + "&REQ0JourneyStopsZ0A=1&REQ0JourneyStopsZ0G=Ekeren+[B]&REQ0JourneyDate=0" + now.getDate() + ".0" + now.getMonth() + ".10&REQ0JourneyTime=" + now.getHours() + ":" + now.getMinutes() + "&Timesel=depart&ViaName=&ViaMode=NEE&DateMode=ANDERS&PLANNER=TRUE&start=1&queryPageDisplayed=yes";
        var awRoute = document.getElementById("aWRoute");
        awRoute.href = googlemapsWalkPrintPage
        var acRoute = document.getElementById("aCRoute");
        acRoute.href = googlemapsPrintPage
        var atRoute = document.getElementById("aTRoute");
        atRoute.href = nmbsLink
        $("#barTrain").animate({ width: "2px" }, 500);
        $("#barBus").animate({ width: "2px" }, 500);
        $("#barCar").animate({ width: "2px" }, 500, function() {
            $("#pnlCalculateRoute").animate({ height: "150px" }, 500, function() {
                $("#pnlCalculateRoute").animate({ height: "270px" }, 500, function() {
                    $("#barTrain").animate({ width: trainpx + "px" }, 1500);
                    $("#barBus").animate({ width: buspx + "px" }, 2000);
                    $("#barCar").animate({ width: "100px" }, 2500);
                });    
            });
        });
    }
}