// This is our own version of lightbox that let's us load any type of content 
// into the lightbox instead of just images.
function ajax_lightbox(ajax_url, options) {
    if (myLightbox) {
        myLightbox.start(ajax_url, options);
    }
    // dont allow the href to get processed if we make it into this function
    return false;	
}
// Used for sub-navigation of content nodes
function switchToTab(index, tabCount) {
    $('products').hide();
    $('software_tab').removeClassName("active");
    for(var i = 0; i < tabCount; i++) {
        var tab = $('tab_' + i);
        var tab_panel = $('tab_panel_' + i);
        if (tab_panel) {
            if (index == i) {
                tab_panel.show();
                tab.addClassName("active");
            } else {
                tab_panel.hide();
                tab.removeClassName("active");
            }
            load_product_images("tab_panel_" + i);
        }
    }
    $('tab_panels').show();
}
function switchToTemplateTab(index, tabCount) {
    for(var i = 0; i < tabCount; i++) {
        var tab = $('tab_' + i);
        var tab_panel = $('tab_panel_' + i);
        if(tab_panel) {
            if(index == i) {
                tab_panel.show();
                tab.addClassName("active");
            } else {
                tab_panel.hide();
                tab.removeClassName("active");
            }
            load_product_images("tab_panel_" + i);
        }
    }
}
// Loads product images in the background found withing the given container.
// Set the rel attribute of the img tag to the actual url of the image.
function load_product_images(container) {
  $(container).getElementsBySelector('img').each(function(img) {
    var rel = img.getAttribute('rel');
    if (rel != null && rel.length > 0) {
        img.src = rel;
    }
  })
}
// Used for sub-navigation of default category and home pages where no parameters are passed
function switchToSubtab(url) {
    var today = new Date();
    var now = today.getTime();
    new Ajax.Request(url + "?t=" + now, { asynchronous:true, evalScripts:true, onLoading:function(request) { $('products').hide();$('load_indicator').show(); } }); 
    return false;
}
// Used to load products remotely were URL parameters are present, like for sorting
function loadProducts(url) {
    var today = new Date();
    var now = today.getTime();
    new Ajax.Request(url + "&t=" + now, {asynchronous:true, evalScripts:true, onLoading:function(request){$('products').hide();$('load_indicator').show();}}); 
    return false;
}
// Used to load the up-sells on the product page
function loadRelatedProducts(url) {
    var today = new Date();
    var now = today.getTime();
    new Ajax.Request(url + "?t=" + now, { asynchronous:true, evalScripts:true });
}
function setRelatedProductHeights() {
    $$('.product_container').each(function(s) {s.setStyle({height:'150px'});});
}
function highlightSelectedTab(selectedTab) {
    var tabs = [ "top_sellers", "new_releases", "coming_soon", "software", "accessories", "bundles" ];
    tabs.each(function(t) {
        var tab_id = t + "_tab";
        if (t == selectedTab) {
            if ($(tab_id)) {
                $(tab_id).addClassName('active');
            }
        } else {
            if ($(tab_id)) {
                $(tab_id).removeClassName('active');
            }
        }
    });
}
function hand_off_order(order_number) {
    new Ajax.Request("/store/hand_off/" + order_number, { asynchronous:false, onSuccess:function(transport) { $('checkout_form').submit(); } });
}
function highlightAddToCartButton(preorder) {
    if ($('add_to_cart_button')) {
        if (preorder == "true") {
            $('add_to_cart_button').addClassName("preorder_selected");
        } else { 
            $('add_to_cart_button').addClassName("add_to_cart_selected");
        }
    }
}
function unhighlightAddToCartButton(preorder) {
    if ($('add_to_cart_button')) {
        if (preorder == "true") {
            $('add_to_cart_button').removeClassName("preorder_selected");
        } else {
            $('add_to_cart_button').removeClassName("add_to_cart_selected");
        }
    }
}
function addingToCart() {
    $('add_to_cart_button').addClassName("adding_to_cart");
    window.setTimeout("$('add_to_cart_form').submit()", 500);
}
