r = "/";

function nav_on()
{
   document.getElementById("nav").style.zIndex = 10;
   document.getElementById("cont").style.zIndex = 1;
}

function nav_off()
{
   document.getElementById("nav").style.zIndex = 1;
   document.getElementById("cont").style.zIndex = 10;
}

function init_forms()
{
   $(document).ready(function(){
      // Add styling to INPUT TEXT except RECAPTCHA
      $("input[type=text]").not("#recaptcha_response_field").after("<div class=cap></div>");
      $("input[type=text]").addClass("txt");
      $("input[type=text]").focus(function(){
         if (($(this).val() == "ZIP") || ($(this).val() == "Postal")) $(this).val("");
      })

      // Add styling to TEXTAREA
      $("textarea").after("<div class=cap_textarea></div>");
      $("textarea").addClass("txt");

      // Add styling to SELECT
      $("select").each(function(){
         var u = navigator.userAgent.toLowerCase();
         // Create dummy image to maintain SELECT width with an addition of 2px. If FF Mac, add 29px.
         if (u.indexOf('mac') != -1 && u.indexOf('firefox')!=-1)
         {
            $(this).parent().prepend("<div><img src="+r+"images/pixel.gif width="+($(this).width()+29)+" height=20></div>");
         }
         else
         {
            $(this).parent().prepend("<div><img src="+r+"images/pixel.gif width="+($(this).width()+2)+" height=20></div>");
         }

         $(this).parent().prepend("<div class=select_replace></div>");
         $(this).parent().find(".select_replace").prepend("<div class=cur>"+$(this).find(":selected").text()+"</div>");

         // If not FF Mac, reduce the stylesheet width by 27px
         if (u.indexOf('mac') != -1 && u.indexOf('firefox')!=-1)
         {
            $(this).parent().find(".select_replace .cur").css("width",$(this).width());
         }
         else
         {
            $(this).parent().find(".select_replace .cur").css("width",$(this).width()-27);
         }

         $(this).parent().find(".select_replace").append("<div class=cap_select></div>");
         $(this).parent().find(".select_replace").append("<ul></ul>");

         var list = $(this).find("option").map(function(){
            var rep = $("<li>").text($(this).text()).get();
            return rep;
         })

         $(this).parent().find("ul").append(list);
//         $(this).toggle();
      })

      // Expand drop downs on click and contract others
      $(".select_replace").click(function(){
         $(this).find("ul").toggle();
         $(".select_replace ul").not($(this).find("ul")).hide();
         $(".select_replace").css("zIndex","100");
         $(this).css("zIndex","101");
      })

      // Display selected item and update corresponding option
      $(".select_replace ul li").click(function(){
         $(this).parent().parent().find(".cur").text($(this).text());
         $(this).parent().parent().parent().find("select option[text="+$(this).text()+"]").attr("selected","selected");

         // Check if SELECT is a jump
         if ($(this).parent().parent().parent().find("select").hasClass("jump"))
         {
            self.location = $(this).parent().parent().parent().find("select option:selected").val();
         }
      })

      // Jump to selected item for browsers that do not display the replacement SELECT
      $("select").find("jump").change(function(){
         self.location = $(this).find("option:selected").val();
      })

      // Contract pull down when click outside
      $("body").click(function(){
         $(".select_replace ul").hide();
      })

      $(".select_replace").click(function(e){
          e.stopPropagation();
      })
   })
}

function init_matrix()
{
   $.ajaxSetup
   ({  
      cache: false  
   }); 

   $(document).ready(function(){
      // Uncheck all checkboxes to avoid Back button problem
      $("input[type=checkbox]").attr("checked",false);
      $("input[type=checkbox]").click(function(){
         var answers = [];
         var models = [];

         $("#ldr").show();
         $("div[id ^= 'c_']").fadeTo(0.5,0.05);
         $("input[type=checkbox]:checked").each(function(){
            answers.push($(this).val());
         })

         $.get("/x_get-matrix.php", {answers: answers.join(",")}, function(data){
            $("#ldr").hide();
            $("div[id ^= 'c_']").fadeTo(0.5,0.2);
            models = data.split(",");

            for (i=0; i<models.length; i++)
            {
               $("#c_"+models[i]).fadeTo(0.5,1);
            }
         })
      })
   })
}

function init_year_select()
{
   $(document).ready(function(){
      $(".year_select").click(function(){
         $(".year_select .menu").toggle();
      })

      // Contract pull down when click outside
      $("body").click(function(){
         $(".year_select .menu").hide();
      })

      $(".year_select").click(function(e){
          e.stopPropagation();
      })
   })
}


function init_overlay()
{
	$(function(){ 
	   $("a[rel]").overlay(
	   {
	      expose: { color: '#ffffff', opacity: 0.7 },
	      onLoad: function() { 
	         this.getContent().find("div.w").load(this.getTrigger().attr("href")); 
	      }
	   })
	})
}

// MISC FUNCTIONS
function goto(val)
{
   if (val != "") self.location = val;
}

function toggle_answer(id)
{
   document.getElementById(id).style.display = (document.getElementById(id).style.display == '') ? "none" : "";
}