/*
 * Integration code for the YourShot Custom Covers cart
 *
 *   Depends on YSCC.ecURL being set in the page that loads this
 */

var YSCC;
if (!YSCC) { YSCC = new Object(); }

// Attach event handlers
YSCC.cartInit = function() {
    yui.Event.addListener('proceed_to_checkout_button','click', YSCC.proceedToCheckout);
};
yui.Event.onDOMReady(YSCC.cartInit);

YSCC.proceedToCheckout = function() {
    // Make sure the TOS checkbox is checked
    if (!yui.Dom.get('tos_confirm').checked) {
        yui.Dom.get('photo_info_error').innerHTML = 'You must agree to the Terms and Conditions to continue.';
        return;
    }

    // This is a bit odd - we need to simultaneously
    // add an item to a cart, log in, and end up at the checkout page.
    // So, we do this on the EC side with a substore hook.  All we need
    // to pass is the directory where we stored the images; if we're revisiting,
    // there will be a cookie sent along with this.
    var dir = yui.Dom.get('random_dir').value;

    var mode_str = ((YSCCUI.mode == 'none') ? '' : '&mode=' + YSCCUI.cart_mode);
    var host_str = '&host=' + window.location.host;
    var url = YSCC.cartURL + '/store/add-to-cart-and-proceed-to-checkout?file_dir=' + dir + mode_str + host_str;
    window.location = url;

};
