$(function() {
    
preloadImg('imgages/ajaxLoader.gif');

$('#jCarousel').jcarousel({
    	wrap: 'circular'
});
        
$('#testimonials').jcarousel({
	wrap: 'circular',
	auto: 8,
	initCallback: mycarousel_initCallback,
	animation: 1000
});

$( ".openDialogTrigger" ).click(function() {
	$( "#contact" ).dialog( "open" );
    return false;
});

$( "#contact" ).dialog({
    	autoOpen: false,
    	width: 550,
    	modal: true,
        hide: {effect: "fade", duration: 1000},
        show: {effect: "fade", duration: 1000},
        create: addElements,
        close: resetIt,
    	buttons: {
    		"Send": function() {
    		  $("#contactForm").submit();
                return false;
    		},
    		Cancel: function() { $( this ).dialog( "close" );}
        }
    });
    
    validateForm=true; //you can toggle this value to turn JS validation on and off so you can test server side validation
    if (validateForm){
        var contactForm =$("#contactForm").validate({
            rules: {
                name: {
        		     required: true
                },
        		email: {
        			required: true,
        			email: true
        		},
                body: {
                    required: true
                }
            },
        	messages: {
                //plans: "Please choose a payment plan.",
        		name: "Please enter your name.",
        		email: {
        			required: "Please enter a valid email address.",
        			email: "Please enter a valid email address."
        		},
                body: "Please let us know how we can help."
        	},
            submitHandler: function(form) {
                $(form).ajaxSubmit({
                    success: showResp,
                    target: "#contactResp",
                    beforeSubmit: showRequest,
                    dataType: "xml",
                    url: "userauth/user/newTicket.php"
                  });
            },
            errorPlacement: function(error, element) {
                $("#"+element.attr("id")+"Label").children("span.errorForm").html(error);
            },
            errorContainer: "em",
            debug:true
        });
    }
});

//add a place for our server error responses to go.
function addElements (){
    if(!$("#"+$(this).attr("id")+"Resp").length){
        $(this).siblings("div.ui-dialog-buttonpane").prepend("<div id='" + $(this).attr("id") + "Resp' style='float:left; width:300px;' >&nbsp;</div>");
    }
} 
// pre-submit callback 
function showRequest(formData, jqForm, options) { 
    $(".ui-dialog-buttonset button").each(function(){$(this).button("disable");});
    $("#contactResp").html("Processing...");
    return true; 
} 
 
// post-submit callback 
function showResp(responseXML, statusText, xhr, $form)  {
    var message = $('message', responseXML).text();
    $("#contactResp").html(message);
}

//reset the from if it closed without submission
function resetIt(event, ui) {
    var curDialog = $(this);
    $(curDialog).children(0).resetForm();
    $(curDialog).siblings().find(".ui-dialog-buttonset button").each(function(){$(this).button("enable")});
    $("span.errorForm").html("");
    $("#contactResp").html("&nbsp;");
}

function mycarousel_initCallback(carousel)
{
    // Disable autoscrolling if the user clicks the prev or next button.
    carousel.buttonNext.bind('click', function() {
        carousel.startAuto(0);
    });
 
    carousel.buttonPrev.bind('click', function() {
        carousel.startAuto(0);
    });
 
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
};

function preloadImg(image) {
	var img = new Image();
	img.src = image;
}
