// Homepage Slideshow
$(document).ready(function () {
    $('#partnerlogos').jcarousel({
        auto: 10,
        wrap: 'circular',
        scroll: 4
    });
    $('.slideshow').cycle({
        fx: 'fade',
        timeout: 15000,
        speed: 1500 // choose your transition type, ex: fade, scrollUp, shuffle, etc...
    });
    // CustomerCare Drop Down
    $('a[href="#customercare"]').click(function () {
        showandhideCustomerCare();
        return false;
    });

    //current page highlighter in nav and topnav
    thisURL = window.location.pathname.substring(window.location.pathname.lastIndexOf("/") + 1);

    if (thisURL == "contact_us.php") {

        if (window.location.search.indexOf("location") != -1) {
            var location = window.location.search.substring(10);
            $(".contactBox h4").each(function () {

                if ($(this).text() == location) {
                    
                    var contact_div = $(this).parent();
                    var src = $("a", contact_div).attr("href"); 
                    $("#contact_map_iframe").attr("src", src);

                }
            });
        }
    }




    if (thisURL == "index.php" || thisURL == "") {
        $("#pagecontainer").addClass('home');
    } else {

        if (!$("#sidenav").length) {
            $("#pagecontainer").addClass("nosidenav");
        }

    }

    $('#nav  li a[href="' + thisURL + '"]').each(function () {
        $(this).parent().addClass('current');
    });

    $('#sidenav  li a[href="' + thisURL + '"]').each(function () {
        $(this).parent().addClass('active');
        var li_parent = $(this).parent().parent().parent();
        $('#nav  li a').each(function () {

            if ($(this).text() == $("span", li_parent).text()) {

                $(this).parent().addClass('current');
            }
        });

    });

    $('.long_news').hide();
    $('#searchbox').example('Search site');
    $('div.newsdiv p a').click(function () {
        $('.newsdiv .long_news').hide();
        $('.newsdiv .short_news').show();
        $('.newsdiv .readmore').show();
        $(this).parent().parent().children('.short_news').hide();
        $(this).parent().parent().children('.readmore').hide();
        $(this).parent().parent().children('.long_news').show();
        return false;

    })

    $('#contact_form').submit(function () {
        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            //alert(errorcount);
            //all good - submit the form and carry on
            //$(this).text('huzzah!');
            //var form = $(this).parents('form:first');
            var form=$('#contact_form');
            //alert(form.attr('name'));
            form.submit();
            return true;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });


    $('#register_form').submit(function () {

        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            return true;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });

    $('#support_call_form').submit(function () {

        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            $.ajax({
                url: "mailto_db.php",
                type: "POST",
                data: $('#support_call_form').serialize()
            });
            $('#contact_div').html("<h1>We have received your service request.  Our Service team will be in touch shortly.</h1>");
            return false;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });

    $('#login_form').submit(function () {

        var errorcount = validate_form();

        //check the error count
        if (errorcount === 0) {
            return true;
        } else if (errorcount === 1) {
            //something's wrong.
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There is an error with the form. Please check that all the fields have been entered correctly.</p>');
        } else if (errorcount > 1) {
            $('body').animate({ scrollTop: 0 }, 'slow');
            $('p.intro').after('<p class="intro error">There are ' + errorcount + ' errors with the form. Please check that all the fields have been entered correctly.</p>');
        }
        return false;
    });

    //clear errors if the form field is corrected
    $('input.error, select.error, textarea.error').live('blur', function () {
        //$('input, select, textarea').blur(function(){
        //if ($(this).hasClass('error') == false) {
        //	return;
        //} else {
        if (!$(this).val()) {
            return;
        } else {
            this_id = $(this).attr('id');
            $('label[for="' + this_id + '"]').removeClass('error');
            $(this).removeClass('error');
            $('p#' + this_id + '_error.error').fadeOut(300);
            //	}
        }
    });


    $("div.contactBox a").click(function () {
        $("#contact_map_iframe").attr("src", $(this).attr("href"));
        return false;



    });

});




function validate_form() {

    //required fields checkermajigger

    //reset the error count
    errorcount = 0;
    $('p.error').remove();
    $('.error').removeClass('error');


    //for textboxes, textareas and selects
    $('label:contains("*")').each(function () {
        //if a label has a * in it, and the next form element is empty
        if ($(this).next('input, select, textarea').val() == "" || $(this).next('fieldset').children('input, select, textarea').val() == "") {
            //add .error to the label
            $(this).addClass('error');

            //OFFSET (IF IT'S REQUIRED)
            //get the label's position (relative to the top)
            //var offset = $(this).position();
            //var offset_correction = -80;
            //var offset_correction = $('#offset_correction').html();
            //$(this).text(offset.top);

            //add .error to the field as well
            $(this).next('input, select, textarea').addClass('error');
            $(this).next('fieldset').children('input, select, textarea').addClass('error');

            //WITH OFFSET show an error message with <label> + "is required" (hidden by default)
            //$(this).next('input, select, textarea').after('<p id="'+$(this).attr('for')+'_error" class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');
            //$(this).next('fieldset').children('input, select, textarea').after('<p class="error" style="top:'+(offset.top-offset_correction)+'px;">'+$(this).text().replace('*','')+'  is required</p>');

            //WITHOUT OFFSET
            $(this).next('input, select, textarea').after('<p id="' + $(this).attr('for') + '_error" class="error">' + $(this).text().replace('*', '') + '  is required</p>');

            //add one to the error count

            errorcount = errorcount + 1;

            //show all the error messages
            $('p.error').slideDown('slow');
        }
    });


    return errorcount;

}




function showandhideCustomerCare() {
	if ($('#customercare').hasClass('shown')) {
		hideCustomerCare();
	} else {
		showCustomerCare();
	}
}

function showCustomerCare() {
	if ($('#customercare:animated').length) {
		return false;
	} else {
		$('#customercare').css('border-bottom', '1px solid #fff');
		$('#customercare').animate({
		    height: 118
		  }, 600, function() {
		  	$('#customercare').addClass('shown');
		  });	
	}
}

function hideCustomerCare() {
	if ($('#customercare:animated').length) {
		return false;
	} else {
		$('#customercare').animate({
		    height: 0
		  }, 600, function() {
		  	$('#customercare').removeClass('shown');
			$('#customercare').css('border-bottom', 'none');	  
		});	
	}	
}
