$(document).ready(function() {
	
	checkbox_init();
	
	$('#fldlogin').blur(function() {
		$.ajax({
			type: 'POST',
			data: { "action": "unique-username", "username": $(this).val() },
			success: function(data) {
				if (data == 'false') {
					$('#unique_username').html('This username is in use. Could you please choose another?');
					$('#fldlogin').focus();
					$('#fldlogin').addClass('error');
					ajax/$('#fldlogin').val('');
					
				}
				else {
					$('#unique_username').html('');
					$('#fldlogin').removeClass('error');
				}
			}
		});
	});
	
	$('#fldemailaddress').blur(function() {
		$.ajax({
			type: 'POST',
			data: { "action": "unique-email-address", "email-address": $(this).val() },
			success: function(data) {
				if (data == 'false') {
					$('#unique_emailaddress').html('This email is in use. Could you please choose another?');
					$('#fldemailaddress').focus();
					$('#fldemailaddress').addClass('error');
					$('#fldemailaddress').val('');
					
				}
				else {
					$('#unique_emailaddress').html('');
					$('#fldemailaddress').removeClass('error');
				}
			}
		});
	});
	
	$('#confirm_password').keyup(function() {
		if ($(this).val() != $('#fldpasswd').val()) {
			$('#confirm_password').addClass('error');
		}
		else {
			$('#confirm_password').removeClass('error');
		}
	});
	
	$('#confirm_password').blur(function() {
		
	});
	
	$('form.oldform').each(function(){
		$(this).validate({
			rules: {
				'profile[fldemailaddress]': {
					required: true,
					email: true
				},
				'confirm_password': {
					equalTo: '#fldpasswd'
				}
			}
		});
	});
    
    $('input.datetime').datetimepicker({
        ampm: true,
        dateFormat: 'D, M d yy',
        minDate: 0,
        timeFormat: 'hh:mm tt',
    });
    
    $('input.date').datepicker({
        dateFormat: 'D, M d yy',
        minDate: 0,
    });

	// gallery here
    $('.gallery .thumbs a').fadeTo('slow', 0.5);

    $('.gallery .thumbs a:first').fadeTo('fast', 1);
    $('.gallery .thumbs a:first').addClass('active');

    $('.gallery .thumbs a:not(.active)').mouseenter(function() { if (!$(this).hasClass('active')) { $(this).fadeTo('fast', 1); } });
    $('.gallery .thumbs a:not(.active)').mouseleave(function() { if (!$(this).hasClass('active')) { $(this).fadeTo('fast', 0.5); } });

    $('.gallery .thumbs a').click(function() {
        $('.gallery .thumbs a').removeClass('active');
        $(this).addClass('active');
        $('.gallery .thumbs a:not(.active)').fadeTo('fast', 0.5);
        $(this).fadeTo('fast', 1);

        $('.gallery .pic img').attr('src', $(this).attr('href'));
        $('.gallery p.title').text($(this).attr('title'));
        if ($('#divCredit #span' + $(this).attr('imid')).html()) {
            $('.gallery p.credit span').html($('#divCredit #span' + $(this).attr('imid')).html());
            $('.gallery p.credit').show();
        }
        else
            $('.gallery p.credit').hide();
        return false;
    });

    $('.gallery .pic img').attr('src', $('.gallery .thumbs li a:first').attr('href'));
    $('.gallery p.title').text($('.gallery .thumbs a:first').attr('title'));
    if ($('#divCredit span:first').html()) {
        $('.gallery p.credit span').html($('#divCredit span:first').html());
        $('.gallery p.credit').show();
    }
    else
        $('.gallery p.credit').hide();    
    $('#thumbs').jCarouselLite({
        btnNext: ".next",
        btnPrev: ".prev",
        visible: 5,
        circular: false			
    });
	// gallery end
    
    $('form input, form select, form textarea').tooltip({
    	showBody: true,
    	bodyHandler: function(){
    		if( $(this).hasClass('error') ){
    			var name = $(this).attr('id') || $(this).attr('name');
    			var msg = $('label.error[for="'+ name +'"]').html();
    			
    			if( msg )
    				return "<div class='errortip'>"+msg+"</div>";
    			return "";
    		}
    		return "";
    	}
    });
    
});


function loadpageevent($page){
	
	var $url = "/createprofile/paging_events/" + $page;
	
	if( window.iddiscipline != null )
		$url = $url + "/" + window.iddiscipline; 
	
	$.get($url, function($data){
		
		if( $data == ""){
			location.href = "/account/login";
			return;
		}
		
		$("#evententries").html($data);
		
		checkbox_init();
		lottery.rebuild_behavior();
	});
}

function checkbox_init(){
	$('.checkbox').click(function(){
		
		var name = $(this).attr('name');
	
		if ($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$('input#'+name).val('');
		}
		else {	
	
			$('.checkbox[name='+name+']').removeClass('checked');
			$(this).addClass('checked');
			
			$('input#'+name).val($(this).attr('value'));
		}
	});
	
	$('.single_checkbox').click(function() {
		
		var name = $(this).attr('name');
		if ($(this).hasClass('checked')) {
			$(this).removeClass('checked');
			$('input#'+name).val('0');
		}
		else {	
			$(this).addClass('checked');			
			$('input#'+name).val($(this).attr('value'));
		}
	})
}

