var $netz98 = $netz98 || {};
// namespace $netz98.Utils

jQuery.extend(true, $netz98, {Utils: new function() {
    
    /**
     * create object reference
     */

    var _this = this;
    
    /**
     * define variables
     */
    this.base_url;
    this.nav;
    this.content;
    this.footer;
    this.url;
    this.scrollbar;
    this.backlink;
    this.appname;
    
    /**
     * imitate flash animation (of old page)
     * page slide effect
     */
    
    this.init = function() {
        jQuery('#page').removeAttr('class'); /* remove "no-script" class from page element */
        
        this.base_url = jQuery('base').attr('href');
        this.nav = jQuery('#nav');
        this.content = jQuery('#content');
        this.footer = jQuery('#footer');
        this.backlink = jQuery('a.back-link');
        
        var _location = window.location.href;
        var _pageId = _location.toString().split('&')[0].split('=')[1];
        
        if (_location.indexOf('.html') != -1 && _location.search('home') == -1) {
            this.setContent(null, _location);
            this.stopBubbles();
        }
        
        if (_location.indexOf('?id=') != -1 && _location.search('home') == -1 && _location.indexOf('id=3') == -1) {
            this.setContent(null, _location);
            this.stopBubbles();
        }
        
        this.convertLinks();
        
        this.nav.find('a').click(function(event) {
            _this.linkHandleClick(jQuery(this), event);
        });
        
        this.footer.find('.meta-link a').click(function(event) {
            _this.linkHandleClick(jQuery(this), event);
        });
        
        this.content.find('a.back-link').click(function() {
           _this.startBubbles();
           _this.hideContent();
        });
        
        this.initFlash();
        this.checkBrowser(jQuery.browser);
    };
    
    this.checkBrowser = function(obj) {
        
        jQuery.each(obj, function(i, val) {
            if (i != 'version') {
                _this.appname = i;
            }
        });

        if (this.appname != 'undefined') {
            this.loadCSS(this.appname);
        }
    };
    
    this.getAgent = function(agent) {
        var _event;
        if (agent.match(/iPad/i))  {
            _event = 'touchstart';
        } else {
            _event = 'click';
        }
        return _event;
    };
    
    this.setScrollbar = function() {
        this.scrollbar = this.content.find('#scrollable').jScrollPane();
        this.updateScrollbar();
    };
    
    this.updateScrollbar = function() {
        this.content.find('.jspDragBottom').height(this.content.find('.jspDrag').height()-8);
    };
    
    this.loadCSS = function(browser) {
        var _browserFile;
        
        switch (browser) {
            case 'safari':
                _browserFile = 'safari';
                break;
        }
        
        jQuery('head').append('<link rel="stylesheet" type="text/css" href="fileadmin/templates/css/' + _browserFile + '.css" media="all" />');
    };
    
    this.convertLinks = function() {
        if (jQuery('object#main_nav').length < 1) {
            this.nav.find('li').each(function() {
               var _anchor = jQuery(this).find('a');
               _anchor.data('origin-href', _anchor.attr('href'));
               _anchor.removeAttr('href');
            });

            this.footer.find('.meta-nav li').each(function() {
               var _anchor = jQuery(this).find('a');
               if (!jQuery(this).hasClass('ext-link')) {
                   _anchor.data('origin-href', _anchor.attr('href'));
                   _anchor.removeAttr('href');
               }
               if (jQuery(this).hasClass('ext-link')) {
                   jQuery(this).find('a').attr('target', '_blank');
               }
            });
        }
        
        this.content.find('a.back-link').removeAttr('href');
    };
    
    this.linkHandleClick = function(elem, event) {
        if (!elem.parent('li').hasClass('ext-link')) {
            this.stopBubbles();
            this.setContent(elem);
            event.preventDefault();
        }
        return false;
    };
    
    this.initFlash = function() {
        var _width = jQuery(window).width();
        var _language = window.location.pathname.split('/')[1];
        var _filename = 'fileadmin/flash/xml/config.xml';
        
        if (window.location.search) {
            switch(window.location.search.split('L=')[1]) {
                case '1':
                    _language = 'de';
                    break;
                case '2':
                    _language = 'en';
                    break;
                case '3':
                    _language = 'es';
                    break;
                case '4':
                    _language = 'ru';
                    break;
            }
        }
        
        if (_language && _language != 'de') {
            _filename = 'fileadmin/flash/xml/config_' + _language + '.xml';
        }
        
        var _flashvars = {
          base_path: this.base_url,
          config_path: _filename
        }
        
        var _params = {
          allowscriptaccess: 'always',
          scale: 'default',
          wmode: 'transparent'
        };

        var _attributes = false;

        swfobject.embedSWF("fileadmin/flash/bubbles.swf", "main_nav", _width, "563", "9", "fileadmin/templates/js/lib/expressInstall.swf", _flashvars, _params, _attributes);
        this.realign();
    };
    
    this.realign = function() {
        window.setTimeout(function() {
            if (jQuery('object#main_nav').length < 1) {
                jQuery('#flash_wrap').addClass('noFlash');
            }
        }, 250);
    };
    
    this.clickHandleFlashButton = function(link) {
        this.stopBubbles();
        this.setContent(false, link);
    };
    
    this.stopBubbles = function() {
        var obj = swfobject.getObjectById("main_nav");
        if (obj && typeof obj.stopBubbles != "undefined") {
          obj.stopBubbles();
        }
    };
    
    this.startBubbles = function() {
        var obj = swfobject.getObjectById("main_nav");
        if (obj && typeof obj.startBubbles != "undefined") {
          obj.startBubbles();
          this.hideContent();
        }
    };

    this.setContent = function(elem, link) {
        var _url = link;
        
        if (elem) {
            _url = elem.data('origin-href');
        }
        
        jQuery.get(
           _url,
           function(data) {
                try {
                    if(jQuery('#scrollable').data('jsp')) {
                        jQuery('#scrollable').data('jsp').destroy();
                    }

                    var newContent = jQuery(data).find('.main-content').html();

                    _this.content.html('');
                    _this.content.html('<div id="scrollable" class="main-content">' + newContent + '</div>');
                    _this.listener(_url);
                    _this.showContent();
                    
                    if (!jQuery.browser.msie) {
                        if (jQuery('#certified').length < 1) {
                            window.setTimeout(function() {
                                jQuery('#scrollable').jScrollPane();
                                _this.updateScrollbar();
                            }, 250);
                        }
                    }

                    _this.toolTip.init();
                } catch(e){alert(e);}
            
                _this.backlink.click(function() {
                   _this.startBubbles();
                   _this.hideContent();
                });

                jQuery('#content').prepend(_this.backlink);
           }
        );
        this.toggleContent();
    };

    this.hideContent = function() {
        this.content.animate({left: -950}, function() {_this.clear();});
        //this.content.find('.main-content').fadeOut();
    };
    
    this.showContent = function() {
        _this.content.css('left', '0px');
        _this.content.show();
    }
    
    this.toggleContent = function() {
        if (this.content.position().left < 0) {
            this.content.animate({left: 0},
                function() {
                    _this.content.find('.main-content').fadeIn();
                    _this.toolTip.init();
            });
        } else {
            this.content.find('.main-content').fadeOut();
            this.content.animate({left: -1050},
                function() {
                    _this.content.animate({left: 0}, 
                        function() { 
                            _this.content.find('.main-content').fadeIn();
                            _this.toolTip.init();
                        });
                });
        }
    };
    
    this.clear = function() {
        this.content.find('#scrollable').remove();
    };
    
    this.listener = function(url) {
        var _contact = jQuery('#mailform');
        
        if (_contact.length > 0) {
            _contact.find('#submit').click(function() {
                _this.submitForm(_contact, url);
                return false;
            });
        }
    };
    
    this.submitForm = function(form, url) {
        jQuery.post(
            url,
            form.serialize(),
            function(data) {
                _this.content.find('.main-content').html(jQuery(data).find('.main-content').html());
                _this.errorHandler.init();
            }
        );
    };
    
    this.toolTip = {
        element: null,
        tipcontent: null,

        init: function() {
            var _self = this;
            this.element = jQuery('#certified');
            
            this.alignBoxes();
            
            jQuery(this.element.find('dt a')).click(function() {
                _self.showBox(jQuery(this).parent('dt'));
            });
            
            jQuery(this.element.find('.dd-close')).click(function() {
                _self.hideBox();
            });
            
            this.element.find('dd').hide();
        },
        
        alignBoxes: function() {
            this.element.find('dd').each(function() {
                jQuery(this).next('dt').html('<a class="' + jQuery(this).next('dt').attr('class') + '" style="display: block;">&nbsp</a>');
                jQuery(this).next('.dd-close').html('<a class="dd-close" style="display: block;">&nbsp</a>');
                var _top = parseInt(jQuery('#content').outerHeight()/2 - jQuery(this).height());
                
                jQuery(this).css('margin-top', _top );
                jQuery(this).find('.dd-content').append('<span class="dd-close">&nbsp;</span>')
            });
        },
        
        showBox: function(elem) {
            this.hideBox();
            elem.next('dd').fadeIn();
        },
        
        hideBox: function(elem){
            this.element.find('dd').fadeOut();
        }
    };
    
    this.errorHandler = {
        form: null,
        
        init: function() {
            this.form = jQuery('#contact_form');
            
            if (this.form.length > 0) {
                this.form.find('.required').each(function() {
                    jQuery(this).parent('span').parent('span').addClass('field-error');
                });
            }
        }
    };
    
    jQuery(document).ready(function() {
        _this.init();
        /*
        jQuery('#nav').roundabout({
                easing: 'swing',
                minScale: 0.3,
                maxScale: 1.0,
                clickToFocus: false,
                shape: 'conveyorBeltLeft',
        });
        */
        /* DEVELOPEMENT        
        jQuery('body').append('<textarea id="blubber"></textarea><input type="submit" id="doit"/>');
        jQuery('#doit').click(function() {
            try {
                eval('{' + jQuery('#blubber').text() + '}');
            } catch(e) {alert(e.message);}
            return false;
        });
        */
    });

return this;}()});

showPage = function(link) {
    $netz98.Utils.clickHandleFlashButton(link);
}


