var expanding_menu_ids = new Array("used_equipment","engines","used_parts","tires","new_parts");

function close_all(except) {
     var debug = 0;
     if (except) {
          debug && console.log("Closing everything but ("+except+")");
     }
    for (i=0; i<expanding_menu_ids.length; i++) {
          var current = expanding_menu_ids[i];
          var menu = "#"+current;
          debug && console.log("close_all is trying to close ("+current+")");
          if (current == except) {
               debug && console.log("Keeping (" + except  + "). Making sure it's open...");
               $(menu).slideDown();
          }
          else {
               debug && console.log("close_all is closing this menu " + menu);
               $(menu).slideUp();
          }
    }
}

function open_all() {
    for (i=0; i<expanding_menu_ids.length; i++) {
          var menu = "#"+expanding_menu_ids[i];
          // console.log("Opening this " + menu);
          $(menu).slideDown();
    }
}



function toggle_accordian_style(menu) {
     toggleAccordianStyle(menu);
}
function toggleAccordianStyle(theid){
     var aid = ("div.csmexpandingmenu#" + theid);
     // $(aid).slideToggle();
     // console.log("Searching  (" + aid + ")");
     $(aid).show();
     $(aid).find("a").each( function() {
          // console.log("accordion found " + this + " with visibility " + $(this).css("display"));
          $(this).slideToggle();
          // console.log("after toggling, it had visibility " + $(this).css("display"));
     });
}

function reveal_gallery(description) {
     console.log("I'm going to show you the gallery named " + description);
     // put the images in the list
     // now what?
     var url = '/cgi-bin/csm-gallery2-cgi.pl?base-album='+description+'&function=fetch-images&format=div'; 
          // stick all of the images in the 'gallery_response' box
     if (!description) {
          console.log('since description is empty, im not getting images.\n');
     }
     else {
          console.log('getting images for the description ' + description);
          get_image_list(url);
     }
     //$(".thickboxcontainer").html("<a href=\"#TB_inline?height=300&amp;width=300&amp;inlineId=myOnPageContent\" class='thickbox'></a>");
     $("#thickboxactivator").click();
}


function openAccordianStyle(theid){
     var aid = ("div.csmexpandingmenu#" + theid);
     $(aid).slideDown();
     // console.log("Searching  (" + aid + ")");
     $(aid).show();
     $(aid).find("a").each( function() {
          console.log("accordion found " + this);
          $(this).slideToggle();
          // $(this).css("display","inline");
     });
}

function toggleSlider(theid){
     var aid = ("div.csmexpandingmenu#" + theid);
     console.log("Slider is toggling  (" + aid + ")");
     $(aid).slideToggle();
}

function update_gallery_selection(myspan) {
     // console.log("myspan is " + myspan);
     console.log("You clicked on ("+myspan.innerHTML+")");
     $("#gallery_description").attr("value",myspan.innerHTML);
     $("#gallery_id").attr("value",$(myspan).attr('gallery_id'));
     $("#gallery_ref").attr("value",$(myspan).attr('gallery_ref'));
}

function set_up_detail_gallery(thisid) {
     var gallery = thisid;
     if (gallery) {
          get_image_list("/cgi-bin/csm-gallery2-cgi.pl?base-album="+thisid+"&function=fetch-images&format=div");
     }
     else {
          console.log("No gallery so not doing anything.");
     }
     // alert("Binding gallview");
     // $("#gallery ul").jqGalViewII();
     // alert("Done binding galview");
     console.log("gvIIContainer is " + $(".gvIIContainer"));
     set_height(550);
     // alert("Done setting up detail gallery");
}

function set_height(additional) {
     var height_mainbody = $("#mainbody").height();
     var height_maincontainer = $("#maincontainer").height();
     // console.log("Additional is ("+ additional + ") and height_mainbody is " + height_mainbody + " and height_maincontainer is " + height_maincontainer);
     var height_new;
     if (height_mainbody > height_maincontainer) {
          height_new = height_mainbody;
     }
     else {
          height_new = height_maincontainer;
     }
     if (height_new < 500) {
          height_new = 500;
     }
     else {
          height_new += 20;
     }
     if (additional) {
          height_new += additional;
     }
     // console.log("height_new is ("+height_new+")\n");
     $("#maincontainer").css("height",height_new);
     // console.log("After setting, the height of the maincontainer is " +  $("#maincontainer").height());
}


function display_add() {
}

function add_price() {
     // console.log("Adding price (multiple)\n");
          // get the prices and push them onto an array
     var commas = $("#idslist").text();
     // console.log("commas are ("+commas+")");
     var ids = commas.split(",",100);
     var url = "/cgi-bin/admin/featured-divs2.pl?function=get_price&format=multiple&ids="+commas;
     // console.log("add_price: url is " + url);
     $.ajax({
          url: url,
          async: false,
          success: function(html) {
               // console.log("Result is ("+html+")");
               $("#ajax_response").html(html);
               var something = $("#ajax_response").html();
               $("#ajax_response .secret-price").each(function() {
                    var id = $("p.record",this).text();
                    var price = $("div.price",this).html();
                    // console.log("id is " + id + " and price is " + price);
                    var total = "<div class='secret_price'><p>" + price + "</p></div>";
                    $("#tile-list-"+id).append(total);
               });
          }
     });
}


function add_price_single() {
     console.log("Adding price.\n");
     
     $("#mainbody div.editinfo").each( function() {
          var id = $("p.featured_id",this).text();
          console.log("class is " + $(this).attr("class") + " and id is " + id);
          if (!id) { 
               console.log("oct.js: No id. Moving to the next one.\n");
          }
          else {
               if ($.cookie('csmadvanced')) {
                    $.ajax( {
                         url: "/cgi-bin/admin/featured-divs2.pl?function=get_price&featured_id="+id ,
                         async: false,
                         success: function(html) {
                              // console.log("this was the response" + html);
                              $("#ajax_response").html(html);
                              var something = $("#ajax_response").html();
                              $("#tile-list-"+id).each ( function () {
                                   var total = "<div class='secret_price'><p>" + something + "</p></div>";
                                   // console.log("Found the destination... appending this: " + total);
                                   $(this).append(total);
                                   });
                         }
                    });
               }
               else {
                    //console.log("No cookie.");
               }
          }
     });
     // console.log("Done adding price.\n");
}

function add_tire_stuff() {
     // console.log("Adding price.\n");
     $("#mainbody div.tire").each( function() {
          var id = $(this).attr('featured_id');
          // console.log("class is " + $(this).attr("class") + " and id is " + $(this).attr("id"));
          if (!id) { // console.log("oct.js: No id. Moving to the next one.\n");
          }
          else {
               if ($.cookie('csmadvanced')) {
                    var job_number = $("div.job_number",this).css("display","inline");
                    var row_number = $("div.row_number",this).css("display","inline");
                    // console.log("The row is (" + row_number + ") and the job is ("+ job_number +")");
                    // var total = "<br>Row: " + row_number + "<br>Job: "+job_number;
                    // $("td.price",this).append(total);
               }
               else {
                    //console.log("No cookie.");
               }
          }
     });
     // console.log("Done adding price.\n");
}

function add_new_tire_size(size) {
     console.log("new size is " + size);
     var html = "<option value='"+size+"'>"+size+"</option>";
     console.log("new html is " + html);
     $('#tire-size-selector').append(html);

}


function markup_boxes() {
     // console.log("This is markup_boxes");
     $(".menucontainer a, .submenucontainer a").each(function() {
          var link = $(this).attr("href");
          // console.log("Hello world. link is ("+link+")");
          $(this).siblings().each( function () {
               // console.log("Sibling is " + this + " with class " + $(this).attr("class"));
               $(this).wrap("<a href='"+link+"'></a>");
          });
     });
     // console.log("Done with markup_boxes");
}


