// Define variables with fallbacks outside document.ready
var acc_obj_relate = {};
var images_arr = {};
var acc_obj_order = {};
var rental_name_template = '';
var alt_text_template = '';

if (typeof window.io_rental_data !== 'undefined') {
    try{
        acc_obj_relate = JSON.parse(window.io_rental_data['acc_obj_relate']);
    } catch{
        acc_obj_relate = {};
    }
    try{
        images_arr = JSON.parse(window.io_rental_data['images_arr']);
    } catch {
        images_arr = {};
    }
    try{
        acc_obj_order = JSON.parse(window.io_rental_data['acc_obj_order']);
    } catch {
        acc_obj_order = {};
    }
    rental_name_template = window.io_rental_data['rental_name'] || '';
    alt_text_template = window.io_rental_data['image1alttext'] || '';
}

/* when clicking the thumb swatches under main image (2nd image for an item) */
function updateMainPic(img) {
    /* calling image */
    let thumb_id = img.id;
    let big_id = thumb_id.replace('thumb', 'big');

    /* main image we are updating */
    let main_img = document.querySelector(".io_image_big");

    let img_big = document.querySelector("#" + big_id);
    main_img.src = img_big.href;
    /* document.querySelector("#single_image").href =img_big.href; */
}
jQuery(document).ready(function () {
    //stars
    let io_star_pic = document.querySelector("#io_star_pic");
    if (io_star_pic != null) {
        let io_star_src = io_star_pic.src;
        if (io_star_src.indexOf("0star") > -1 || io_star_src.indexOf("1star") > -1 || io_star_src.indexOf("2star") > -1 ||
            io_star_src.indexOf("3star") > -1) {
            io_star_pic.style.display = "none";
        }
    }

    /* build multiple thumb images */
    let img_container = document.querySelector(".io_product_picbox .io_image_thumbbox");
    let page_picbox = document.querySelector(".io_product_picbox");
    if (typeof images_arr !== "undefined" && img_container && page_picbox) {
        /* Clear noopeners to rebuild */
        let no_openers = document.querySelectorAll(".io_product_picbox [id^='io_img_big']");
        for (let n of no_openers) {
            n.parentElement.removeChild(n);
        }
        img_container.innerHTML = "";
        for (key in images_arr) {
            /* build and add the noopener */
            img_noopener_ele = document.createElement("a");
            img_noopener_ele.rel = "img_group noopener";
            img_noopener_ele.href = images_arr[key]["rentalimage_imagelocbig"];
            img_noopener_ele.id = "io_img_big" + (parseInt(key, 10) + 1);
            page_picbox.appendChild(img_noopener_ele);

            /* build and add the thumb image */
            img_thumb_ele = document.createElement("img");
            img_thumb_ele.className = "io_image_thumb skip-lazy";
            img_thumb_ele.title = rental_name_template;
            img_thumb_ele.src = images_arr[key]["rentalimage_imageloc"];
            img_thumb_ele.alt = alt_text_template;
            img_thumb_ele.id = "io_img_thumb" + (parseInt(key, 10) + 1);
            img_thumb_ele.onmouseover = function() { updateMainPic(this) };
            img_container.appendChild(img_thumb_ele);
        }
    }

    // Check if rental data exists before accessing
    if (typeof window.io_rental_data === 'undefined') {
        console.warn('io_rental_data not found - shortcode may not have loaded yet');
        return;
    }

    for(i=0;i<parseInt(window.io_rental_data['rating']);i++){
        star_html = `<div class=\"fas fa-star\" style=\"color:goldenrod\"></div>`;
        jQuery(".io_stars_"+window.io_rental_data["rental_id"]).append(star_html);
    }

    acc_obj_relate = order_acc_obj_relate();
    jQuery(".io_product_price_top_left").each(function () {
        if( !Number( this.textContent.replace(/[^0-9.-]+/g,"") ) ){
            jQuery(this.parentElement).hide();
        }
    });
    jQuery(".io_product_price_left").each(function () {
        if( !Number( this.textContent.replace(/[^0-9.-]+/g,"") ) ){
            jQuery(this.parentElement).attr("style","display:none !important");
        }
    });

    /* if an item is consumable IO-21288*/
    if(jQuery(".item_qtytype").length && jQuery(".item_qtytype")[0].innerText == "Supply"){
        jQuery("#io_all_prices").hide();
        jQuery(".io_product_price_fixed").show();
    }
});