// Define variables
var to_top_btn = 0;

// Ready
jQuery().ready(function() {
    // prettyPhoto
    $("a[rel^='prettyPhoto']").prettyPhoto({'social_tools' : false, 'slideshow' : false});

    // Load video
    $('#home_video a, #news .preview a, #case .columns .video a, #project ul .video a').live('click', function() {
        var parent = $(this).parent();
        var link = $(this).attr('href');
        var img = $('img', parent);

        var weight = img.width();
        var height = img.height();

        parent.loadFlash({
            'filename' : link,
            'width' : weight,
            'height' : height,
            'params' : {
                'allowscriptaccess' : 'always',
                'allowfullscreen' : 'true'
            }
        });

        return false;
    });
    
    // Read News
    $('#sidebar .news a').click(function() {
        show_overlay();
        $('body').append('<div id="box" style="visibility: hidden;">&nbsp;</div>');
        
        var link = $(this).attr('rel');
        
        load_news(link);
        return false;
    });
    $('#box .other a').live('click', function() {
        var link = $(this).attr('rel');
        load_news(link);
        return false;
    });
    
    // Go top button
    if ($('#wrapper').length)
    {
        put_scroll_to_top_button();
        
        $(window).resize(function() {
            put_scroll_to_top_button();
        }).scroll(function(){
            put_scroll_to_top_button();
        });
    }
});

function load_news(link)
{
    var box = $('#box');
    
    box.css({
        'visibility': 'visible',
        opacity: 0
    });
    
    box.load(link, function(data) {
        var news = $('#news', box);
        
        setTimeout(function() {
            var height = news.height();
            var width = news.width();
        
            box.css({
                //width: width + 'px',
                //height: height + 'px',
                'margin-left': -(width/2) + 'px',
                'margin-top': -(height/2) + 'px'
            });

            box.animate({
                opacity: 1
            }, 'fast');

            $('a.close', box).click(function() {
                box.remove();
                hide_overlay();
            });
            $('#overlay').click(function() {
                box.remove();
                hide_overlay();
            });
        }, 200);
    });
}

function put_scroll_to_top_button()
{
    clearTimeout(to_top_btn);
    
    var content = $('#wrapper');
    var button = $('#button-gotop');
    if (!button.length)
    {
        content.append('<a href="javascript:;" id="button-gotop">Scroll to top...</a>');
        $('#button-gotop').click(function(){
            $( 'html, body' ).animate( {scrollTop: 0}, 'slow' );
        });
    }
    else
    {
        button.fadeOut();
    }
    
    var window_height = $(window).height();
    var content_height = content.height();
    var height_from_top = content.position().top;
    
    if (window_height < content_height)
    {
        var scroll_top = $(window).scrollTop();

        var where = scroll_top-height_from_top;
        if (where > 0)
        {
            where = (where + window_height) - 36;

            

            to_top_btn = setTimeout(function(){
                button.css('top', where+'px');
                button.fadeIn();
            }, 500);
        }
    }
}


function show_overlay()
{
    $('body').append('<div id="overlay" style="display: none;">&nbsp;</div>');
    
    var height = $('body').height();
    var wrapper_height = $('#wrapper').height();
    if (height < wrapper_height)
    {
        height = wrapper_height;
    }
    
    $('#overlay').css({
        opacity: 0,
        height: height,
        display: 'block'
    }).animate({opacity: 0.4}, 500);
}
function hide_overlay()
{
    $("#overlay").fadeOut('fast', function() {
        $(this).remove();
    });
}
