/**
 * @depend jquery-1.3.2.js
 * @depend routes.js
 */
try {
    var pageTracker = _gat._getTracker("UA-61093-4");
    pageTracker._trackPageview();
} catch(err) {}

$(document).ready(function() {
    var currentPath = location.href.replace(/^http:\/\/[^\/]+/,'');
    
    $('#globalmenu, #topmenu').each(function(){
        var menu = $(this);
        $(this).find('li a').each(function(){
            var menuitem = $(this);
            var href = menuitem.attr('href');
            //console.log( href + ' - ' + currentPath );
            if( href == currentPath.substr( 0, href.length ) ) {
                menu.find('li').removeClass('selected');
                menuitem.parent().addClass('selected');
            }
        })
    });
    
    var colHeight = 0;
    var columns = $('.equalheight .content'); 
    columns.each(function(){
        var height = $(this).height();
        if( height > colHeight ) {
            colHeight = height;
        }
    });
    columns.css({
        height: colHeight + 'px'
    });
    
    $('#support #faq h3').click(function(){
        var h3 = $(this);
        var content = h3.find('+.content');
        h3.siblings().find('+.content').slideUp('fast');
        content.slideToggle('fast');
    })
    
    var centerImage = function( container, image ) {
        cw = container.width();
        ch = container.height()
        iw = image.width();
        ih = image.height();
        
        image.css({
            top: ( ch - ih ) / 2 + 'px',
            left: ( cw - iw ) / 2 + 'px'
        });
    }
    
    var gallery  = $('.gallery');
    gallery.find('.thumbs').scrollable();
    gallery.each(function(){
        if ( $(this).find('.items > div').length < 6 ) {
            $(this).find('a.nextPage').addClass('disabled');
        }
    })
    
    var player   = gallery.find('.player');
    var image    = player.find('.image');
    var thumbs   = gallery.find('.thumbs div');
    
    centerImage( player, player.find('img') );
    
    thumbs.each(function(){
        var thumb = $(this);
        var link  = thumb.find('a');
        
        if( thumb.hasClass('image') ) {
            var imageFull = new Image();
    
            var newImage = $('<img />');
            newImage.attr( 'src', link.attr('href') )
            newImage.hide();
            
            thumb.hover(
                function(){
                    $(this).fadeTo( 'fast', 0.5 );
                },
                function(){
                    $(this).fadeTo( 'fast', 1 );
                }
            );
            
            link.click(function(){
                thumbs.removeClass('active');
                thumb.addClass('active');
                var oldImage = image.find('img');
                var video = image.find('object');
                
                if ( video ) {
                    image.empty();
                    newImage.appendTo(image);
                    centerImage( player, newImage );
                    newImage.fadeIn('fast');
                }
                if ( oldImage ) {
                    if( link.attr('href') != oldImage.attr('src') ) {
                        newImage.appendTo(image);
                        centerImage( player, newImage );
                        newImage.fadeIn('fast');
                        oldImage.fadeOut('fast', function() {
                            $(this).remove();
                        });
                    }
                } 
                return false;
            })
        } else if ( thumb.hasClass('video') ) {
            var videoFile = link.attr('href');

            link.click(function(){
                thumbs.removeClass('active');
                thumb.addClass('active');

                image.find('img').remove();
                image.flowplayer({ 
                    src: "/design/edr/js/flowplayer-3.1.1.swf"
                },{ 
                    clip: videoFile
                });
                return false;
            });
        }
    });

    
    $("#courses ul.tabs").tabs("div.panes > div", {
        tabs: 'li'
    });
    
    $('table.list tbody tr:last-child').addClass('last');
    
    if( $('#courses') ) {
        $('div.panes > div').each(function(){
            var pane = $(this);
            var courses      = pane.find('table.list tbody tr');
            var allProducts  = pane.find('input[name=product-all]');
            var products     = pane.find('input[name=product]');
            var allCountries = pane.find('input[name=country-all]');
            var countries    = pane.find('input[name=country]');
            
            var filterCourses = function(){
                var productFilter = new Array();
                var countryFilter = new Array();
                
                if( !allProducts.attr('checked') ) {
                    products.each(function(){
                        if( this.checked ) {
                            productFilter.push( this.value );
                        }
                    });
                }
    
                if( !allCountries.attr('checked') ) {
                    countries.each(function(){
                        if( this.checked ) {
                            countryFilter.push( this.value );
                        }
                    });
                }
                
                // Check if we need to filter anything
                if( productFilter.length > 0 || countryFilter.length > 0 ) {
                    // Init filter string, we eval this on each table row to show or hide the row 
                    var filterString = 'if(';
                    
                    // We need to filter on products
                    if( productFilter.length > 0 ) {
                        filterString += '(';
                        for( i = 0; i < productFilter.length; i++ ) {
                            filterString += 'th.hasClass("' + productFilter[i] + '")';
                            if( i != ( productFilter.length - 1 ) ) {
                                filterString += ' || ';
                            }
                        }
                        filterString += ')';
                    }
        
                    // Combine product and country filtering if both are present 
                    if( productFilter.length > 0 && countryFilter.length > 0 ) {
                        filterString += ' && ';
                    }
        
                    // We need to filter on countries
                    if( countryFilter.length > 0 ) {
                        filterString += '(';
                        for( i = 0; i < countryFilter.length; i++ ) {
                            filterString += 'th.hasClass("' + countryFilter[i] + '")';
                            if( i != ( countryFilter.length - 1 ) ) {
                                filterString += ' || ';
                            }
                        }
                        filterString += ')';
                    }
                    filterString += '){th.show()}else{th.hide()}';
    
                    courses.each(function(){
                        var th = $(this);
                        eval(filterString); 
                    });
                    
                    courses.removeClass('last');
                    pane.find('table.list tbody tr:last').addClass('last');
                    //console.log(filterString);
                }
    
                // No filter, show everything
                else {
                    courses.show();
                }
            }
            
            allProducts.click(function() {
                if( this.checked ) {
                    products.attr('checked','');
                }
                filterCourses();
            });
    
            products.click( function(){
                var check = 'checked';
                products.each( function(){
                    if( this.checked ) {
                        check = false;
                        return false;
                    }
                });
                allProducts.attr('checked', check);
                filterCourses();
            })
    
            allCountries.click(function() {
                if( this.checked ) {
                    countries.removeAttr('checked');
                }
                filterCourses();
            });
            
            countries.click(function(){
                var check = 'checked';
                countries.each(function(){
                    if( this.checked ) {
                        check = false;
                        return false;
                    }
                });
                allCountries.attr('checked', check);
                filterCourses();
            })
        })
    }
});
