$(document).ready(function() {

	$('#home-grid-banner-text')  // Add Scroller Object
	$jScroller.add("#home-grid-banner","#home-grid-banner-text","left",6);
	$jScroller.start();

	$('#errorbox a').live('click', function () {
	    $('#errorbox').hide();
	    return false;
	});
	$('#messagebox a').live('click', function () {
	    $('#messagebox').hide();
	    return false;
	});

	initFormValidation();
	//initCenterPhotos();
	//initDatePicker();

});

function initFormValidation() {
    
    // Check for minimum age.
    jQuery.validator.addMethod('minage', function (value, element, params) {

        minAge = params[0];

        if (params.length > 1) {
            birth_year = $(element).val();
            birth_month = $('#sel-' + params[2] + ' option:selected').val();
            birth_day = $('#sel-' + params[1] + ' option:selected').val();
        } else {

            birth_date = new Date(value);
            birth_year = birth_date.getFullYear();
            birth_month = birth_date.getMonth() + 1;
            birth_day = birth_date.getDate();
        }

        today_date = new Date();
        today_year = today_date.getFullYear();
        today_month = today_date.getMonth();
        today_day = today_date.getDate();


        age = (today_year + 1900) - birth_year;
        
        if (today_month < (birth_month - 1)) {
            age--;        
        }
        if (((birth_month - 1) == today_month) && (today_day < birth_day)) {
            age--;
        }
        if (age > 1900) {
            age -= 1900;
        }

        isOverage = age >= minAge;
        
        if (age > 100) {
            isOverage = age <= minAge;
        }

        return isOverage;

    }, "You must be at least {0} years old to register join TangerClub!");

    // Make sure that at least one field has a valid value.
    jQuery.validator.addMethod('minqty', function (value, element, params) {

        minqty = params[0];
        selector = params[1];

        if (!$(selector)[0]) {
            return false;
        }

        checkString = '';
        checkSum = 0;

        console.debug($(selector));

        $(selector).each(function (index, object) {
            checkString += $.trim($(object).val());

            if (parseInt($(object).val())) {
                checkSum += parseInt($(object).val());
            }
        });

        return checkString && (checkSum >= minqty);

    }, "Please fill out at least {0} of these fields.");

    // Validate
    $('form').validate({

        //		errorLabelContainer: $('#errorbox'),
        onfocusout: false,
        highlight: function (element, errorClass, validClass) { },
        //		submitHandler: function() {},
        unhighlight: function (element, errorClass, validClass) {/*
			$(element).removeClass(errorClass).addClass(validClass);
			$('#errorbox').hide();*/
        },
        //		onsubmit: function(label) {},
        invalidHandler: function (form, validator) {
            var errors = validator.numberOfInvalids();
            if (errors) {

                errorMessage = validator.errorList[0].message;

                if (errorMessage == jQuery.validator.messages.required) {
                    errorMessage = 'The field <label for="' + validator.errorList[0].element.id + '">' + $($('label[for="' + validator.errorList[0].element.id + '"]').contents()[0]).text() + '</label> is required.';
                }

                $('#errorbox span').html(errorMessage).parent().show();
                $('.error').removeClass('error');
                $(validator.errorList[0].element).addClass('error');

                $(document).scrollTop($('#' + validator.errorList[0].element.id).position().top - 60)
                validator.errorList[0].element.focus();
            }
        },
        rules: {
            'birthday-year': {
                minage: [18, 'birthday-day', 'birthday-month']
            },
            birthday: {
                minage: [18]
            },
            'coupon-books-quantity': {
                minqty: [1, 'input[name*="quantity"][value!=""]']
            }
        },
        messages: {
            'coupon-books-quantity': {
                minqty: 'Please enter the quantity of coupon books you wish to purchase.'
            }
        },
        errorPlacement: function (error, element) { }
        
    });
}

//function initDatePicker() {
//    $("#profile-birthday").datepicker();
//}

//function initCenterPhotos() {
//    $("a[rel='center-photos']").colorbox({ transition: "none", photo: true });
//    //$('#sidebar-center-photos ul li a').colorbox();

//    $('#sidebar-center-photos > a').bind('click', function () {
//        $('a[rel="center-photos"]').colorbox({ open: true });
//        return false;
//    });
//}

//function initCenterPhotos() {
//    $("a[rel='center-photos']").colorbox({ transition: "none", photo: true });
//}

/* Wireup any links with the specified class to popup in an iframe */
function initCompanyContact() {
    $('.company-contact-link').colorbox({ iframe: true, innerWidth: 425, innerHeight: 344 });    
}
