﻿function timeStamp() {
    var time = new Date();
    return time.getTime();
}
$(document).ready(function() {
    InitializeBox();
    InitializeStart();
    if (!Silverlight.isInstalled("4.0.50303")) {
        $('span.silverlight').css('display', 'block');
        $('.sl').css('display', 'block');
        if ($('body').hasClass('start') && $('input[name=hfCurView]').val() == "") {
            $.ajax({
                url: 'SilverlightSwitch.axd',
                data: { "silverlight": "0", "nocache": timeStamp() },
                complete: function() {
                    $('input[name=hfCurView]').val("0");
                }
            });
        } else if ($('input[name=hfCurView]').val() == "") {
            $.ajax({
                url: 'SilverlightSwitch.axd',
                data: { "silverlight": "0", "nocache": timeStamp() },
                complete: function() {
                    var overlay = $('.slBox');
                    overlay.css({
                        'top': ($(window).height() - $('.slBox').height()) / 2,
                        'left': ($(window).width() - 530) / 2,
                        'display': 'block'
                    });

                    $('<div id="overlay">').css({
                        'position': 'absolute',
                        'top': 0,
                        'left': 0,
                        'width': $(window).width(),
                        'height': $(window).height(),
                        'background': '#000',
                        'opacity': 0.5,
                        'z-index': 99,
                        'display': 'none'
                    }).appendTo('body').css('display', 'block');

                    $('.closeSlBox').click(function() {
                        overlay.css('display', 'none');
                        $('#overlay').remove();
                    });
                    $('input[name=hfCurView]').val("0");
                }
            });
        }
    }

    $('span.silverlight').click(function() {
        var overlay = $('.slBox');
        overlay.css(
            {
                'top': ($(window).height() - $('.slBox').height()) / 2,
                'left': ($(window).width() - 530) / 2,
                'display': 'block'
            });
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
        $('.closeSlBox').click(function() {
            overlay.css('display', 'none');
            $('#overlay').remove();
        });
    });


    $('.scroll-pane').each(function() {
        $(this).setSlider();
    });

    var ie = ($.browser.msie && parseInt($.browser.version.substring(0, 1)) == 7) ? true : false;
    if (ie) {
        $('.thirdLevel').width($('.thirdLevel').prev().width());
    }

    $('.imageHover').find('img').mouseenter(function(e) {
        $('<div class="tooltipBox">')
            .css({ 'display': 'block', 'top': e.pageY, 'left': e.pageX + 30 })
            .html('<div class="leftCorner"></div><div class="middle">' + $(this).attr('alt') + '</div><div class="rightCorner"></div>')
            .appendTo('.container');
    });
    $('.imageHover').find('img').mousemove(function(e) {
        $('.tooltipBox:not(.locked)').css({ 'top': e.pageY, 'left': e.pageX + 30 });
    });

    $('.imageHover').find('img').mouseleave(function() {
        $('.tooltipBox:not(.locked)').remove();
    });

    $('.archivImg').hover(function() {
        var img = $(this);
        var text = img.find('img').attr('alt');
        $('<table class="info" cellpadding="0" cellspacing="0"></table>').html('<tr><td><span>' + text + '</span></td></tr>').appendTo(img);
    }, function() {
        var img = $(this);
        img.find('table.info').remove();
    });

    $('ul.firstNav').children('li').each(function(){
        $(this).hideNaviTop();
    });

    $('ul.secondNav').children('li').each(function() {
        $(this).hideNaviThird();
    });
    
    $('.navi').children('ul').children('li').each(function() {
        $(this).hideLi();
    });
    
    $('.meta').hideNaviFooter();
    $('.footer').setBottom();
    $('#deepzoom').setHeight();

    if ($('.zoomControls').length > 0) {
        $('.zoomControls').css('left', $(window).width() - 35);
    }

    if ($('#deepzoom').find('.slideShow').length > 0) {
        $('#deepzoom').find('.slideShow img').each(function(index) {
            var deep = $('#deepzoom');
            var image = $(this);
            image.hide();
            if (index == 0 && $('input[name=hfCurBackgroundDocument]').val() == "") {
                image.attr('class', 'active');
                $('input[name=hfCurBackgroundDocument]').val(image.attr('axid'));
            } else if (image.attr('axid') == $('input[name=hfCurBackgroundDocument]').val()) {
                image.attr('class', 'active');
            }
            image.load(function() {
                if (deep.width() > 960) {
                    deep.width($(window).width());
                    image.width($(window).width());
                } else {
                    deep.width(960);
                    image.width(960);
                }

                var height = -1;
                height = image.height();
                image.css('top', ($('#deepzoom').height() - height) / 2);
                if (image.hasClass('active')) {
                    image.show();
                }
            });
            $(window).resize(function() {
                if ($(window).width() > 960) {
                    deep.width($(window).width());
                    image.width($(window).width());
                } else {
                    deep.width(960);
                    image.width(960);
                }
                height = image.height();
                image.css('top', (deep.height() - height) / 2);
            });
            if (this.complete) $(this).trigger('load');
        });
    } else if ($('#deepzoom').children('img').length > 0) {
        var deep = $('#deepzoom');
        var image = deep.children('img');
        image.load(function() {
            if (deep.width() > 960) {
                deep.width($(window).width());
                image.width($(window).width());
            } else {
                deep.width(960);
                image.width(960);
            }

            var height = -1;
            height = image.height();
            image.css('top', ($('#deepzoom').height() - height) / 2);
        });

        $(window).resize(function() {
            if ($(window).width() > 960) {
                deep.width($(window).width());
                image.width($(window).width());
            } else {
                deep.width(960);
                image.width(960);
            }
            height = image.height();
            image.css('top', (deep.height() - height) / 2);
        });
    }

    if ($('.slideShow').length > 0 && !$('.slideShow').parent().attr('id').match('deepzoom')) {
        var timerval = setInterval("slideSwitch()", 4000);
        $(function() {
            timerval;
        });
    }

    if ($('.slideShowAuto').length > 0) {
        var timerval = setInterval("slideSwitchAuto()", 4000);
        $(function() {
            timerval;
        });
    }

    if ($('.slideShowWithNav').length > 0) {
        $('.slideNavi').find('.left').click(slideBackNavi);
        $('.slideNavi').find('.right').click(slideSwitchNavi);
        $('.slideShowWithNav').find('img').each(function() {
            $(this).click(lightBox);
        });
    }

    $('.next').click(function() {
        $('div.tooltipBox.rechts').hide();
        slideSwitch();
    });

    $('.prev').click(function() {
        $('div.tooltipBox.links').hide();
        slideBack();
    });

    $('.btnNext, .btnPrev').click(count);

    $('.tooltip').each(function() {
        $(this).tooltip();
    });

    $('.icons').setBottomFav();

    $('img.tellIcon', '.icons').hover(function() {
        $(this).attr('src', 'templates/Bilder/corporate/bubbleHover.png');
    }, function() {
        $(this).attr('src', 'templates/Bilder/corporate/bubble.png');
    });

    $('img.favBtn', '.icons').hover(function() {
        $(this).attr('src', 'templates/Bilder/corporate/favHover.png');
    }, function() {
        $(this).attr('src', 'templates/Bilder/corporate/fav.png');
    });

    $('img.fbIcon', '.icons').hover(function() {
        $(this).attr('src', 'templates/Bilder/corporate/facebookHover.png');
    }, function() {
        $(this).attr('src', 'templates/Bilder/corporate/facebook.png');
    });


    $('.item').find('img').each(function() {
        $(this).farbOverlay();
    });

    OverlayFav();
    OverlayTell();
    OverlayLang();
    setNaviBtn();
    HändlerNav();
    var intervalFB = window.setInterval(function() {
        if ($('.fb_edge_comment_widget').length) {
            $('.fb_edge_comment_widget').each(function() {
                $(this).css('top', '-' + $(this).height() + 'px');
            });
        }
    }, 100);

});

function count() {
    var pageid = $('.btnNext').attr('pageid');
    $.ajax({
        url: 'BGClickHandler.axd',
        data: { "pageid": pageid, "nocache": timeStamp() }
    });
}

$.fn.hideLi = function() {
    var li = $(this);
    var curLi;
    if (li.children('ul').children('li.zwLevel').length > 0) {
        var pos = 0;
        var sel = false;
        if (li.find('a.sel').length > 0) {
            li.children('ul').children('li').each(function() {
                var li2 = $(this);
                if (li2.hasClass('zwLevel')) {
                    pos = li2.index();
                    li2.show();
                } else {

                    li2.hide();
                }

                if (li2.children('a').hasClass('sel')) {
                    sel = true;
                    //                li.parent().find('li.zwLevel:eq(' + pos + ')').show();
                    for (var i = (li2.index() - 1); i >= pos; i--) {
                        li2.parent().children('li:eq(' + i + ')').show();
                    }

                }

                if (sel && li2.index() > pos) {
                    li2.show();
                }

                if (li2.next().hasClass('zwLevel')) {
                    sel = false;
                }

            });
        } else {
            li.children('ul').children('li').each(function() {
                if (!$(this).hasClass('zwLevel')) {
                    $(this).hide();
                }
            });
        }
    }
}

function setNaviBtn() {
    var left = ($(window).width() - 26) ;
    var top = ($(window).height() - 65) / 2;
    $('.btnPrev, .btnNext').css('top', top);
    $('.btnNext').css('left', left);
    $('div.tooltipBox.locked.rechts').hide().css({ 'right': 25, 'top': top + 17 }).show();
    $('div.tooltipBox.locked.links').hide().css({ 'left': 25, 'top': top + 17 }).show();
    $(window).resize(function () {
        var left = ($(window).width() - 26);
        var top = ($(window).height() - 65) / 2;
        $('.btnNext').css('left', left);
        $('.btnPrev, .btnNext').css('top', top);
    });
}

$.fn.hideNaviTop = function() {
    var liFirstLevel = $(this);
    var overlay = $('.navOverlayTop');
    var overlayThird = $('.navOverlayTop2');
    var parent = $('.firstNav');
    var child = '.secondNav';
    var thirdLevel = $('.thirdNav');
    if (liFirstLevel.hasClass('sel') && liFirstLevel.find(child).length) {
        overlay.show();
    }

    liFirstLevel.hover(function() {
        if (!liFirstLevel.hasClass('sel')) {
            if (liFirstLevel.find(child).length) {
                overlay.show();
                parent.children('li.sel').find(child).hide();
            } else {
                overlay.hide();
                parent.children('li.sel').find(child).hide();
            }
            if (!thirdLevel.is(':visible')) {
                overlayThird.hide();
            }
        } 
    }, function() {
        if (parent.children('li.sel').find(child).length) {
            parent.children('li.sel').find(child).show();
            overlay.show();
        }

        if (parent.find(child).children('li.sel').find('ul.thirdNav').length) {
            overlayThird.show();
        } else {
            overlayThird.hide();
        }
    });
}

$.fn.hideNaviThird = function() {
    var liSecondLevel = $(this);
    var overlay = $('.navOverlayTop2');
    var parent = $('.secondNav');
    var child = '.thirdNav';

    if (liSecondLevel.hasClass('sel') && liSecondLevel.find(child).length) {
        overlay.show();
    }

    liSecondLevel.hover(function() {
        if (!liSecondLevel.hasClass('sel')) {
            if (liSecondLevel.find(child).length) {
                overlay.show();
                parent.children('li.sel').find(child).hide();
            } else {
                overlay.hide();
                parent.children('li.sel').find(child).hide();
            }
        } else if (liSecondLevel.find(child).length < 1) {
            overlay.hide();
        }
    }, function() {
        if (parent.children('li.sel').find(child).length) {
            parent.children('li.sel').find(child).show();
            overlay.show();
        } else {
            overlay.hide();
        }
    });
}

$.fn.hideNaviFooter = function () {
    if ($(this).children('li.sel').find('ul').length > 0) {
        $('.navOverlayBottom').css('display', 'block');
    }

    $(this).children('li').hover(function () {
        if (!$(this).hasClass('sel') && $(this).find('ul').length > 0) {
            $(this).parent().children('li.sel').find('ul').css('display', 'none');
            $('.navOverlayBottom').css('display', 'block');
        }
        if (!$(this).hasClass('sel') && $(this).find('ul').length < 1) {
            $(this).parent().children('li.sel').find('ul').css('display', 'none');
            $('.navOverlayBottom').css('display', 'none');
        }
        if ($(this).hasClass('sel') && $(this).find('ul').length > 0) {
            $(this).parent().children('li.sel').find('ul').css('display', 'block');
            $('.navOverlayBottom').css('display', 'block');
        }
    }, function () {
        if (!$(this).hasClass('sel')) {
            if ($(this).parent().children('li.sel').find('ul').length > 0) {
                $(this).parent().children('li.sel').find('ul').css('display', 'block');
                $('.navOverlayBottom').css('display', 'block');
            } else {
                $(this).parent().children('li.sel').find('ul').css('display', 'none');
                $('.navOverlayBottom').css('display', 'none');
            }
        }

        if ($(this).hasClass('sel') && $(this).find('ul').length > 0) {
            $(this).parent().children('li.sel').find('ul').css('display', 'block');
            $('.navOverlayBottom').css('display', 'block');
        }

        if ($(this).hasClass('sel') && $(this).find('ul').length < 1) {
            $(this).parent().children('li.sel').find('ul').css('display', 'none');
            $('.navOverlayBottom').css('display', 'none');
        }
    });
}

$.fn.setBottom = function() {
    var footer = $(this);
    var winHeight = $(window).height();
    var boxHeight = 0;

    if (!$('body').hasClass('start')) {
        $('.box').each(function() {
            if (!$(this).hasClass('facebook')) {
                if (boxHeight < $(this).height() + $(this).offset().top + 30) {
                    boxHeight = $(this).height() + $(this).offset().top + 30;
                }
            }
        });

        if (winHeight - 30 > boxHeight) {
            footer.css('top', (winHeight - 30));
        }
        else {
            footer.css('top', boxHeight);
            $(".container").css({ "overflow-y": "scroll" });
        }

        $(window).resize(function() {
            winHeight = $(window).height();
            boxHeight = 0;
            $('.box').each(function() {
                if (boxHeight < $(this).height() + $(this).offset().top + 30) {
                    boxHeight = $(this).height() + $(this).offset().top + 30;
                }
            });

            if (winHeight - 30 > boxHeight) {
                footer.css('top', (winHeight - 30));
            }
            else {
                footer.css('top', boxHeight);
                $(".container").css({ "overflow-y": "scroll" });
            }
        });
    } else {
        if (!Silverlight.isInstalled("1.0")) {
            footer.css("margin-top", "70px");
        }
    }
}

$.fn.setBottomFav = function () {
    var icons = $(this);
    var winHeight = $(window).height();
    var winWidth = $(window).width();
    icons.css(
    {
        'top': winHeight - icons.height() - 55,
        'left': winWidth - 39
    });
    $(window).resize(function () {
        var win = $(this);
        boxHeight = 0;
        $('.box').each(function () {
            if (boxHeight < $(this).height()+$(this).offset().top) {
                boxHeight = $(this).height()+$(this).offset().top;
            }
        });
        var curHeight = win.height();
        var curWidth = win.width();
        var curTop = parseInt(icons.css('top'));
        var curLeft = parseInt(icons.css('left'));
        icons.css({
            'top': curHeight>boxHeight ? curTop + (curHeight - winHeight): boxHeight- icons.height() - 55,
            'left': curWidth>960 ? curLeft + (curWidth - winWidth): 921
        });
        winHeight = curHeight;
        winWidth = curWidth>960?curWidth:960;
    });
}

$.fn.setHeight = function () {
    var div = $(this);
    var winHeight = $('.footer').offset().top-100;
    var winWidth = $(window).width();
    div.css(
    {
        'height':  winHeight ,
        'width': winWidth
    });
    $(window).resize(function () {
        var win = $(this);
        var curMargin = div.height();
        div.css({'height': $('.footer').offset().top-100, 'width': win.width() > 960 ? win.width() : 960});
    });
}

function HändlerNav() {
    if ($('.unternehmen').length > 0) {
        var last = $('.thirdLevel').prev().find('.scroll-content').children('div:eq(' + 0 + ')');
        if ($('.kontakt').hasClass('active')) {
            $('.kontakt').css('display', 'block');
            $('.thirdLevel').find('a.sel').removeClass();
            $('.thirdLevel').find('a:eq(3)').addClass('sel');
            last = $('.kontakt');
        } else {
            last.css('display', 'block');
        }
        
        $('.thirdLevel ul li').find('a').each(function (index) {

            $(this).click(function () {
                $('.thirdLevel').find('a.sel').removeClass();
                $(this).addClass('sel');
                var cur = $('.thirdLevel').prev().find('.scroll-content').children('div:eq(' + (index) + ')');
                last.css('display', 'none');
                cur.css('display', 'block');
                last = cur;
                return false;
            });
        });
    }
}

function OverlayFav() {
    
    var overlay = $('.favOverlay');
    var left = ($(window).width() - 530) / 2;
    var top = ($(window).height() - 410) / 2;
    
    overlay.css(
    {
        'left': left,
        'top':top
    });
    
    var loadImgs = 0;
    $('img', overlay).each(
				function() {
				    var img = new Image();
				    var soc = $(this).attr('src');
				    $(img).load(
						function() {
				    	    loadImgs++;
						}
					).attr('src', soc);
				}
			);
    var intVal = window.setInterval(function(){
        if ( loadImgs == $( "img" , overlay ).length ) {
            window.clearInterval( intVal );
            if (overlay.hasClass('open')) {
        overlay.css('display', 'block');
        
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
        overlay.find('.scroll-pane-overlay').setSliderOverlay();
    }
        }
    });

    $('.favIcon').click(function () {
        overlay.css('display', 'block');
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
        overlay.find('.scroll-pane-overlay').setSliderOverlay();
        return false;
    });

    $('.closeOverlay').click(function () {
        $(this).parent().find('.slider-wrap').remove();
        $(this).parent().find('.scroll-content-overlay').css('top', '0px').unbind();
        overlay.css('display', 'none');
        $('#overlay').remove();
    });
}

function OverlayTell() {

    var overlay = $('.tellOverlay');
    var left = ($(window).width() - 530) / 2;
    var top = ($(window).height() - 410) / 2;

    overlay.css(
    {
        'left': left,
        'top': top
    });

    if (overlay.hasClass('open')) {
        overlay.css('display', 'block');
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
    }

    $('.tellIcon').click(function () {
        overlay.css('display', 'block');
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
        return false;
    });

    $('.closeTellOverlay').click(function () {
        overlay.css('display', 'none');
        $('.error',overlay).remove();
        overlay.removeClass('open');
        $('#overlay').remove();
    });
}

function OverlayLang() {

    var overlay = $('.boxOverlay');
    var left = ($(window).width() - overlay.width()) / 2;
    var top = ($(window).height() - overlay.height()) / 2;

    $('.switch').click(function() {
        var url = $(this).attr('href');
        $('.left', overlay).slideUp(function() {
            $('.oView').slideDown();
        });
        $('.btnOK').click(function() {
            window.location.href = url;
            return false;
        });
        $('.btnAbort').click(function() {
            $('.oView').slideUp(function() {
                $('.left', overlay).slideDown();
            });
            return false;
        });
        return false;
    });

    overlay.css(
    {
        'left': left,
        'top': top
    });

    $('.lang').click(function () {
        overlay.css('display', 'block');
        $('<div id="overlay">').css({
            'position': 'absolute',
            'top': 0,
            'left': 0,
            'width': $(window).width(),
            'height': $(window).height(),
            'background': '#000',
            'opacity': 0.5,
            'z-index': 99,
            'display': 'none'
        }).appendTo('body').css('display', 'block');
        return false;
    });

    $('.closeBoxOverlay').click(function () {
        overlay.css('display', 'none');
        $('#overlay').remove();
    });
}

function InitializeBox() {
    if ($('.box').length > 0) {
        var ie = ($.browser.msie && parseInt($.browser.version.substring(0, 1)) == 7) ? true : false;

        $('.box').each(function() {
            var box = $(this);
            var boxWidth = box.width();
            var contentNav = box.parent().hasClass('contentNavi');
            var facebook = box.hasClass('facebook');
            var open = $('.box').children().find('.show').length;
            if (open < 1) {
                open = $('.box').parent().find('.show').length;
            }

            if (!facebook) {
                if (contentNav && box.hasClass('right')) {
                    if ($(window).width() > 960) {
                        boxWidth += ($(window).width() - 960) / 2;
                        box.css('left', (box.width() - boxWidth));
                    }

                    $(window).resize(function() {
                        if ($(window).width() > 960) {
                            boxWidth = box.width() + ($(window).width() - 960) / 2;
                            box.css('left', (box.width() - boxWidth));
                        }
                    });
                }
                
                if (box.height() > 480 && box.find('.scroll-pane').hasClass('messe')) {
                    var beigeHeight = box.find('.beige').height();
                    var winHeight = $(window).height() - 280 - beigeHeight;
                    box.find('.scroll-pane').css(
                    {
                        'overflow': 'hidden',
                        'height': winHeight
                    });
                } else if (box.height() > 480 && box.find('.scroll-pane').height() > $(window).height() - 340) {
                    box.find('.scroll-pane').css(
                    {
                        'overflow': 'hidden',
                        'height': ($(window).height() - 340 < 480) ? "480px" : $(window).height() - 340
                    });
                }

                if ($(window).width() < 960) {
                    $(".container").css({ "overflow-x": "scroll", "width": "960px" });
                } else if (($(window).height() - 190) < box.height() && $(window).width() > 960) {
                    $(".container").css({ "overflow-y": "scroll", "width": "auto" });
                } else {
                    $(".container").css({ "overflow": "hidden", "width": "auto" });
                }

                $(window).resize(function() {
                    if ($(window).width() < 960) {
                        $(".container").css({ "overflow-x": "scroll", "width": "960px" });
                    } else if (($(window).height() - 190) < box.height() && $(window).width() > 960) {
                        $(".container").css({ "overflow-y": "scroll", "width": "auto" });
                    } else {
                        $(".container").css({ "overflow": "hidden", "width": "auto" });
                    }
                });

                if (box.find('.fourthLevel').length > 0) {
                    box.find('.scroll-content')
                .width(box.width() - 199)
                .css({ 'float': 'right' })
                .find(':first').css('padding-top', '38px');
                    if (ie) {
                        box.find('.scroll-content').css({ 'float': 'none' });
                    }

                    box.find('.farbkarte').find('.item').each(function(index) {
                        if (((index + 1) % 5) == 0) {
                            $(this).addClass('paddRight');
                        }
                    });
                }
                else {
                    box.find('.farbkarte').find('.item').each(function(index) {
                        if (((index + 1) % 7) == 0) {
                            $(this).addClass('paddRight');
                        }
                    });
                }
                box.hideBox();
            } else {
                if (box.height() > 480 && box.find('.scroll-pane').height() > $(window).height() - 340) {
                    box.find('.scroll-pane').css(
                    {
                        'overflow': 'hidden',
                        'height': ($(window).height() - 340 < 480) ? "518px" : $(window).height() - 340
                    });
                }

                if ($(window).width() < 960) {
                    $(".container").css({ "overflow-x": "scroll", "width": "960px" });
                } else if (($(window).height() - 190) < box.height() && $(window).width() > 960) {
                    $(".container").css({ "overflow-y": "scroll", "width": "auto" });
                } else {
                    $(".container").css({ "overflow": "hidden", "width": "auto" });
                }

                $(window).resize(function() {
                    if ($(window).width() < 960) {
                        $(".container").css({ "overflow-x": "scroll", "width": "960px" });
                    } else if (($(window).height() - 190) < box.height() && $(window).width() > 960) {
                        $(".container").css({ "overflow-y": "scroll", "width": "auto" });
                    } else {
                        $(".container").css({ "overflow": "hidden", "width": "auto" });
                    }
                });
                $('.fbIcon').click(function() {
                    var left = ($(window).width() - 960) / 2;
                    box.animate({
                        right: (left)
                    }, 1000);
                });
                box.find('.closeIcon').click(function() {
                    var left = ($(window).width() - 960) / 2;
                    box.animate({
                        right: -450
                    }, 1000);
                });
            }
        });
    }
}

var interval;
var sliderAnimate = false;
function InitializeStart() {
    if ($('.startContent').length > 0) {
        setStartNavi();
        var start = $('.startContent');
        var sl = $('.sl');
        var i = 1;
        
        if($(window).height()>560){
            start.css(
            {
                'height': $(window).height() - 130,
                'width': $(window).width()
            });
            $('.text',start).css('top', (start.height() - 400) / 2);
            $('.btnNextStart').css('top', (start.height() - 140) / 2);
            $('.btnPrevStart').css('top', (start.height() - 140) / 2);
        }else{
            start.css(
            {
                'height': 430,
                'width': $(window).width()
            });
            $('.text',start).css('top', 15);
            $('.btnNextStart').css('top', (430 - 140) / 2);
            $('.btnPrevStart').css('top', (430 - 140) / 2);
            $('body').css('overflow-y', 'scroll');
        }
        $(window).resize(function () {
            if($(window).height()>560){
                start.css(
                    {
                        'height': $(window).height() - 130,
                        'width': $(window).width()
                    });
                slider.css('top', (start.height() - 320) / 2);
                $('.text',start).css('top', (start.height() - 400) / 2);
                $('.btnNextStart').css('top', (start.height() - 140) / 2);
                $('.btnPrevStart').css('top', (start.height() - 140) / 2);
            }else{
                $('body').css('overflow-y', 'scroll');
                }
        });

        if ($('.startslider').length > 0) {
            var slider = $('.startslider');
            var loadImgs = 0;
            var width = 0;
            slider.children().hide();
            $('img', slider).each(
				function() {
				    var img = new Image();
				    var soc = $(this).attr('src');
				    $(img).load(
						function() {
				    	    loadImgs++;
						}
					).attr('src', soc);
				}
			);
            var intVal = window.setInterval(function(){
                if ( loadImgs == $( "img" , slider ).length ) {
                    window.clearInterval( intVal );
                    var count = slider.children().length;
					$('li', slider).each(function(index) {
                        $(this).show();
                        $(this).attr('class', i++);
                        width += $(this).width() + 20;
                        if(index+1 == count){
                            slider.css({"width": width});
                        }
                    });
                    if (width < $(window).width()) {
                        $('li', slider).each(function() {
                            $(this).clone().appendTo(slider);
                        });
                        width = 0;
                        count = slider.children().length;
                        $('li', slider).each(function(index) {
                            $(this).show();
                            $(this).attr('class', i++);
                            width += $(this).width() + 20;
                            if(index+1 == count){
                                slider.css({"width": width});
                            }
                        });
//                        slider.width(width);
//                        slider.css({"width": width});
//                    } else {
//                        slider.css({"width": width});
//                        if(!$.browser.msie){
//                            slider.width(width);
//                        }else{
//                            slider.css("width",width+"px");
//                        }
                    };
                    if($(window).height()>560){
                        slider.css('top', (start.height() - 320) / 2);
                    }else{
                        slider.css('top', 50);
                    }
                    animateStart(25, slider, true);
                    $('.btnNextStart').click(function() {
                        if(!sliderAnimate){
                            clearInterval(interval);
                            sliderAnimate = true;
                            slider.animate({
                                'left': '+=500px'
                            },function(){
                                sliderAnimate = false;
                                animateStart(25, slider, false);
                            });
                        }
                    });

                    $('.btnPrevStart').click(function() {
                        if(!sliderAnimate){
                            clearInterval(interval);
                            sliderAnimate = true;
                            slider.animate({
                                'left': '-=500px'
                            },function(){
                                sliderAnimate= false;
                                animateStart(25, slider, true);
                            });
                        }
                    });
                }
            },100);          
        }
    }
}

function setStartNavi() {
    $('.footerContent').children('ul').children('li').each(function () {
        $(this).hideLi();
    });
    $('.meta').hideNaviFooter();
}

function animateStart(speed, slider, right) {
    if (right) {
        interval = setInterval(function () {
            $('li', slider).unbind();
            $('li', slider).mousemove(function (e) {
                clearInterval(interval);
                $('.slideBox', $(this)).css({
                    'top': e.pageY - $(this).offset().top - 15,
                    'left': e.pageX - $(this).offset().left + 20,
                    'display': 'block'
                });
            });
            $('li', slider).mouseleave(function () {
                animateStart(25, slider, true);
                $('.slideBox', $(this)).hide();
            });
            var first = $('li:first', slider);
            var last = $('li:last', slider);
            var lastleft = last.offset().left;
            var lastwidth = last.width() + 20;
            var firstleft = first.offset().left;
            var firstwidth = first.width() + 20;
            if ((lastleft + lastwidth) <= $(window).width() && firstleft + firstwidth <= 0) {
                if (!first.hasClass(last.attr('class'))) {
                    first.clone().appendTo(slider);
                    slider.css({
                        'left': (slider.offset().left + firstwidth)
                    });
                    first.remove();
                } else {
                    first.next().clone().appendTo(slider);
                    slider.css({
                        'left': (slider.offset().left + firstwidth),
                        'width': slider.width() + ((first.next().width() + 20) - firstwidth)
                    });
                    first.remove();
                }
            } else if ((lastleft + lastwidth) <= $(window).width()) {

                if (!first.hasClass(last.attr('class'))) {
                    first.clone().appendTo(slider);
                    slider.css({
                        'width': slider.width() + firstwidth
                    });
                } else {
                    first.next().clone().appendTo(slider);
                    slider.css({
                        'width': slider.width() + (first.next().width() + 20)
                    });
                }
            }
            
            slider.css({
                    'left': slider.offset().left - 1
                });
            
//            if(speed == 25){
//                slider.css({
//                    'left': slider.offset().left - 1
//                });
//            }else{
//                clearInterval(interval);
//                sliderAnimate = true;
//                slider.animate({
//                    'left': '-=500px'
//                },function(){
//                    sliderAnimate= false;
//                    animateStart(25, slider, true);
//                });
//                
//            }
        }, speed);
    } else {
        interval = setInterval(function () {
            $('li', slider).unbind();
            $('li', slider).mousemove(function (e) {
                clearInterval(interval);
                $('.slideBox', $(this)).css({
                    'top': e.pageY - $(this).offset().top - 15,
                    'left': e.pageX - $(this).offset().left + 20,
                    'display': 'block'
                });
            });
            $('li', slider).mouseleave(function () {
                animateStart(25, slider, false);
                $('.slideBox', $(this)).hide();
            });
            var first = $('li:first', slider);
            var last = $('li:last', slider);
            var lastleft = last.offset().left;
            var lastwidth = last.width() + 20;
            var firstleft = first.offset().left;
            var firstwidth = first.width() + 20;
            if (firstleft >= 0 && lastleft >= $(window).width()) {
                if (!last.hasClass(first.attr('class'))) {
                    last.clone().prependTo(slider);
                    slider.css({
                        'left': (slider.offset().left - lastwidth)
                    });
                    last.remove();
                } else {
                    last.prev().clone().prependTo(slider);
                    slider.css({
                        'left': (slider.offset().left - last.prev().width() - 20)
                    });
                    last.remove();
                }
            } else if ((lastleft + lastwidth) >= $(window).width() && firstleft > 0) {
                if (!last.hasClass(first.attr('class'))) {
                    last.clone().prependTo(slider);
                    slider.css({
                        'left': (slider.offset().left - lastwidth),
                        'width': slider.width() + lastwidth
                    });
                } else {
                    last.prev().clone().prependTo(slider);
                    slider.css({
                        'left': (slider.offset().left - (last.prev().width() + 20)),
                        'width': slider.width() + (last.prev().width() + 20)
                    });
                }
            }
            slider.css({
                'left': slider.offset().left + 1
            });
//            if(speed == 25){
//                slider.css({
//                    'left': slider.offset().left + 1
//                });
//            }else{
//                clearInterval(interval);
//                sliderAnimate = true;
//                slider.animate({
//                    'left': '+=500px'
//                },function(){
//                    sliderAnimate = false;
//                    animateStart(25, slider, false);
//                });
//                
//            }
        }, speed);
    }
}



