$j(document).ready(function()
{
    $j('div.media_container').hover(
    function()
    {
        if ( $j(this).parent().find('div.article-tab-media-info').html() == '' )
        {
            return;
        }
        
        $j('div.article-tab-media-info').jScrollPane(
        {
            showArrows:true,
            scrollbarMargin:0
        });
        
        $j('div.jScrollPaneContainer').css('zIndex', 10);
        
        $j('img.info_overlay_icon').css('display', 'none');
    },
    function()
    {
        $j('div.jScrollPaneContainer').css('zIndex', -1);
        $j('img.info_overlay_icon').css('display', 'block');
    });
    
    $j('div.jScrollPaneContainer').hover(
    function()
    {
        if ( $j(this).find('div.article-tab-media-info').html() == '' )
        {
            return;
        }
        
        if ( $j(this).css('zIndex') == 10 )
        {
            return;
        }
        
        $j('div.article-tab-media-info').jScrollPane(
        {
            showArrows:true,
            scrollbarMargin:0
        });
        
        $j('div.jScrollPaneContainer').css('zIndex', 10);
        
        $j('img.info_overlay_icon').css('display', 'none');
    },
    function()
    {
        $j('div.jScrollPaneContainer').css('zIndex', -1);
        $j('img.info_overlay_icon').css('display', 'block');
    });
});

var Getaway = {};

Getaway.ArticleCarousel = Class.create(
{
    num_items: 0,
    shift_amount: 104,
    carousel_slider: null,
    max_items_visible: 4,
    animating: false,
    item_width: 104,
    slider_left: 0,
    left_arrow: null,
    right_arrow: null,
    current_item: null,
    popup_info_visible: false,
    popup_timer: null,
    popup_animating: false,
    popup_effect:null,
    media_container:null,
    media_info_container:null,
    
    initialize: function(num_items, item_width, max_items_visible, num_items_to_shift, carousel_container)
    {
        this.num_items = num_items;
        this.shift_amount = item_width * num_items_to_shift;
        this.carousel_slider = $(carousel_container).down('div.article-tab-carousel-thumbs-slider');
        this.max_items_visible = max_items_visible;
        this.item_width = item_width;
        
        this.media_container = $(carousel_container).up('div.tab-content').down('div.media_container');
        this.media_info_container = $(carousel_container).up('div.tab-content').down('div.article-tab-media-info');
        
        var carousel_thumbs = $(carousel_container).down('div.article-tab-carousel-thumbs-slider').childElements();
        
        if ( carousel_thumbs != undefined)
        {
            carousel_thumbs.each(function(element)
            {
                element.observe('click', this.loadItem.bind(this));
                element.observe('mouseout', this.hideThumbDescriptionTimeout.bind(this));
                element.observe('mouseover', this.showThumbDescription.bind(this));
            }.bind(this));

            this.loadFirstItem($(carousel_thumbs[0]));
        }
        
        this.left_arrow = $(carousel_container).down('div.article-tab-carousel-arrow-left');
        this.right_arrow = $(carousel_container).down('div.article-tab-carousel-arrow-right');
        
        if ( this.num_items > 1 )
        {
            if ( this.num_items > this.max_items_visible )
            {
                this.left_arrow.observe('click', this.shiftItemsRight.bind(this));
                this.right_arrow.observe('click', this.shiftItemsLeft.bind(this));
            }
            else
            {
                this.right_arrow.style.visibility = 'hidden';
            }
            
            this.left_arrow.style.visibility = 'hidden';
        }
        else
        {
            $(carousel_container).style.display = 'none';
            
            $(carousel_container).up('div.tab-content').setStyle(
            {
                minHeight:'0px'
            });
            
            $(carousel_container).up('div.tab-content').down('div.media_container').setStyle(
            {
                minHeight:'0px'
            });
        }
    },
    
    showDescription: function()
    {
        if ( this.media_info_container.empty() )
        {
            return;
        }
        
        $j('div.article-tab-media-info').jScrollPane(
        {
            showArrows:true,
            scrollbarMargin:0
        });
        
        $j('div.jScrollPaneContainer').css('zIndex', 10);
    },
    
    hideDescription: function()
    {
        $j('div.jScrollPaneContainer').css('zIndex', -1);
    },
    
    showThumbDescription: function(event)
    {
        if ( this.popup_timer != null )
        {
            clearTimeout(this.popup_timer);
            this.popup_timer = null;
        }
        
        if ( this.popup_effect != null )
        {
            this.popup_effect.cancel();
            this.popup_effect = null;
        }
        
        if ( this.popup_animating == true )
        {
            this.popup_timer = setTimeout(this.showThumbDescription.bind(this, event), 200);
            return;
        }
        
        var hover_thumb;
        
        if ( !(hover_thumb = event.findElement('img')) )
        {
            return;
        }
        
        hover_thumb = hover_thumb.up('div');
        
        var thumb_description = hover_thumb.readAttribute('name');
        
        if ( thumb_description == '' )
        {
            this.hideThumbDescriptionTimeout();
            return;
        }
        
        var anchor_index = thumb_description.indexOf('<br/>');
        
        if ( anchor_index == -1 )
        {
            anchor_index = thumb_description.indexOf('<a');
        }
        
        var last_pos = 0;
        
        if ( anchor_index >= 30 || anchor_index == undefined || anchor_index == -1 || anchor_index == null )
        {
            last_pos = 30;
        }
        else
        {
            last_pos = anchor_index;
        }
        
        if ( thumb_description.length > 30 )
        {
            var hover_thumb_description = thumb_description.substr(0, last_pos) + "...";
        }
        else
        {
            var hover_thumb_description = thumb_description.substr(0, last_pos);
        }
        
        if ( hover_thumb_description == '...' )
        {
            this.hideThumbDescriptionTimeout();
            return;
        }
        
        var media_description_popup = $('media_description_popup');
        var popup_text = media_description_popup.down('div.media-description-text');
        
        popup_text.update(hover_thumb_description);
        
        if ( this.popup_info_visible == false )
        {
            var offset_top = -30;
            
            media_description_popup.show();
            
            media_description_popup.setStyle(
            {
                opacity:1.0
            });
            
            Element.clonePosition(media_description_popup, hover_thumb,
            {
                setWidth:false,
                setHeight:false,
                offsetTop:offset_top,
                offsetLeft:38
            });
            
            this.popup_effect = new Effect.Move(media_description_popup, { y:-10, mode:'relative', duration:0.1 });
        }
        else
        {
            var offset_top = -40;
            
            media_description_popup.show();
            
            Element.clonePosition(media_description_popup, hover_thumb,
            {
                setWidth:false,
                setHeight:false,
                offsetTop:offset_top,
                offsetLeft:38
            });
        }
        
        this.popup_info_visible = true;
    },
    
    doneFadingPopup: function()
    {
        this.popup_info_visible = false;
        this.popup_animating = false;
    },
    
    hideThumbDescription: function()
    {
        this.popup_animating = true;
        Effect.Fade($('media_description_popup'), { duration:0.1, afterFinish:this.doneFadingPopup.bind(this) });
    },
    
    hideThumbDescriptionTimeout: function(event)
    {
        if ( this.popup_info_visible == true )
        {
            this.popup_timer = setTimeout(this.hideThumbDescription.bind(this), 200);
        }
    },
    
    shiftItemsLeft: function(event)
    {
        if ( (this.carousel_slider != null || this.carousel_slider != undefined) && !this.animating)
        {
            this.animating = true;
            new Effect.Move(this.carousel_slider, { x:-this.shift_amount, duration:0.2, mode:'relative', afterFinish:this.doneAnimating.bind(this) });
            this.slider_left -= this.shift_amount;
        }
    },
    
    shiftItemsRight: function(event)
    {
        if ( (this.carousel_slider != null || this.carousel_slider != undefined) && !this.animating)
        {
            this.animating = true;
            new Effect.Move(this.carousel_slider, { x:this.shift_amount, duration:0.2, mode:'relative', afterFinish:this.doneAnimating.bind(this) });
            this.slider_left += this.shift_amount;
        }
    },
    
    doneAnimating: function()
    {
        this.animating = false;
        var max_left = 0 - ((this.num_items - this.max_items_visible) * this.item_width);
        
        if ( this.slider_left <= max_left )
        {
            this.right_arrow.style.visibility = 'hidden';
            this.left_arrow.style.visibility = 'visible';
            this.left_arrow.next('div.article-tab-carousel-thumbs-container').removeClassName('repositioned');
        }
        else
        if ( this.slider_left == 0 )
        {
            this.left_arrow.style.visibility = 'hidden';
            this.right_arrow.style.visibility = 'visible';
            this.left_arrow.next('div.article-tab-carousel-thumbs-container').addClassName('repositioned');
        }
        else
        {
            this.right_arrow.style.visibility = 'visible';
            this.left_arrow.style.visibility = 'visible';
            this.left_arrow.next('div.article-tab-carousel-thumbs-container').removeClassName('repositioned');
        }
    },
    
    loadItem: function(event)
    {
        var thumb = event.findElement('div');
        
        if ( this.current_item != null && this.current_item != thumb )
        {
            this.current_item.removeClassName('selected');
            this.current_item = thumb;
            thumb.addClassName('selected');
            
            var filename = thumb.readAttribute('src');
            var new_height = thumb.readAttribute('rel');
            var file_extension = thumb.readAttribute('type');
            var description = thumb.readAttribute('name');
            var download_links = thumb.readAttribute('value').escapeHTML();
            
            var media_container = thumb.up('div.tab-content').down('div.media_container');
            var info_container = thumb.up('div.tab-content').down('div.article-tab-media-info');
            var links_container = thumb.up('div.tab-content').down('div.article-tab-download-container');
            
            if ( file_extension == 'mpeg' ||
                 file_extension == 'mp4' ||
                 file_extension == 'flv' )
            {
                var image = thumb.down('img').readAttribute('value');
            
                if ( image == null )
                {
                    image = '/images/rsp/getaway/default_video_image.jpg';
                }
                
                media_container.setStyle(
                {
                    height: '293px'
                });
                
                flowplayer(media_container, { src: '/includes/js/getaway/flowplayer/flowplayer.commercial-3.1.5.swf', wmode: 'opaque'}, 
                {
                    key:'#@082df0e7e3723f976e6',
                    
                    plugins:
                    {
                        controls: conf.skins.standard
                    },
            
                    screen:
                    {
                        height:270,
                        width:480
                    },
                    
                    playlist:
                    [
                        {
                            url:image,
                            duration:0
                        },
                        
                        {
                            url:filename,
                            autoPlay:false,
                            autoBuffering:false,
                            scaling:'orig'
                        }
                    ]
                });
            }
            else
            if ( file_extension == 'mp3' )
            {
                var image = thumb.down('img').readAttribute('value');
            
                if ( image == null )
                {
                    image = '/images/rsp/getaway/default_audio_image.jpg';
                }
                
                media_container.setStyle(
                {
                    height: '293px'
                });
                
                flowplayer(media_container, { src: '/includes/js/getaway/flowplayer/flowplayer.commercial-3.1.5.swf', wmode: 'opaque'}, 
                {
                    key:'#@082df0e7e3723f976e6',
                    
                    plugins:
                    {
                        controls: conf.skins.standard
                    },
            
                    screen:
                    {
                        height:270,
                        width:480
                    },
                    
                    playlist:
                    [
                        {
                            url:image,
                            duration:0
                        },
                        
                        {
                            url:filename,
                            autoPlay:false,
                            autoBuffering:false
                        }
                    ]
                });
            }
            else
            if ( file_extension == 'jpg' ||
                 file_extension == 'gif' ||
                 file_extension == 'png' )
            {
                var new_content = '<img src="' + filename + '" width="480px" alt="' + description + '" />';
                
                if ( new_height != undefined && new_height != '' )
                {
                    if ( media_container.getHeight() != new_height )
                    {
                        if ( media_container.getHeight() < new_height )
                        {
                            new Effect.Morph(media_container,
                            {
                                style: 'height:' + new_height + 'px',
                                duration:0.25,
                                
                                afterFinish: function()
                                {
                                    media_container.update(new_content);
                                }
                            });
                        }
                        else
                        {
                            media_container.update(new_content);
                            
                            new Effect.Morph(media_container,
                            {
                                style: 'height:' + new_height + 'px',
                                duration:0.25
                            });
                        }
                        
                        media_container.setStyle(
                        {
                            minHeight: '0px'
                        });
                    }
                    else
                    {
                        media_container.update(new_content);
                    }
                }
                else
                {
                    media_container.setStyle(
                    {
                        minHeight: '100px',
                        height: 'auto'
                    });
                    
                    media_container.update(new_content);
                }
            }
            
            if ( description != null )
            {
                info_container.update(description);
                
                $j(media_container).parent().find('img.info_overlay_icon').remove();
                
                if ( file_extension == 'jpg' || file_extension == 'gif' || file_extension == 'png' )
                {
                    if ( description != '' )
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:110px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                }
                else
                {
                    $j(media_container).parent().find('div.jScrollPaneContainer').css('top','-89px');
                    
                    if ( description != '' )
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:132px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                }
                
                $j('div.article-tab-media-info').jScrollPane(
                {
                    showArrows:true,
                    scrollbarMargin:0
                });
            }
            
            if ( download_links != null )
            {
                links_container.update(download_links.unescapeHTML());
            }
        }
    },
    
    loadFirstItem: function(element)
    {
        var thumb = element;
        this.current_item = thumb;
        thumb.addClassName('selected');
        
        var filename = thumb.readAttribute('src');
        var new_height = thumb.readAttribute('rel');
        var file_extension = thumb.readAttribute('type');
        var description = thumb.readAttribute('name');
        var download_links = thumb.readAttribute('value').escapeHTML();
        
        var media_container = thumb.up('div.tab-content').down('div.media_container');
        var info_container = thumb.up('div.tab-content').down('div.article-tab-media-info');
        var links_container = thumb.up('div.tab-content').down('div.article-tab-download-container');
        
        if ( file_extension == 'mpeg' ||
             file_extension == 'mp4' ||
             file_extension == 'flv' )
        {
            var image = thumb.down('img').readAttribute('value');
            
            if ( image == null )
            {
                image = '/images/rsp/getaway/default_video_image.jpg';
            }
            
            media_container.setStyle(
            {
                height: '293px'
            });
            
            flowplayer(media_container, { src: '/includes/js/getaway/flowplayer/flowplayer.commercial-3.1.5.swf', wmode: 'opaque'}, 
            {
                key:'#@082df0e7e3723f976e6',
                
                plugins:
                {
                    controls: conf.skins.standard
                },
        
                screen:
                {
                    height:270,
                    width:480
                },
                
                playlist:
                [
                    {
                        url:image,
                        duration:0
                    },
                    
                    {
                        url:filename,
                        autoPlay:false,
                        autoBuffering:false,
                        scaling:'orig'
                    }
                ]
            });
        }
        else
        if ( file_extension == 'mp3' )
        {
            var image = thumb.down('img').readAttribute('value');
            
            if ( image == null )
            {
                image = '/images/rsp/getaway/default_audio_image.jpg';
            }
            
            media_container.setStyle(
            {
                height: '293px'
            });
            
            flowplayer(media_container, { src: '/includes/js/getaway/flowplayer/flowplayer.commercial-3.1.5.swf', wmode: 'opaque'}, 
            {
                key:'#@082df0e7e3723f976e6',
                
                plugins:
                {
                    controls: conf.skins.standard
                },
        
                screen:
                {
                    height:270,
                    width:480
                },
                
                playlist:
                [
                    {
                        url:image,
                        duration:0
                    },
                    
                    {
                        url:filename,
                        autoPlay:false,
                        autoBuffering:false
                    }
                ]
            });
        }
        else
        if ( file_extension == 'jpg' ||
             file_extension == 'gif' ||
             file_extension == 'png' )
        {
            var new_content = '<img src="' + filename + '" width="480px" alt="' + description + '" />';
            
            if ( new_height != undefined && new_height != '' )
            {
                media_container.setStyle(
                {
                    height: new_height + 'px',
                    minHeight: '0px'
                });
            }
            else
            {
                media_container.setStyle(
                {
                    minHeight: '100px',
                    height: 'auto'
                });
            }
            
            media_container.update(new_content);
        }
        
        if ( description != null )
        {
            info_container.update(description);
            
            $j('div.article-tab-media-info').jScrollPane(
            {
                showArrows:true,
                scrollbarMargin:0
            });
            
            if ( file_extension == 'jpg' || file_extension == 'gif' || file_extension == 'png' )
            {
                if ( description != '' )
                {
                    if ( this.num_items > 1 )
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:110px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                    else
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:37px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                }
            }
            else
            {
                $j(media_container).parent().find('div.jScrollPaneContainer').css('top','-89px');
                
                if ( description != '' )
                {
                    if ( this.num_items > 1 )
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:132px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                    else
                    {
                        $j('<img src="/images/rsp/getaway/info.png" style="position:absolute;right:25px;bottom:59px;" class="info_overlay_icon" />').appendTo($j(media_container).parent());
                    }
                }
            }
        }
        
        if ( download_links != null )
        {
            links_container.update(download_links.unescapeHTML());
        }
    }
});