function cl(msg){
    if(typeof console != 'undefined')
        console.log(msg);
}

jQuery.noConflict();

jQuery(document).ready(function($) {
    $("#tabs").tabs().tabs('rotate',5000);

    $(".more-views .thumbnails a").fancybox({
        'titleShow': false,
        'overlayOpacity': 0.5,
        'overlayColor': '#000'
    });

    $(".measuring-diagram").fancybox({
        'titleShow': false,
        'overlayOpacity': 0.5,
        'overlayColor': '#000'
    });

    $('.level0-wrap1').bgiframe();
});

function showReviewsAdditional(event) {
    Effect.SlideDown('reviews_additional');
    Effect.BlindUp('toggle_reviews', { duration: 0.5 });
};

document.observe('dom:loaded', function() {
    $$('a[rel="external"]').each(function(link) {
        if(link.readAttribute('href') != '' && link.readAttribute('href') != '#') {
            link.writeAttribute('target', '_blank');
        }
    });

    if($('toggle_reviews') != null) {
        $('toggle_reviews').observe('click', showReviewsAdditional);
    };
});

function toggleMenu(el, over, second) {
    if(!second && !over) {
        setTimeout(function(){toggleMenu(el, 0, 1);}, 100);
        el.setAttribute('sameAttribute', 1);
        return;
    } else if(!over && el.getAttribute('sameAttribute') == 0) {
        return;
    }
    el.setAttribute('sameAttribute', 0);

    if (over) {
        Element.addClassName(el, 'over');
    }
    else {
        Element.addClassName(el, 'hold');
        Element.removeClassName(el, 'over');
        setTimeout(function(){Element.removeClassName(el, 'hold');}, 300);
    }
}

function submitAjaxForm(item) {
    var wrapperFunction = this;
    var currentForm = $(item);
    var currentFormId = currentForm.readAttribute('id');

    var statusBox = $$('form#' + currentFormId + ' div#status_' + currentFormId);
    statusId = 'status_' + currentFormId;
    statusDiv = new Element('div', {
        'id': statusId
    });
    if (typeof statusBox[0] != 'undefined') {
        statusBox[0].remove();
    }
    currentForm.insert({ 'bottom': statusDiv });
    
    $('status_' + currentFormId).addClassName('cart-loading');
    
    ajaxSuccess = function(json) {
        var currentFormId = this.readAttribute('id');
        if (json.noEntry) {
            return false;
        }
        $('status_' + currentFormId).removeClassName('cart-loading');
        if (json.headerJSON == false) {
            return false;
        }
        var statusBox = $$('form#' + currentFormId + ' div#status_' + currentFormId);
        var messageQty = 0;
        var backorderQty = 0;
        if (typeof $$('#status_' + currentFormId + ' .success-qty')[0] != 'undefined') {
            messageQty = parseInt($$('#status_' + currentFormId + ' .success-qty')[0].innerHTML);
            backorderQty = parseInt($$('#status_' + currentFormId + ' .backorder-qty')[0].innerHTML);
            messageQty = isNaN(messageQty) ? '' : 0;
            backorderQty = isNaN(backorderQty) ? '' : 0;
        }
        if (json.headerJSON.error == '') {
            $$('form#' + currentFormId + ' div#status_' + currentFormId).invoke('update', '<span class="cart-success"><span class="success-qty">' + (messageQty + parseInt(json.headerJSON.messageQty) > 0 ? messageQty + parseInt(json.headerJSON.messageQty) : '') + '</span>' + json.headerJSON.messageText + '</span><span class="cart-backordered"><span class="backorder-qty">' + (backorderQty + parseInt(json.headerJSON.backorderQty) > 0 ? backorderQty + parseInt(json.headerJSON.backorderQty) : '') + '</span>' + json.headerJSON.backorderText + '</span>');
            $$('a.top-link-cart').invoke('update', json.headerJSON.cartHtml);
        } else {
            $$('form#' + currentFormId + ' div#status_' + currentFormId)[0].update('<span class="cart-backordered">' + json.headerJSON.error + '</span>');
        }
        return true;
    }

    new Ajax.Request(currentForm.readAttribute('action'), {
        method:'post',
        onSuccess: this.ajaxSuccess.bind(currentForm),
        parameters: currentForm.serialize(true)
    });
}

