$(document).ready(function() {

/* Variablen
********************************************************************************************************************/

    var height    = 0,
        pos       = new Object,
        page      = '/index.php',
        fadeSpeed      = 400,
        imageSpeed     = 400,
        pageSpeed      = 400,
        naviSpeed      = 'slow',
        naviFadeSpeed  = 400,
        naviSlideSpeed = 400,
        gradientSpeed = 400,
        bounceSpeed    = 800;

    pos = {
        0 : {
            'grad_left' : {
                'img1' : 'gradient_blue_left.jpg',
                'img2' : 'gradient_rose_left.jpg',
                0      : '356px 0',
                1      : '100px 0px',
                2      : '100px 0px',
                3      : '170px 0'
            },
            'grad_right' : {
                'img1' : 'gradient_blue_right.jpg',
                'img2' : 'gradient_rose_right.jpg',
                0      : '100% 100%',
                1      : '150% 100%',
                2      : '150% 100%',
                3      : '100% 100%'
            },
            'navi_image' : {
                0 : '357px',
                1 : '212px'
            },
            'left_content' : {
                0 : '357px',
                1 : '212px'
            },
            'content' : {
                0 : '390px',
                1 : '700px'
            }
        },
        'imgPath' : '/source/images/'
    };

/* Initialisieren
********************************************************************************************************************/

    initSite(pos);

    $.history.init(function(hash) {
        loadSite(hash);
    });


/* Funktionen
********************************************************************************************************************/

    function gallery() {
        new_src = $(this).attr('href');
        if (new_src == $('#gallery_big a').attr('href')) return false;
        new_desc   = $(this).parent('div').parent('li').children('p').html();
        old_height = $('#gallery_big a img').height();
        old_width  = $('#gallery_big a img').height();
        con_height = $('#content').outerHeight();
        $('#gallery_big').css('height',old_height);
        $('#gallery_big a img').removeAttr('height');

        newImage     = new Image();
        newImage.src = new_src;

        newImage.onload = function() {
            new_height = (430/newImage.width)*newImage.height;
            $('#gallery_big a img').fadeOut(fadeSpeed,function() {
                $('#gallery_big a img').attr('src',newImage.src);
                height = con_height-old_height+new_height;
                height = (height >= 520) ? height : 520;
                height = (height >= $('#navi').outerHeight()) ? height : $('#navi').outerHeight();
                $('#gallery_big').animate({height:new_height});
                $("#right").animate({
                    height: height
                }, pageSpeed, function() {
                    $('#gallery_big a').unbind('click');
                    $('#gallery_big a').attr('href',new_src);
                    $('#gallery_big a img').fadeIn(fadeSpeed);
                    tb_init('#gallery_big a.thickbox');
                    imgLoader = new Image();
                    imgLoader.src = tb_pathToImage;
                });
            });
            $('#imgsidebar p').html(new_desc);
        }
        return false;
    }

    function initSite(pos) {
        $('#wrapper').css({backgroundPosition: pos[0]['grad_right'][0]});
        $('#content').html('');
        $('.headnavi').html('');
        if (window.location.hash != '') {
            page = window.location.hash.substring(1);
            if (checkIfCatalog()) {
                $('#gradient_top').css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_left']['img1']+')'});
                $('#gradient_top').css({backgroundPosition: pos[0]['grad_left'][3]});
                $('#wrapper')     .css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_right']['img1']+')'});
                $('#backimage')   .css('width',pos[0]['navi_image'][1]);
                $('#left')        .css('width',pos[0]['left_content'][1]);
                $('#content')     .css('width',pos[0]['content'][1]);
                $('#content')     .css('paddingRight','0');
                $('#right')       .css({width: '745px'});

                $('body').addClass('catalog');
                $('.change_view').removeClass('go_to_catalog').addClass('go_to_main').attr('href','/index.php');
            } else {
                $('body').removeClass('catalog');

                $('#gradient_top').css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_left']['img2']+')'});
                $('#gradient_top').css({backgroundPosition: pos[0]['grad_left'][0]});
                $('#wrapper')     .css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_right']['img2']+')'});
                $('#backimage')   .css('width',pos[0]['navi_image'][0]);
                $('#left')        .css('width',pos[0]['left_content'][0]);
                $('#content')     .css('width',pos[0]['content'][0]);
                $('#content')     .css('paddingRight','0');
                $('#right')       .css({width: '600px'});

                $('.change_view').removeClass('go_to_main').addClass('go_to_catalog').attr('href','/Catalog/index.php');
            }
            $('.headnavi .active').removeAttr('class');
            $('.headnavi a[href='+page+']').parent().addClass('active');
        } else { 
            page = window.location.pathname+window.location.search;
            if (checkIfCatalog()) {
                $('body').addClass('catalog');
                $('#gradient_top').css({backgroundPosition: pos[0]['grad_left'][3]});
            } else {
                $('body').removeClass('catalog');
                $('#gradient_top').css({backgroundPosition: pos[0]['grad_left'][0]});
            }
        }
        var nextNavi = (checkIfCatalog()) ? 'catalognavi' : 'mainnavi';
    
        loadContent(page,1);
        loadMainnavi(page,nextNavi);

        $('.change_view').each(initAjaxReload).click(initHistory);
        $('.ajaxLoad').each(initAjaxReload).click(initHistory);
    }

    function initHistory() {
        $.history.load(this.href.replace(/^.*#/, ''));
        return false;
    }
    
    function loadSite(hash) {
        if ($('#innerleft *').is('a[href=#'+hash+']')) {
            var obj = $('#innerleft a[href=#'+hash+']');
            if (obj.html() != null) {
                if (typeof(obj) == 'object') {
                    if (obj.hasClass('change_view')) {
                        // Animationen und Funktionsaufruf
                        $('#content').fadeOut(fadeSpeed,function() {
                            $('#innerleft').fadeOut(fadeSpeed,function() {
                                if ($('#content').width() < 400) {
                                    animate_to_catalog(pos,hash);
                                } else {
                                    animate_to_main(pos,hash);
                                }
                            });
                        });
                    } else {
                        // Navigation Aktiv-Zustand setzen
                        setNaviActive(hash);

                        // Subnavi laden
                        checkSubnavi(hash,obj);

                        // Content Animation + Ajax
                        loadContent(hash);
                    }
                }
            }
        } else if ($('#content *').is('a[href=#'+hash+']')) {
            if (hash.indexOf('/Catalog/') >= 0 && $('#content').width() < 400) {
                $('#content').fadeOut(fadeSpeed,function() {
                    $('#innerleft').fadeOut(fadeSpeed,function() {
                        animate_to_catalog(pos,hash);
                    });
                });
            } else if (hash.indexOf('/Catalog/') == -1 && $('#content').width() > 400) {
                $('#content').fadeOut(fadeSpeed,function() {
                    $('#innerleft').fadeOut(fadeSpeed,function() {
                        animate_to_main(pos,hash);
                    });
                });
            } else {
                loadContent(hash);
            }
        } else {
            if (checkIfCatalog() && $('#content').width() < 400) {
                $('#inlinecontent').fadeOut(fadeSpeed,function() {
                    $('#innerleft').fadeOut(fadeSpeed,function() {
                        animate_to_catalog(pos,hash);
                    });
                });
            } else if (!checkIfCatalog() && $('#content').width() > 400) {
                $('#inlinecontent').fadeOut(fadeSpeed,function() {
                    $('#innerleft').fadeOut(fadeSpeed,function() {
                        animate_to_main(pos,hash);
                    });
                });
            }
        }
    }

    function setNaviActive(hash) {
        var parampos = hash.indexOf('?');
        if (parampos>0) hash = hash.substring(0,parampos);
        obj = $('#innerleft a[href=#'+hash+']').parent();
        if (obj.parent().hasClass('subnavi')) {
            $('#innerleft .active').removeAttr('class');
            obj.parent().parent().addClass('active');
        } else {
            $('#innerleft .active').removeAttr('class');
        }
        obj.addClass('active');
    }

    function loadContent(hash,first) {
        first = (first) ? first : 0;
        var parampos = hash.indexOf('?');
        if (parampos>0 && first==0) {
            content_obj = $('#tabbar a[href=#'+hash+']');
            $('#tabbar a.active').removeClass('active');
            content_obj.addClass('active');

            objID  = '#tab_wrap';
            loadID = '#inlinetab_wrap';
        } else {
            objID  = '#content';
            loadID = '#inlinecontent';
        }
        if (objID != '#content') {
            old_height = $('#content').outerHeight();
            tab_height = $('#tab_content').outerHeight();
        }
        obj = $(objID);
        $('#content h1,#content h2,#content h3,#content h4,#content h5,#content h6,#content p,#content li,.underline').css('backgroundColor','#fff');

        obj.fadeOut(fadeSpeed,function() {
            obj.html('');
            obj.show();
            obj.load(hash+' '+loadID,{},function() {
                $('title:first').load(hash+' title',{},function() {
                    var title = $('title:first').text();
                    $('title:first').html(title);
                    document.title = title;
                });
                obj.hide();
                if (objID == '#content') {
                    height = obj.outerHeight();
                } else {
                    height = old_height-tab_height+obj.outerHeight();
                }
                height = (height >= 520) ? height : 520;
                height = (height >= $('#navi').outerHeight()) ? height : $('#navi').outerHeight();
                $("#right").animate({
                    height: height
                }, pageSpeed, function() {
                    obj.fadeIn(fadeSpeed,function() {
                        var noImageAni = 0;
                        jQuery.each(jQuery.browser, function(i, val) {
                            if(i=="mozilla" && jQuery.browser.version.substr(0,3)=="1.8") {
                                noImageAni = 1;
                            }
                        });
                        if (noImageAni == 0) {
                            loadImage(hash);
                        }
                    });
                    $(objID+' a.ajaxLoad').each(initAjaxReload).click(initHistory);
                    tb_init(objID+' a.thickbox');
                    imgLoader = new Image();
                    imgLoader.src = tb_pathToImage;
                    $('.clickimage').click(gallery);
                });
            });
        });
    }

    function checkIfCatalog() {
        var hash = (window.location.hash != '') ? window.location.hash.substring(1) : window.location.pathname.substring(1);
        if (hash.indexOf('Catalog') >= 0) return true;
        else return false;
    }

    function initAjaxReload() {
        $(this).attr('href','#'+$(this).attr('href'));
    }

    function checkSubnavi(hash,obj) {
        //alert(hash);
        if ((obj.parent().parent().hasClass('subnavi') && obj.parent().parent().css('display') != 'block') || (!obj.parent().parent().hasClass('subnavi') && obj.parent().children('ul').css('display') != 'block')) {
            var fadeNavi = $('.subnavi').filter(function() {
                if ($(this).css('display') == 'block')
                    return true;
            });
            if (fadeNavi.children('li').size() > 0) {
                if (checkIfCatalog()) {
                    fadeNavi.slideUp(naviSlideSpeed,function() {
                        loadSubnavi(hash,obj);
                    });
                } else {
                    fadeNavi.fadeOut(naviFadeSpeed,function() {
                        loadSubnavi(hash,obj);
                    });
                }
            } else {
                loadSubnavi(hash,obj);
            }
        }
    }

    function loadSubnavi(hash,obj) {
        if (obj.parent().children('ul').hasClass('subnavi') || obj.parent().parent().hasClass('subnavi')) {
            var subnavi = (obj.parent().children('*').is('.subnavi')) ? obj.parent().children('.subnavi') : obj.parent().parent();
            if (checkIfCatalog()) {
                height = $('#navi').outerHeight()+subnavi.outerHeight();
                if (height > $('#navi').outerHeight()) {
                    $("#right").animate({
                        height: height
                    }, pageSpeed, function() {
                        subnavi.slideDown(naviSlideSpeed);
                    });
                } else {
                    subnavi.slideDown(naviSlideSpeed);
                }
            } else {
                subnavi.fadeIn(naviFadeSpeed);
            }
        }
    }

    function loadMainnavi(hash,newNavi) {
        var parampos = hash.indexOf('?');
        if (parampos>0) hash = hash.substring(0,parampos);
        $('.headnavi').remove();
        $('#navi').load('/source/ajaxNavi.php #'+newNavi,{navi:newNavi},function() {
            var obj = $('.headnavi a[href='+hash+']');
            obj.parent().addClass('active');
            if (obj.parent().children('*').is('.subnavi')) {
                $('.subnavi').hide();
                obj.parent().children('.subnavi').show();
            } else {
                $('.headnavi .subnavi').each(function() {
                    var i = 0;
                    $(this).children('li').each(function() {
                        if ($(this).children('a').attr('href') == hash) {
                            i++;
                        }
                    });
                    if(i>0){$(this).parent('li').addClass('active');}else{$(this).hide();}
                });
            }
            $('.headnavi').hide().fadeIn(naviSpeed);
            $('.headnavi a').each(initAjaxReload).click(initHistory);
        });
    }

    function loadImage(hash) {
        $('#backimage').prepend('<div id="image2"></div>');
        $('#image2').load(hash+' #image1 img',{},function() {
            $('#image1').fadeTo(imageSpeed,0,function(){
                $('#image1').remove();
                $('#image2').attr('id','image1');
            });
        });
        //var height = $('#wrapper').height();
        //$('#backimage').prepend('<div id="image2" style="display:none"><img src="/source/images/navi_back/back'+i+'.jpg" alt="" /></div>');
        //$('#layout').height(height);
        //$('#image2').show();
        //$('#layout').css('position','static');
        //$('#layout').css('position','absolute');
    }


    function animate_to_main(pos,hash) {
        $('#right').css({width: '600px'});
        $('#content').css({width: '390px'});
        loadMainnavi(hash,'mainnavi');

        $('.change_view').addClass('go_to_catalog').removeClass('go_to_main').attr('href','#/Catalog/index.php');

        $('#gradient_top').animate({backgroundPosition: '('+pos[0]['grad_left'][2]+')'},gradientSpeed,'easeOutQuad');
        $('#wrapper')     .animate({backgroundPosition: '('+pos[0]['grad_right'][2]+')'},gradientSpeed,'easeOutQuad',function() {

            $('#wrapper')  .css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_right']['img2']+')'});

            $('#wrapper')  .animate({backgroundPosition: '('+pos[0]['grad_right'][0]+')'},gradientSpeed,'easeOutQuad');
            $('#left')     .animate({width: pos[0]['left_content'][0]},bounceSpeed,'easeOutBounce');
            $('#backimage').animate({width: pos[0]['navi_image'][0]},bounceSpeed,'easeOutBounce',function() {
                $('#gradient_top').css({backgroundPosition: pos[0]['grad_left'][1]});
                $('#gradient_top').css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_left']['img2']+')'});
                $('#gradient_top').animate({backgroundPosition: '('+pos[0]['grad_left'][0]+')'},gradientSpeed,'easeOutQuad');

                $('body').removeClass('catalog');
                if (jQuery.browser.msie) {
                    if (jQuery.browser.version < 7) {
                        $('#content').css({paddingRight: '156px'});
                    }
                    $('#innerleft').show();
                    loadContent(hash);
                } else {
                    $('#innerleft').fadeIn(naviSpeed,function() {
                        loadContent(hash);
                    });
                }
            });
        });
    }

    function animate_to_catalog(pos,hash) {
        loadMainnavi(hash,'catalognavi');

        $('.change_view') .addClass('go_to_main').removeClass('go_to_catalog').attr('href','#/index.php');

        $('#wrapper')     .animate({backgroundPosition: '('+pos[0]['grad_right'][1]+')'},gradientSpeed,'easeOutQuad');
        $('#gradient_top').animate({backgroundPosition: '('+pos[0]['grad_left'][1]+')'},gradientSpeed,'easeOutQuad',function() {

            $('#gradient_top').css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_left']['img1']+')'});
            $('#wrapper')     .css({backgroundImage: 'url('+pos['imgPath']+pos[0]['grad_right']['img1']+')'});

            $('#wrapper')     .animate({backgroundPosition: '('+pos[0]['grad_right'][3]+')'},gradientSpeed,'easeOutQuad');
            $('#gradient_top').animate({backgroundPosition: '('+pos[0]['grad_left'][3]+')'},gradientSpeed,'easeOutQuad');
            $('#backimage')   .animate({width: pos[0]['left_content'][1]},bounceSpeed,'easeOutBounce');
            $('#left')        .animate({width: pos[0]['navi_image'][1]},bounceSpeed,'easeOutBounce',function() {
                $('#right')  .css({width: '745px'});
                $('#content').css({width: '700px'});

                $('body').addClass('catalog');
                if (jQuery.browser.msie) {
                    if (jQuery.browser.version < 7) {
                        $('#content').css({paddingRight: '0px'});
                    }
                    $('#innerleft').show();
                    loadContent(hash);
                } else {
                    $('#innerleft').fadeIn(naviSpeed,function() {
                        loadContent(hash);
                    });
                }
            });
        });
    }

});
