Bfa = window.Bfa || {};

(function($) {
$(function() { // document ready
    
    
    
    
    var _drawedButton = function($btn,dir,inactiveClr) {
        
        if (!inactiveClr) inactiveClr = '#555555'
        
        var clr     = $btn.css('color');
        var _btn    = Raphael($btn.get(0),60,120);
        
        var _circle = null;
        var _arrow = null;
        
        var arr = null;
        if (dir == 1) {
            arr = ["23 57","31 57","31 53","38 60","31 67","31 63","23 63"];
        }
        else {
            arr = ["37 57","29 57","29 53","22 60","29 67","29 63","37 63"];
        }

        _circle = _btn.circle(30,60,14).attr('stroke-width','3').attr('stroke',inactiveClr);
        _arrow = _btn.path("M"+arr.join("L")+"Z").attr('stroke-width','1').attr('stroke',inactiveClr).attr('fill',inactiveClr);
        
        var _draw = function() {
            _circle.attr('stroke',inactiveClr);
            _arrow.attr('stroke',inactiveClr).attr('fill',inactiveClr);
        }

        var _drawHover = function() {
            _circle.attr('stroke',clr);
            _arrow.attr('stroke',clr).attr('fill',clr);
        }

        _draw();
        $btn.hover(
            function() { _drawHover(); },
            function() { _draw(); }
        );
    }


    /**
     * Subnavigation
     */
    // var $subnav = $('#navigation>ul>li.selected>.child-menu');
    // if ($subnav.length > 0) {
    //     $('.paragraph-standard-subnav .subnavigation').each(function() {
    //         var $nav = $("<ul/>").addClass('subnav').html($subnav.html());
    //         $(this).append($nav);
    //     });
    // }
    
    /**
     * Open external teaser links in new window.
     */
     
    $('.paragraph-teaser .teaser-link').click(function() {
        var $a = $(this);
        if ($a.attr('href').substring(0,7) == 'http://') {
            window.open($a.attr('href'));
            return false;
        }
    });
    
    
    
    
    /**
     * Content dropdown (append)
     */
     
    $.fn.inplaceContentAppender = function() {
        
        return this.each(function() {
            
            var $obj = $(this);
                
            var animDurationOpen = 400;
            var animDurationClose = 300;
        
            var $intro = $obj.find('.intro');
            var $body = $obj.find('.body');
            var $content = $body.find('.body-content');
                
            var expanded = false;
            var _toggle = function() {
                expanded = !expanded;
                if (expanded) {
                    $intro.addClass('bfa-expanded');
                    $body.animate({
                        height: $content.height()+'px'
                    }, animDurationOpen);
                }
                else {
                    $intro.removeClass('bfa-expanded');
                    $body.trigger('willCollapse');
                    $body.animate({
                        height: 0
                    }, animDurationClose, function() {
                        $body.trigger('didCollapse');
                    });
                }
            }
                
            var $_close = null;            
            var $close = function() {
                if (!$_close) {
                    $_close = $("<a/>").attr('href','#close').addClass('close-button').appendTo($intro).click(function() {
                        _toggle();
                        $(this).hide();
                        return false;
                    });

                    var clr = $_close.css('color');
                    var _btn = Raphael($_close.get(0),32,32);
                    
                    var btn = _btn.set();
                    btn.push(
                        _btn.circle(16,16,14).attr('stroke-width','3'),
                        _btn.path("M10 10L22 22").attr('stroke-width','5'),
                        _btn.path("M22 10L10 22").attr('stroke-width','5')
                    );
                    
                    var _draw = function() {
                        btn.attr('stroke', '#555555');
                    }
                    
                    var _drawHover = function() {
                        btn.attr('stroke', clr);
                    }

                    _draw();
                    $_close.hover(
                        function() { _drawHover(); },
                        function() { _draw(); }
                    );
                    
                    $_close.hide();

                }

                setTimeout(function() {
                    $_close.fadeIn(200);
                },150);
                return $_close;
            }
            
                
            $intro.click(function() {
                
                $close();
                if (!expanded) {
                    _toggle();
                }
                
                return false;
                
            })
            
            
        });
        
    }
    
    
    
    /**
     * Content dropdown (replace)
     */
    
    $.fn.inplaceContentLoader = function() {
        
            return this.each(function() {
                var $obj    = $(this);
                var $intro  = $obj.find('.intro');
                var $detail = $obj.find('.detail');

                var introHeight = 0;
                var detailHeight = 0;
                
                var animDurationOpen = 400;
                var animDurationClose = 300;
                
                var $_close = null;            
                var $close = function() {
                    if (!$_close) {
                        $_close = $("<a/>").attr('href','#close').addClass('close-button').appendTo($detail).click(function() {
                            $intro.fadeIn(animDurationClose);
                            $detail.fadeOut(animDurationClose);
                            $obj.animate({
                                height: introHeight+'px'
                            },animDurationClose);
                            return false;
                        });

                        var clr = $_close.css('color');
                        var _btn = Raphael($_close.get(0),32,32);
                        
                        var btn = _btn.set();
                        btn.push(
                            _btn.circle(16,16,14).attr('stroke-width','3'),
                            _btn.path("M10 10L22 22").attr('stroke-width','5'),
                            _btn.path("M22 10L10 22").attr('stroke-width','5')
                        );
                        
                        var _draw = function() {
                            btn.attr('stroke', '#555555');
                        }
                        
                        var _drawHover = function() {
                            btn.attr('stroke', clr);
                        }

                        _draw();
                        $_close.hover(
                            function() { _drawHover(); },
                            function() { _draw(); }
                        );

                    }
                    return $_close;
                }
                                
                $intro.click(function() {
                    if (introHeight == 0) {
                        introHeight = $intro.outerHeight();
                        $detail.show();
                        detailHeight = $detail.outerHeight();
                        $detail.hide();
                    }
                    
                    $intro.fadeOut(animDurationOpen);
                    $detail.fadeIn(animDurationOpen);
                    
                    $obj.animate({
                        height: detailHeight+'px'
                    },animDurationOpen);
                    
                    // Make sure the close button is added to $detail
                    $close();
                    
                    return false;
                });
            });
    }
    
    
    
    
    
    /**
     * Gallery strip
     */
        
    $.fn.strip = function() {
        
        return this.each(function() {
            
            var $container = $(this);
            
            var $next = $("<span/>").addClass('next').appendTo($container).hide();
            var $prev = $("<span/>").addClass('prev').appendTo($container).hide();




            var imagesPerRow    = 4;
            var $images         = $container.find('li');
            var index           = 0;
            var $list           = $container.find('ul');

            var _validate = function() {
                if (index > 0) $prev.show();
                else $prev.hide();
                if (index+imagesPerRow < $images.length) $next.show();
                else $next.hide();
            }
            
            _validate();
            
            
            var _slide = function(dir) {

                var w = $images.eq(0).outerWidth();

                var indexBefore = index;

                index += dir*4;

                if (index < 0) index = 0;
                if (index >= $images.length-4) index = $images.length - 4;
                
                var left = index * -1 * w;
                                
                $list.animate({
                    left: left+'px'
                },500);
                
                _validate();

            }

            $next.click(function() {
               _slide(1);
               return false; 
            });
            
            $prev.click(function() {
                _slide(-1);
                return false;
            });
            
            
            
            
            
            /**
             * Draw next/prev buttons.
             */
             
            (function() {

                var _drawedButton = function($btn,dir) {
                    var clr     = $btn.css('color');
                    var _btn    = Raphael($btn.get(0),60,120);
                    
                    var _circle = null;
                    var _arrow = null;
                    
                    var arr = null;
                    if (dir == 1) {
                        arr = ["23 57","31 57","31 53","38 60","31 67","31 63","23 63"];
                    }
                    else {
                        arr = ["37 57","29 57","29 53","22 60","29 67","29 63","37 63"];
                    }

                    _circle = _btn.circle(30,60,14).attr('stroke-width','3').attr('stroke','#999999');
                    _arrow = _btn.path("M"+arr.join("L")+"Z").attr('stroke-width','1').attr('stroke','#999999').attr('fill','#999999');
                    
                    var _draw = function() {
                        _circle.attr('stroke','#999999');
                        _arrow.attr('stroke','#999999').attr('fill','#999999');
                    }

                    var _drawHover = function() {
                        _circle.attr('stroke',clr);
                        _arrow.attr('stroke',clr).attr('fill',clr);
                    }

                    _draw();
                    $btn.hover(
                        function() { _drawHover(); },
                        function() { _draw(); }
                    );
                }
                
                _drawedButton($prev,-1);
                _drawedButton($next, 1);
                
                
            })();
            
            
            
        });
        
    }
    
    /**
     * Gallery
     */
         
    $.fn.gallery = function() {
            
        return this.each(function() {
                        
            var $container  = $(this);
            var $overview   = $container.find('.gallery-overview');
            
            var containerHeight = $overview.height();
            var containerWidth = $container.width();
            
            var padding = 60;
            
                        
            var index = 0;
            var $images = $container.find("[data-src]");

            var $next = $("<span/>").addClass('next').appendTo($container).hide();
            var $prev = $("<span/>").addClass('prev').appendTo($container).hide();
            var $back = $("<span/>").addClass('close').appendTo($container).hide();
            
            
            
            
            /**
             * Back/Close button drawing.
             */
             
            (function() {
                var clr = $back.css('color');
                var _btn = Raphael($back.get(0),60,60);
            
                var btn = _btn.set();
                btn.push(
                    _btn.circle(30,30,14).attr('stroke-width','3'),
                    _btn.path("M24 24L36 36").attr('stroke-width','5'),
                    _btn.path("M36 24L24 36").attr('stroke-width','5')
                );
            
                var _draw = function() {
                    btn.attr('stroke', '#555555');
                }
            
                var _drawHover = function() {
                    btn.attr('stroke', clr);
                }

                _draw();
                $back.hover(
                    function() { _drawHover(); },
                    function() { _draw(); }
                );
            })();
            
            
            /**
             * Draw next/prev buttons.
             */
             
            (function() {

                var _drawedButton = function($btn,dir) {
                    var clr     = $btn.css('color');
                    var _btn    = Raphael($btn.get(0),60,120);
                    
                    var _circle = null;
                    var _arrow = null;
                    
                    var arr = null;
                    if (dir == 1) {
                        arr = ["23 57","31 57","31 53","38 60","31 67","31 63","23 63"];
                    }
                    else {
                        arr = ["37 57","29 57","29 53","22 60","29 67","29 63","37 63"];
                    }

                    _circle = _btn.circle(30,60,14).attr('stroke-width','3').attr('stroke','#555555');
                    _arrow = _btn.path("M"+arr.join("L")+"Z").attr('stroke-width','1').attr('stroke','#555555').attr('fill','#555555');
                    
                    var _draw = function() {
                        _circle.attr('stroke','#555555');
                        _arrow.attr('stroke','#555555').attr('fill','#555555');
                    }

                    var _drawHover = function() {
                        _circle.attr('stroke',clr);
                        _arrow.attr('stroke',clr).attr('fill',clr);
                    }

                    _draw();
                    $btn.hover(
                        function() { _drawHover(); },
                        function() { _draw(); }
                    );
                }
                
                _drawedButton($prev,-1);
                _drawedButton($next, 1);
                
                
            })();
            
            
            $next.click(function(){
                index++;
                if (index >= $images.length) index = 0;
                dir = 1;
                $images.eq(index).click();
                return false;
            });
            $prev.click(function() {
                index--;
                if (index < 0) index = $images.length - 1;
                dir = -1;
                $images.eq(index).click();
                return false;
            });
            
            $back.click(function() {
                
                dir = 1;
                $prev.hide();
                $next.hide();
                $back.hide();
                
                $container.animate({
                    height: containerHeight+'px'
                });
                
                $overview
                    .css({left: '-1000px'})
                    .animate({
                        left: 0
                    }, function() {
                        $overview.css({
                            top: 'auto',
                            bottom: 0
                        })
                    });
                
                if ($current) {
                    $current.animate({
                        left: '1000px'
                    }, function() {
                        $(this).remove();
                    })
                }
                
                $current = null;
                return false;
            });
            
                
            
            var $current = null;
            
            var dir = 1;
            var _slide = function(img) {
                
                var $div = $("<div/>").addClass('gallery-viewer').append(img);
                $(img).css({
                    margin: padding+'px auto'
                });
                
                $div
                    .css({
                        left: (dir*1000)+'px'
                    })
                    .appendTo($container)
                    .animate({
                        left: 0
                    });
                
                if ($current) {
                    $current.animate({
                        left: (dir*-1000)+'px'
                    }, function() {
                        $(this).remove();
                    })
                }
                else {
                    $overview
                        .css({
                            top: 0,
                            bottom: 'auto'
                        })
                        .animate({
                            left: (dir*-1000)+'px'
                        });
                }
                
                $current = $div;
            }
            
            
            $images.click(function(e) {
                
                if ($(e.target).hasClass('oi-button')) return;
                                
                index = $images.index($(this));
                
                var $obj    = $(this);
                var src     = $obj.attr('data-src');
                var height  = parseInt($obj.attr('data-height')) + 2*padding;

                var img = new Image();
                
                
                img.onload = function() {
                    _slide(img);

                    $next.fadeIn();
                    $prev.fadeIn();
                    $back.fadeIn();

                    $container.animate({
                        height: height+'px'
                    });
                }
                img.src = src;
                
                

            });
            
            
            $container.bind('willCollapse', function() {
                if ($current) {
                    $current.css({
                        top: 'auto',
                        bottom: '-1px'
                    });
                }
            });
            $container.bind('didCollapse', function() {
                if ($current) {
                    $current.remove();
                    $current = null;
                }
                dir = 1;
                $prev.hide();
                $next.hide();
                $back.hide();
                $overview.css({left:0,top:'auto',bottom:0});
            });
            
            
        });
    }   
    
    
    
    
    /**
     * Text/Image image viewer.
     */
    
    $.fn.textimage = function() {
            return this.each(function() {
               
               var $obj = $(this);
               var $loader = $obj.find('.image-loader');
               
               if ($loader.length == 0) return;
               
               
               $loader.click(function() {

                   var $img     = null;
                   var $text    = null;

                   var textHeight   = 0;
                   var imageHeight  = 0;
                   
                   $loader.trigger('mouseout');
                   
                   if (!$img) {
                       $text = $obj.find('.text');
                       $img = $("<div/>").addClass('image');
                       $obj.append($img);
                       
                       textHeight = $obj.height();
                                              
                       
                   }
                   
                   $img.css({
                       left: '1000px'
                   }).hide();
       
                   var img = new Image();
                   img.onload = function() {


                       var mov = $loader.attr('data-mov');
                    
                       var $imgOrMov = null;

                       if (mov) {
                           $imgOrMov = $("<a/>")
                               .attr('href',mov)
                               .addClass('img')
                               .addClass('click-to-flowplayer')
                               .append(img)
                               .append($("<span/>").addClass('legend').html($loader.attr('data-legend')))
                               .css({width: img.width+'px', height: img.height+'px'})
                           ;
                       }
                       else {
                           $imgOrMov = $("<span/>")
                               .addClass('img')
                               .css('width',img.width+'px')
                               .append(img)
                               .append($("<span/>").addClass('legend').html($loader.attr('data-legend')))
                           ;
                           
                       }
                       
                       $img.append($imgOrMov);

                       //$img.append(this);
                                              
                       
                       
                       imageHeight = $img.outerHeight();
                        

                       $obj.css({
                           height: textHeight+'px'
                       });

                       $text.animate({
                           left: '-1000px'
                       });
                       $obj.animate({
                           height: imageHeight+'px'
                       })



                       var $_close = null;            
                       var $close = function() {
                           if (!$_close) {
                               $_close = $("<a/>").attr('href','#close').addClass('close-button').appendTo($img).click(function() {
                                   
                                   $img.animate({
                                       left: '1000px'
                                   },function() {
                                       $img.empty().hide();
                                   });
                                   
                                   $text.animate({left:0});
                                   $obj.animate({height:textHeight+'px'},function() {
                                       $obj.css('height','auto');
                                   });
                                   
                                   $(this).fadeOut();
                                   
                                                                      
                                   return false;
                               });

                               var clr = $_close.css('color');
                               var _btn = Raphael($_close.get(0),32,32);

                               var btn = _btn.set();
                               btn.push(
                                   _btn.circle(16,16,14).attr('stroke-width','3'),
                                   _btn.path("M10 10L22 22").attr('stroke-width','5'),
                                   _btn.path("M22 10L10 22").attr('stroke-width','5')
                               );

                               var _draw = function() {
                                   btn.attr('stroke', '#555555');
                               }

                               var _drawHover = function() {
                                   btn.attr('stroke', clr);
                               }

                               _draw();
                               $_close.hover(
                                   function() { _drawHover(); },
                                   function() { _draw(); }
                               );

                               $_close.hide();

                           }

                           return $_close;
                       }


                       $close().hide();
                       $img.show().animate({
                           left: 0
                       }, function() {
                           $close().fadeIn();
                       });

                   }
                   
                   img.src = $loader.attr('href');
                   
                   return false;
                   
               });
                
            });
            
    }
    
    
    
    $('.paragraph-text-image').each(function() {

        var $c = $(this).find('.content');
        
        if ($c.find('.fulltext').length > 0) {
            
            var $btn =  $("<a/>")
                    .attr('href','#')
                    .addClass('full-loader')
                    .appendTo($c);
                                        
            _drawedButton($btn, 1);
            
            var $intro = $c.find('.intro');
            var $full = $c.find('.fulltext');
            var $text = $c.find('.text');
            
            $btn.click(function() {
                
                $btn.fadeOut();
                                                                            
                var introHeight = $intro.outerHeight();
                
                $text.css('height',introHeight+'px');
                
                var fullHeight = $full.css({visibility:'hidden',display:'block'}).outerHeight() + 14;
                
                
                $full.css({visibility:'visible',display:'none'});
                
                $text.animate({height: fullHeight+'px'});
                
                
                $full.css({
                    position: 'absolute',
                    top: 0,
                    left: 0,
                    width: $intro.width()+'px'
                }).fadeIn();
                $intro.fadeOut();
               
               
               
                $_close = $("<a/>").attr('href','#close').addClass('close-button').appendTo($text.parent()).click(function() {

                    $intro.fadeIn();
                    $full.fadeOut();
                    $text.animate({height:introHeight+'px'});
                    $btn.fadeIn();

                    $(this).fadeOut(function() {
                        $(this).remove();
                    });
                                                                           
                    return false;
                });

                var clr = $_close.css('color');
                var _btn = Raphael($_close.get(0),32,32);

                var btn = _btn.set();
                btn.push(
                    _btn.circle(16,16,14).attr('stroke-width','3'),
                    _btn.path("M10 10L22 22").attr('stroke-width','5'),
                    _btn.path("M22 10L10 22").attr('stroke-width','5')
                );

                var _draw = function() {
                    btn.attr('stroke', '#555555');
                }

                var _drawHover = function() {
                    btn.attr('stroke', clr);
                }

                _draw();
                $_close.hover(
                    function() { _drawHover(); },
                    function() { _draw(); }
                );

                $_close.hide();
                
                setTimeout(function() {
                    $_close.fadeIn();
                },200);


                return false;
            });
            
            
        }
        
    });
    
    
    
    // Init plugins
    $('.inplace-content-loader').inplaceContentLoader();
    $('.inplace-content-appender').inplaceContentAppender();
    $('.gallery-images').gallery();
    $('.paragraph-gallery-strip').strip();
    $('.paragraph-text-image').textimage();
    
    
});
})(jQuery);



(function($) {
    
    
    /**
     * Video
     */
    $.fn.video = function() {
        return this.each(function() {
            //TODO ????
        });
    }
    
$(function() { // document ready

    var pathToPlayer = themeDisplay.getPathThemeRoot()+'javascript/flowplayer/flowplayer-3.1.5.swf';
    
    /**
     * Flowplayer
     */
     
     $('.click-to-flowplayer').livequery('click', function() {

         $(this).empty().css({height:($(this).height()+24)+'px'});
         var $vid = $(this);
         $vid.flowplayer(pathToPlayer, {
 			clip: {
 			    onMetaData: function() {
 			    },
 				autoPlay: true,
 				scaling: 'fit'
 			}
         });

         return false;

     });
    
    

     $('.flowplayer').css({height:'300px'}).each(function() {

         var $fp = $(this);
         var fw = $fp.width();

         $fp.flowplayer(pathToPlayer, {
     		clip: {
     		    onMetaData: function() { 
     		        var w = this.getClip().metaData.width;
     		        var h = this.getClip().metaData.height;

                     // fh/fw = h/w
                     var fh = Math.floor(fw * h / w);
                     $fp.css({height: fh+'px'});
                 },
     			autoPlay: true,
     			scaling: 'fit'
     		},
             plugins: { 
                 controls: null 
             }
         });
     });

    
});   
})(jQuery);




/**
 * Sling: Custom dialogs
 */
 
(function($) {
    
    Bfa.Dialog = {}
    Bfa.Dialog.Control = {}
        
})(jQuery);
