/*!
 * Site weites JavaScript
 */

var CHE = CHE || {};

$(document).ready(function(){

        CHE['addressCssConfig'] = {
            configDefault: true,
            cssValues: ['center 0px','center -30px','center -60px','center -90px'], //all white, mail black, both black, address black
            /** switch from all white default address tagline to the one with the email in black */
            switchConfig: function(){
                this.configDefault = false;
            },
            getInitialCss:  function(){
                return (this.configDefault) ? this.cssValues[0] : this.cssValues[1];
            },
            getEmailMouseOverCss: function(){
                return (this.configDefault) ? this.cssValues[1] : this.cssValues[0];
            },
            getAddressMouseOverCss: function(){
                return (this.configDefault) ? this.cssValues[3] : this.cssValues[2];
            }
        }

        $("#maintext div.content").slideDown();


        var addressOffset = Math.round($("#maintext").offset().top);
        var adrPartCoords = {
            right: 300,
            top: 30
        }

        var emailPartCoords = {
            right: 700,
            length: 130,
            top: 30
        }

        var hilite = function(e) {
            var coords = getAddressCoords(e, this);
            if(isOverAddressPart(coords)) {
                $(this).css('background-position', CHE['addressCssConfig'].getAddressMouseOverCss());
                $(this).css('cursor', 'pointer');
            } else if(isOverEmailPart(coords)) {
                $(this).css('background-position', CHE['addressCssConfig'].getEmailMouseOverCss());
                $(this).css('cursor', 'pointer');
            } else {
                $(this).css('background-position', CHE['addressCssConfig'].getInitialCss());
                $(this).css('cursor', 'default');
            }
        }


        $("#maintext div.address").mouseover(function(e){
            $(this).bind('mousemove', hilite);
        }).mouseout(function(e){
            $(this).unbind('mousemove', hilite);
            $(this).css('background-position', CHE['addressCssConfig'].getInitialCss());
        });

        $("#maintext div.address").click(function(e){
            var coords = getAddressCoords(e, this);
            if(isOverAddressPart(coords)) {
                window.location.href = './index.html';
            } else if(isOverEmailPart(coords)){
                window.location.href = 'mailto: mail@hemmerich.de';
            }
        });

        function getAddressCoords(ev, elem) {
            var res = {};
            res.x = ev.pageX - Math.round($(elem).offset().left);
            res.y = ev.pageY - (Math.round($(elem).offset().top));
            return res;
        }

        function isOverAddressPart(coords) {
            return coords.x <= adrPartCoords.right && coords.y <= adrPartCoords.top;
        }

        function isOverEmailPart(coords) {
            return coords.x >= emailPartCoords.right && coords.x <= emailPartCoords.right
                    + emailPartCoords.length && coords.y <= emailPartCoords.top;

        }

        $("#navigation ul").mouseover(function(e){
            var li = $(e.target).closest('li');
            if(!li.hasClass('active')){
                $(e.target).closest('li').addClass("active dynamicAdd");
            }
        }).mouseout(function(e){
            var li = $(e.target).closest('li');
            if(li.hasClass('dynamicAdd')){
                li.removeClass('active dynamicAdd');
            }
        })


        CHE['nav'] = {
            navCookieName : 'navinfo',
            pageRefresh : null,
            getCurrentPage : function(){
                var currPage = window.location.pathname;
                $.cookie(this.navCookieName, currPage);
                return currPage;
            },
            isPageRefresh : function(){
                return this.pageRefresh;
            },
            init : function(){
                var cookieValue = $.cookie(this.navCookieName);
                var currPage = this.getCurrentPage();
                this.pageRefresh = (cookieValue && cookieValue == currPage);
            }
        }

        CHE['nav'].init();
        

        CHE['colors'] = {
            colors : ['green','lightblue','lightgreen','orange','red','redblue'],
            colorCookieName : 'sessionColor',
            currentColor : null,
            getRndColor : function(){
                return this.colors[Math.floor(Math.random() * this.colors.length)];
            },

            getCurrColor : function(){
                if(CHE['nav'].isPageRefresh() && !this.currentColor) {
                    this.currentColor = this.getRndColor();
                    $.cookie(this.colorCookieName, this.currentColor);
                    return this.currentColor;
                } else if(this.currentColor) {
                    return this.currentColor;
                } else if($.cookie(this.colorCookieName)) {
                    this.currentColor = $.cookie(this.colorCookieName);
                    return this.currentColor;
                } else {
                    this.currentColor = this.getRndColor();
                    $.cookie(this.colorCookieName, this.currentColor);
                    return this.currentColor;
                }
            }
            
        }
        $('link[title="color"][type="text/css"]').attr('href', './css/' + CHE['colors'].getCurrColor() + '.css');
    });
