var selected = new Array();
selected['all'] = new Array();
$(document).ready(function () {
   if ( $("#packages-list.products-menu").length > 0 ) {
        $("#packages-list.products-menu div.sub").hide();
        $("#packages-list.products-menu div.item img.check").hide();
        $("#packages-list.products-menu div.sub .active").removeClass("active");
        $("#packages-list.products-menu div.sub .blue").removeClass("blue").addClass("black");
        $(".paqresult").find(".toticon").hide();
        $(".paqresult").children().hide().find(".plus").hide();
        var section_current, section_new, section_subindex;
        var prices = new Array();
        section_current = -1;

        $(".products-menu div.item").live("click",function( e ){
            e.preventDefault();
            section_current = $(".products-menu div.item.active");
            section_new = $(this);
            if (section_current.index()!=section_new.index()) {
                section_new
                    .addClass("active")
                    .next(".sub")
                    .slideDown();
                section_current
                    .removeClass("active")
                    .next(".sub")
                    .slideUp();
            }
        });

       $(".item.cable").trigger("click")

        $(".caracteristica, .recomendamos:not(.options), .nompaq.mod, .modulo, .nompaq.extras, .prdExt").hide();
        $(":checkbox").attr("checked",false);

        var activeCaracteristics = 0;
        var activeModules = 0;
        var activeExtras = 0;
        var currentPckg = "";
        var $thePack_data;
        var $theSec_data;

        $("input[type='checkbox']").click(function () {$(this).trigger("selectItem")}).bind("selectItem",function () {
            var isExtra = $(this).parents(".extrasitem").length>0 || $(this).is(".extrasitem");
            var currPack;
            var $theSub;
            var $theSec;
            var sec;
            var checkedSiblings;
            var checkedExtras;

            if (!isExtra) {
                $theSub = $(this).parents(".sub");
                $theSec = $theSub.prev(".item");
                sec = $theSec.attr("sec");
                checkedSiblings = $(":checked",$theSub).length;
                currPack = $theSec.attr("pack");
            } else {
                sec = $(this).attr("sec");
                checkedSiblings = $(".item."+sec).next(".sub").find(" :checked").length;
                currPack = $(this).attr("pack");
            }

            checkedExtras = $(".extrasitem :checked").length;

            //Determine whether to show or hide the <li>
            if (checkedSiblings+checkedExtras>0) {
                $(".caracteristica."+sec).addClass("active").show();
            } else {
                selected[sec] = new Array();
                if (!isExtra) $theSec.attr("pack","");
                $(".caracteristica."+sec).removeClass("active").hide();
            }

            //Determine whether to show or hide the recommentations
            var cars = $(".caracteristica.active").length;
            if (cars <= 1 && activeCaracteristics+cars==1) $(".recomendamos").toggle();
            if (cars == 0) {
                $(".nompaq.mod, .modulo, .nompaq.extras, .prdExt").hide();
            }
            activeCaracteristics = cars;
            if (cars == 0) {
                selected['all'] = new Array();
                currentPckg = "";
                $(".item").each(function() {
                    $(this).attr("pack","");
                });
            }

            //Determine if is Module
            if ($(this).is(".module")) {
                if ($(this).is(":checked")) $("."+sec+".modulo."+$(this).attr("mod")).addClass("active").show();
                else $("."+sec+".modulo."+$(this).attr("mod")).removeClass("active").hide();
                var mods = $(".modulo.active").length;
                if (mods <= 1 && activeModules+mods==1) $(".nompaq.mod").toggle();
                if (mods == 0) $(".nompaq.mod, .modulo").hide();
                activeModules = mods;
            }

            //Determine if is Extra
            if (isExtra) {
                if ($(this).is(":checked")) {
                    $("."+sec+".prdExt."+$(this).attr("ext")).addClass("active").show();
                } else {
                    $("."+sec+".prdExt."+$(this).attr("ext")).removeClass("active").hide();
                }
                var exts = $(".prdExt.active").length;
                if (exts <= 1 && activeExtras+exts==1) $(".nompaq.extras").toggle();
                if (exts == 0) $(".nompaq.extras, .prdExt").hide();
                activeExtras = exts;
            }

            //Package selection
            selected['all'] = new Array();
            $(".item").each(function() {
                var sec = $(this).attr("sec");
                checkedSiblings = $(".item."+sec).next(".sub").find(" :checked").length;
                checkedExtras = $(":checkbox[sec='"+sec+"']:checked").length;
                if (checkedSiblings+checkedExtras>0) {
                    $(".caracteristica."+sec).addClass("active").show();
                } else {
                    $(".caracteristica."+sec).removeClass("active").hide();
                }
                selected[sec]= new Array();
                $(":checked",$(this).next(".sub")).each(function() {
                    var sec = $(this).parents(".sub").prev(".item").attr("sec");
                    selected[sec].push($(this).attr("pack"));
                });
                $(":checkbox[sec='"+sec+"']:checked").each(function() {
                    var sec = $(this).attr("sec");
                    selected[sec].push($(this).attr("pack"));
                });
                for (key in selected) {
                    if (selected.hasOwnProperty(key)) {
                        selected[key].sort();
                        $(".item."+key).attr("pack",selected[key][selected[key].length-1]);
                    }
                }
                var p = $(this).attr("pack");
                if (p!=null && p!="") selected['all'].push(p);
            });
            selected['all'].sort();
            currentPckg = selected['all'].pop();

            $thePack_data = $("data .packages ."+currentPckg);

            $(".item").each(function() {
                var sec = $(this).attr("sec");
                checkedSiblings = $(".item."+sec).next(".sub").find(" :checked").length;
                checkedExtras = $(":checkbox[sec='"+sec+"']:checked").length;
                $(".caracteristica."+sec).find("span").remove();
                if (checkedSiblings+checkedExtras>0) {
                    $(".caracteristica."+sec).append(" <span class='"+currentPckg+"'>"+$("data .sections ."+sec+" ."+currentPckg).html()+"</span>");
                }
            });

            $(".paqname").html($thePack_data.find("name").html());
            $(".pricepaq span.price").html($thePack_data.find("price").html());
        });

        $(".extrasitem").click(function (e) {
            var $c = $(this).find(':checkbox');
            if (!$(e.target).is(":checkbox")) {
                $c.attr("checked",!$c.attr("checked"));
                $c.trigger("selectItem");
            }
        });
   }
});
