



var sternschleuder = {};

sternschleuder.Website = function() {

    this.showVideo = false;
    this.flashRegistered = false;
    this.videoData;
    this.scrollAPI;

    var that = this;

    $.extend({
        getUrlVars: function() {
            var vars = [], hash;
            var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
            for(var i = 0; i < hashes.length; i++) {
              hash = hashes[i].split('=');
              vars.push(hash[0]);
              vars[hash[0]] = hash[1];
            }
            return vars;
        },
            getUrlVar: function(name) {
                return $.getUrlVars()[name];
            }


    });




    $(window).resize(function() {
        resizeContent();
        posLogo();
    });

    $(document).ready(function() {
        resizeContent();
        setBrowserDif();
        posLogo();
        setScrollPaneDynamicWidth();

        var root = "";
        if (Boolean($.getUrlVar('nonPublic')) === true) {
            root = "../";
        }
        if (Boolean($.getUrlVar('lightbox')) === true) {
            setLightbox(root);
        }
        if (Boolean($.getUrlVar('video')) === true) {
           setVideoPlayer(root);
        }

        YAHOO.plugin.SwapImage.bind(".swapImage", true, true, "mouseover", "mouseout");

    });


    function setLightbox(root) {
        $('.screenshots').each(
            function() {
                console.log("root 2: "+root);
                $(this).find('a').lightBox( {
                    overlayBgColor: '#2a79ea',
                    overlayOpacity: 1,
                    imageLoading: root+'image/lightbox-ico-loading.gif',
                    imageBtnClose: root+'image/lightbox-btn-close.gif',
                    imageBtnPrev: root+'image/lightbox-btn-prev.gif',
                    imageBtnNext: root+'image/lightbox-btn-next.gif',
                    containerResizeSpeed: 350,
                    txtImage: 'Image',
                    txtOf: '/'
                });
            });

    }



    // VIDEO PLAYER
    function setVideoPlayer(root) {
        
         // For version detection, set to min. required Flash Player version, or 0 (or 0.0.0), for no version detection.
        var swfVersionStr = "10.2.0";
        // To use express install, set to playerProductInstall.swf, otherwise the empty string.
        var xiSwfUrlStr = "";
        var flashvars = {};
        var params = {};
        params.quality = "high";
        params.bgcolor = "#2a79ea";
        params.allowscriptaccess = "always";
        params.allowfullscreen = "true";
        var attributes = {};
        attributes.id = "Sternschleuder_Video_Player";
        attributes.name = "Sternschleuder_Video_Player";
        attributes.align = "middle";
        swfobject.embedSWF(root+"swf/Sternschleuder_Video_Player.swf", "flashContent", "100%", "100%",
            swfVersionStr, xiSwfUrlStr,
            flashvars, params, attributes);
        // JavaScript enabled so display the flashContent div in case it is not replaced with a swf object.
        swfobject.createCSS("#flashContent", "display:block;text-align:left;");
    }


    this.trySetVideoInFlash = function() {
        if (this.flashRegistered && this.showVideo) {
            var ff = $("#Sternschleuder_Video_Player")[0]["setVideo"];
            if (ff != undefined) $("#Sternschleuder_Video_Player")[0]["setVideo"](this.videoData);
            this.showVideo = false;
        }
    };

    
    // VIDEO PLAYER END





    function setScrollPaneDynamicWidth() {
        $(function() {
            $('#contentToScroll').each(
                function() {
                    $(this).jScrollPane( {
                            showArrows: $(this).is('.arrow')
                        }
                    );
                    var api = $(this).data('jsp');
                    that.scrollAPI = api;
                    console.log(that.scrollAPI);
                    var throttleTimeout;
                    $(window).bind(
                        'resize',
                        function() {
                            if ($.browser.msie) {
                                // IE fires multiple resize events while you are dragging the browser window which
                                // causes it to crash if you try to update the scrollpane on every one. So we need
                                // to throttle it to fire a maximum of once every 50 milliseconds...
                                if (!throttleTimeout) {
                                    throttleTimeout = setTimeout(
                                        function() {
                                            api.reinitialise();
                                            throttleTimeout = null;
                                        },
                                        50
                                    );
                                }
                            } else {
                                api.reinitialise();
                            }
                        }
                    );
                }
            )
        });
    }



    function setBrowserDif() {
        if ( $.browser.msie ) {
            $("#boxNavigation").css("width", "auto");
        } else {
            $("#boxNavigation").css("width", 50);
        }
    }


    var wrapperWidth;
    function resizeContent() {
        var w = $("#boxWrapper").width();
        if (w != wrapperWidth) {
            wrapperWidth = w;
            var contentWidth = wrapperWidth - $("#boxLogo").innerWidth() - $("#boxNavigation").width();
            $("#boxContent").css("width", contentWidth - 2); // -2 IE hack. else navigation will sometimes be placed outside the stage.
            $("#contentToScroll").css("height", $("#boxWrapper").height() - $("#contentToScroll").position().top - 120);
            //$("#contentToScroll").css("width", $("#content").innerWidth() - $("#scroller").width() - 10);
        }
    }


    function posLogo() {
        var paddingTop = (($("#boxLogo").height() - $("#logo").height())/2) - 25;
        $("#boxLogo").css("padding-top", paddingTop);
    }
};



sternschleuder.Website.prototype.setShowVideo = function(path, width, height, mute) {
    this.videoData = new Object();
    this.videoData["path"] = path;
    this.videoData["mute"] = mute;
    this.videoData["autoplay"] = true;
    this.videoData["width"] = width;
    this.videoData["height"] = height;
    this.showVideo = true;
    $("#videoLightbox").css("display", "block");
    this.trySetVideoInFlash();
};


sternschleuder.Website.prototype.setFlashRegistered = function() {
    this.flashRegistered = true;
    this.trySetVideoInFlash();
};

sternschleuder.Website.prototype.hideVideo = function() {
    this.videoData = null;
    this.flashRegistered = false;
    $("#videoLightbox").css("display", "none");
};


sternschleuder.Website.prototype.scrollToTop = function() {
    this.scrollAPI.scrollToPercentY(0);
};

// flash wrapping. called from flash
function setFlashRegistered() {
    website.setFlashRegistered();
}
function hideVideo() {
    website.hideVideo();
}



var website = new sternschleuder.Website();
