(function($) {
    
    $.fn.screens = function() {
        return this.each(function() {
            
            var $container = $(this);
            var $screens = $container.children().eq(0);
            
            var $prev = $("<span/>").addClass('prev').html('&lsaquo;').appendTo($container);
            var $next = $("<span/>").addClass('next').html('&rsaquo;').appendTo($container);
            
            var $images = $screens.find('img');
            
            var $slider = $("<div/>").append($images).appendTo($screens).css({position: 'absolute', left: 0, top: 0});
            
            var x = 0;
            $images.each(function() {
                $(this).css({left: x+'px'});
                x += parseInt($(this).attr('data-width'));
            }).show();

            $slider.css({height: $screens.height()+'px', width: x+'px'});
            
            var $current = $images.eq(0);
            
            var _load = function($img, dir) {
                

                var w = $current.attr('data-width')+'px';
                var h = $current.attr('data-height')+'px';
                
                $img.css({width:w,height:h});

                var l = -1 * $img.position().left;
                $slider.animate({left:l+'px'}, {duration: 600, easing: 'easeInOutCubic'});
                
                w = $img.attr('data-width')+'px';
                h = $img.attr('data-height')+'px';
                
                var t = Math.floor((532.0-parseInt(h))/2.0);
                var l = Math.floor((710.0-parseInt(w))/2.0);
                $img.animate({width:w, height:h}, {duration: 600, easing: 'easeInOutCubic'});
                $current.animate({width:w, height:h}, {duration: 600, easing: 'easeInOutCubic'});                
                $container.animate({width:w, height:h, top: t+'px', left: l+'px'}, {duration: 600, easing: 'easeInOutCubic'});
            }
            
            $prev.click(function() {
                var idx = $images.index($current);

                if (idx == 0) return;

                idx--;
                _load($images.eq(idx),-1);
                if (idx == 0) {
                    $prev.fadeOut();
                }
                $next.fadeIn();
                $current = $images.eq(idx);
            });

            $next.click(function() {
                var idx = $images.index($current);

                if (idx + 1 ==  $images.length) return;

                idx++;
                _load($images.eq(idx),1);
                if (idx + 1 ==  $images.length) {
                    $next.fadeOut();
                }
                $prev.fadeIn();
                $current = $images.eq(idx);
            });
            
        });
    }
    
    $.fn.m = function() {
        return this.each(function() {
            var $m = $(this);
            var t = this.getAttribute('data-to');
            $m.html(t+'&#64;&#114;&#101;&#101;&#100;&#101;&#114;&#46;&#99;&#104;');
            $m.attr('href','mailto:'+$m.text());
        });
        
	}
	
    $.fn.indicator = function() {
        return this.each(function() {
            var $obj = $(this);

            var $rows = $obj.find('a');
            
            var $indicator = $("<div/>").addClass('indicator').appendTo($obj);
            
            var $current = null;
            
                        
            var _set = function(anim) {
                var top = $current.position().top;
                
                var diff = Math.ceil(($current.outerHeight() - $indicator.outerHeight()) / 2.0);
                
                top += diff;
                
                
                if (anim) {
                    $indicator.animate({top: top+'px'}, {easing: 'easeOutCubic', duration: 300});
                }
                else {
                    $indicator.css({top: top+'px'});
                }
                
                setTimeout(function() {
                    $rows.removeClass('selected');
                    $current.addClass('selected');
                },100);
            }
            
            $rows.click(function() {
                $current = $(this);
                _set(true);
                var href = $current.attr('href');
                window.location.hash = href;
            });
            
            
            var path = window.location.pathname;
            $current = $rows.filter('[href$="'+path+'"]');
            if ($current.length == 0) {
                path = path.substring(0, path.lastIndexOf('/'));
                $current = $rows.filter('[href$="'+path+'"]');
            }
            if ($current.length > 0) {
                $current = $current.eq(0);
                _set(false);
            }
            
        });
    }
    
    
    $.fn.subnavigation = function(first) {
        
        return this.each(function() {
            
            var $obj = $(this);
            var $rows = $obj.find('a');
            
            var $current = null;
            
            if (first) {
                $current = $rows.eq(0);
            }
            else {
                var path = window.location.pathname;
                $current = $rows.filter('[href$="'+path+'"]');
                if ($current.length == 0) {
                    path = path.substring(0, path.lastIndexOf('/'));
                    $current = $rows.filter('[href$="'+path+'"]');
                }
                if ($current.length == 0) {
                    $current = $rows.eq(0);
                }
            }
                        
            $current.addClass('selected');

            var _load_content = function(ret, asc) {

                var up = '-';
                var down = '';
                if (!asc) {
                    up = '';
                    down = '-';
                }
                
                var $currentcontent = $('.sub-content');

                var $html = $(ret).children().eq(1);
                $html.css({top: down+'600px'});
                $('.content').append($html);
                $currentcontent.stop().animate({top: up+'600px',opacity: 0},{duration: 750, complete: function() {
                    $(this).remove();
                }});
                $html.animate({top: '-1px'},{ duration: 750});
            }
            
            $rows.click(function() {

                var $a = $(this);

                if ($a.attr('href') == $current.attr('href')) {
                    return false;
                }
                
                var asc = ($rows.index($current) < $rows.index($a));

                $current.removeClass('selected');
                $current = $a;
                $current.addClass('selected');
                
                var cache = {};

                var href = $a.attr('href');

                if (cache[href]) {
                    _load_content(cache[href],asc);
                }
                else {
                    $.get(href, function(ret) {
                        cache[href] = ret;
                        _load_content(ret,asc);
                    });
                }
                var href = $current.attr('href');
                window.location.hash = href;
                
                return false;
            });
        });
    }
    
    
    $.fn.navigation = function() {
        return this.each(function() {
            
            var $obj = $(this);
            
            
            $obj.indicator();
            
            var $rows = $obj.find('a');
            
            var _cache = {};
            
            var $content = $('#content');
            
            $content.find('.subnavigation').subnavigation();
            
            $('.subnav-proxy').live('click', function() {
                var href = $(this).attr('data-href');
                $('.subnavigation a').filter('[href$="'+href+'"]').click();
                return false;
            });
            
            var _loaded = function(html) {

                var w = $content.width();
                
                var $old = $content.children();
                
                $old.css({position: 'absolute', top: 0, left: 0, width: $old.width()+'px', height: $old.height()+'px'});
                $old.animate({left: '-'+(w+0)+'px', opacity: 0}, {easing: 'easeOutCubic', duration: 750, complete: function() {
                    $(this).remove();
                }});

                var $html = $(html);
                $html.prependTo($content).css({left: '-'+(w+100)+'px', opacity: 0}).animate({left: 0, opacity: 1},{easing: 'easeOutCubic', duration: 750});
                
                $html.find('.subnavigation').subnavigation(true);   
                $html.find('.screens-container').screens();
                $html.find('.m').m().removeClass('m');
        	    
                     
            }
            
            $rows.click(function() {
                
                var $a = $(this);
                
                if ($a.hasClass('selected')) {
                    return false;
                }
                
                var src = $a.attr('href');
                
                if (_cache[src]) {
                    _loaded(_cache[src]);
                }
                else {
                    $.get(src, function(ret) {
                        _cache[src] = ret;
                        _loaded(ret);
                    });
                }
                
                return false;
            });
        });
    }
    
    
    
    
    $(function() {
        $('#navigation').navigation();
        $('.screens-container').screens();
        $('.m').m().removeClass('m');
	    
        
        if ($.browser.webkit) $(document.documentElement).addClass('webkit');
        
        
        
        $('.help .q').live('click',function() {
            var $q = $(this);
            var $current = $('.help .selected');
            
            $current.removeClass('selected');
            $q.parent().addClass('selected');
            
            return false;
        });
        
    });

    if (window.location.hash && window.location.hash.length > 1) {
        window.location.href = window.location.hash.replace('#','');
    }
    
})(jQuery);

