(function($){
    $(document).ready(function() {
        //so the correct value is displayed right after the page loads
        if($("#valve1 option:selected").val() == "metric"){
            $("option#opt_lbs").html('kg');
            $("option#opt_psi").html('kg/cm2');
        }
        $("input:submit, button, a.jqbutton, :reset, #nav > li > a").button();
        //$("#nav li:hover, #nav ul li a:hover, #nav a").addClass("noBg");
        $('#faq_tabs').tabs();
        $("#gen_accordion, #tech_accordion, #accordion1, #accordion2, #accordion3, #accordion4, #accordion5")
        .accordion({
            header: "h3", 
            autoHeight:false
        });

        //$(".customselect select").customStyle();
        $("#valvecalc input[type=text]").focus(function(){
            var v = $(this).val();
            if(v=="Piston Diameter (in)" || v=="Car Speed (FPM)" || v=="Empty Car Weight" || 
                v=="Capacity (lbs)" || v=="Empty Car Pressure (psi)" || v=="Loaded Car Pressure (psi)" || 
                v=="Rated Flow (gpm)" || v=="Piston Diameter (mm)" || v=="Car Speed (m/s)" || 
                v=="Empty Car Weight" || v=="Capacity (kg)" || v=="Empty Car Pressure (kg/cm^2)" || 
                v=="Loaded Car Pressure (kg/cm^2)" || v=="Rated Flow (lpm)"){       
    
                $(this).val(""); 
            }
        });
        $('input.clear_form1').click(function() {
            $("#valve1 option").removeAttr("selected");
            $("#jacktype option").removeAttr("selected");
            $("#NumJack option").removeAttr("selected");
            $("#valve1 :first-child").attr('selected', true);
            $("#jacktype :first-child").attr('selected', true);
            $("#NumJack :first-child").attr('selected', true);
            $("#v1PistD").attr('value',"Piston Diameter (in)");
            $("#v1CarS").attr('value',"Car Speed (FPM)");
            $("#v1CarW").attr('value',"Empty Car Weight");
            $("#v1Capacity").attr('value',"Capacity (lbs)");
            $("input:checked#dwn_spd_1").removeAttr('checked');
            $("select#lbs_psi :last").removeAttr("selected");
            $("option#opt_lbs").html('lbs.');
            $("option#opt_psi").html('PSI');
            $("option#opt_lbs").attr('selected', true);
            return false;//prevents event propagation/ bubbling.
        });
        $('input.clear_form2').click(function() {
            //this option not working
            $("#valve2 option").removeAttr("selected");
            $("#valve2 :first-child").attr('selected', true);
            //these options are working
            $("#v2CarP").attr('value',"Empty Car Pressure (psi)");
            $("#v2LCarP").attr('value',"Loaded Car Pressure (psi)");
            $("#v2RFlow").attr('value',"Rated Flow (gpm)");
            $("input:checked#dwn_spd_2").removeAttr('checked');
            return false;//prevents event propagation/ bubbling.
        });
        //$('form#valvecalc').reset(); //alternative reset calculator code.
        $("#valvecalc input[type=text]").blur(function(){
            var v = $(this).val();
            var n = $(this).attr("id");
            var x = $("#valve1 option:selected").val();
            var y = $("#valve2 option:selected").val();
            //english
            if (n== "v1PistD" && v=="" && x=="inch")   {
                $(this).val("Piston Diameter (in)");
            }
            else if (n== "v1CarS"  && v=="" && x=="inch")   {
                $(this).val("Car Speed (FPM)");
            }
            else if (n== "v1CarW"  && v=="" && x=="inch")   {
                $(this).val("Empty Car Weight");
            }
            else if (n== "v1Capacity" && v=="" && x=="inch")   {
                $(this).val("Capacity (lbs)");
            }
            else if (n== "v2CarP"  && v=="" && y=="inch")   {
                $(this).val("Empty Car Pressure (psi)");
            }
            else if (n== "v2LCarP" && v=="" && y=="inch")   {
                $(this).val("Loaded Car Pressure (psi)");
            }
            else if (n== "v2RFlow" && v=="" && y=="inch")   {
                $(this).val("Rated Flow (gpm)");
            }
            //metric
            else if (n== "v1PistD" && v=="" && x=="metric") {
                $(this).val("Piston Diameter (mm)");
            }
            else if (n== "v1CarS"  && v=="" && x=="metric") {
                $(this).val("Car Speed (m/s)");
            }
            else if (n== "v1CarW"  && v=="" && x=="metric") {
                $(this).val("Empty Car Weight");
            }
            else if (n== "v1Capacity" && v=="" && x=="metric") {
                $(this).val("Capacity (kg)");
            }
            else if (n== "v2CarP"  && v=="" && y=="metric") {
                $(this).val("Empty Car Pressure (kg/cm^2)");
            }
            else if (n== "v2LCarP" && v=="" && y=="metric") {
                $(this).val("Loaded Car Pressure (kg/cm^2)");
            }
            else if (n== "v2RFlow" && v=="" && y=="metric") {
                $(this).val("Rated Flow (lpm)");
            }
        });
        $("#valve1").change(function(){
            //remove PSI when metric is selected
            if($("#valve1 option:selected").val() == "inch"){
                $("option#opt_lbs").html('lbs.');
                $("option#opt_psi").html('PSI');
            }
            if($("#valve1 option:selected").val() == "metric"){
                $("option#opt_lbs").html('kg');
                $("option#opt_psi").html('kg/cm2');
            }
            //set other parameters
            var v = $(this).val();
            if ($("#valve1 option:selected").text()=="English Units"){
                //set values
                if($("#v1PistD").val() == "" || ($("#v1PistD").val() == "Piston Diameter (mm)")){
                    $("#v1PistD").val("Piston Diameter (in)");
                }
                if($("#v1CarS").val() == "" || $("#v1CarS").val() == "Car Speed (m/s)"){
                    $("#v1CarS").val("Car Speed (FPM)");
                }
                if($("#v1CarW").val() == "" || $("#v1CarW").val() == "Empty Car Weight"){
                    $("#v1CarW").val("Empty Car Weight");
                }
                if($("#v1Capacity").val() == "" || $("#v1Capacity").val() == "Capacity (kg)"){
                    $("#v1Capacity").val("Capacity (lbs)");
                }
                //set titles
                $("#v1PistD").attr("title","Piston Diameter (in)");
                $("#v1CarS").attr("title","Car Speed (FPM)");
                $("#v1CarW").attr("title","Empty Car Weight");
                $("#v1Capacity").attr("title","Capacity (lbs)");
                //lbs/psi/kg
                $("option#opt_lbs").html('lbs.').val('lbs.');
            } else {
                //set values
                if($("#v1PistD").val() == "" || $("#v1PistD").val() == "Piston Diameter (in)"){
                    $("#v1PistD").val("Piston Diameter (mm)");
                }
                if($("#v1CarS").val() == "" || $("#v1CarS").val() == "Car Speed (FPM)"){
                    $("#v1CarS").val("Car Speed (m/s)");
                }
                if($("#v1CarW").val() == "" || $("#v1CarW").val() == "Empty Car Weight"){
                    $("#v1CarW").val("Empty Car Weight");
                }
                if($("#v1Capacity").val() == "" || $("#v1Capacity").val() == "Capacity (lbs)"){
                    $("#v1Capacity").val("Capacity (kg)");
                }
                //set titles
                $("#v1PistD").attr("title","Piston Diameter (mm)");
                $("#v1CarS").attr("title","Car Speed (m/s)");
                $("#v1CarW").attr("title","Empty Car Weight");
                $("#v1Capacity").attr("title","Capacity (kg)");
                //lbs/psi/kg
                $("option#opt_lbs").html('kg').val('kg');
            }
        });
        $("#valve2").change(function(){
            var v = $(this).val();
            if ($("#valve2 option:selected").text()=="English Units"){
                //set values
                if($("#v2CarP").val() == "" || $("#v2CarP").val() == "Empty Car Pressure (kg/cm^2)"){
                    $("#v2CarP").val("Empty Car Pressure (psi)");
                }
                if($("#v2LCarP").val() == "" || $("#v2LCarP").val() == "Loaded Car Pressure (kg/cm^2)"){
                    $("#v2LCarP").val("Loaded Car Pressure (psi)");
                }
                if($("#v2RFlow").val() == "" || $("#v2RFlow").val() == "Rated Flow (lpm)"){
                    $("#v2RFlow").val("Rated Flow (gpm)");
                }
                //set titles
                $("#v2CarP").attr("title","Empty Car Pressure (psi)");
                $("#v2LCarP").attr("title","Loaded Car Pressure (psi)");
                $("#v2RFlow").attr("title","Rated Flow (gpm)");
            } else {
                //set values
                if($("#v2CarP").val() == "" || $("#v2CarP").val() == "Empty Car Pressure (psi)"){
                    $("#v2CarP").val("Empty Car Pressure (kg/cm^2)");
                }
                if($("#v2LCarP").val() == "" || $("#v2LCarP").val() == "Loaded Car Pressure (psi)"){
                    $("#v2LCarP").val("Loaded Car Pressure (kg/cm^2)");
                }
                if($("#v2RFlow").val() == "" || $("#v2RFlow").val() == "Rated Flow (gpm)"){
                    $("#v2RFlow").val("Rated Flow (lpm)");
                }
                //set titles
                $("#v2CarP").attr("title","Empty Car Pressure (kg/cm^2)");
                $("#v2LCarP").attr("title","Loaded Car Pressure (kg/cm^2)");
                $("#v2RFlow").attr("title","Rated Flow (lpm)");
            }
        });
        if ($("#slides").length) {
            //clear out all the a's in the slideshow list and make it into an array.
            images = new Array();
            curimg = 0;
            slidetime = "";
            sliding = true;
            showWidth = $("#slides").width(); //showwidth will set the left position for sliding images
            $("#slides li").each(function(i){
                images[i] = new Array();
                images[i]["img"] = $(this).find("a").attr("href");
                images[i]["title"] = $(this).find("a").html();
                $(this).remove();
            });
            //then start us off by just appending the first slide (no animation) or hiding the slides div if no images were found
            if (images.length >= 1) {
                $("#slides").css({
                    "background":"white"
                });
                var newimg = "<img src='" + images[curimg]["img"] + "' />";
                $(newimg).load(function(){
                    var slide = "<li>";
                    slide += newimg;
                    if (images[curimg]["title"] != null && images[curimg]["title"] != "") {
                        slide += "<h1>" + images[curimg]["title"] + "</h1>";
                    }
                    slide += "</li>";
                    $(slide).appendTo("#slides").show().css({
                        left:0
                    });
                    sliding = false;
                    //if there is more than one picture, start the slideshow and append the slider controls
                    if (images.length > 1) {
                        slidetime = setTimeout("slideIt()",7000);
                        $("#slide_prev,#slide_next").show();
                    }
                });
            }
        }
        $("#slide_next").click(function(e){
            e.preventDefault();
            if (sliding == false) {
                clearTimeout(slidetime);
                slideIt();
            }
        });
        $("#slide_prev").click(function(e){
            e.preventDefault();
            if (sliding == false) {
                clearTimeout(slidetime);
                slideIt("prev");
            }
        });
    });
    $.fn.extend({
 
        customStyle : function(options) { 
        //            if(!$.browser.msie || ($.browser.msie&&$.browser.version>6)){
        //                return this.each(function() {
        //	  
        //                    var currentSelected = $(this).find(':selected');
        //                    $(this).after('<span class="customStyleSelectBox"><span class="customStyleSelectBoxInner">'+currentSelected.text()+'</span></span>').css({
        //                        position:'absolute', 
        //                        opacity:0,
        //                        fontSize:$(this).next().css('font-size')
        //                    });
        //                    var selectBoxSpan = $(this).next();
        //                    var selectBoxWidth = parseInt($(this).width()) - parseInt(selectBoxSpan.css('padding-left')) -parseInt(selectBoxSpan.css('padding-right'));			
        //                    var selectBoxSpanInner = selectBoxSpan.find(':first-child');
        //                    selectBoxSpan.css({
        //                        display:'inline-block'
        //                    });
        //                    selectBoxSpanInner.css({
        //                        width:selectBoxWidth, 
        //                        display:'inline-block'
        //                    });
        //                    var selectBoxHeight = parseInt(selectBoxSpan.height()) + parseInt(selectBoxSpan.css('padding-top')) + parseInt(selectBoxSpan.css('padding-bottom'));
        //                    $(this).height(selectBoxHeight).change(function(){
        //                        // selectBoxSpanInner.text($(this).val()).parent().addClass('changed');   This was not ideal
        //                        selectBoxSpanInner.text($(this).find(':selected').text()).parent().addClass('changed');
        //                    // Thanks to Juarez Filho & PaddyMurphy
        //                    });
        //			
        //                });
        //            }
        }
    });
})(jQuery);

function slideIt(d) {
    //sliding var prevents mutliple clicks screwing things up.
    sliding = true;
	
    //set up some vars depending on the direction
    if (d == "prev") {
        curimg--;
        startL = -1 * showWidth;
        endL = showWidth;
    } else {
        curimg++;
        startL = showWidth;
        endL = -1 * showWidth;
    }
	
    //make sure we don't go outside our range of images.
    if (curimg >= images.length) {
        curimg = 0;
    } else if (curimg < 0) {
        curimg = images.length - 1;
    }
	
    var newimg = "<img src='" + images[curimg]["img"] + "' />";
    jQuery(newimg).load(function(){
        var slide = "<li>";
        slide += newimg;
        if (images[curimg]["title"] != null && images[curimg]["title"] != "") {
            slide += "<h1>" + images[curimg]["title"] + "</h1>";
        }
        slide += "</li>";
        //slide the images
        jQuery("#slides li").addClass("lastimg").animate({
            left:endL
        },1000);
		
        jQuery(slide).appendTo("#slides").show().css({
            left:startL
        }).animate({
            left:'0px'
        },1000,function(){
            jQuery(".lastimg").remove();
            slidetime = setTimeout("slideIt()",7000);
            sliding = false;
        });
    });
}
