/*
    Coded by Steven Bower
    TurnWheel Designs - http://turnwheel.com
    BPWebDesign - http://bpwebdesign.com
*/

$(function() {
    var hideDelay = 600;
    var hideTimer = null;
    var hideProducts = function() {
        if (hideTimer) clearTimeout(hideTimer);
        
        hideTimer = setTimeout(function() {
            $('#products_popout').fadeOut('slow');
        }, hideDelay);
    };
    var hideStaff = function() {
        if (hideTimer) clearTimeout(hideTimer);
        
        hideTimer = setTimeout(function() {
            $('#staffinfo').css('overflow','hidden').fadeOut('slow');
        }, hideDelay);
    };
    
    // Handle external links
    $('a[rel="external"]').click(function() {
        window.open($(this).attr('href'));
        return false;
    });
    
    // Handle popups
    $('a[rel="popup"]').click(function() {
        window.open($(this).attr('href'),"","toolbar=0,width=500,height=400,top=100,left=100,scrollbars=0");
        return false;
    });
    
    // Close staff details
    $('#staffinfo a[rel="close"]').click(function() {
        $('#staffinfo').hide();
        return false;
    });
    
    // Show staff details
    $('#staffimg').mouseover(function() {
        if (!$(this).data('init')) {
            $(this).data('init',true);
            
            $(this).hoverIntent(function() {
                if (hideTimer) clearTimeout(hideTimer);
                $('#staffinfo').css('overflow','hidden').fadeIn('fast',function() { $(this).css('overflow','auto'); }); // Allows us to control animation
            },hideStaff);
            
            $(this).trigger('mouseover');
        }
        
        $('#staffinfo').mouseover(function() {
            if (hideTimer) clearTimeout(hideTimer);
        });
        
        $('#staffinfo').mouseout(hideStaff);
    });
    
    // Products Popup
    $('#menu_products').mouseover(function() {
        if (!$(this).data('init')) {
            $(this).data('init',true);
            
            $(this).hoverIntent(function() {
                if (hideTimer) clearTimeout(hideTimer);
                
                if (!$('#products_popout').is(':hidden')) return;
                
                $('#products_popout').show(); // Allows us to control animation
            },hideProducts);
            
            $(this).trigger('mouseover');
        }
        
        $('#products_popout').mouseover(function() {
            if (hideTimer) clearTimeout(hideTimer);
        });
        
        $('#products_popout').mouseout(hideProducts);
    });
    
    // Random Images
    var images = ['right_1.jpg','right_2.jpg','right_3.jpg','right_4.jpg','right_5.jpg','right_6.jpg','right_6.jpg'];
    var rand = Math.round(Math.random()*(images.length-1));
    $('#randtile').attr('src','/images/'+images[rand]);
    
    $.get('/products_popout.html', function(data) {
        $('<div id="products_popout">'+data+'</div>').hide().appendTo('body');
    });
    
    // Load Tool Tip if needed
    if ($('.tooltip').length > 0) {
        $.getCSS('/css/jquery.tooltip.css');
        $.getScript('/js/jquery.tooltip.min.js',function() {
            $('.tooltip').tooltip({
                showURL: false
            }).find('img[alt]').attr('alt','');
        });
    }
});

/*
    jQuery Addition by Steven Bower
    Append a CSS file to the HTML document
*/
$.getCSS = function(file) {
    if (!file) return false;
    
    $('head').append('<link rel="stylesheet" type="text/css" href="'+file+'" media="screen,projection" />');
    
    return true;
};