//---------------------------------------
// eXpansys.com - Core jQuery functions (6e)
//---------------------------------------

//start jquery
$(document).ready(function(){


//*************************
//Product listing sort drop down 
$('#change_order select').change(onSelectChange);  //select menu change
//end product listing sort drop down 
//*************************


//*************************
//Show coming soon/in stock checkboxes
$("#checkbox_filter input:checkbox").click(onCheckboxChange);
$("#checkbox_filter input").attr('disabled', false); //force checkboxes to enabled onload
//End coming soon/in stock checkboxes    
//*************************

//*************************
//Refine free text search 
  $("#refine_search input.text").hint(); //use textbox hint plugin (func. in core_nnnn.js)
//end Refine free text search 
//*************************



//*************************
//Left nav filter navigation
initMenu(); //run initMenu function
//end Left nav filter navigation
//*************************
 
});

//functions

//auto redirect the user to a new URL on select menu change
function onSelectChange () {
    var sort_action,sort_filter;

    //get all the elements to build the redirect path
    sort_action = $('#change_order').attr("action"); //get the current form action (i.e. /smartphone)
    sort_filter = $(this).parent().find('input[name=filter]').val(); // get the current filter (stored in the hidden field named 'filter')
    
    //if we have a filter, add it to the path
    if (sort_filter != undefined) {
      sort_action += '?filter=' + sort_filter;
    }

    //update submit action 
    $("#change_order").attr("action",sort_action);

    //submit form
    $("#change_order").submit(); 
}


//auto redirect the user to a new URL on select menu change
function onCheckboxChange () {
    
    var submit_action,current_filter;
    current_filter = $("#checkbox_filter input[name=filter]").val();
    
    //store original form action
    submit_action = $('#checkbox_filter').attr("action");
    
    //now check for filter
    if (current_filter != undefined) {
        submit_action += "?filter=" + current_filter;
    }
    
    //update submit action 
    $("#checkbox_filter").attr("action",submit_action);

    //submit form
    $("#checkbox_filter").submit(); 
   
    //submit can take a while, so lock checkboxes and change text + class on current label
    $(this).parent().find("label").text("Loading").addClass("loading"); //set text
    $("#checkbox_filter input").attr('disabled', true); //disable all checkboxes
}



//left nav behavior 
function initMenu() {
  $('#left_col .menu h3').wrapInner("<a></a>"); //wrap h3 contents in a null anchor (<a>) so it's a bit easier to style
  $('#left_col .menu ul.hide').hide(); //hide all menus flagged as hidden (those with <ul class="hide">
  $('#left_col .menu ul.show').parent().prev().addClass('active'); //find any active menus and set their titles to active (so we can show the correct image)
  $('#left_col .menu ul .more ul').hide(); //hide all 'view more' menus
  
  //setup title clicks + menu show/hide
  $('#left_col .menu h3 a').click( // on h3 <a> click
    function() {
      $(this).parent().next().find("> ul").slideToggle('fast'); //slide this menu in/out of view
      $(this).parent().toggleClass('active'); //apply active class to h3 tag
    }
  );  
   
 
  //setup view more clicks + menu show/hide
  $('#left_col .menu ul .more a').click( // on h3 <a> click
    function() {
      $(this).parent().find("ul").slideToggle('fast'); //slide this menu in/out of view
      $(this).parent().toggleClass('active'); //apply active class to h3 tag
    }
  );     
} 


//inline pugins - put frequent used plugins inline to save on https requests
//hint source: http://remysharp.com/2007/01/25/jquery-tutorial-text-box-hints/
jQuery.fn.hint = function (blurClass) {
  if (!blurClass) { 
    blurClass = 'blur';
  }

  return this.each(function () {
    // get jQuery version of 'this'
    var $input = jQuery(this),

    // capture the rest of the variable to allow for reuse
      title = $input.attr('title'),
      $form = jQuery(this.form),
      $win = jQuery(window);

    function remove() {
      if ($input.val() === title && $input.hasClass(blurClass)) {
        $input.val('').removeClass(blurClass);
      }
    }

    // only apply logic if the element has the attribute
    if (title) { 
      // on blur, set value to title attr if text is blank
      $input.blur(function () {
        if (this.value === '') {
          $input.val(title).addClass(blurClass);
        }
      }).focus(remove).blur(); // now change all inputs to title

      // clear the pre-defined text when form is submitted
      $form.submit(remove);
      $win.unload(remove); // handles Firefox's autocomplete
    }
  });
};



