/* --------------------------------------------------------------------------------------

FROM THIS:
<select class="customselect">
  <option value="">Search Engine</option>
  <option value="http://google.com">google</option>
  <option value="http://yahoo.com">yahoo</option>
  <option value="http://www.ask.com">ask</option>
  <option value="http://www.bing.com">bing</option>
  <option value="http://www.dogpile.com">dogpile</option>
</select>

TO THIS:
<div class="fauxselect">
  <div class="fauxselectcon">
    <div class="fslabel" id="fslabel1"><span>Search Engine</span></div>
    <div class="fsbtn"><a href="#" class="fscon" id="fsbtn1">&#8226;</a></span></div>
  </div>
  <ul class="selectlist" id="selectlist1">
    <li><a href="#" value="">Search Engine</a></li>
    <li><a href="#" value="http://google.com">google</a></li>
    <li><a href="#" value="http://yahoo.com">yahoo</a></li>
    <li><a href="#" value="http://www.ask.com">ask</a></li>
    <li><a href="#" value="http://www.bing.com">bing</a></li>
    <li><a href="#" value="http://www.dogpile.com">dogpile</a></li>
  </ul>
  <input type="hidden" value="" id="customselect1" name="customselect1" />
</div>

----------------------------------------------------------------------------------------*/

$(document).ready(function(){

  /*
    PREREQUISITES:
                  each select element desired to be replaced should have a class of 'customselect'
                  and an id of 'customselectN' where N represents a unique numeric identifier
                  
                  
    TODO:
         have to set the hidden field id to the id of the select object id

  */
  
  /* set to 'true' if you want the demo alerts to fire 
  ----------------------------------------------------- */
  var demo = false;



  // unique id support
  var uid = 1;



  // collect the marked select elements
  var selects = $(".customselect");

  // call the constructor that replaces the select with a div structure
  if(selects) {
    for(var i=0; i < selects.length; i++) {
      var thischildset = selects[i];
      constructFauxSelect(thischildset);
    }
  }
  
  function constructFauxSelect(objin) {  //alert(objin.id);

    if(demo) {
      alert('Constructing the new drop down structure');
    }

    // var thisNum = objin.id.slice(12);           // grab the id's numerical value - is based on the pattern: customselect#  *******************
    
    //var thisHeight = objin.length*liHeight;     // height of the ul - ONLY REQUIRED IF WE RUN INTO A BROWSER COMPAT PROB

    var objinID = "#"+objin.id;                 // grab the object's id to manipulate later
    var newDiv = document.createElement("div"); // create the faux select's parent container
    var newDivID = "faux"+objin.id;             // create an id for the parent based on the orig object's id
    newDiv.id = newDivID;                       // give the newly created div that id
    var jqID = "#"+newDivID;                    // create a temp JQ version

    if(demo) {
      alert('Now going to remove the Form Select drop down and build the interior of the new drop down structure');
    }
    $(objinID).after(newDiv).remove();          // we have the object here, so remove it from the DOM

    // add the interior structure
    $(jqID).addClass('fauxselect').html('<div class="fauxselectcon"><div class="fslabel" id="fslabel'+uid+'"><span></span></div><div class="fsbtn"><a href="#" class="fscon" id="fsbtn'+uid+'">&#8226;</a></span></div></div>');

    // append the list structure
    $(jqID).append('<ul class="selectlist" id="selectlist'+uid+'"></ul>');

    // add and populate all of the list elements
    for(var i=0; i<objin.length; i++) {
      //alert(objin[i].nodeName);
      
      var thisValue = $(objin[i]).attr('value');        //alert(thisValue);
      var thisLabel = $(objin[i]).html();               //alert(thisLabel);
      
      if(i==0) {
        var staticLabelID = "#fslabel"+uid+" span";
        $(staticLabelID).html(thisLabel);
        
        var listID = "#selectlist"+uid;
        $(listID).append('<li><a href="#" value="'+thisValue+'">'+thisLabel+'</a></li>');

      } else {
      
        var listID = "#selectlist"+uid;
        $(listID).append('<li><a href="#" value="'+thisValue+'">'+thisLabel+'</a></li>');


      }

    }
    
    if(demo) {
      alert('Now appending the hidden input field. This is given the same name/id as the select field just removed');
    }
    // append the hidden input field
    $(jqID).append('<input type="hidden" value="" id="'+objin.id+'" name="'+objin.id+'" />');  //alert(objin.id);
    
    uid++;  //alert(uid);

  }


  
  // the select arrow functionality
  $(".fscon").click(function () {
  
    var allLists = $('.selectlist');

    var thisID = this.id;                   //alert(thisID);
    var thisNum = thisID.slice(5);          //alert(thisNum);
    
    for(var i=0; i<allLists.length; i++) {

      var listNum = allLists[i].id.slice(10);

      if(listNum != thisNum ){

        var thisParentEls = $(allLists[i]).parents();
        var thisGrandParent = thisParentEls[0];
        $(thisGrandParent).css({'position':'static'});
        $(allLists[i]).hide();

      }

    }

    var thisList = "#selectlist"+thisNum;
    var parentEls = $(this).parents();
    var grandParent = parentEls[2];
    $(grandParent).css({'position':'relative'});

    $(thisList).slideToggle("fast", function () {
      if($(thisList).css('display') == 'none') {
        $(grandParent).css({'position':'static'});
      }
    });
    
    $(this).blur(function () {
         $(thisList).fadeOut(500);
    });


    return false;
  });
  

  // what happens when an option is selected
  $(".selectlist li a").click(function () {

    var thisHTML = $(this).html();                            //alert(thisHTML);
  
    var thisList = $(this).parents("ul").attr('id');          //alert(thisList);
    var thisNum = thisList.slice(10);                         //alert(thisNum);

    //var thisHidden = "#customselect"+thisNum;  // **********************************************************************************
    
    var thisParentEls = $(this).parents();
    var thisGrandParentID = thisParentEls[2].id;  //alert(thisGrandParentID);
    
    var thisHidden = "#"+thisGrandParentID.replace('faux','');

    //var thisGPsibling = thisGrandParent.next();
    //alert('hi');
    //var thisGrandParent = $(this).parent();
    
    //$(thisGPsibling).css({'border':'1px solid red'});

    var thisListHTML = "#fslabel"+thisNum+" span";
    $(thisListHTML).html(thisHTML);

    var thisValue = $(this).attr('value');
    
    if(hiddenval != "") {
      $(thisHidden).attr('value',thisValue);                //alert($(thisHidden).attr('value'));
    }

    $(".selectlist").hide();

    // demo
    var hiddenval = $(thisHidden).attr('value');
    //alert("The hidden input value associated with this drop down is "+hiddenval+".\nBut for now we send you off to "+hiddenval);
    
    $("#fauxselectmessage").html("The hidden input value associated with the item just selected is "+hiddenval);

    if(hiddenval != "") {
      //window.location.href=hiddenval;
    }

    return false;
  });



  $(window).load(function(){

  }); // (window).load
  
  
  $(window).unload(function(){

  }); // (window).unload




});

