﻿/*
______________________
| ED Object            |___________________________
|                                                  |
| Ecole de Design Main Front end scripts           |
|                                                  |
|                                                  |
|__________________________________________________|


*/

ED =
{


    init: function ()
    {
        this.chooseBg();
        this.initMenu();
        this.bgResize();
        this.initScrollers();
        this.initMasthead();
        try
        {
            this.initDD();
        } catch (e) { }
        window.onresize = function () { ED.bgResize() };
        //alert("hello");
    },
    initMasthead: function ()
    {

        // after 1 minute after page loads
        setTimeout(function ()
        {
            // hide masthead
            $j(".masthead").slideUp(700, function ()
            {              
                    // then bind an enter function to reshow masthead
                    $j(".mastheadContainer").bind("mouseenter", function ()
                    {
                        $j(".masthead").stop();
                        if ($j(".masthead").css("display") == "none")
                            $j(".masthead").height(0).show();
                        $j(".masthead").animate({ height: 72 }, 500);
                    });
                    //bind a leave function 
                    $j(".mastheadContainer").bind("mouseleave", function ()
                    {
                        $j(".masthead").stop();
                        $j(".masthead").slideUp(500);
                    });
               
            }); // end slideUp callback
        }, 2000);

    },
    initDD: function ()
    {
        //alert('makin menus');
        var menu = new UvumiDropdown('dropdown-demo');
        var menu = new UvumiDropdown('dropdown-demo2');


    },
    initPortfolio: function ()
    {


        var portfolioIsEditMode;
        try
        {
            portfolioIsEditMode = _PORTFOLIO_EDIT

        } catch (ex)
        {
            portfolioIsEditMode = false;
        }

        if (!portfolioIsEditMode)
        {
            var w = 0;
            $j(".pfMainDesc").parent().children().each(function ()
            {
                w += $j(this).outerWidth();

                //alert(w);
            });
            w += 300;
            $j("#pfContainer").width(w);
            var scroller = new UvumiScrollbar("portfolio");
        }

    },
    initScrollers: function ()
    {
        $j('#mainContentScroller').jScrollPane(
            {
                showArrows: true,
                scrollbarWidth: 15,
                arrowSize: 16,
                reinitialiseOnImageLoad: true
            }
          );
        $j('#mainContentScrollerProfs').jScrollPane(
            {
                showArrows: true,
                scrollbarWidth: 15,
                arrowSize: 16,
                reinitialiseOnImageLoad: true
            }
          );
            $j('#mainContentCoord').jScrollPane(
            {
                showArrows: true,
                scrollbarWidth: 15,
                arrowSize: 16,
                reinitialiseOnImageLoad: true
            }
          );
        $j('#mainContentHorz').jScrollPane();

    },
    initHome: function (itemCount)
    {
        var ideal = Math.ceil(Math.sqrt(itemCount));
        var rows = Math.ceil(itemCount / ideal);
        var hm = $j(".home");

        var w = hm.width();
        var h = hm.height();
        //alert(itemCount);
        var itemWidth = w / ideal;
        //var itemHeight = h/rows;
        var totalh = rows * itemWidth;
        hm.height(totalh > 910 ? 910 : totalh).css("overflow", "hidden");
        $j(".cover", hm).width(itemWidth).height(itemWidth).css({ float: "left", overflow: "hidden" });
        $j(".cover", hm).each(function ()
        {
            $this = $j(this);
            var thelink = $j("a", this).attr("href");
            var doLink = function ()
            {
                document.location =
                thelink
            }
            $this.children().andSelf().click(doLink);

        });
        $j(".cover", hm).fadeTo("fast", 1);
        $j(".homeText").width(itemWidth);
        $j(".cover", hm).hover(
            function ()
            {
                $j(this).fadeTo("fast", 0.7);
                var tx = $j(".homeText", $j(this))
                if (tx.css("opacity") == 0)
                    tx.fadeTo("fast", 0.7);
            },
            function ()
            {
                $j(this).fadeTo("fast", 1)
                $j(".homeText", $j(this)).fadeTo("fast", 0)
            }
            );

        $j(".homeText", hm).width(itemWidth - 10).fadeTo("fast", 0).hover(
            function ()
            {
                var $this = $j(this);
                $this.fadeTo("fast", 0.9);
                //$j(".detail",$this).slideDown("slow");
            },
            function ()
            {
                var $this = $j(this);
                $j(".detail", $this).slideUp("fast");
                // $this.fadeTo("slow",0);
            }
        );


        $j("img", hm).each(function ()
        {
            var $this = $j(this);

            $this.data("imgWidth", itemWidth);
            $this.bind("load", function ()
            {
                $this.css({ width: "auto", height: "auto" }).width(itemWidth);
                var h = $this.height();
                if (h < itemWidth)
                    $this.height(itemWidth).css("width", "auto");
            });
            $this.width(itemWidth);
            h = $this.height();
            if (h < itemWidth)
                $this.height(itemWidth).css("width", "auto");
            $this.show();

        });
        var months = new Array("jan", "fév", "mars", "avril", "mai", "juin", "juil", "aôut", "sep", "oct", "nov", "déc");
        var sl = $j("#homeSlider");
        sl.remove();

        $j("body").append(sl);
        sl.css(
        {
            position: "absolute",
            top: $j(".home").offset().top + 10 + "px",
            left: $j(".home").offset().left + $j(".home").width() + 15 + "px"
        });

        // sl.show();
        sl.slider({
            orientation: "vertical",
            range: true,
            step: 1,
            values: [1, 52],
            min: 1,
            max: 52,
            slide: function (event, ui)
            {


                ED.homeReflow(ui);

            }

        });
        $j("body").append('<div id="sliderText" style="width:10px;height:400px;position:absolute;"></div>');
        //sl.css({top:0,left:0,zIndex:9999999999}).show();
        $j("#sliderText").css(
        {
            position: "absolute",
            top: $j(".home").offset().top + 10 + "px",
            left: $j(".home").offset().left + $j(".home").width() + 38 + "px"
        });

        var mn = (new Date()).getMonth();
        var yr = (new Date()).getFullYear();
        var endYear; var startYear;
        if (mn >= 9)
        {
            startYear = yr;
            endYear = yr + 1;
        } else
        {
            startYear = yr - 1;
            endYear = yr;
        }

        var smonth = [7, 6, 5, 4, 3, 2, 1, 12, 11, 10, 9, 8];

        for (var i = 0; i < 12; i++)
        {
            //alert(smonth[i]);
            var txt = '<div style="width:5px;overflow:visible;font-size:0.8em;border-top:0px solid #999999;position:absolute;left:0px;top:'
             + Math.round((_DAY_MONTH[i] + 31) * (400 / 365))
             + 'px"><div style="position:relative;left:0px;top:-7px;width:50px;overflow:visible;">'
             + (smonth[i] < 10 ? "0" : "")
             + (smonth[i])
             + (smonth[i] == 1 ? " " + endYear : (smonth[i] == 8 ? " " + startYear : "")) + '</div></div>';
            // alert(txt);
            $j("#sliderText").append(txt);
        }

        var start = _DAY_MONTH[6] + 31;
        var augstart = 400;
        var top;
        var multiplier = 365 / 400;
        if (_MONTH > 7)
        {
            top = augstart - _DAY * multiplier;
        } else
        {
            top = start - _DAY * multiplier;
        }

        top -= 3 * multiplier;

        $j("#sliderText").append('<div style="width:5px;overflow:visible;font-size:0.8em;border-top:0px solid #999999;position:absolute;left:0px;top:0px"><div style="position:relative;left:0px;top:0px;width:50px;overflow:visible;">08 ' + endYear + '</div></div>');
        $j("#sliderText").append('<div style="position:absolute; border-bottom:0px solid #ffcc00; background-image:url(/data/css/images/yellow_arrow.png);background-repeat:no-repeat;background-position:0 0; width:30px; height:5px; left:-28px;top:' + Math.round(top) + 'px;z-index:10"><div style="position:relative;top:-7px;left:7px;">' + _DATE + '</div></div>');
        this.homeReflow(sl);
    },
    homeReflow: function (theSlider)
    {
        // theSlider.disable();
        var high = $j("#homeSlider").slider('values', 1);
        var low = $j("#homeSlider").slider('values', 0);
        var hm = $j(".home");
        var covers = $j(".cover", hm);
        var itemCount = 0;
        covers.each(function ()
        {
            $this = $j(this);
            if ($this.attr("sweek") < low || $this.attr("sweek") > high)
            {
                $this.hide();
                $this.data("visible", false);
            }
            else
            {
                $this.show();
                itemCount++;
                $this.data("visible", true);
            }
        });

        var ideal = Math.ceil(Math.sqrt(itemCount));
        var rows = Math.ceil(itemCount / ideal);


        var w = hm.width();
        var h = hm.height();
        //alert(itemCount);
        var itemWidth = w / ideal;
        //alert(itemWidth);
        //var itemHeight = h/rows;
        // var totalh = rows * itemWidth+25;
        //  hm.height(totalh>930?930:totalh);//.css("overflow","hidden");
        hm.height(910);
        $j(".cover", hm).each(function ()
        {
            $this = $j(this);
            if ($this.data("visible"))
            {
                $this.width(itemWidth).height(itemWidth).css({ float: "left", overflow: "hidden" });
                var theImg = $j("img", $this);
                theImg.css({ width: "auto", height: "auto" });
                theImg.width(itemWidth)
                if (theImg.height() < itemWidth)
                {
                    theImg.height(itemWidth).css("width", "auto");
                }
                theImg.bind("load", function ()
                {

                    $this = $j(this);
                    $this.css({ width: "auto", height: "auto" });
                    $this.width(itemWidth)
                    if ($this.height() < itemWidth)
                    {
                        $this.height(itemWidth).css("width", "auto");
                    }
                });

                $j(".homeText", $this).width(itemWidth - 10)

            }
        });

        // theSlider.enable();

    },
    initProfs: function ()
    {



        $j("#linkPub").click(function ()
        {
            $j(".profLink").removeClass("selected");
            $j(".profPanel").removeClass("selected");
            $j("#publicationsPanel").addClass("selected");
            ED.initScrollers();

        });

        $j("#linkProfil").click(function ()
        {
            $j(".profLink").removeClass("selected");
            $j(".profPanel").removeClass("selected");
            $j("#profilPanel").addClass("selected");
            $j(this).addClass("selected");
            ED.initScrollers();

        });

        $j("#linkPrix").click(function ()
        {
            $j(".profLink").removeClass("selected");
            $j(".profPanel").removeClass("selected");
            $j("#prixPanel").addClass("selected");
            $j(this).addClass("selected");
            ED.initScrollers();

        });

        switch (window.location.hash)
        {
            case '#publications':
                $j("#linkPub").click();
                break;
            case '#prix':
                $j("#linkPrix").click();
                break;
            default:
                break;
        }

    },
    initMenu: function ()
    {
        // alert($j("#menuUn").length);
        $j("#menuUn").hover(function (e)
        {
            $this = $j(this);
            $this.stop();
            $this.animate({ height: "120px" }, 1000)
        }, function (e)
        {
            $this = $j(this);
            $this.stop()
            $this.animate({ height: "20px" }, 1000)
        });

        $j("#menuDeux").hover(function (e)
        {
            $this = $j(this);
            $this.stop();
            $this.animate({ height: "120px" }, 1000)
        }, function (e)
        {
            $this = $j(this);
            $this.stop()
            $this.animate({ height: "20px" }, 1000)
        });


    },
    chooseBg: function ()
    {
        try
        {
            if (bgPic)
            {
                var randomNum = Math.floor((Math.random() * bgPic.length));
                //document.getElementById("bgImg").src = bgPic[randomNum];
                $j("#bgImg").attr("src", bgPic[randomNum]).bind("load", function () { ED.bgResize(); });
            } else
            {
                $j("#bgImg").hide();
            }
        } catch (e) { $j("#bgImg").hide(); }
    },

    bgResize: function ()
    {
        // The current aspect ratio of the window
        var browserHeight, browserWidth;
        window.innerHeight ? (browserHeight = window.innerHeight) : (browserHeight = document.body.offsetHeight);
        window.innerWidth ? (browserWidth = window.innerWidth) : (browserWidth = document.body.offsetWidth);

        var winAR = browserWidth / browserHeight;
        var imgRatio = document.getElementById("bgImg").width / document.getElementById("bgImg").height;
        if (winAR >= imgRatio)
        {
            document.getElementById("bgContainer").className = "wide";
            var imgHeight = document.getElementById("bgImg").height;
            var topM = -((imgHeight - browserHeight) / 2);
            document.getElementById("bgImg").style.margin = topM + "px 0px 0px 0px";
        }

        else if (winAR < imgRatio)
        {
            document.getElementById("bgContainer").className = "tall";
            var imgWidth = document.getElementById("bgImg").width;
            var leftM = -((imgWidth - browserWidth) / 2);
            document.getElementById("bgImg").style.margin = "0px 0px 0px " + leftM + "px";
        }



    }


}
