$(document).ready(function() {

    /* Need to keep in 'undefined' condition */
    if ($('select').size() > 0 && !($('select').selectbox + '' == 'undefined')) {
        $('.select').selectbox({ animationSpeed: 0 });
    }

    /* menu */
    $('.option UL LI').hover(function() {
        var cnt = $(this).children('.level').children('UL').length;
        $(this).children('.level').css('width', cnt * 178);
        $(this).children('.level').children('.cc').css('width', cnt * 178 - 14);
    }, function() {
        return;
    });

    if ($('#rotator').length > 0) {
        $('#rotator')
	.after('<div id="nav">')
	.cycle({
	    fx: 'fade',
	    speed: 1000,
	    timeout: 5000,
	    pager: '#nav'
	});
    }

    $('#nav A').click(function() {
        if ($(this).hasClass('stopped')) {
            $('#rotator').cycle('resume');
        } else {
            $('#rotator').cycle('pause');
        }
        $(this).toggleClass('stopped');
        return false;
    });



    $('.sidebar UL A IMG').fadeTo('fast', 0.8, function() {
    });

    $('.sidebar UL A IMG').hover(function() {
        $(this).stop(true, true).fadeTo('fast', 1, function() {
            $(this).fadeTo(1, 1);
        });

    }, function() {
        $(this).fadeTo("fast", 0.8);
    });


    $('.sidebar UL DIV BUTTON').fadeTo('fast', 0.9, function() {
    });
    $('.sidebar UL DIV BUTTON').hover(function() {

        $(this).stop(true, true).fadeTo(1, 1, function() {
            $(this).fadeTo(1, 1);
        });

    }, function() {
        $(this).fadeTo("fast", 0.9);
    });


    $('.auto-hint').focus(function() {
        if ($(this).attr('value') == $(this).attr('title')) $(this).attr('value', '');
    });
    $('.auto-hint').blur(function() {
        if ($(this).attr('value') == '') $(this).attr('value', $(this).attr('title'));
    });

    $('#_cc_number').keyup(function() {
        var num = $(this).val();

        if (CheckCreditNumber(num) == 'master') {
            if (num.length == 16) {
                hideCards();
                $('#master').attr('src', '/images/icons/master-card-on.gif');
                $('#amex-sercurity-number').hide();
                setCCType('MasterCard');
            }
            else {
                hideCards();
                $('.credit-off img').attr('src', '/images/no-credit-card.png');
                $('#amex-sercurity-number').hide();
            }
        }
        else if (CheckCreditNumber(num) == 'visa') {
            if (num.length == 16) {
                hideCards();
                $('#visa').attr('src', '/images/icons/visa-on.gif');
                $('#amex-sercurity-number').hide();
                setCCType('Visa');
            }
            else {
                hideCards();
                $('.credit-off img').attr('src', '/images/no-credit-card.png');
                $('#amex-sercurity-number').hide();
            }
        }
        else if (CheckCreditNumber(num) == 'amex') {
            if (num.length == 15) {
                $('#amex-sercurity-number').show();
                hideCards();
                $('#amex').attr('src', '/images/icons/american-express-on.gif');
                $('#amex-sercurity-number').show();
                setCCType('AMEX');
            }
            else {
                hideCards();
                $('.credit-off img').attr('src', '/images/no-credit-card.png');
                $('#amex-sercurity-number').hide();
            }
        }
        else if (CheckCreditNumber(num) == 'discover') {
            if (num.length == 16) {
                hideCards();
                $('#discover').attr('src', '/images/icons/discover-on.gif');
                $('#amex-sercurity-number').hide();
                setCCType('Discover');
            }
            else {
                hideCards();
                $('#amex-sercurity-number').hide();
            }
        }
        else if (CheckCreditNumber(num) == 'unknown' || num == '') {
            hideCards();
            $('#amex-sercurity-number').hide();
        }
    });

});

function setCCType(ccType) {
    $('input[name="cc_type"]').attr('id', ccType);
    $('input[name="cc_type"]').attr('value', ccType);
    //alert($('input[name="cc_type"]').val());

}

function resetCCType() {
    $('input[name="cc_type"]').attr('id', '');
    $('input[name="cc_type"]').attr('value', '');
}

function CheckCreditNumber(num) {
    // first, sanitize the number by removing all non-digit characters.
    num = num.replace(/[^\d]/g,'');
    // now test the number against some regexes to figure out the card type.
    if(num.match(/^5[1-5]\d{14}$/) ) {
        return 'master';
    } 
    if(num.match(/^4\d{15}/) || num.match(/^4\d{12}/)) {
        return 'visa';
    } 
    if(num.match(/^3[4,7]\d{13}/)) {
         return 'amex';
    } 
    if(num.match(/^6011\d{12}/)) {
         return 'discover';
    }
    
    return 'unknown';
}

function hideCards(){
	$('#discover').attr('src','/images/icons/discover-off.gif');
	$('#amex').attr('src','/images/icons/american-express-off.gif');
	$('#visa').attr('src','/images/icons/visa-off.gif');
	$('#master').attr('src', '/images/icons/master-card-off.gif');
	resetCCType();
}


