if (typeof CUMobilUtils === 'undefined') {
    var CUMobilUtils = {};
}
CUMobilUtils.mobileDetect = function() {
    return {
        getMobileAgent: function() {
            this.userAgent = navigator.userAgent.toLowerCase();
            var strMobile;
            for (var property in this.mobileAgent) {
                if (this.userAgent.indexOf(this.mobileAgent[property].productFamily) != -1) {
                    strMobile = property;
                    if (this.mobileAgent[property].model) {
                        var tempProp = property;
                        for (var property1 in this.mobileAgent[tempProp].model) {
                            for (var i = 0; i < this.mobileAgent[tempProp].model[property1].length; i++) {
                                if (this.userAgent.indexOf(this.mobileAgent[tempProp].model[property1][i]) != -1) {
                                    strMobile += ", " + property1;
                                }
                            }
                        }
                    }
                    return strMobile;
                }
            }
            return null;
        },
        mobileAgent: {
            "iphone": {
                "productFamily": "iphone"
            },
            "ipad": {
                "productFamily": "ipad"
            },
            "android": {
                "productFamily": "android"
            }
        }
    }
}();



function HomePage() {
    var self = this;

    var panel = $('#home-nav-menu-slide');
    var closeBtn = $('#home-nav-menu-close-btn');
    var menuWrap = $('#home-nav-menu-wrap');
    var navLi = $('#feature-controls>li');
  //var homepageRotate = $('#home-nav-content img.feature-image');
    var homepageRotate = $('#home-nav-content .feature-image');
    //var homePageLinks = $('#home-nav-feature-links span');
    var homePageLinks = $('#home-nav-feature-links span');
    var slidePause = $('#feature-control-pause');
    var slidePlay = $('#feature-control-play');
    var ieNav = document.getElementById("navigation-ie");
    var ieSlide = document.getElementById("ie-slide");
    var recentTestsDiv = document.getElementById("recent-tests-content-slide");
    var recentTestsChildren = $("#recent-tests-content-slide>.recent-tests-product");
    var theWCM = {};
    if ("CQ" in window)
    {
        theWCM = CQ.WCM;
    }
    var index = 0;
    var rotateAdd;


    function init() {
        rotateAdd = setInterval(self.rotateHomepageAdd, 8000);

        if (!self.isEditMode()) {
            $('.sub-categories-images-item, .sub-categories-images-end, .recent-tests-product').descriptionToolTip();
        }

        //fix firefox iframes
        if ($.browser.mozilla) {
            var f = document.getElementsByTagName("iframe");
            for (var i = 0; i < f.length; i++) {
                if (f[i].src.substr(f[i].src.length - 1) == "#") f[i].src = f[i].src.substr(0, f[i].src.length - 1);
                else f[i].src = f[i].src + "#";
            }
        }

        //recentTestsDiv.style.width = recentTestsChildren.length * 130 + "px";
        slidePause.bind('click', function() {
            self.showPauseButton(false);
            clearInterval(rotateAdd);
        });

        slidePlay.bind('click', function() {
            self.showPauseButton(true);
            rotateAdd = setInterval(self.rotateHomepageAdd, 8000);
        });

        navLi.bind('click', function() {
            navLi.removeClass('feature-btn-active');
            $(this).addClass('feature-btn-active');
            index = $(this).prevAll().length;
            homepageRotate.removeClass('feature-image-active');
            $(homepageRotate[index]).addClass('feature-image-active');
            //homePageLinks.removeClass('feature-links-active');
            //$(homePageLinks[index]).addClass('feature-links-active');
            clearInterval(rotateAdd);
            self.showPauseButton(false);
        });

        $('.nvm-nav-item').filter(':not(:has(:animated))').bind('mouseenter', function() {
            self.slide(false, this);
            return false;
        });


        if (!self.isEditMode()) {
            menuWrap.filter(':not(:has(:animated))').bind('mouseleave', function() {
                self.slide(true, this);
                return false;
            });
        }

        closeBtn.bind('click', function() {
            self.slide(true, this);
        });

        if(self.isEditMode() && $.cookie('slideIndex')) {
            self.slide(false, $('div.nvm-nav-item').eq($.cookie('slideIndex')));
        }


    }

    this.showPauseButton = function(show) {
        if(!show) {
            slidePlay.css('display', 'block');
            slidePause.css('display', 'none');
        } else {
            slidePlay.css('display', 'none');
            slidePause.css('display', 'block');
        }
    };


    this.rotateHomepageAdd = function() {
    	var numberOfSlides = navLi.length -2;
    	//index = (index > 3) ? 0 : index + 1;
        index = (index > numberOfSlides) ? 0 : index + 1;
        //homePageLinks.removeClass('feature-links-active');
        //$(homePageLinks[index]).addClass('feature-links-active');
        navLi.removeClass('feature-btn-active');
        $(navLi[index]).addClass('feature-btn-active');
        homepageRotate.removeClass('feature-image-active');
        $(homepageRotate[index]).addClass('feature-image-active');
    };

    this.slide = function(close, inSrcElement) {
        var index = $(inSrcElement).index();
        var theContent = panel.children('.hnm-slide-content').eq(index);

        if (!close && theContent.children().size() == 0) {
            close = true;
        }

        if (close) {
            if ($.browser.msie) {
                ieNav.style.display = "none";
                ieSlide.style.display = "none";
                ieNav.style.width = "208px";
            }
            menuWrap.animate({width: 212}, {queue: false,duration: 250,complete:function() {
                if ($.browser.msie) {
                    ieNav.style.display = "block";
                    ieSlide.style.display = "block";
                }
                closeBtn.css('display', "none");
                panel.css('display', "none");
            }});
        } else {
            //todo: optimize code to only hide the one that needs to be hidden.
            panel.children('.hnm-slide-content').css('display', 'none');
            panel.children('.hnm-slide-content').css('visibility', 'hidden');
            if ($.browser.msie && menuWrap.filter(':not(:has(:animated))').width() < 900) {
                ieNav.style.display = "none";
                ieSlide.style.display = "none";
            }

            theContent.css('display', 'block');
            theContent.css('visibility', 'visible');
            menuWrap.filter(':not(:has(:animated))').animate({width: 930}, {queue: false,duration: 250, complete:function() {
                setTimeout(function() {
                    if (menuWrap.filter(':not(:has(:animated))').width() > 900) {
                        closeBtn.css('display', "block");
                        panel.css('display', "block");
                        if ($.browser.msie) {
                            ieNav.style.display = "block";
                            ieNav.style.width = "220px";
                            ieSlide.style.display = "block";
                        }
                    }
                }, 260);
            }});
        }

        if(self.isEditMode()) {
            if(close) {
                $.cookie('slideIndex', null)
            } else {
                $.cookie('slideIndex', index)
            }
        }

    };

    this.isEditMode = function() {
        return ((theWCM && theWCM.getMode) && theWCM.getMode() != 'preview');
    };

    init();
}


var theHomePage

$(document).ready(function() {
   theHomePage = new HomePage();
});
