/*
 * The TV-Kaista toolbar.
 *
 * Dependent on
 * - jquery.cookie
 */

// globals
var toolbarDetached = false;
var detachPosition = 0;
var hasToolbarAttachment = false;
var calendarShown = false;

function detachToolbar() {
    detachPosition = $('#toolbar').position().top;
    $('#toolbar').css('position', 'fixed');
    $('#toolbar').css('top', '0');
    $('#toolbar').css('zIndex', 1000);

    $('#toolbarplaceholder').show();
    $('#toolbarshadowbottom').css('position','fixed');
    $('#toolbarshadowbottom').css('zIndex', 2);

    if (hasToolbarAttachment) {
        $('#toolbarattachmentplaceholder').show();
        $('#toolbarattachment').css('position','fixed');
        $('#toolbarattachment').css('zIndex', 2);
        if (/MSIE/.test(navigator.userAgent)) {
            $('#toolbarattachment').css('top', '30px');
            $('#toolbarshadowbottom').css('top', '' + (30 + $('#toolbarattachment').outerHeight()) + 'px');
        } else {
            $('#toolbarattachment').css('top', '32px');
            $('#toolbarshadowbottom').css('top', '' + (32 +  $('#toolbarattachment').outerHeight() ) + 'px');
        }
    } else {
        $('#toolbarshadowbottom').css('top', '32px');
    }

    if ($.support.opacity) {
        $('#toolbarshadowbottom').show();
    }

    toolbarDetached = true;
    syncToolbar();

}

function attachToolbar() {
    // Retach top bar
    $('#toolbar').css('position', 'static');
    $('#toolbar').css('width', '100%');
    $('#toolbar').css('zIndex', 0);
    $('#toolbarplaceholder').hide();

    var additionalOffsetY = 0;
    if( hasToolbarAttachment ) {
        additionalOffsetY = $('#toolbarattachment').outerHeight();
    }

    $('#toolbarshadowbottom').css('position', 'absolute');
    $('#toolbarshadowbottom').css('top', ( $('#toolbar').position().top + $('#toolbar').height() + additionalOffsetY ) + 'px' );
    $('#toolbarshadowbottom').css('zIndex', 0);

    if (hasToolbarAttachment) {
        $('#toolbarattachmentplaceholder').hide();
        $('#toolbarattachment').css('position', 'static');
        $('#toolbarattachment').css('width','100%');
        $('#toolbarattachment').css('zIndex', 0);
    }

    if ($.support.opacity) {
        $('#toolbarshadowbottom').hide();
    }

    toolbarDetached = false;
    syncToolbar();
}

function pinToolbar() {

    $.cookie('toolbarUnpinned', 'no', {
        expires: 365,
        path: '/'
    });

    $('#pin').css('backgroundPosition', '0 0');
    attachToolbar();
}

function unpinToolbar() {
    $.cookie('toolbarUnpinned', 'yes', {
        expires: 365,
        path: '/'
    });

    $('#pin').css('backgroundPosition', '-20px 0');

    var additionalOffsetY = 0;
    if (hasToolbarAttachment) {
        additionalOffsetY = $('#toolbarattachment').outerHeight();
        $('#toolbarattachment').css('zIndex', 0);
    }

    $('#toolbarshadowbottom').css('position', 'absolute');
    $('#toolbarshadowbottom').css('zIndex', 0);
    $('#toolbarshadowbottom').css('top', ($('#toolbar').position().top + $('#toolbar').height() + additionalOffsetY) + 'px');
    $('#toolbarshadowbottom').css('width', $(document).width() + 'px');
}

function syncToolbar() {
    var width = $('#channelboardtable').width();
    if ( toolbarDetached && hasToolbarAttachment ) {
        $('#toolbarattachmenttable').width( width );
        $('#channelboardtable tr:first > td').each(function(i) {
            $($('#toolbarattachmenttable tr:first > td')[i]).width( $(this).width() );
        });
        $('#toolbarattachmenttable').css('margin-left', '-' + $(window).scrollLeft() + 'px' );
    } else if ( hasToolbarAttachment ) {
        $('#toolbarattachmenttable').width( '100%' );
        $('#channelboardtable tr:first > td').each(function(i) {
            $($('#toolbarattachmenttable tr:first > td')[i]).width( $(this).width() );
        });
        $('#toolbarattachmenttable').css('margin-left', '0px' );
    }
    if ( toolbarDetached ) {
		if( width )
        	$('#toolbarshadowbottom').width( width );
    } else {
        $('#toolbarshadowbottom').width( '100%' );
    }
}

$( document ).ready( function() {

    $.event.special.hover.delay = 200;
    $.event.special.hover.speed = 50;

    if ($('#toolbar').length) {

        // Check if there's a top bar attachment
        if ($('#toolbarattachment').length > 0) {
            hasToolbarAttachment = true;
        }

        $('#toolbarcalendartrigger').hover(
            function() {
                $('#toolbar #dayselector').show();
                $('#toolbar #dayselector').dropShadow({
                    blur: 0,
                    opacity: 0.25
                });
                calendarShown = true;
            },
            function() {
                $('#toolbar #dayselector').removeShadow();
                $('#toolbar #dayselector').hide();
                calendarShown = false;
            }
        );

        // If there's no cookie will unpin the toolbar
        if ( !$.cookie('toolbarUnpinned') || $.cookie('toolbarUnpinned') == 'yes') {
            unpinToolbar();
        }

        // Allow user to unpin/pin toolbar
        $('#pin').click(function(){
            if ($.cookie('toolbarUnpinned') == 'yes') {
                pinToolbar();
            } else {
                unpinToolbar();
            }
        });

        // Make top bar follow on scroll
        $(window).scroll(function(){
            if ($.cookie('toolbarUnpinned') != 'no') {
                if ( !toolbarDetached ) {
                    if( $(window).scrollTop() > $('#toolbar').position().top ) {
                        // detach top bar
                        detachToolbar();
                    }
                } else if ( $(window).scrollTop() <= detachPosition ) {
                    // Retach top bar
                    attachToolbar();
                } else if ( hasToolbarAttachment && toolbarDetached ) {
                     syncToolbar();
                }
                if ( calendarShown ) {
                    $('#toolbar #dayselector').redrawShadow();
                }
            }
            $('#toolbartable').width( $(window).width() + $(window).scrollLeft() );
            $('#bbarmiddle').width( $(window).width() + $(window).scrollLeft() );
        });
        $(window).resize(function(){
            syncToolbar();
            $('#toolbartable').width( $(window).width() + $(window).scrollLeft() );
            $('#bbarmiddle').width( $(window).width() + $(window).scrollLeft() );
        });
    }
} );
