// onload events
$(document).ready(function() {
    rebind();
});

// events for load / rebind after ajax calls
function rebind()
{
    // prevent default action on class="editable"
    $('.editable').click(function(e) {
        e.preventDefault();
    });

    // run subdomain clash check on signup
    $('#subdomain').keyup(subdomain_check); // subdomain availability check - for some reason adding () causes it to fire onload

    // Apply fancybox to multiple items
    $("a.group").fancybox({
      'transitionIn'	:	'elastic',
      'transitionOut'	:	'elastic',
      'speedIn'		:	600,
      'speedOut'		:	200,
      'overlayShow'	:	false
    });

    // slidedown coupon code field
    $('#coupon').click(function() {
      $('#coupon_section').slideDown('slow', function() {
      // Animation complete.
      });
    });
}

function subdomain_check(){

    var subdomain = $('#subdomain').val();

    if(subdomain == "" || subdomain.length < 3){
        $('#subdomain').css('background', '#C33');
        $('#subdomain-result').css('color', '#C33');
        $('#subdomain-result').html('Enter a value at least 3 characters in length');
    } else {
        jQuery.ajax({
        type: "POST",
        url: "check-subdomain.php",
        data: 'subdomain='+ subdomain,
        cache: false,
        success: function(response){
        if(response == 1){
            $('#subdomain').css('background', '#C33');
            $('#subdomain-result').css('color', '#C33');
            $('#subdomain-result').addClass('bold');
            $('#subdomain-result').html('Sorry, that name is already taken!');
        } else {
            $('#subdomain').css('background', '');
            $('#subdomain-result').css('color', '#090');
            $('#subdomain-result').html('Site address is available!');
        }
        }});
    }
}

// watch video
function watchVideo(name)
{
    $.fancybox({
      'href'    : '/video/'+ name,
      'width'   : 960,
      'height'  : 540,
      'padding' : 0,
      'type'    : 'iframe'
    });

    return false;
}
