var fader_color = "";

jQuery(document).ready(function() {
    if (jQuery("#fader-color").length) {
        fader_color = jQuery("#fader-color").val();
    } else {
        fader_color = "yellow";
    }
    rebuild_fader();
    jQuery(window).resize(rebuild_fader);
    jQuery('#header #menu1 .with-submenu').not('.current-menu').mouseenter(show_submenu).mouseleave(hide_submenu);
    jQuery('#header #menu2 .with-submenu').not('.current-menu').mouseenter(show_submenu).mouseleave(hide_submenu);
    jQuery('.images-grid .post-item').mouseenter(show_description).mouseleave(hide_description);
    jQuery('.press-grid .post-item').mouseenter(highlight_thumb).mouseleave(unhighlight_thumb);
    jQuery('.showroom #left-carousel .item-thumb').click(show_full_image);
});

function highlight_thumb() {
    jQuery(this).find('.post-description-wrapper').css('height', jQuery(this).find('.post-image').first().height());
    jQuery(this).find('.post-description-wrapper').stop(true, true).fadeIn('fast');
}

function unhighlight_thumb() {
    jQuery(this).find('.post-description-wrapper').stop(true, true).fadeOut('fast');
}

function rebuild_fader() {
    jQuery("body").css('height', jQuery(document).height());
    if (jQuery(window).width() > 1240) {
        overflowing = 400 - (jQuery(window).width() - 1024);
        if (overflowing < 0) {
            overflowing = 0;
        }
        if (jQuery('#fader').length != 0) {
            fader = jQuery('#fader');
        } else {
             fader = jQuery('#fader');
           jQuery('body').prepend(fader);
        }
        fader.css('height', jQuery(document).height() + 'px')
             .css('background-position', overflowing + 'px 0px');
    } else {
        jQuery('#fader').remove();
    }
}

function show_submenu() {
    jQuery(this).find('.submenu').fadeIn('fast');
}

function hide_submenu() {
    jQuery(this).find('.submenu').fadeOut('fast');
}

function show_description() {
    jQuery(this).find('.post-description-wrapper').stop(true, true).animate({'height': jQuery(this).height()/2 + 5}, 200);
}

function hide_description() {
    jQuery(this).find('.post-description-wrapper').stop(true, true).animate({'height': 0}, 200);
}

function show_full_image() {
    current = jQuery('.showroom #stage #current-shown').removeAttr('id');
    new_el = jQuery(this).find('.full-image').clone();
    jQuery('.showroom #stage').append(new_el);
    current.fadeOut(function() { jQuery(this).remove(); });
    new_el.fadeIn(function() { jQuery(this).attr('id', 'current-shown'); });
    rebuild_fader();
}

function show_full_press(el) {
    jQuery.facebox({ajax: jQuery(this).attr('href')});
}
