var page = 0;
var pages = 0;
var all = 1;
var frame_list_checksum = '';
var current_frame_list = null;
var new_list_frames_start = 6;
var scroll_to_top = false;
var framelist_cache_div = null;
var framelist_div = null;

function do_on_input_click(f) {
    return function(event) {
        var i = $(event.currentTarget).find("input");
        if ((i.attr("type") == "radio" && !i.attr("checked"))
                || i.attr("type") !== "radio") {
            i.attr("checked", !i.attr("checked"));
            f();
        }
    }
}
function frames_ajax_wait(wait) {
    ajax_wait(wait, "framelist_");
    if (wait) {
        $("#frames_page_title span.show_frames").addClass("hidden");
    } else {
        $("#frames_page_title span.show_frames").removeClass("hidden");
    }
}
function update_fieldset_label_not_checked() {
    var fieldset = $(this)
    if ($("input:checked", fieldset).length) {
        $("div.not_checked:has(input:checked)", fieldset).removeClass(
                "not_checked");
        $("div:not(.not_checked):has(input:not(:checked))", fieldset).addClass(
                "not_checked");
    } else {
        $("div.not_checked", fieldset).removeClass("not_checked");
    }
}
function update_navi_line(from, to, frames_count) {
    $(".framelist_current_from").text(from);
    $(".framelist_current_to").text(to);
    $("#frame_list_navi td.frame_list_navi_pages").toggleClass("hidden",
            pages <= 1);
    if (pages > 1) {
        for ( var n = 0; n < max_pages; n++) {
            number = $(
                    "#frame_list_navi td.frame_list_navi_pages .navi_page_" + n)
                    .toggleClass("hidden", n >= pages).toggleClass("current",
                            n == page);
        }
    }
    $("#frame_list_navi td.frame_page_prev").toggleClass("hidden", page <= 0);
    $("#frame_list_navi td.frame_page_next").toggleClass("hidden",
            page >= pages - 1).toggleClass("no_border", page >= pages - 1);
    $("#frame_list_navi td.frame_list_navi_pages .navi_page_0").toggleClass(
            "no_border", page <= 0);
    $("#frame_list_navi td.frame_list_navi_all div span.show_all_text").text(
            all ? show_all_reduce_text : show_all_extend_text);
    $("#frame_list_navi td.frame_list_navi_info").toggleClass("hidden",
            frames_count == 0);
    $("#frame_list_navi .frame_list_navi_all").toggleClass("hidden",
            frames_count <= default_frames_per_page);
}
function update_title_line(hits) {
    $(".framelist_count").text(hits);
    switch (parseInt(hits)) {
    case 0:
        $("#frames_page_title td.title .hits_text").text(hits_text_0);
        break;
    case 1:
        $("#frames_page_title td.title .hits_text").text(hits_text_1);
        break;
    default:
        $("#frames_page_title td.title .hits_text").text(hits_text_more);
        break;
    }
    $("#frames_page_title td.option div:has(input:not(checked))")
            .removeClass("checked");
    $("#frames_page_title td.option div:has(input:checked)").addClass(
            "checked");
    $(".sort_option_name").text($("#frames_page_title td.option div:has(input:checked)").find('#sort_text').text());
   
}
function update_frame_list(new_list) {
    frame_list_checksum = new_list[0];
    var frames_count = parseInt(new_list[1]);
    page = parseInt(new_list[2]);
    pages = parseInt(new_list[3]);
    var frames_per_page = parseInt(new_list[4]);
    all = parseInt(new_list[5]);

    var current_to = 0;
    var current_from = 0;    
    if (page == 0) {
        current_from = 1;
        if (frames_count < frames_per_page) {
        	current_to = frames_count
        }  else {
    	    current_to = frames_per_page;
        }
    }  else if (page == pages - 1) {
        current_from = (page * frames_per_page) + 1;
        current_to = frames_count;
    } else {
        current_from = (page * frames_per_page) + 1;
        current_to = ((page + 1) * frames_per_page);
    }    
    update_navi_line(current_from, current_to, frames_count);
    update_title_line(new_list[1]);

    width = 220;
    var width_option = $("#frames_page_title #show_option").val();
    if (width_option == "0") {width = 222;}
    if (width_option == "1") {width = 222;}
    if (width_option == "2") {width = 220;}
    prod_box_type(width);

    if (scroll_to_top) {
        scroll_to_top = false;
        window.scrollTo(0, 0);
    }

    var img_group = null;
    if (width_option == "0") {img_group = "f";}
    if (width_option == "1") {img_group = "m";}

    for (var i_new = new_list_frames_start; i_new < new_list.length; i_new++) {
        prod_box_set(i_new - new_list_frames_start, parseInt(new_list[i_new]), width, img_group);
    }

    current_frame_list = new_list;
    frames_ajax_wait(false);
}
function reload_framelist(data) {
    frames_ajax_wait(true);
    var request = {};
    request["f_page"] = page;
    request["f_all"] = all;
    request["f_sort"] = $("#frames_page_title td.sort_option input:checked").val();
    request["f_show"] = $("#frames_page_title #show_option").val();
    request["checksum"] = frame_list_checksum;
    if (data) {
        update_frame_list(data.split(" "));
    } else {
        $.get("/frames/list", request, function(body, statusMessage) {
            if (body == "all_frames") {
                location = "/frames/";
            } else if (body == "female_plastic") {
            	location = "/frames/female_plastic";
            } else if (body == "female_metal") {
            	location = "/frames/female_metal";
            } else if (body == "male_plastic") {
            	location = "/frames/male_plastic";
            } else if (body == "male_metal") {
            	location = "/frames/male_metal";
            }
            update_frame_list(body.split(" "));
        });
    }
}
function jump_page(to_page) {
    if (page !== to_page) {
        page = to_page;
        scroll_to_top = true;
        reload_framelist(null);
    }
}


