/**
 * Netcompany 
 */

if(window.console === undefined){
	window.console = {};
	window.console.log = function(s){
		$("#debug").append("<div>"+s.toString()+"</div>");
	}
}
var App = {
    Cache: {
        data: {},
        get: function (key) {
            //alert(key)
            return this.data[key];
        },
        set: function (key, value) {
            this.data[key] = value;
        }
    },
    SlideDeck: {
        init: function () {
            // initialze the slideshow that will automatically change the image every 6 seconds (interval is set in init())
            App.SlideDeck.SlideShow.init();

            // mark the first linkitem as selected
            this.markAsSelected(0);

            // hover function for all buttons
            var handleHover = function (e) {
                // next three lines copied from jQuery.hover, ignore children onMouseOver/onMouseOut
                var p = (e.type == "mouseover" ? e.fromElement : e.toElement) || e.relatedTarget;
                while (p && p != this) { try { p = p.parentNode; } catch (e) { p = this; } }
                if (p == this) { return false; }
                if (e.type == "mouseover") {
                    if (!$(this).hasClass("selected")) {
                        $(this).css("background-position", "0 0");
                    }
                } else {
                    if (!$(this).hasClass("selected")) {
                        $(this).animate({
                            backgroundPosition: "(0px 62px)"
                        }, 80, "linear", function () { });
                    }
                }
            };

            // add events to all linkitems
            var hrefs = [], titles = [], targets = [];
            $("#customerMenu a").each(function (i) {
                hrefs.push($(this).attr("href"));
                titles.push($(this).attr("alt"));
                targets.push($(this).attr("target"));
                $(this).data("index", i);
                $(this).click(function (e) {
                    if (!App.SlideDeck.Animation.animating) {
                        App.SlideDeck.Animation.goto(i);
                        //this.focus(); // maybe not?
                        try {
                            pageTracker._trackEvent('Forsideanimation', 'Click', ($(this).data("index") + 1));
                        } catch (e) { }
                        e.preventDefault();
                    }
                }).mouseover(handleHover).mouseout(handleHover);
            });

            // default read more text
            var defaultReadMoreText = $("#customerLinkDescription").html();
            $("#customers .customer").each(function (i) {
                $(this).data("href", hrefs[i]);
                $(this).data('target', targets[i]);
                $(this).click(function () {
                    if ('_blank' == $(this).data("target"))
                        window.open($(this).data("href"));
                    else
                        location.href = $(this).data("href");
                }).hover(
					function () {
					    $("#customerLinkDescription").stop().animate({ top: "0px" }, 500, "linear");
					    $("#customerLinkDescription").html(titles[i] || defaultReadMoreText);
					},
					function () {
					    $("#customerLinkDescription").stop().animate({ top: "-36px" }, 500, "linear", function () { $("#customerLinkDescription").html(defaultReadMoreText); });
					});
            });
        },
        // helper method to hide all linkitems
        hideAll: function () {
            $("#customers .customer").each(function (i) {
                if (i !== 0) {
                    $(this).addClass("hidden");
                }
            });
        },
        markAsSelected: function (index) {
            $("#customerMenu a").eq(index).addClass("selected");
            $("#customerMenu a").eq(index).css("background-position", "0px 0px");
        },
        setSelectedButton: function () {
            this.markAsSelected(App.SlideDeck.Animation.current);
            if (App.SlideDeck.Animation.previous != App.SlideDeck.Animation.current) {
                $("#customerMenu a").eq(App.SlideDeck.Animation.previous).animate({
                    backgroundPosition: "(0px 62px)"
                }, 200, "linear", function () {
                    $(this).removeClass("selected");
                });
            }
        },
        Animation: {
            animating: false,
            current: 0,
            previous: 0,
            goto: function (next) {
                //console.log(next)
                var fixPoints = [0, -954, -1908, -2862];
                App.SlideDeck.Animation.animating = true;
                App.SlideDeck.Animation.previous = App.SlideDeck.Animation.current;
                App.SlideDeck.Animation.current = App.SlideDeck.Animation.resolveNext(next);
                App.SlideDeck.setSelectedButton(next);
                $("#customersMaskSlider").animate({
                    left: fixPoints[next] + "px"
                }, 500, "swing", function () {
                    App.SlideDeck.Animation.animating = false;
                });
            },
            resolveNext: function (next) {
                //console.log("#"+next);
                return (next == 4 ? 0 : next);
            }
        },
        SlideShow: {
            timer: null,
            init: function () {
                this.timer = new KeepTime(6, function () { var next = App.SlideDeck.Animation.resolveNext(parseInt(App.SlideDeck.Animation.current) + 1); $("#customerMenu a").eq(next).click(); });
                this.timer.startTimer();
                // create event that monitors when to reset the SlideShow timer. 
                $("#customers").mousemove(function () {
                    App.SlideDeck.SlideShow.timer.restartTimer();
                });
            }
        }
    },
    UI: {
        init: function () {
            App.UI.TipAFriend.init();
            App.UI.NewsLetterSignup.init();
            $(".jCubes a").jCubes({ speed: 200, direction: "lr" });
            $(".toggleDefaultValue").jToggleValue();
        },
        Corners: {
            init: function () {
                var decorator = function () {
                    var floating = "none ",
                        $this = $(this);

                    if ($this.hasClass("left")) {
                        floating = "left ";
                    }
                    if ($this.hasClass("right")) {
                        floating = "right ";
                    }
                    $this.wrap("<span class='" + floating + "corners corners-white' style='width:" + $this.width() + "px; height:" + $this.height() + "px;'></span>");
                    $this.after("<img src='/images/corners/corner_se_white.png' class='corner se' /><img src='/images/corners/corner_sw_white.png' class='corner sw' /><img src='/images/corners/corner_ne_white.png' class='corner ne' /><img src='/images/corners/corner_nw_white.png' class='corner nw' />");
                };

                $("img.corners.corners-white").each(decorator);
                $("img.auto-corners").each(decorator);

                //console.log($("img.auto-corners"));
            }
        },
        NewsLetterSignup: {
            init: function () {
                $("#tilmeld").click(function () {
                    var doAnimation = function () {
                        $(".newsletter label").removeClass("notvalid");
                        $("#hiddenNewsletterFields").show("fast");
                    };
                    //if($("#layout-frontpage").length > 0){
                    //	$.scrollTo("max",200,{margin:true,onAfter:function (){
                    //		doAnimation();
                    //	}});
                    //} else {
                    doAnimation();
                    //}
                });
                $("#frameld").click(function () {
                    $(".newsletter label").removeClass("notvalid");
                    $("#hiddenNewsletterFields").hide("fast");
                });
                $("#btnSendNewsletterSignup").click(function (event) {
                    // field values
                    var cbSignupState = $("input:radio[name=cbNewsletterSignup]:checked").val();
                    var txtSignupName = $("#txtSignupName").val();
                    var txtSignupFirm = $("#txtSignupFirm").val();
                    var txtSignupEmail = $("#txtSignupEmail").val();

                    var postVars = [cbSignupState, txtSignupName, txtSignupFirm, txtSignupEmail];

                    var validForm = true;
                    if (cbSignupState === undefined) {
                        validForm = false;
                        $(".newsletter label").addClass("notvalid");
                    } else {
                        $(".newsletter label").removeClass("notvalid");
                    }
                    function resetNewsletterForm() {
                        $("#signup,#signoff,#hiddenNewsletterFields,#newsletterSignupFeedback").hide();
                        $("input:radio[name=cbNewsletterSignup]:checked").attr("checked", false);
                        $("#txtSignupName,#txtSignupFirm,#txtSignupEmail").val("").blur();
                    }
                    function showStatusMessage() {
                        if (cbSignupState === "tilmeld") {
                            $("#signup").show();
                            $("#signoff").hide();
                        } else if (cbSignupState === "frameld") {
                            $("#signup").hide();
                            $("#signoff").show();
                        }
                    }
                    var yourEmailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(txtSignupEmail);
                    if (txtSignupEmail === "" || !yourEmailRegex) {
                        validForm = false;
                        $("#txtSignupEmail").addClass("notvalid");
                    } else {
                        $("#txtSignupEmail").removeClass("notvalid");
                    }
                    if (cbSignupState === "tilmeld") {
                        if (txtSignupName === "" || txtSignupName === $("#txtSignupName").attr("defaultValue")) {
                            validForm = false;
                            $("#txtSignupName").addClass("notvalid");
                        } else {
                            $("#txtSignupName").removeClass("notvalid");
                        }
                        if (txtSignupFirm === "" || txtSignupFirm === $("#txtSignupFirm").attr("defaultValue")) {
                            validForm = false;
                            $("#txtSignupFirm").addClass("notvalid");
                        } else {
                            $("#txtSignupFirm").removeClass("notvalid");
                        }
                    }
                    if (validForm) {
                        // ajax call back handler
                        function onSuccess(msg) {
                            if (msg === "1") {
                                setTimeout(function () {
                                    $("#newsletterSignupLoading").fadeOut("slow", function () {
                                        showStatusMessage();
                                        $("#newsletterSignupThankYou").fadeIn("fast", function () {
                                            setTimeout(function () {
                                                $("#newsletterSignupForm").fadeIn("fast");
                                                resetNewsletterForm();
                                            }, 2000);
                                        });
                                    });
                                }, 2000);
                            } else {
                                setTimeout(function () {
                                    $("#newsletterSignupLoading").fadeOut("slow", function () {
                                        $("#newsletterSignupError").fadeIn("fast", function () {
                                            setTimeout(function () {
                                                $("#newsletterSignupForm").fadeIn("fast");
                                                resetNewsletterForm();
                                            }, 2000);
                                        });
                                    });
                                }, 2000);
                            }
                        }
                        $.ajax({ type: "POST", url: "/newsletterSignup.aspx", data: postVars.join("|"), success: onSuccess });
                        $("#newsletterSignupForm").fadeOut("slow", function () {
                            $("#newsletterSignupFeedback").show();
                            $("#newsletterSignupLoading").fadeIn("fast");
                        });
                    }
                    event.preventDefault();
                });
            }
        },
        TipAFriend: {
            init: function () {
                App.Cache.set("tipAFriendState", 0);
                $("#mail").click(function (event) {
                    if (!$(this).data("disabled")) {
                        $("#tipAFriendWrapper").show();
                        var top = App.Cache.get("tipAFriendState") === 0 ? "0" : "-326";
                        var speed = App.Cache.get("tipAFriendState") === 0 ? 400 : 200;
                        $("#tipAFriend").animate({
                            top: top
                        }, speed, "linear", function () {
                            if (App.Cache.get("tipAFriendState") == 1) {
                                $("#tipAFriendWrapper").hide();
                            }
                            var openClosed = App.Cache.get("tipAFriendState") === 0 ? 1 : 0;
                            App.Cache.set("tipAFriendState", openClosed);
                        });
                    }
                    event.preventDefault();
                });
                $("#tipAFriendClose").click(function (event) {
                    $("#tipAFriend").animate({
                        top: "-326"
                    }, 200, "linear", function () { App.UI.TipAFriend.hide(); });
                    event.preventDefault();
                });
                $("#tipAFriendSend").click(function (event) {
                    this.blur();
                    $("#mail").data("disabled", true);
                    var elms = [$("#tipAFriend_yourname"), $("#tipAFriend_youremail"), $("#tipAFriend_friendsname"), $("#tipAFriend_friendsemail"), $("#tipAFriend_comment").val()];
                    var postVars = [$("#tipAFriend_yourname").val(), $("#tipAFriend_youremail").val(), $("#tipAFriend_friendsname").val(), $("#tipAFriend_friendsemail").val(), $("#tipAFriend_comment").eq(0).val()];
                    //console.log(postVars)
                    var validForm = true;
                    var yourEmailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(postVars[1]);
                    if (postVars[1] === "" || !yourEmailRegex) {
                        validForm = false;
                        $(elms[1]).addClass("notvalid");
                    } else {
                        $(elms[1]).removeClass("notvalid");
                    }
                    var friendEmailRegex = /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i.test(postVars[3]);
                    if (postVars[3] === "" || !friendEmailRegex) {
                        validForm = false;
                        $(elms[3]).addClass("notvalid");
                    } else {
                        $(elms[3]).removeClass("notvalid");
                    }
                    if (validForm) {
                        // ajax call must be here and on succes go further
                        function onSuccess(msg) {
                            if (msg === "1") {
                                //console.log("mail sent!")
                            } else {
                                //console.log("miserable mail failure!")
                            }
                        }
                        $.ajax({ type: "POST", url: "/tipenven.aspx", data: postVars.join("|"), success: onSuccess });
                        $("#tipAFriendSend").fadeOut();
                        $("#tipAFriendClose").fadeOut();
                        $("#tipAFriend").animate({
                            top: "-283"
                        }, 400, "linear", function () {
                            $("#tipAFriendLoading").fadeIn();
                        });
                        var finish = setTimeout(function () {
                            $("#tipAFriendLoading").animate({
                                opacity: "0"
                            }, 1000, "linear", function () {
                                $(this).hide();
                                $("#tipAFriendThankYou").animate({
                                    left: "30"
                                }, 200, "linear", function () {
                                    setTimeout(function () {
                                        $("#tipAFriend").animate({
                                            top: "-332"
                                        }, 300, "linear", function () { App.UI.TipAFriend.hide(); });
                                    }, 2000);
                                });
                            })
                        }, 2000);
                    }
                    event.preventDefault();
                });
            },
            hide: function () {
                App.Cache.set("tipAFriendState", 0);
                $("#mail").data("disabled", false);
                $("#tipAFriendWrapper").hide();
                $("#tipAFriendSend").show();
                $("#tipAFriendClose").show();
                $("#tipAFriendThankYou").css("left", "200px");
                $("#tipAFriendLoading").css("opacity", 100);
                $("#tipAFriendLoading").hide();
            }
        }
    },
    begin: function () {
        if ($("#layout-frontpage").length > 0 || $("#layout-frontpage-mobile").length > 0) {
            App.SlideDeck.init();
        }
        App.UI.init();
        /*
        $("a[rel=lightbox],a.lightbox").lightBox({
        imageLoading: '/images/lightbox/loading.gif',
        imageBtnClose: '/images/lightbox/close.gif',
        imageBtnPrev: '/images/lightbox/prev.gif',
        imageBtnNext: '/images/lightbox/next.gif',
        containerResizeSpeed: 350
        });
        */
        $("#print").click(function (e) {
            window.print();
            e.preventDefault();
        });
    }
};

function KeepTime(seconds,callBack){
	var internalCounter = seconds;
	var internalInterval = null;
	var seconds = seconds;
	var that = this;
	
	function countDown(){
		internalCounter--;
		//console.log("Count: " + internalCounter)
		if(internalCounter === 0){
			callBack();
			that.restartTimer();
		}
	};
	this.startTimer  = function (){
		internalCounter = seconds;
		internalInterval = setInterval(countDown,1000);
	};
	this.stopTimer = function(){
		clearInterval(internalInterval);
	};
	this.restartTimer = function (){
		this.stopTimer();
		this.startTimer();
	};
} 
// fire the app
$(App.begin);

// the corners on the images must be added on wndow.onload instead of DOMReady because the full image must be loaded before trying to position the corner-images correctly
$(window).load(App.UI.Corners.init);



