$(document).ready(function(){
    /*  Previews  */
    if($('A.preview-thumbnail')){
        $('A.preview-thumbnail').bind('mouseenter', function(){
            var originalImg = $(this).attr('rel');
            $('#preview-original-img').html('<img src="' + originalImg + '" alt="" />');
            if($(this).children() && $(this).children()[1]){
                $('#preview-info').html($($(this).children()[1]).html());
            }
        });
    }

    /*  Option links  */
    if($('.option-links A')){
        $('.option-links A').bind('mouseenter', function(){
            $('#option-description').html( $(this).attr('title') );
        });
        $('.option-links A').bind('mouseleave', function(){
            $('#option-description').html('&nbsp;');
        });
    }
    if ($.browser.msie && $.browser.version == 6) {
        $('.main-menu-box ul.main-menu li.top').mouseover(function() {
            $(this).find('.sub-nav').show();
            if ($(this).find('.sub-nav').length > 0) {
                subMenuVisible = true;
                $('.dropdowns').hide();
            }
        });

        $('.main-menu-box ul.main-menu li.top').mouseout(function() {
            subMenuVisible = false;
            setTimeout('moutMenu();', 0);
            $('.sub-nav').hide()
        });
    }
});

function moutMenu() {
    if (!subMenuVisible) {
        $('.sub-nav').hide();
        $('.dropdowns').show();
    }
};

// not animated collapse/expand
function togglePannelStatus(content) {
    var expand = (content.style.display == "none");
    content.style.display = (expand ? "block" : "none");
    toggleChevronIcon(content);
}

// current animated collapsible panel content
var currentContent = null;

function togglePannelAnimatedStatus(content, interval, step) {
    // wait for another animated expand/collapse action to end
    if (currentContent == null) {
        currentContent = content;
        var expand = (content.style.display == "none");
        if (expand)
            content.style.display = "block";
        var max_height = content.offsetHeight;

        var step_height = step + (expand ? 0 : -max_height);
        toggleChevronIcon(content);

        // schedule first animated collapse/expand event
        content.style.height = Math.abs(step_height) + "px";
        setTimeout("togglePannelAnimatingStatus("
            + interval + "," + step
            + "," + max_height + "," + step_height + ")", interval);
    }
}

function togglePannelAnimatingStatus(interval,
    step, max_height, step_height) {
    var step_height_abs = Math.abs(step_height);

    // schedule next animated collapse/expand event
    if (step_height_abs >= step && step_height_abs <= (max_height - step)) {
        step_height += step;
        currentContent.style.height = Math.abs(step_height) + "px";
        setTimeout("togglePannelAnimatingStatus("
            + interval + "," + step
            + "," + max_height + "," + step_height + ")", interval);
    }
    // animated expand/collapse done
    else {
        if (step_height_abs < step)
            currentContent.style.display = "none";
        currentContent.style.height = "";
        currentContent = null;
    }
}

// change chevron icon into either collapse or expand
function toggleChevronIcon(content) {
    var chevron = content.parentNode
        .firstChild.childNodes[1].childNodes[0];
    var expand = (chevron.src.indexOf("expand.gif") > 0);
    chevron.src = chevron.src
        .split(expand ? "expand.gif" : "collapse.gif")
        .join(expand ? "collapse.gif" : "expand.gif");
}