
/**
 * Get the width and left position of the main content column
 */
function get_page_size_info() {
    var column = null;
    var body = $('body');

    if (body.hasClass('page_blog')) {
        column = $('#page');
    } else {
        var body_class = body.attr('class');
        // For any index page there must be a frame_body tag
        column = (body_class.indexOf('page_index') > 0 || body.hasClass('page_invite') || body.hasClass('page_newsletter')) ? $('#frame_body .bg') : $('#content');
    }

    return {
        width: column.width(),
        left_column: parseFloat($('#panfu_banner').css(body.css('direction') == 'ltr' ? 'margin-left' : 'margin-right'))
    };
}

/**
 * If the main content and the flag container do not fit into current view port, try to move these blocks
 *
 * This resizing does not take place for body tags containing the class 'page_login'
 */
function resize_to_fit() {
    var margin_correction = 5;

    var body = $('body');

    if (body.hasClass('page_login')) {
        return;
    }
    
    var page_info = get_page_size_info();
    var empty_column_size = page_info.left_column + margin_correction;
    var flags_size = $('#flags_container').width();
    var space_needed = page_info.width + flags_size;
    var window_size = $(window).width();
    var free_space = window_size - space_needed;

    if (YI.isDebugEnabled()) {
        YI.debug("Parameters: left/offset=" + page_info.left_column + ' width=' + page_info.width + ' flags_size=' + flags_size +
                ' need=' + space_needed + ' window_size=' + window_size + ' diff=' + free_space);
    }
    
    if (YI.get('org_body_position') == undefined) {
        YI.set('org_body_position', body.css('position'));
    }

    if (window_size - space_needed - empty_column_size > 0) {
        YI.debug('Restoring original position');
        body.css('position', YI.get('org_body_position'));
    } else {
        var move = empty_column_size - (free_space > margin_correction ? free_space : margin_correction);

        if (YI.isDebugEnabled()) {
            YI.debug('Setting new postion moving_by=' + move);
        }

        body.css('position', 'absolute');

        if (body.css('direction') == 'ltr') {
            body.css('left', '-' + move + 'px');
        } else {
            body.css('right', '-' + move + 'px');
        }
    }
}

$(document).ready(function () {
    // no script
	$('#nojs').remove();

    $.cookie('test_cookie', '', {expires: 1});
	var test_cookie = $.cookie('test_cookie');

    if ($.browser.msie && $.browser.version == "6.0") {
        if ($('#ie6_popup').length && !$.cookie('ie6_popup_shown')) {
            if (TRACKING_ENABLED) {
                $.getJSON("/tracking.php", {
                    category: "website.ie6_popup.show"
                });
            }

            $("#popup_right").hover(function() {
                $(this).addClass('pointer_cursor');
            });

            $("#popup_right").click(function() {
                $("#ie6_popup").hide();
                $.cookie('ie6_popup_shown', 1);
                if (TRACKING_ENABLED) {
                    $.getJSON("/tracking.php", {
                        category: "website.ie6_popup.close"
                    });
                }
            });

            $(".browser_pic > a").click(function() {
                var browser = $(this).find("span").attr("class").split(' ').slice(0, 1);
                if (TRACKING_ENABLED) {
                    $.getJSON("/tracking.php", {
                        category: "website.ie6_popup.click.browser",
                        data: browser
                    });
                }
            });
        }

        try {
            DD_belatedPNG.fix('.pngFix');
        } catch(err) {}
    }

    // mouseovers for images, css :hover makes a lot of problems in IE
    $(".img_hover").hover(function() {
        $(this).addClass('hover');
    }, function() {
        $(this).removeClass('hover');
    });

    $(".background_hover").hover(function() {
        $(this).parent().addClass('hover');
    }, function() {
        $(this).parent().removeClass('hover');
    });

    $(".submit_hover").hover(function() {
        var classes = $(this).attr('class').split(' ');

        for (var i in classes) {
            var clazz = classes[i];

            if (clazz != 'submit_hover') {
                $(this).addClass(clazz + '_hover');
            }
        }
    }, function() {
        var classes = $(this).attr('class').split(' ');

        for (var i in classes) {
            var clazz = classes[i];

            if (clazz != 'submit_hover') {
                $(this).removeClass(clazz + '_hover');
            }
        }
    });

    YI.logger.switchGroup('Processing preloading');

    $(".img_hover, .submit_hover, span.background_hover").each(function() {
        YI.image.preloadBackgroundHover(this);
    });

    YI.image._processPreloading();

    resize_to_fit();
    $(window).resize(resize_to_fit);
});
