/*
* Mobility Service Nederland - MyShortlease ~ master.js
* Author:    Christiaan Hemerik ~ Whoooz! Webmedia [http://www.whoooz.nl/]
* Purpose:  
* Requires: jQuery ( min.v. 1.4.2 ) 
* Last edit: 
*/

$(document).ready(function () {

    $(function () {
        $('#CarPicker .tab h3').hide();
    });

    /* ------------------------------------------
    * Homepage > Tabs - Specials
    * ------------------------------------------
    */
    $(function () {
        specialsTabContainers = $('#Specials > section');
        $('#Specials #NavSpecials a').click(function () {
            specialsTabContainers.hide().filter(this.hash).show();
            $('#Specials #NavSpecials a').removeClass('selected');
            $(this).addClass('selected');
            return false;
        }).filter(':first').click();
    });

    /* ------------------------------------------
    * Homepage > Tabs - Car Picker
    * ------------------------------------------
    */
    $(function () {
        carpickerTabContainers = $('#CarPicker > section');
        $('a[href^=#CarPicker]').click(function () {
            carpickerTabContainers.hide().filter(this.hash).show();
            $('#CarPicker #NavCarPicker a.selected').removeClass('selected');
            $("#CarPicker #NavCarPicker a[href=" + this.hash + "]").addClass('selected');
            return false;
        }).filter(':first').click();
    });

    /* ------------------------------------------
    * Tarieven > Display mode rates list
    * ------------------------------------------
    */
    $(function () {
        $('#Filter .switch').show();
        $('#switch-01').click(function () {
            listState = $('#RatesList').attr('class');
            switchState = $('#switch-01').attr('class');
            if (listState != 'rows') {
                $('#RatesList').addClass('rows');
                if (switchState != 'selected') {
                    $('#switch-01').addClass('selected');
                    $('#switch-02').removeClass('selected');
                }
            }
            return false;
        });
        $('#switch-02').click(function () {
            listState = $('#RatesList').attr('class');
            switchState = $('#switch-02').attr('class');
            if (listState == 'rows') {
                $('#RatesList').removeClass('rows');
                if (switchState != 'selected') {
                    $('#switch-02').addClass('selected');
                    $('#switch-01').removeClass('selected');
                }
            }
            return false;
        });
    });

    /* ------------------------------------------
    * Offerte > Client side form validation
    * ------------------------------------------
    */
    $(function () {
        if (!elementSupportsAttribute('input', 'required')) {
            if ($("#FormQuotation").length > 0) {
                $("#FormQuotation").validate({
                    rules: {
                        startdate: {
                            required: true,
                            date: true
                        },
                        kilometres: {
                            required: true,
                            number: true
                        },
                        company: "required",
                        contact: "required",
                        email: {
                            required: true,
                            email: true
                        }
                    }
                });
            }
        }
    });

    /* ------------------------------------------
    * Offerte > Datepicker
    * ------------------------------------------
    */
    $(function () {
        if (!inputSupportsType('date')) {
            if ($("#FormQuotation").length > 0) {
                var now = new Date();
                var day = now.getDate();
                var month = now.getMonth() + 1;
                var year = now.getFullYear();
                today = day + '/' + month + '/' + year;
                $('.datepicker').addClass('unselected');
                $('.datepicker').val(today);
                $('.datepicker').datePicker();
                $('.datepicker').click(function () {
                    $(this).removeClass('unselected');
                });
                $('#FormQuotation .dp-choose-date').click(function () {
                    $('.datepicker').removeClass('unselected');
                });
            }
        }
    });

    /* ---------------------------------------------
    * Contact > Contact form client side validation
    * ---------------------------------------------
    */
    $(function () {
        if (!elementSupportsAttribute('input', 'required')) {
            if ($("#FormContact").length > 0) {
                $("#FormContact").validate({
                    rules: {
                        name: "required",
                        email: {
                            required: true,
                            email: true
                        },
                        remarks: "required"
                    }
                });
            }
        }
    });

});

/* ------------------------------------------
 * Default functions
 * ------------------------------------------
 */ 
function elementSupportsAttribute( element, attribute ) {
	var test = document.createElement( element );
	if (attribute in test) {
		return true;
	} else {
		return false;
	}
}
function inputSupportsType( test ) {
	var input = document.createElement('input');
	input.setAttribute('type', test);
	if (input.type == 'text') {
		return false;
	} else {
		return true;
	}
}
