var prod_box_width = null;
var prod_box_frames = {};
var prod_box_backgrounds = {};
var prod_box_hover_current = null;
function prod_box_type(width) {
    var boxes = $("div.prod_box").css("display", "none");
    if (prod_box_width !== width) {
        if (prod_box_width !== null) {
            boxes.removeClass("box_" + prod_box_width);
        }
        boxes.addClass("box_" + width);
        prod_box_width = width;
    }
}
function prod_box_hover_stop(j_obj) {
    return $(".hover_box", j_obj).stop(true, true);
}
function prod_box_hover_show(id) {
    if (prod_box_hover_current == id) {
        return;
    }
    if (prod_box_hover_current !== null) {
        prod_box_hover_hide(prod_box_hover_current);
    }
    prod_box_hover_current = id;
    prod_box_hover_stop($("#" + id)).fadeIn("slow");
}
function prod_box_hover_hide(id) {
    prod_box_hover_stop($('#' + id)).fadeOut("fast");
    prod_box_hover_current = null;
}
function prod_box_init(prod_boxes) {
    prod_boxes.mouseover(function() {
        prod_box_hover_show(this.id);
    }).mouseleave(function() {
        prod_box_hover_hide(this.id);
    });
}
function prod_box_cart(event) {
    event.stopPropagation();
    if (!$("div.prod_box a.prod_box_trial_" + event.data.id).hasClass("disabled")) {
        add_to_cart(event.data.id, function(success) {
            if (success) {
                $("div.prod_box a.prod_box_trial_" + event.data.id).text(
                        prodbox_is_in_cart_text).attr("title",
                        prodbox_is_in_cart_hint_text).addClass("disabled");
            }
        });
    }
}
function prod_box_cart_verify(event) {
    event.stopPropagation();
    $("#" + event.data.id_prefix + "_prod_box_add_to_cart_" + event.data.id).submit();
}
function prod_box_trial_link_text(width) {
    if (width == 250) {
        return prodbox_is_available_250;
    }
    if (width == 240) {
        return prodbox_is_available_240;
    }
    if (width == 210) {
        return prodbox_is_available_210;
    }
    if (width == 140) {
        return prodbox_is_available_140;
    }
    return "";
}

function prod_box_buy_link_text(width) {
    if (width == 250) {
        return prodbox_buy_text_250;
    }
    if (width == 240) {
        return prodbox_buy_text_240;
    }
    if (width == 210) {
        return prodbox_buy_text_210;
    }
    if (width == 140) {
        return prodbox_buy_text_140;
    }
    return "";
}
function prod_box_links(box, fp_id, width, name, available_trial, available_buy, in_cart, verifymode, id_prefix) {
    var title = null;
    var text = prod_box_trial_link_text(width);
    var disabled = false;
    var click = null;
    var click_data = null;
    var a = $("a.trial", box);
    if (available_trial) {
        if (in_cart) {
            text = prodbox_is_in_cart_text;
            title = prodbox_is_in_cart_hint_text;
            disabled = true;
        }
        else {
            title = prodbox_is_available_hint_text;
            if (verifymode) {
                click = prod_box_cart_verify;
                click_data = {id:fp_id, id_prefix:id_prefix};
            }
            else {
                click = prod_box_cart;
                click_data = {id:fp_id};
            }
        }
    }
    else {
        title = prodbox_is_out_hint_text[0] + name + prodbox_is_out_hint_text[1];
        disabled = true;
    }
    a.removeClass().addClass("trial").addClass("prod_box_trial_" + fp_id);
    a.text(text).attr("title", title).toggleClass("disabled", disabled);
    a.unbind("click", prod_box_cart);
    a.unbind("click", prod_box_cart_verify);
    if (click) {
        a.bind("click", click_data, click);
    }
    
    a = $("a.buy_l", box);
    title = prodbox_can_order_hint_text;
    text = prod_box_buy_link_text(width);
    var link = "/order/" + fp_id + "/";
    disabled = false;
    if (!available_buy) {
        title = prodbox_can_not_order_hint_text;
        link = "javascript:void(0);";
        disabled = true;
    }
    a.text(text).attr("title", title).toggleClass("disabled", disabled).attr("href", link);
}

function prod_box_set(prod_box_id, fp_id, width, img_group) {
    var box = $("#prod_box_" + prod_box_id);
    var fp = prod_box_frames[fp_id];
    box.unbind("click");
    box.bind("click", function() {window.location=fp["url"]});
    
    var i = 1;
    var vote = Math.round(fp["vote"]);
    var vote_container = $("div.vote_gray_container", box);
    if (fp["votes"] < prodbox_vote_wait_limit) {
        for (; i < 6; i++) {
            $(".vote_gray" + i, vote_container).removeClass("check");
        }
        vote_container.attr("title", prodbox_vote_wait_hint);
    }
    else {
        for (; i < 6; i++) {
            $(".vote_gray" + i, vote_container).toggleClass("check", i <= vote);
        }
        vote_container.attr("title", prodbox_vote_hint[0] + (Math.round(fp["vote"] * 100) / 100.0) + prodbox_vote_hint[1] + fp["votes"] + prodbox_vote_hint[2]);
    }
    
    $("div.price", box).text(fp["price"]);
    
    prod_box_links(box, fp_id, width, fp["name"], fp["available_trial"], fp["available_buy"], fp["in_cart"], false, null);
    
    $("a.fname", box).attr("href", fp["url"]).text(fp["name"]); 
    
    if (!img_group) {
        img_group = Math.round(Math.random()) ? "f" : "m";
    }
    background = prod_box_backgrounds[width + img_group];
    if (background && background[fp_id]) {
        box.css("background-image", "url(" + background[fp_id] + ")")
    } else {
        if (img_group == "f") {img_group = "m"; }
        else  {img_group = "f";}
        background = prod_box_backgrounds[width + img_group];
        if (background && background[fp_id]) {
            box.css("background-image", "url(" + background[fp_id] + ")")
        }
        else {
            box.css("background-image", null);
        }
    }
    
    box.css("display", "block");
}
