$().ready(function(){
    //Homepage Carosel
    if ($('#mycarousel').length > 0) {
        $('#mycarousel').jcarousel({
            auto: 5,
            easing: 'swing',
            scroll: 1,
            animation: 900,
            wrap: 'both',
            initCallback: mycarousel_initCallback,
            itemVisibleInCallback: {
                onAfterAnimation: mycarousel_itemVisibleInCallbackAfterAnimation
            },
            itemVisibleOutCallback: {
                onBeforeAnimation: mycarousel_itemVisibleOutCallbackBeforeAnimation
            }
        });
    }
    //Image Gallery
    if ($('.imageGallery').length > 0) {
        $("a[rel=example_group]").fancybox({
            'transitionIn': 'none',
            'transitionOut': 'none',
            'titlePosition': 'inside',
            'titleFormat': function(title, currentArray, currentIndex, currentOpts){
                return '' + title + '';
            }
        });
    }
    //Global trigger for fancyBoxes
    $(".fancyTrigger").fancybox({
        ajax: {
            type: "POST"
        },
        autoDimensions: true,
        height: 'auto',
        autoScale: false
    });   
});

//Homepage Carosel
function mycarousel_itemVisibleInCallbackAfterAnimation(carousel, item, idx, state){
    $("#overlay #" + idx).fadeIn('fast');
};
function mycarousel_itemVisibleOutCallbackBeforeAnimation(carousel, item, idx, state){
    $("#overlay #" + idx).fadeOut('fast');
};
function mycarousel_initCallback(carousel){  
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function(){
        carousel.stopAuto();
    }, function(){
        carousel.startAuto();
    });
};

function validateContactFields() {
	var errors = "";
	var flag = false;
	$('.contactUs #contactErrors').css('display', 'none');
	$('.contactUs #contactErrors').html('');
	if($('.contactUs #first_name').val() == "") {
		errors += "First name is required<br/>";
		flag = true;
	}
	if($('.contactUs #last_name').val() == "") {
		errors += "Last name is required<br/>";
		flag = true;
	}
	if($('.contactUs #email').val() == "") {
		errors += "Email is required<br/>";
		flag = true;
	}
	if(flag) {
		$('.contactUs #contactErrors').css('display', 'block');
		$('.contactUs #contactErrors').html(errors);
	}
	return !flag;
}

function postContact(){
	var first_name = escape($('#first_name').val());
	var last_name = escape($('#last_name').val());
	var daytime_phone = escape($('#daytime_phone').val());
	var evening_phone = escape($('#evening_phone').val());
	var email = escape($('#email').val());
	var best_time = escape($('#best_time').val());
	var referrer = escape($('#referrer').val());
	var when_move = escape($('#when_move').val());
	var comments = escape($('#comments').val());
	$.ajax({ 
		type: 'POST', 
		url: '/formci/index.php/form/submitContact', 
		data: 'first_name=' + first_name + '&last_name=' + last_name + '&daytime_phone=' + daytime_phone + '&evening_phone=' + evening_phone + '&email=' + email + '&best_time=' + best_time + '&referrer=' + referrer + '&when_move=' + when_move + '&comments=' + comments, 
		success: function(msg){			
			if (msg == "-1") { 
				$('.contactUs #contactErrors').css('display', 'block');
				$('.contactUs #contactErrors').html("Error occurred, please try again.");
			} 
			else { 
				$('.contactUs').html('<h1>Thank you!</h1><p>A Kahlen-Carey representative will contact you shortly.</p>');
			}
		} 
	}); 
}

function validateServiceFields() {
	var errors = "";
	var flag = false;
	$('.contactUs #serviceErrors').css('display', 'none');
	$('.contactUs #serviceErrors').html('');
	if($('.contactUs #first_name').val() == "") {
		errors += "First name is required<br/>";
		flag = true;
	}
	if($('.contactUs #last_name').val() == "") {
		errors += "Last name is required<br/>";
		flag = true;
	}
	if($('.contactUs #email').val() == "") {
		errors += "Email is required<br/>";
		flag = true;
	}
	if(flag) {
		$('.contactUs #serviceErrors').css('display', 'block');
		$('.contactUs #serviceErrors').html(errors);
	}
	return !flag;
}

function postService(){
	var first_name = escape($('#first_name').val());
	var last_name = escape($('#last_name').val());
	var daytime_phone = escape($('#daytime_phone').val());
	var evening_phone = escape($('#evening_phone').val());
	var email = escape($('#email').val());
	var best_time = escape($('#best_time').val());
	var street_address = escape($('#street_address').val());
	var city = escape($('#city').val());
	var comments = escape($('#comments').val());
	$.ajax({ 
		type: 'POST', 
		url: '/formci/index.php/form/submitService', 
		data: 'first_name=' + first_name + '&last_name=' + last_name + '&daytime_phone=' + daytime_phone + '&evening_phone=' + evening_phone + '&email=' + email + '&best_time=' + best_time + '&street_address=' + street_address + '&city=' + city + '&comments=' + comments, 
		success: function(msg){			
			if (msg == "-1") { 
				$('.contactUs #serviceErrors').css('display', 'block');
				$('.contactUs #serviceErrors').html("Error occurred, please try again.");
			} 
			else { 
				$('.contactUs').html('<h1>Thank you!</h1><p>A Kahlen-Carey representative will contact you shortly.</p>');
			}
		} 
	}); 
}