var ls_url = '/index.php/page/ajaxfinder'; var ls_forselection = ''; /*the order of the hidden fields containing the values we post back to server*/ var la_fieldsortorder = new Array('sectionid','agerange','pricerange','brandrange','categoryrange'); var la_searchboxorder = new Array('For','age','price','brand','category'); var la_instructionlabels = new Array('First choose either boy or girl' ,'Now choose an age range or click "show all" to view toys matching your criteria so far' ,'Now choose a price range or click "show all" to view toys matching your criteria so far' ,'Now choose a brand or click "show all" to view toys matching your criteria so far' ); //handles top level section selection $(function() { $("#For a").bind('click',function() { if (typeof ev != 'undefined') { window.clearInterval(ev); //stop the promo which causes the page to "jump" in some browsers } ls_forselection = $(this).html(); sectionid = $(this).attr('href').match(/[0-9]+/); $("#sectionid").val(sectionid); $('.summary:eq(0)').html(ls_forselection); len = la_fieldsortorder.length; for (i = 1; i < len ;i++) { /*if its been searched before, clear the previous search value in hidden field and also the summary*/ if ($('#' + la_fieldsortorder[i]).val()) { $('#' + la_fieldsortorder[i]).val(''); $('.summary:eq(' + (i) + ')').html(''); } /*if the box has html in it, clear it out, its from a previous search*/ if ($('#' + la_searchboxorder[i]).html()) { $('#' + la_searchboxorder[i]).html(''); } } $.post(ls_url,$("#fields :input").serialize(),populateBox,'json'); } ); }); //creates links and populates the appropriate box function populateBox(oJson) { if (oJson.data == null || !oJson.data) { return; } var boxToPopulate = oJson.what; //"what" refers to the id of the search container we're populating $('#' + boxToPopulate).html(''); switch (boxToPopulate) { case 'age' : case 'price' : var i = 0; jQuery.each(oJson.data, function(label, val) { //create a new link for each result that was returned i+=1 ; if (val > 0) { //check if search results were returned $('#' + boxToPopulate).append('
Thats it! Now choose one of the links above to view your perfect toys
') .appendTo($('#' + boxToPopulate)); break; } nextPos = jQuery.inArray(boxToPopulate,la_searchboxorder) + 1; $('#' + la_searchboxorder[nextPos] + '.content').html(la_instructionlabels[nextPos-1]); } /*creates the 'show all' link that appears at the bottom of each box*/ function createSubmitLink(div) { $('Show All') .appendTo($('#' + div)) .wrap('') .bind('click',postForm); } /*does the ajax post for the each result link*/ function process() { var what = $(this).attr('class'); /*need to clear any form values for steps that appear after this one e.g user searches first 4 steps, researches step 2 - only the value of step 1 should be posted*/ searchPosition = jQuery.inArray(what,la_fieldsortorder) + 1; $('.summary:eq(' + (searchPosition -1) + ')').html(' ' + $(this).html()); len = la_fieldsortorder.length; for (i = searchPosition; i < len ;i++) { /*if its been searched before, clear the previous search value in hidden field and also the summary*/ if ($('#' + la_fieldsortorder[i]).val()) { $('#' + la_fieldsortorder[i]).val(''); $('.summary:eq(' + (i) + ')').html(''); } /*if the box has html in it, clear it out, its from a previous search*/ if ($('#' + la_searchboxorder[i]).html()) { $('#' + la_searchboxorder[i]).html(''); } } $('#' + what).val($(this).attr('href').match(/[0-9]+/)); //populate the hidden field $.post(ls_url,$("#fields :input").serialize(),populateBox,'json'); } /*callback for submit click*/ function postForm() { var what = $(this).attr('class'); $('#' + what).val($(this).attr('href').match(/[0-9]+/)); //populate the hidden field $("#fields").submit(); } $("#loading").ajaxStart(function(){ $(this).show(); }); $("#loading").ajaxStop(function(){ $(this).hide(); });