/**
 * Created by JetBrains PhpStorm.
 * User: thomaskasper
 * Date: 10.10.11
 * Time: 20:11
 * To change this template use File | Settings | File Templates.
 */

$(function() {
    var navigationSlider = new NavigationSlider();
});

function NavigationSlider() {
    this.TIMEOUT_LENGTH = 8000;
    this.$navigationControlBox = $('#navigationControl');
    this.$navigationContainer = $('#totalNavigationWrap');
    this.$claim = $('.controlLink').text();
    this.$langMenu = $('#bemoGroup ul li ul li a');
    this.$navigationHeight = this.$navigationContainer.height();
    this.elementClicked = false;

    this.setLandingpageCookie();
    this.registerClickHandler();
    this.setDefaultNavigationStateCookie();
    this.setNavigationClass();
    this.showHideNavigationOnLoad();
    this.setText();
    this.setTimeout();
    this.changeClaimerText();
    this.setRedirectTimeout();
    this.foldNavigationIfIsLandingPage();
}

NavigationSlider.prototype.registerClickHandler = function() {
    var _this = this;
    var inAnimation = false;
    this.$navigationControlBox.click(function() {
        if(inAnimation) {
            return;
        }
        inAnimation = true;
        _this.elementClicked = true;
        $(_this.$navigationControlBox).toggleClass("active");
        _this.$navigationContainer.animate({
                height  :   'toggle'
            }, 'normal', function() {
                inAnimation = false;
                _this.setNavigationStateCookie();
                _this.setText();
                _this.changeClaimerText();
            });
        return false;
    });

    this.$langMenu.click(function() {
        // Men� einklappen wenn man die Sprache �ber Language-Men� wechselt
        document.cookie = 'navigationState=true';
    });
};

NavigationSlider.prototype.setText = function() {
    if(this.getNavigationState()) {
        this.$navigationControlBox.children('a').text('Navigation einklappen');
    } else {
        this.$navigationControlBox.children('a').text('Navigation ausklappen');
    }
};

NavigationSlider.prototype.setNavigationClass = function() {
    if("true" == this.getCookie('navigationState')) {
        this.$navigationControlBox.addClass('active');
    } else {
        return false;
    }
};

NavigationSlider.prototype.getNavigationState = function() {
    _this = this;
    if(this.$navigationControlBox.hasClass('active')) {
        return true;
    } else {
        return false;
    }
};

NavigationSlider.prototype.isLandingpage = function() {
        if($('body').hasClass('landingpage')) {
        return true;
    } else {
        return false;
    }
};

NavigationSlider.prototype.setNavigationStateCookie = function() {
      document.cookie = 'navigationState=' + this.getNavigationState();
};

NavigationSlider.prototype.setDefaultNavigationStateCookie = function() {
    if(null == this.getCookie('navigationState')) {
        document.cookie = 'navigationState=false';
    }
}

NavigationSlider.prototype.setLandingpageCookie = function() {
    document.cookie = "landingpage=" + this.isLandingpage();
};

NavigationSlider.prototype.showHideNavigationOnLoad = function() {
    if("true" == this.getCookie('navigationState')) {
        this.$navigationContainer.css('display', 'block');
    } else {
        this.$navigationContainer.css('display', 'none');
    }
}

NavigationSlider.prototype.getCookie = function(cookieName) {
    strValue = null;

    if( strCookie = document.cookie ) {
        if( arrCookie = strCookie.match( new RegExp( cookieName + '=([^;]*)', 'g'))) {
            strValue=RegExp.$1;
        }
    }
    return(strValue);
};

NavigationSlider.prototype.setTimeout = function() {
    var _this = this;
    this.clearTimeout();

    this.timeoutHandler = window.setTimeout(
        function() {
            if (_this.isLandingpage() && "false" == _this.getCookie('navigationState') && false === _this.elementClicked)  {
                _this.$navigationControlBox.click();
                _this.clearTimeout();
            }
        },
        this.TIMEOUT_LENGTH
    );
};

NavigationSlider.prototype.clearTimeout = function() {
    if(null != this.timeoutHandler) {
        window.clearTimeout(this.timeoutHandler);
        this.timeoutHandler = null;
    }
};

NavigationSlider.prototype.setRedirectTimeout = function() {

    var _this=this;
    if (this.isLandingpage()) {
        var timeOut = window.setTimeout("_this.redirect()",9000);
    }
}

NavigationSlider.prototype.redirect = function() {
    var redirectUrl;
    if($('body').hasClass('ger')) {
        redirectUrl = 'index.php?id=6';
    } else if ($('body').hasClass('int')) {
        redirectUrl = 'index.php?id=132';
    }

    this.$navigationContainer.css('display', 'visible');
    window.location = redirectUrl;
}

NavigationSlider.prototype.changeClaimerText = function() {
    var _this = this;
    if ( this.getNavigationState() ) {
        if($('body').hasClass('ger')) {
            this.$navigationControlBox.children('span').text('Navigation einklappen');
        } else {
            this.$navigationControlBox.children('span').text('Fold navigation');
        }
    } else {
        this.$navigationControlBox.children('span').text(_this.$claim)
    }
}



function centerContent(container, heightParam,widthParam){
    jQuery(window).bind('resize',function(){
        var height = jQuery(this).height();
        var wiWidth = jQuery(this).width();
        var newmargin = ((height-heightParam)/2);
        var newleft = ((wiWidth-widthParam)/2);
//        if(newmargin <= 0){
//            newmargin = 0;
//        }
        jQuery(container).css('margin-top',newmargin*1.2);
//        jQuery(container).css('left',newleft);
//        jQuery('li.sky').css('top',newmargin/2 * 0.6);

    });
    jQuery(function(){
        jQuery(window).trigger('resize');
    });
}

NavigationSlider.prototype.foldNavigationIfIsLandingPage = function() {
    if (this.isLandingpage()) {
        this.$navigationContainer.css('display', 'none');
        this.$navigationControlBox.children('span').text(_this.$claim);
        this.$navigationControlBox.removeClass('active');
    } else if (this.isLandingpage()==false) {
        this.$navigationContainer.css('display', 'visible');
        this.$navigationControlBox.children('span').text(_this.$claim);
        if($('body').hasClass('ger')) {
            this.$navigationControlBox.children('span').text('Navigation einklappen');
        } else {
            this.$navigationControlBox.children('span').text('Fold navigation');
        }
    }
}

jQuery(document).ready(function(){
    var heightParam = jQuery('.landingpageContent ul').height();
    var widthParam = jQuery('.landingpageContent ul').width();
    var centerClass = new centerContent('.landingpageContent ul',heightParam,widthParam);
});
