function empty(obj)
{
  return (typeof obj == "undefined");
}




jQuery(function($) {

    var catalog = {
        additive: false,
        section_item_separator_template_class: "",
        basket_url: "",
        section_item_template_class: "",
        section_template_class: "",
        $section_template: null,
        sections_active: [],
        addSections: function (sections, are_elements)
        {
            var i = 0;
            var l = sections.length;
            var $section_last;
            if (this.sections_active.length)
                $section_last = this.sections_active[this.sections_active.length - 1];
            else
                $section_last = this.$section_template;
            $section_last = this.$section_template.clone().insertAfter($section_last);
            var $section_item = $section_last.children("." + this.section_item_template_class);
            var $section_separator = $section_last.children("." + this.section_item_separator_template_class);
            var section, section_name;
            for (i=0;i<l;i++)
            {
                if (!are_elements)
                    section = catalog_sections["section" + sections[i]];
                else
                    section = catalog_elements["element" + sections[i]];
                if (!empty(section))
                {
                    section_name = "section" + this.sections_active.length;
                    if (catalog.additive)
                      section_name += "_" + section.id;
                    $section_item.clone().appendTo($section_last).removeClass(this.section_item_template_class).append(section.name).children("input").attr("value", section.id).attr("name", section_name).click(function(section_level_index, section_id, are_elements){
                        return function(){
                            if (!catalog.additive && $(this).is(".checked_section"))
                              return;
                            if (!catalog.additive)
                            {
                              $("input", $(this).parents("td")).removeClass("checked_section").removeAttr("checked");
                              $(this).addClass("checked_section").attr("checked", true);
                            }
                            else
                            {
                              if (!$(this).hasClass("checked_section"))
                                $(this).addClass("checked_section");
                              else
                                $(this).removeClass("checked_section");
                            }
                            catalog.showSectionSubSections(section_level_index, section_id, are_elements);
                        }
                    }((this.sections_active.length + 1), sections[i], are_elements));
                    $section_separator.clone().appendTo($section_last).removeClass(this.section_item_separator_template_class);
                }
            }
            var section_header_index = null, $header = "", $tip = null;
            /*
            if (!this.sections_active.length)
                section_header_index = 0;
            */
            if (!are_elements && !empty(catalog_sections["section" + sections[0]].children_elements) && catalog_sections["section" + sections[0]].children_elements.length)
                section_header_index = $(".catalog_headers li").length - 2;
            else
                if (!are_elements && $(".catalog_headers li").length > 2)
                    section_header_index = $(".catalog_headers li").length - 3;
            if (are_elements)
                section_header_index = $(".catalog_headers li").length - 1;

            if (section_header_index != null)
            {
                var $catalog_column = $(".catalog_headers li:eq(" + section_header_index + ")");
                $header =  $catalog_column.children("span");
                $tip = $catalog_column.children("p");
            }
            if ($header.length)
            {
                var $section_header = $section_last.children(".catalog_section_header");
                $section_header.prepend((this.sections_active.length + 1) + ".&nbsp;" + $header.html() + ":");
                if ($tip != null && $tip.length && $tip.html() != "")
                {
                    $(".id-printsubmit").trigger("init_tips", [$tip.html(), $section_header.children(".tip_icon")]);
                    $section_header.addClass("q");
                }
/*
                    $section_header.addClass("q").append("<div class='pop'><div class='tip'>" + $tip.html() + "</div></div>").children(".tip_icon").hover(function(){
                        $(".tip", $(this).parent()).show();
                    }, function(){
                        $(".tip", $(this).parent()).hide();
                    });
*/
            }
            /*
            if (are_elements)
            {
                var headers = $(".catalog_headers li");
                var headers_l = headers.length - 1;
                var headers_i, $header;
                var $headers_section = $section_last;
                for (headers_i=headers_l;headers_i > -1;headers_i--)
                {
                    $header = $(".catalog_headers li:eq(" + headers_i + ")");
                    if ($header.length)
                        $headers_section.children(".catalog_section_header").html($header.html() + ":");
                    $headers_section = $headers_section.prev();
                    if (!$headers_section.length)
                        break;
                }
            }
            */
            $section_last.removeClass(this.section_template_class);
            this.sections_active[this.sections_active.length] = $section_last;
        },
        showSectionSubSections: function(section_level_index, section_id, are_elements)
        {
            var i;
            var l = this.sections_active.length;
            for (i=section_level_index;i<l;i++)
                this.sections_active[i].remove();

            var tmp = [];
            for (i=0;i<section_level_index;i++)
                tmp[i] = this.sections_active[i];
            this.sections_active = tmp;

            

            if (section_level_index < l)
            {
                $(".catalog_active_description").html("");
                $(".catalog_active_pic").hide();
                $(".catalog_active_pic img").hide();
                $(".id-printsubmit").trigger("reset_form");
            }


            if (!are_elements)
            {
                //$(".catalog_delivery").hide();
                $(".id-printsubmit").trigger("show_error");
                if (!empty(catalog_sections["section" + section_id]))
                {
                    $(".catalog_active_description").html(catalog_sections["section" + section_id].description);
					$(".desc_comments").attr("value", catalog_sections["section" + section_id].description);
					$(".element_code").attr("value", catalog_sections["section" + section_id].element_code);
					var p = window.location.pathname.split("/");

                    if(p[1] != "press") 
					{
						$(".catalog_active_description").html($(".desc_comments").val() + '<br><a href="/mobile/more/' + catalog_sections["section" + section_id].element_code + '/">Подробнее</a>');						
					}
                    if (!empty(catalog_sections["section" + section_id].pic) && catalog_sections["section" + section_id].pic.length)
                    {
                        $(".catalog_active_pic img").attr("src", catalog_sections["section" + section_id].pic).show();
                        $(".catalog_active_pic").show();
                    }
                }
                if (!empty(catalog_sections["section" + section_id]) && !empty(catalog_sections["section" + section_id].children_elements) && catalog_sections["section" + section_id].children_elements.length)
                {
                    this.addSections(catalog_sections["section" + section_id].children_elements, true);
                    $(".id-printsubmit").trigger("catalog_new_sections_added");
                    return true;
                }
                if (!empty(catalog_sections["section" + section_id]) && catalog_sections["section" + section_id].children_sections.length)
                {
                    this.addSections(catalog_sections["section" + section_id].children_sections, false);
                    $(".id-printsubmit").trigger("catalog_new_sections_added");
                    return true;
                }
            }
            if (are_elements && !empty(catalog_elements["element" + section_id]))
            {
                $(document).trigger("catalog_first_step_complete", [section_id, this.basket_url]);
            }
        }
    }

    catalog.additive = $(".catalog_order_form").hasClass("additive");

    $(".id-printsubmit").bind("init_tips", function(event, tip_html, $sender){
        $sender.hover(function(tip_html){return function(){
            var o = $(this).offset();
            var left = o.left;
            var tip_width = 340;
            if ((left + tip_width) > $(window).width())
                left -= tip_width;
            $("#tip_overlay").html(tip_html).css("left", left + 10).css("top", o.top).show();
        }}(tip_html), function(){
            $("#tip_overlay").html("").hide();
        });
    });
    $(".id-printsubmit").bind("show_error", function(){
        $(".catalog_order_form").removeClass("active");
        $("a.catalog_submit").removeClass("active");
        $(".id-printsubmit .catalog_price").html($(".empty_price_stub").html());
        $(".id-printsubmit .catalog_submit_error").show();
    });
    $(".id-printsubmit").bind("hide_error", function(){
        $(".catalog_order_form").addClass("active");
        $(".id-printsubmit .catalog_submit_error").hide();
        $("a.catalog_submit").addClass("active");
    });
    $(".id-printsubmit .catalog_submit").click(function(){
        if (!$(this).is(".active"))
            return false;
    });

    if (typeof catalog_initial_sections != "undefined")
    {
        $(".catalog_active_pic").hide();
        catalog.basket_url = $(".id-printsubmit .catalog_submit").attr("href");
        if (typeof catalog.basket_url == "undefined")
            catalog.basket_url = $("form.catalog_order_form").attr("action");
        catalog.section_item_separator_template_class = "jstemplate_separator";
        catalog.section_item_template_class = "jstemplate_label";
        catalog.section_template_class = "catalog_section_jstemplate";
        catalog.$section_template = $("." + catalog.section_template_class);
        if (catalog_initial_sections.length)
            catalog.addSections(catalog_initial_sections, false);
         else
         {
             if (!empty(catalog_elements))
             {
                var i=0, tmp = [];
                for (element in catalog_elements)
                {
                    tmp[i] = catalog_elements[element].id;
                    i++;
                }
                catalog.addSections(tmp, true);
             }   
         }

        $(".catalog_order_form").removeClass("loading");
    }

    if (typeof catalog_section != "undefined")
    {
        $(".catalog_active_description").html(catalog_section.description);
		$(".desc_comments").attr("value", catalog_section.description);
        if (!empty(catalog_section.pic) && catalog_section.pic.length)
        {
            $(".catalog_active_pic img").attr("src", catalog_section.pic).show();
            $(".catalog_active_pic").show();
        }
    }
});