jQuery(document).ready(function($) {
  //--- Tooltips ---//
  $(".show-container").tooltip({
  	tip: '.tooltip',
  	position: ['top', 'center']
  })

  //--- Dialogs ---//
	// Load up all anchor tags that have the class dialog
  $("a.dialog").fancybox({
	  'closeOnClick' : false,
    'hideOnContentClick': false
  })

  var qs = $.getQueryString({ ID:"dialog" })
  if (qs == "login") {
    $("#login").click()
  }
  else if (qs == "reset") {
    $("#reset").attr("href", function(){
      var url = this.href.split('?')
      return url[0] + '?password_reset_code=' + $.getQueryString({ ID: "password_reset_code" })
    })
    $("#reset").click()
  }

  //--- Form Inputs ---//
  $('.field-background :text:enabled').snazy_input()
  $('.field-background :password:enabled').snazy_input()
  $('.field-background-50 :text:enabled').snazy_input()
  $('.field-background-84 :text:enabled').snazy_input()
  $('.field-background-170 :text:enabled').snazy_input()
});

//--- Fancy Inputs ---//
(function($) {
  $.fn.snazy_input = function() {
    this.each(function() {
      var input = $(this)
      var label = input.prev()

      input.bind('focus', function() {
        if (input.val().length == 0) {
          label.addClass('selected')
        }
      })

      input.bind('blur', function() {
        if (input.val().length == 0) {
          label.removeClass('selected').removeClass('hastext')
        }
      })

      input.bind('keypress', function() {
        label.addClass('hastext')
      })

    	if (input.val().length != 0) {
    		label.addClass('hastext')
    	}
    })

    return this;
  }
})(jQuery);

//--- Dialog Forms ---//
function dialogSubmit() {
  $.post(
    $('#dialog_form').attr('action') + '?format=js',
    $('#dialog_form').serialize(),
    dialogSubmitHandler,
    'json'
  )

  return false
};

function dialogSubmitHandler(data, status) {
  $('#dialog_messages').removeClass('errors')
  $('#dialog_messages').removeClass('messages')

  if (data.status == "ok") {
    $('#dialog_messages').addClass('messages')
    $('#dialog_messages').html(data.msg)

    if (data.url) top.location.href = data.url

    if (data.append_html) {
      for (var el in data.append_html) {
        var dom_el = '#'+el.replace(/_/g, '-')
        $(dom_el).append(data.append_html[el])
        $(dom_el+" a.dialog").fancybox({
      	  'closeOnClick' : false,
          'hideOnContentClick': false
        })
      }
    }

    if (data.prepend_html) {
      for (var el in data.prepend_html) {
        var dom_el = '#'+el.replace(/_/g, '-')
        $(dom_el).prepend(data.prepend_html[el])
        $(dom_el+" a.dialog").fancybox({
      	  'closeOnClick' : false,
          'hideOnContentClick': false
        })
      }
    }

    if (data.replace_html) {
      for (var el in data.replace_html) {
        var dom_el = '#'+el.replace(/_/g, '-')
        $(dom_el).replaceWith(data.replace_html[el])
        $(dom_el+" a.dialog").fancybox({
      	  'closeOnClick' : false,
          'hideOnContentClick': false
        })
      }
    }

    if (data.delete_html) {
      for (var el in data.delete_html) {
        var dom_el = '#'+el.replace(/_/g, '-')
        $(dom_el).remove()
      }
    }

    setTimeout("$.fn.fancybox.close()", 2000)
  }
  else {
    $("#dialog_form :visible:enabled:first").focus()
    $('#dialog_messages').addClass('errors')

    if (data.status == "error") {
      $('#dialog_messages').html(data.errors)
    }
    else {
      $('#dialog_messages').html('An error has occurred, please try again in a few minutes.')
    }
  }
};

function calculateTotal(keyID){
  //alert(keyID);
  
  if(keyID == '8' || keyID == '13' || keyID == '46' || keyID <= '40' && keyID >= '37' || keyID <= '57' && keyID >= '48' || keyID <= '105' && keyID >= '96'){
    var origCost = parseFloat(document.getElementById('totalDisplay').innerHTML);

    plays = document.getElementById('calcField').value;
    playCost = plays * .01;
    document.getElementById('totalDisplay').innerHTML= '$' + playCost.toFixed(2); 
  }
  else{
    alert("Please enter numbers only.");
    var textboxString = document.getElementById('calcField').value;
    textboxString = textboxString.substring(0, textboxString.length-1);
    document.getElementById('calcField').value = textboxString;
  }
}