$(function(){

    // placeholder input type code - handles text changing for in-text input names
    $(".placeholder").each(function(){$(this).attr("tag",$(this).val()).css("color","#666");  // store the origional value in teh tag
    }).focus(function(){if($(this).val() == $(this).attr('tag')) $(this).val("").css("color","black"); // clear default value on focus +color black
    }).blur(function(){if($(this).val() == "") $(this).val($(this).attr("tag")).css("color","#666");}); // if left empty, replace with default value +color grey

    $("div.trifold").click(function(){
        window.location = $(this).attr("href");
    });
    // Contact Us "Find Rep" functionality
    $(".findrep").each(function(){
        var findrep = $(this).get(0);
        $(".section").hide();
        $(".region select",findrep).change(function(){
            $(".section,.rep",findrep).hide(); // hide the section select element +hide rep elements
            var id = $(this).val(); // get id
            if(id == "") return "";
            var doAction = $("input[name=regionaction]",findrep).val();
            $.get("AJAX.php",{"do":doAction,"id":id},function(data){ // get section data xml
                var selector = $(".section select", findrep).get(0); // save the select element
                $("option",selector).remove();
                $("<option>").attr("value","").html(" -- ").appendTo(selector);
                $("section",data).each(function(){ // cycle through each section
                    var id = $("id",this).text(); // pull section data
                    var name = $("name",this).text();
                    $("<option>").attr("value",id).html(name).appendTo(selector); // creat and append option element
                });
                $(".section",findrep).show(); // she the select element
            });
        });
        $(".section select",findrep).change(function(){
            $(".rep.even").removeClass("even"); // clearsout even class so that only even elements will have even class
            var id = $(this).val(); // get id
            var doAction = $("input[name=sectionaction]",findrep).val();
            $.ajax({url:"AJAX.php",data:{"do":doAction,"id":id},success:function(data){ // conjure instructor xml
                $(".rep.active",findrep).remove(); // remove all currently active rep elements
                var repTemplate = $(".reptemplate .rep").get(0); // save the template rep element
                $("instructor",data).each(function(){ // cycle through instructors
                    var thisRep = $(repTemplate).clone().addClass('active'); // copy the rep template
                    var firstName           = $(this).find("firstName").text(); // pull all data from xml
                    var lastName            = $(this).find("lastName").text();
                    var position            = $(this).find("title").text();
                    var certification       = $(this).find("isCert").text();
                    var address             = $(this).find("city").text();
                    var organization        = $(this).find("assoc1").text();
                    var phone               = $(this).find("phone").text();
                    var email               = $(this).find("email").text();
                    var website             = $(this).find("url").text();

                    var certNum = certification;
                    if(certification == 1) certification = "K-VEST TPI 3D Certified (1)"; // extra minor data processing/preperation
                    if(certification == 2) certification = "K-VEST TPI 3D Certified (2)";
                    if(certification == 0) certification = "";
                    var mailto = "mailto:"+email;

                    $(thisRep)
                        .find(".name")            .html(firstName + " " + lastName)   .end() // bind pulled data to respective html fields by class
                        .find(".position")        .html(position)                     .end()
                        .find(".certification")   .html(certification)                .end()
                        .find(".address")         .html(address)                      .end()
                        .find(".organization")    .html(organization)                 .end()
                        .find(".phone")           .html(phone)                        .end()
                        .find(".website a ")      .html(website).attr("href",website) .end()
                        .find(".email a")         .html(email).attr("href",mailto)    .end();
                    // cert image code
                    var thisCert = $(".certification",thisRep).get(0);
                    if(certNum == 1) $("<img>").attr({"src":"images/cert1.png","align":"right"}).css({
                        width:"120px",height:"43px"
                    }).prependTo(thisCert);
                    if(certNum == 2) $("<img>").attr({"src":"images/cert2.png","align":"right"}).css({
                        width:"120px",height:"43px"
                    }).prependTo(thisCert);

                    $(thisRep).appendTo(".findrep");// add to main rep element
                });
                $("error",data).each(function(){
                    $("<div>").text($(this).text()).appendTo(".findrep");
                });
                $(".rep:even").addClass("even");// aid css style: add even class to even elements
                $(".rep.active",findrep).show();
            },error:function(xhr,status,error){
                $(".findrep>.rep").hide();
                var smallerror = $("<small>").text("("+error+")").get(0);
                var errormessage = "There has been an error in transmission.  Please try another location.";
                var errorDiv = $("<div>").addClass("error").text(errormessage).append(smallerror).get(0);
                $(".findrep").append(errorDiv);
                $(errorDiv).delay(5000).slideUp(1000);
            }});
        });
        $(".region select",findrep).val("");
    });
    // end Contact Us "Find Rep" functionality
    $(".mid_section").each(function(){
        var section = this;
        $("input[type=text]").keypress(function(e){
            var code = (e.keyCode ? e.keyCode : e.which);
            if(code == 13){$(this).trigger("submit");}
        }).bind("submit",function(){
            $.get("AJAX.php",{"do":"enewsletter_signup","email":$(this).val()});
            $(section).html("<p>Thank you for your Interest!</p>");
        });

        $("input[type=image]").click(function(){
            $(this).parent().find("input[type=text]").trigger("submit");
        });
    });

    // FAQS Functionality
    $("div.faqs ul li a").click(function(){
        $("a[name="+(/\#(.+)/.exec($(this).attr("href"))[1])+"]").closest("div").toggle("slow");
        return false;
    });
    $("div.faqs>div").hide();
    
    // sensor graphics popup box
    $(".example8").click(function(){
    	var element = $("#inline_example1").clone().get(0);
    	var container = $("<div>").appendTo("body").corner("round 10px").css({
    		padding:"5px",margin:"5px",backgroundColor:"white",position:"absolute",
    		top:"100px",left:"50%",width:"500px",marginLeft:"-270px",zIndex:"9001"
    	}).append(element);
    	var shader = $("<div>").appendTo("body").css({
    		opacity: 0.5,width:"100%",height:"1000px",backgroundColor:"black",
    		position:"absolute",top:"0px",left:"0px;",zIndex:"100"
    	}).click(function(){
    		$(shader).remove();
    		$(container).remove();
    	});
    	var close = $("<a>").attr({href:"#"}).html("Close").css({
    		display:"block",aFloat:"right",textDecoration:"none",textAlign:"right",
    		fontSize:"14px"
    	}).click(function(){
    		$(shader).trigger("click");
    	}).appendTo(container);
    });

    
});











